lolamontes69

Ch11 Ex6-Programming Collective Intelligence

Sep 19th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | None | 0 0
  1. """ Chapter 11 Exercise 6: Grid War player.
  2.  
  3.    Try to hand design your own tree program that does well at grid war.
  4.    If you find this easy, try to write another completely different one.
  5.    Instead of having a completely random initial population, make it mostly
  6.    random with your hand-designed programs included.
  7.    How do they compare to random programs, and can they be improved with evolution
  8.  
  9.    First I hand-designed three programs.
  10.    Then I altered evolve to take them in a list and add them to the initial population.
  11.  
  12.    They easily outperformed the initial randomly generated programs and were
  13.    evolved into quite good Grid War players.
  14. """
  15. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  16. @@@@@@@@@@@@@@@@@@@@@@@@@ HAND-DESIGNED PROGRAMS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  17.  
  18. ### Hand-designed program 1 ###
  19. # Keeps out of corners and doesn't repeat last move
  20.  
  21. def cornw(l):
  22.     if l[0]==0 and l[1]==0:
  23.     a=[1,3]
  24.     if l[2] in a: a.remove(l[2])
  25.     return choice(a)
  26. elif l[0]==0 and l[1]==3:
  27.     a=[3,0]
  28.     if l[2] in a: a.remove(l[2])
  29.     return choice(a)
  30. elif l[0]==3 and l[1]==0:
  31.     a=[1,2]
  32.     if l[2] in a: a.remove(l[2])
  33.     return choice(a)
  34. elif l[0]==3 and l[1]==3:
  35.     a=[0,2]
  36.     if l[2] in a: a.remove(l[2])
  37.     return choice(a)
  38. else:
  39.     a = randint(0,18)%4
  40.     if a==l[2]: a +=1
  41.     return a%4
  42. cornw=fwrapper(cornw,3,'cornw')
  43.  
  44. def strategy1():
  45.     return node(cornw,[paramnode(0),paramnode(1),paramnode(4)])
  46.  
  47. -------------------------------
  48. ### Hand-designed program 2 ###
  49. # Follows and doesn't repeat last move
  50.  
  51. def banzai1(l):
  52.     list1=[]
  53.     if l[1]>l[3]: list1.append(2)
  54.     else: list1.append(3)
  55.     if l[0]>l[2]: list1.append(0)
  56.     else: list1.append(1)
  57.     if l[4] in list1: list1.remove(l[4])
  58.     if len(list1)>0: return choice(list1)
  59.     else:
  60.         a = randint(0,12)%4
  61.         if a==l[4]: a +=1
  62.         return a%4
  63. banzaiw=fwrapper(banzai1,5,'banzaiw')
  64.  
  65. def strategy2():
  66.     return node(banzaiw,[paramnode(0),paramnode(1),paramnode(2),paramnode(3),paramnode(4)])
  67.  
  68. -------------------------------
  69. ### Hand-designed program 3 ###
  70. # Like the previous with a probability of changing strategies between following and avoiding.
  71.  
  72. def scaredy1(l):
  73.     list1=[]
  74.     if l[1]<l[3]: list1.append(2)
  75.     else: list1.append(3)
  76.     if l[0]<l[2]: list1.append(0)
  77.     else: list1.append(1)
  78.     if l[4] in list1: list1.remove(l[4])
  79.     if len(list1)>0: return choice(list1)
  80.     else:
  81.         a = randint(0,12)%4
  82.         if a==l[4]: a +=1
  83.         return a%4
  84. scaredyw=fwrapper(scaredy1,5,'scaredyw')
  85.  
  86. def mix_em(l):
  87.     a = randint(0,5)
  88.     if a==3: return scaredy1(l)
  89.     else: return banzai1(l)
  90. mixemw=fwrapper(scaredy1,5,'scaredyw')
  91.  
  92. def strategy3():
  93.     return node(mixemw,[paramnode(0),paramnode(1),paramnode(2),paramnode(3),paramnode(4)])
  94.  
  95. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  96. @@@@@@@@@@@@@@@@@@ EVOLVE ALTERED TO TAKE NEW PROGRAMS @@@@@@@@@@@@@@@@@@@@@@@@
  97.  
  98. # Now I altered evolve to take these created functions via 'addsome' that takes a list.
  99.  
  100. def evolve(pc,popsize,rankfunction,addsome,maxgen=500,mutationrate=0.1,breedingrate=0.4,pexp=0.7,pnew=0.05):
  101.     def selectindex(lenscores):
  102.         while True:
  103.             ind =  int(log(random())/log(pexp))
  104.             if (ind-lenscores>(lenscores*2)-1) or (ind>lenscores): pass
  105.             else: return ind
  106.  
  107.     # Create a random initial population and add addsome to it
  108.     population=[makerandomtree(pc) for i in range(popsize-len(addsome))]
  109.     population+=addsome
  110.     for i in range(maxgen):
  111.         scores=rankfunction(population)
  112.         print scores[0][0]
  113.         if scores[0][0]==0: break
  114.         newpop=[scores[0][1],scores[1][1]]
  115.         while len(newpop)<popsize:
  116.             if random()>pnew:
  117.                 newpop.append(mutate(
  118.                                 crossover(scores[selectindex(len(scores))][1],
  119.                                         scores[selectindex(len(scores))][1],
  120.                                         probswap=breedingrate),
  121.                                 pc,probchange=mutationrate))
  122.             else:
  123.                 newpop.append(makerandomtree(pc))
  124.         population=newpop
  125.     scores[0][1].display()
  126.     return scores[0][1]
  127.  
  128. # USAGE #
  129.  
  130. import gp_strategy as gp
  131. strat1 = gp.strategy1()
  132. strat2 = gp.strategy2()
  133. strat3 = gp.strategy3()
  134. addsome=[strat1,strat2,strat3]
  135. winner=gp.evolve(5,70,gp.tournament,addsome,maxgen=20)
  136. gp.gridgame([winner,gp.humanplayer()])
Advertisement
Add Comment
Please, Sign In to add comment