|
GeneticAlgorithm
0.5 (beta)
A python framework for rapid GA prototyping
|
00001 import Core 00002 import copy 00003 00004 ## @class BestLogger 00005 # @brief This class stores the best individual of the population, if it is better than the last individual found. 00006 class BestLogger(Core.BasePeriodicOperator): 00007 def __init__(self, **kwargs): 00008 self.bestLog = [] 00009 self.numEvaluations = [] 00010 self.criterion = 'fitness' 00011 self.maximize = True 00012 self.iterationFrequency = 1 00013 super(BestLogger, self).__init__(**kwargs) 00014 00015 ## @fn logCallback(self, population) 00016 # @brief Add a new individual to the log if it is different to the best logged so far 00017 def logCallback(self, population): 00018 currentBest, newBest = self.getBest(population) 00019 ## @todo Check individual fitness 00020 if currentBest != newBest: 00021 self.addToLog(newBest) 00022 00023 00024 iterationCallback = logCallback 00025 evaluationCallback = logCallback 00026 00027 ## @fn logBest(self, population) 00028 # @brief Append the best individual ever found to the log, and add an evaluation counter 00029 def addToLog(self, best): 00030 self.numEvaluations.append( self.evaluationCounter ) 00031 self.bestLog.append( best ) 00032 00033 ## @fn getBest(self, population) 00034 # @brief Get the best individual out of the list containing the population and the best individual found so far 00035 def getBest(self, population): 00036 # Get the population size 00037 n = len(population.individuals) 00038 # Append the current best to the list of individuals 00039 candidates = population.individuals 00040 if len(self.bestLog) > 0: 00041 currentBest = self.bestLog[-1] 00042 candidates.append(currentBest) 00043 else: 00044 currentBest = None 00045 # Get the comparison criteria from all individuals 00046 criteria = [getattr(individual, self.criterion) for individual in candidates] 00047 # Create and zip a lost of pointers 00048 indices = range(n+1) 00049 sortedFitness = sorted(zip(criteria, indices), key=lambda x: x[0]) 00050 # Select the best individual, according to target 00051 if self.maximize: 00052 best = sortedFitness[-1][1] 00053 else: 00054 best = sortedFitness[0][1] 00055 # Return the best candidate 00056 return (currentBest, candidates[best]) 00057 00058 def finalize(self, population): 00059 for eval, individual in zip( self.numEvaluations, self.bestLog ): 00060 print ('%4d\t' % eval) + str(individual) 00061 00062 ## @class LogGenerations 00063 # @brief Log the full population of a GA 00064 class LogGenerations(Core.BasePeriodicOperator): 00065 def __init__(self, **kwargs): 00066 self.generationLog = [] 00067 self.numEvaluations = [] 00068 super(LogGenerations, self).__init__(**kwargs) 00069 00070 def logPopulation(self, population): 00071 # Append the plain number of evaluations to the evaluation counter 00072 self.numEvaluations.append(self.evaluationCounter) 00073 # Make a deep copy of the population and log it 00074 self.generationLog.append(copy.deepcopy(population)) 00075 00076 iterationCallback = logPopulation 00077 evaluationCallback = logPopulation 00078 00079 def finalize(self, population): 00080 for ev, pop in zip( self.numEvaluations, self.generationLog ): 00081 print 'Number of evaluations %d' % ev + str(pop) + '\n'