def process(self,pod,dt):
 
          
     global best_brain
     global best_fitness
            
     # If we are trying to evolve and pod dies
     if reap_pod(pod.state):
         " here then time to replace the brain"
         # save current  brain and fitness in the pool
         fitness=calc_fitness(pod.state)
         
         if fitness > best_fitness:
             best_brain=pod.brain
             best_fitness=fitness
             print fitness, " : ",pod.brain.params
         
         # reset the pod and give it a new brain
         world.init_pod(pod)
         pod.brain=create_new()
         return
         
     # normal control stuff
     
     control=pods.Control()
     
     p=pod.brain.params
     
     sensor=pod.sensors
     state=pod.state
    
     V=p[0]*sensor[0].val   
     if V>p[1]:
         V=p[1]
     
     cont=(V/(abs(state.vel+1e-6)+p[2])) - p[3]
     
     if cont > 0:
         control.up = cont
     else:
         control.down = abs(cont)
         
     diff=(sensor[1].val+sensor[2].val)-(sensor[7].val+sensor[6].val)
     
     turn_compensation=p[4]*sensor[0].val+p[5]
     
     if diff>0.0:
         control.left=abs((diff/p[6])**2/p[7])+turn_compensation    
     else:
         control.right=abs((diff/p[6])**2/p[7])+turn_compensation
     
     return control
 def load(self,file):
     self.list=[]
     n=pickle.load(file)
     print n
     for i in range(n):
         f=pickle.load(file)
         net=loadBrain(file)
         net.proof_count=0    # sorry we lost the proof count when we saved it
         net.fitness=f
         self.add(net)
      
     print "RELOADED POOL"   
     for pod in pods:
         # reset the pod and give it a new brain from the pool
         world.init_pod(pod)
         pod.ang += random()-0.5    # randomize the intial angle
         pod.brain.brain=pool.create_new()
    def process(self,sim):   
        
            # this is called just before each time step
            # do admin tasks here

            global pods

             # output to a log file
            if pool.reaping and log_file!=None and pool.touched:
                log_file.write(str(sim.ticks) +','+ str(pool.best_fitness())+','+str(pool.average_fitness())+'\n')
                pool.touched=False
                                
            keyinput = pygame.key.get_pressed()
        
            # speed up/down  display      
            if keyinput[pygame.K_KP_PLUS] or keyinput[pygame.K_EQUALS]:
                sim.frameskipfactor = sim.frameskipfactor+1
                print "skip factor" ,sim.frameskipfactor
            
            if keyinput[pygame.K_MINUS]:
                sim.frameskipfactor = max(1,sim.frameskipfactor-1)
                print "skip factor" ,sim.frameskipfactor

            # display the performance of the best pod in pool
            if  keyinput[pygame.K_b]:
                
                
                if pool.reaping:    # if reaping copy existing to allow restore
                    self.pods_copy=copy(pods)
                    pod=pods[0]
                    del pods[:]
                    pods.append(pod)
                else:
                    pod=pods[0]
                    
                    
                world.init_pod(pod)
                pod.ang += random()-0.5    # randomize the intial angle
                pod.control.brain=pool.create_best()
                pool.reaping=False
             
            # display the performance of the most proven pod
            if  keyinput[pygame.K_p]:
                
                
                if pool.reaping:    # if reaping copy existing to allow restore
                    self.pods_copy=copy(pods)
                    pod=pods[0]
                    del pods[:]
                    pods.append(pod)
                else:
                    pod=pods[0]
                    
                    
                world.init_pod(pod)
                pod.ang += random()-0.5    # randomize the intial angle
                pod.control.brain=pool.create_most_proven()
                pool.reaping=False   
                
            # go back into evolution mode after looking at best pod   
            if not pool.reaping and keyinput[pg.K_r]:
                del pods[:]
                pods.extend(self.pods_copy)
                pool.reaping=True

            # save the pool to a file
            if keyinput[pygame.K_s]:
                file=open(POOL_FILE_NAME,"w")
                pool.save(file)
                file.close()
                
            # reload the pool from a file
            if keyinput[pygame.K_l]:
                file=open(POOL_FILE_NAME,"r")
                pool.load(file)
                file.close()
        
            if keyinput[pygame.K_d]:
                for brain in pool.list:
                    print brain.fitness," : ",brain.params