def PerformExperiment(cls, poolNumber=None): ''' Perform single experiment and save all possible results (album, gene pool of last population and summary with best and average fitnesses (other parameters are optional) :param poolNumber: int Set, if you want to start your experiment not from the random beginning, but from the saved progress. ''' # Set up environment SetUpManager.SetUp() # Load population if experiment doesn't start from the random beginning. if poolNumber is not None: EvolutonaryAlgorithm.LoadPopulation("data/genePools/pool" + str(poolNumber) + ".txt") # Perform experiment EvolutonaryAlgorithm.Execute() # Create album album = AlbumWriter.AlbumFromAlgorithm(EvolutonaryAlgorithm) # Save results cls.SaveAlbum(album) cls.SaveGenePool() cls.AddSummaryToRegistry() # Increment counter (next experiment will have another number) cls.numberOfExperiment += 1
def SaveGenePool(cls): ''' Save genes of all individuals from the last population. ''' EvolutonaryAlgorithm.SavePopulation(PathsManager.mainPath + "Scripts/Data/genePools/pool" + str(cls.numberOfExperiment) + ".txt")
def PerformExperiment(cls, poolNumber=None): # Set up environment SetUpManager.SetUp() # Load population if experiment doesn't start from the random beginning. if poolNumber is not None: EvolutonaryAlgorithm.LoadPopulation("data/genePools/pool" + str(poolNumber) + ".txt") # Perform experiment EvolutonaryAlgorithm.Execute() # Create album album = AlbumWriter.AlbumFromAlgorithm(EvolutonaryAlgorithm) # Save results cls.SaveAlbum(album) cls.SaveGenePool() cls.AddSummaryToRegistry() # Increment counter (next experiment will have another number) cls.numberOfExperiment += 1
def SetUpEvolutonaryAlgorithm(cls): cls.ExecuteIfHasNotBeenYet(SetUpManager.SetUpPattern) creator.create("FitnessMax", base.Fitness, weights=(1.0, )) EvolutonaryAlgorithm.lverbose = True # INDIVIDUALS EvolutonaryAlgorithm.geneRandomizingFunction = Pattern.RandomWage EvolutonaryAlgorithm.lengthOfIndividual = Pattern.length #LAWS EvolutonaryAlgorithm.probabilityOfCrossing = SETTINGS.GENETICS.CROSSING.PROBABILITY EvolutonaryAlgorithm.crossingMethod = SETTINGS.GENETICS.CROSSING.METHOD EvolutonaryAlgorithm.crossingParameters = {} EvolutonaryAlgorithm.probabilityOfMutation = SETTINGS.GENETICS.MUTATION.PROBABILITY EvolutonaryAlgorithm.mutationMethod = SETTINGS.GENETICS.MUTATION.METHOD EvolutonaryAlgorithm.mutationParameters = {} EvolutonaryAlgorithm.mutationParameters[ 'mu'] = SETTINGS.GENETICS.MUTATION.GAUSSIAN.MEAN_OF_THE_DISTRIBUTION EvolutonaryAlgorithm.mutationParameters[ 'sigma'] = SETTINGS.GENETICS.MUTATION.GAUSSIAN.STD_DEVIATION_OF_THE_DISTRIBUTION EvolutonaryAlgorithm.mutationParameters[ 'indpb'] = SETTINGS.GENETICS.MUTATION.PROBABILITY_OF_ATTRIBUTE_MUTATION EvolutonaryAlgorithm.selectionMethod = SETTINGS.GENETICS.SELECTION.METHOD EvolutonaryAlgorithm.selectionParameters = {} EvolutonaryAlgorithm.selectionParameters[ 'tournsize'] = SETTINGS.GENETICS.SELECTION.TOURNAMENT.SIZE #MAKRO EvolutonaryAlgorithm.paramMu = SETTINGS.GENETICS.SIZE_OF_POPULATION_AFTER_SELECTION EvolutonaryAlgorithm.paramLambda = SETTINGS.GENETICS.SIZE_OF_POPULATION_BEFORE_SELECTION EvolutonaryAlgorithm.numberOfGenerations = SETTINGS.GENETICS.NUMBER_OF_GENERATIONS #STATISTICS EvolutonaryAlgorithm.dictWithStatistics = {} EvolutonaryAlgorithm.dictWithStatistics[ 'min'] = SETTINGS.GENETICS.STATISTICS.MIN EvolutonaryAlgorithm.dictWithStatistics[ 'max'] = SETTINGS.GENETICS.STATISTICS.MAX EvolutonaryAlgorithm.dictWithStatistics[ 'avg'] = SETTINGS.GENETICS.STATISTICS.AVERAGE #GENERAL EvolutonaryAlgorithm.Prepare()
from Modules.AI.Genetics.genetics import AlbumWriter, EvolutonaryAlgorithm from Modules.GUI.displayer import Displayer from Modules.Simulation.map import Map from Modules.General.general_tools import PathsManager from Modules.Settings.set_up_manager import SetUpManager from Modules.Settings.settings import SETTINGS #SETUP SetUpManager.SetUp() EvolutonaryAlgorithm.Execute() amap = Map() amap.LoadFromFile(PathsManager.GetPath("map")) adisplayer = Displayer adisplayer.map = amap albumWriter = AlbumWriter() album = albumWriter.AlbumFromAlgorithm(EvolutonaryAlgorithm) adisplayer.PlayAlbum(album)
def SaveGenePool(cls): EvolutonaryAlgorithm.SavePopulation(PathsManager.mainPath + "Scripts/Data/genePools/pool" + str(cls.numberOfExperiment) + ".txt")
from Modules.AI.Genetics.genetics import EvolutonaryAlgorithm from Modules.Settings.set_up_manager import SetUpManager import unittest def EvaluatePattern(gaPattern): return sum(gaPattern), # #SETUP SetUpManager.SetUp() EvolutonaryAlgorithm.Prepare() EvolutonaryAlgorithm.toolbox.register("evaluate", EvaluatePattern) EvolutonaryAlgorithm.Execute() class TestPatternBreeder(unittest.TestCase): def test_effect(self): self.assertTrue(max(EvolutonaryAlgorithm.logbook.select("max")) > 15) self.assertTrue(max(EvolutonaryAlgorithm.logbook.select("avg")) > 15) if __name__ == '__main__': unittest.main()