lolamontes69

Ch5 Ex3-Programming Collective Intelligence

Jul 7th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. """ Chapter 5 Exercise 3: Genetic optimization stopping criteria.
  2.  
  3.    A function in this chapter runs the genetic optimizer for a fixed number of iterations. Change it so that it stops when there has been no improvement in any of the best solutions for 10 iterations
  4.  
  5. """
  6.  
  7. def geneticoptimize(domain,costf,popsize=50,step=1,mutprob=0.2,elite=0.2,maxiter=100):
  8.     currentbest = 0       # Added for Chapter 5 Exercise 3
  9.     count = 0             # Added for Chapter 5 Exercise 3
  10.     # Mutation Operation
  11.     def mutate(vec):
  12.         i=random.randint(0,len(domain)-1)
  13.         # Corrected so different step values can be added
  14.         if random.random()<0.5 and vec[i]-step>domain[i][0]:
  15.             return vec[0:i]+[vec[i]-step]+vec[i+1:]
  16.         elif vec[i]+step<domain[i][1]:
  17.             return vec[0:i]+[vec[i]+step]+vec[i+1:]
  18.         else:
  19.             return vec           # Corrected otherwise if vec[i] is unsatisfactory nothing returns
  20.  
  21.     # Crossover Operation
  22.     def crossover(r1,r2):
  23.         i=random.randint(1,len(domain)-2)
  24.         return r1[0:i]+r2[i:]
  25.  
  26.     # Build the initial population
  27.     pop=[]
  28.     for i in range(popsize):
  29.         vec=[random.randint(domain[i][0],domain[i][1]) for i in range(len(domain))]
  30.         pop.append(vec)
  31.  
  32.     # How many winners from each generation?
  33.     topelite=int(elite*popsize)
  34.  
  35.     # Main loop
  36.     for i in range(maxiter):
  37.         scores=[(costf(v),v) for v in pop]
  38.         scores.sort()
  39.         if i == maxiter-1: pass            # Corrected from here for last iteration
  40.         else:
  41.             ranked=[v for (s,v) in scores]
  42.  
  43.             # Start with the pure winers
  44.             pop=ranked[0:topelite]
  45.  
  46.             # Add mutated and bred forms of the winners
  47.             while len(pop)<popsize:
  48.                 if random.random()<mutprob:
  49.  
  50.                     # Mutation
  51.                     c=random.randint(0,topelite)
  52.                     pop.append(mutate(ranked[c]))
  53.                 else:
  54.  
  55.                     # Crossover
  56.                     c1=random.randint(0,topelite)
  57.                     c2=random.randint(0,topelite)
  58.                     pop.append(crossover(ranked[c1],ranked[c2]))
  59.  
  60.             # Print current best score
  61.             print scores[0][0]
  62.  
  63.             # if there has been no change with best score in 10 iterations exit   # Added for Chapter 5 Exercise 3
  64.             if scores[0][0] == currentbest: count+=1
  65.             else: count = 0
  66.             if count == 10:
  67.                 return scores[0][1]
  68.             currentbest = scores[0][0]
  69.  
  70.     return scores[0][1]
Advertisement
Add Comment
Please, Sign In to add comment