lolamontes69

Ch11 Ex3-Programming Collective Intelligence

Sep 15th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. """ Chapter 11 Exercise 3: Random crossover.
  2.  
  3.    The current crossover function chooses branches from two trees at the same level.
  4.    Write a different crossover function that crosses two random branches.
  5.    How does this affect evolution?
  6.  
  7. """
  8.  
  9. from copy import deepcopy
  10. import random
  11. import gp as gp
  12.  
  13.  
  14. def newCrossover(t1,t2,probswap=0.7,top=1):
  15.     if hasattr(t1,'children') and random.random()<probswap and not top:
  16.         return deepcopy(t2),0
  17.     else:
  18.         result=deepcopy(t1)
  19.         result.children=[]
  20.         if hasattr(t1, 'children'):
  21.             for c in t1.children:
  22.                 a,b = newCrossover(c,t2,probswap,0)
  23.                 if b==0:
  24.                     result.children.append(a)
  25.                     probswap=0.0
  26.                 else:
  27.                     result.children.append(c)
  28.         return result,1
  29.  
  30. def getRandomBranch(t3,probswap,top=1):
  31.    if hasattr(t3,'children') and random.random()<probswap and not top:
  32.        return t3
  33.    else:
  34.        if hasattr(t3,'children'):
  35.            c = random.choice(t3.children)
  36.            return getRandomBranch(c,probswap,0)
  37.        else: return t3
  38.  
  39. def doNewCrossover(t1,t2,probswap=0.2):
  40.     t3=getRandomBranch(t2,0.3)
  41.     t11=deepcopy(t1)
  42.     a,b=newCrossover(t11,t3,probswap)
  43.     return a
  44.  
  45. """
  46. The branches that this function produces may be smaller or larger than
  47. the branches that they replace. If the branches being produced are consistently
  48. longer try reducing the probchange for t3 in doNewCrossover.
  49.  
  50.  
  51. ============
  52. Test usage
  53. ============
  54. exampletree=gp.exampletree()
  55. exampletree2=gp.makerandomtree(2)
  56.  
  57. for a in range(50):
  58.    exampletree2=gp.makerandomtree(2)
  59.    crossed=doNewCrossover(exampletree,exampletree2,probswap=0.2)
  60.    crossed.display()
  61.    print "-"*33
  62.  
  63.  
  64. I also tested how the two versions of crossover worked given the same
  65. initial populations. Given the fact that they are both doing their
  66. crossing randomly not surprisingly the results reflected that fact.
  67.  
  68. All solved within 500 generations.
  69.  
  70. runtime using original crossover = 63.218499898 seconds.      Shorter
  71. runtime using new crossover      = 20.252122879 seconds.      Longer
  72.  
  73. runtime using original crossover = 22.112336874 seconds.      Shorter
  74. runtime using new crossover      = 1079.0829510 seconds.      Longer
  75.  
  76. runtime using original crossover = 513.59018707 seconds.      Shorter
  77. runtime using new crossover      = 168.15006995 seconds.      Longer
  78.  
  79. runtime using original crossover = 2430.2017447 seconds.      Longer
  80. runtime using new crossover      = 35.639962196 seconds.      Shorter
  81.  
  82. runtime using original crossover = 574.03915596 seconds.      Shorter
  83. runtime using new crossover      = 50.552052974 seconds.      Longer
  84.  
  85.  
  86. """
Advertisement
Add Comment
Please, Sign In to add comment