|
GeneticAlgorithm
0.5 (beta)
A python framework for rapid GA prototyping
|
00001 import Core 00002 import LoggingOperators 00003 import matplotlib.pyplot 00004 00005 ## @class PlotBestLogger 00006 # @brief A BestLogger specialization that plots the historic progression of the best criterion with each tick 00007 class PlotBestLogger(LoggingOperators.BestLogger): 00008 # @fn __init__(self, criterionAxis=None, figure=None, **kwargs) 00009 # @brief The genetic operator constructor 00010 # @pram criterionAxis A matplotlib axes object, used to plot the best found evaluation so far 00011 # @param figure The figure that contains both the criterion and graph axis 00012 # 00013 # Any parameter can be omitted, in that a new object of the required type will be created to initialize properties 00014 def __init__(self, criterionAxis=None, figure=None, **kwargs): 00015 # Call parent initializer 00016 super(PlotBestLogger, self).__init__(**kwargs) 00017 # Configure matplotlib.pyplot to operate interactively 00018 matplotlib.pyplot.interactive(True) 00019 if criterionAxis==None: 00020 figure, criterionAxis = matplotlib.pyplot.subplots() 00021 # Store the target axes 00022 self.criterionAxis = criterionAxis 00023 self.figure=figure 00024 self.figure.show() 00025 00026 ## @fn plotCallback(self, population) 00027 # @brief Logs the best found so far using it's parent callback, and then plots the historic progression of the evaluation criteria 00028 # @param population The population used to look for the best found so far 00029 def plotCallback(self, population): 00030 super(PlotBestLogger, self).logCallback(population) 00031 if not (self.criterionAxis==None): 00032 criteria = [getattr(individual, self.criterion) for individual in self.bestLog] 00033 self.criterionAxis.cla() 00034 self.criterionAxis.plot(self.numEvaluations, criteria) 00035 self.criterionAxis.set_title( 'Best found so far' ) 00036 self.criterionAxis.set_xlabel( 'number of evaluations' ) 00037 self.criterionAxis.set_ylabel( self.criterion ) 00038 matplotlib.pyplot.draw() 00039 00040 00041 iterationCallback = plotCallback 00042 evaluationCallback = plotCallback 00043 00044 ## @fn finalize(self, population) 00045 # @brief This function shows the plot one more time, and then blocks to allow the user to choose when the plot window is terminated. 00046 def finalize(self, population): 00047 super(PlotBestLogger, self).finalize(population) 00048 matplotlib.pyplot.show(block=True)