def test_weierstreass_extended_fitness_function(): factory = FloatingPointIndividualFactory(ExpandedSchaffers(), isModivied=False) x = [factory.getIndividual([0, 0])] eveluation = x[0].getEvaluation() assert eveluation == 0
def test_select(): factory = FloatingPointIndividualFactory(FitnessFunctionQuadratic(), isModivied=False) selector = rouletteParentSelector() population = [ factory.getIndividual([1, 1, 1]), factory.getIndividual([2, 2, 2]), factory.getIndividual([3, 3, 3]), factory.getIndividual([4, 4, 4]) ] fitness = [] for el in population: fitness.append(el.getEvaluation()) maximum = 0 for el in fitness: if maximum < el: maximum = el chosen = selector.chooseOne(population, fitness, maximum) assert chosen in population marriages = selector.getParents(population) for el in marriages: for individual in el: assert individual in population selector = simpleParentSelector() marriages = selector.getParents(population) for el in marriages: for individual in el: assert individual in population
def test_get_partners(): FloatingPointIndividualFactory.createFactory("FitnessFunctionQuadratic", True) factory = FloatingPointIndividualFactory.getFactory() population = [factory.getIndividual([i, i]) for i in range(4)] population = getPartners(population) assert population[0].getPartner() == population[1] assert population[1].getPartner() == population[0] assert population[2].getPartner() == population[3] assert population[3].getPartner() == population[2]
def test_pair_individuals(): factory = FloatingPointIndividualFactory(FitnessFunctionQuadratic(), isModivied=True) # factory.createFactory() a = factory.getIndividual([1, 1]) b = factory.getIndividual([2, 2]) test_variable = False try: a.getEvaluation() except Exception: # assert Exception test_variable = True assert test_variable == True a.setPartner(b) assert a.getEvaluation() == 2.0 assert b.getEvaluation() == 2.0
def build(config: dict): individual = config["Individual"] if individual["Type"] == "floatingPointIndividual": return FloatingPointIndividualFactory( config["FitnessFunction"] ) # TODO add dispathcer to create fitness funtion corectly # if individual["Type"] == "floatingPointIndividualWithConstantPairs": # return FloatingPointIndividualWithConstantPairsFactory( # config["FitnessFunction"]) # TODO add dispathcer to create fitness funtion corectly else: raise NotImplementedError()
def createEvolutionSimulatorFromDict(cls, config: dict): # componentsFactory = cls.getComponentsFactory(config) try: for componentFactory in cls.componentsFactory.values(): componentFactory.validate( config ) # TODO add also validation on OtherElementsOnlyValidationReneameMeFactory elemnents except ValueError as err: print( f"For component {componentFactory}, parameter is invalid. {err}" ) componentsImpl: Dict[str, SimulationComponentInterface] = {} if config["IndividualRepresentation"][ "Type"] == "FloatingPointRepresentation": FloatingPointIndividualFactory.createFactory( config["FitnessFunction"]["Type"], isModivied=False) elif config["IndividualRepresentation"][ "Type"] == "FloatingPointWithConstantPairsRepresentation": FloatingPointIndividualFactory.createFactory( config["FitnessFunction"]["Type"], isModivied=True) else: raise NotImplementedError for componentName, componentFactory in cls.componentsFactory.items(): componentsImpl[componentName] = componentFactory.build(config) observers = [] for observer_config in config[ "Observers"]: # TODO add observers valdation observers.append( cls.observersFactory.build(observer_config, config["WorkingDirectoryPath"], config["SimulationName"])) simulationSteps = config["NumberOfSimulationSteps"] return EvolutionSimulator.fromSimulationComponentList( componentsImpl, simulationSteps, observers)
def test_Recombinator(): factory = FloatingPointIndividualFactory(FitnessFunctionQuadratic(), isModivied=False) marriages = [[ factory.getIndividual([1, 2, 3, 4]), factory.getIndividual([5, 6, 7, 8]) ]] recombinator = singlePointRecombinator(1.0) childrens = recombinator.recombine(marriages) sum = 0 for el in childrens: temp = el.getRepresentation() for i in range(len(temp)): sum = sum + temp[i] assert sum == 36 recombinator = twoPointRecombinator(1.0) childrens = recombinator.recombine(marriages) sum = 0 for el in childrens: temp = el.getRepresentation() for i in range(len(temp)): sum = sum + temp[i] assert sum == 36 recombinator = averageRecombinator(1.0) children = recombinator.subcross(marriages[0], 0.5) assert children.getRepresentation() == [3, 4, 5, 6] childrens = recombinator.recombine(marriages) for children in childrens: for i in range(2): assert children.getRepresentation( ) != marriages[0][i].getRepresentation()
def __init__(self, probability: float): self.probability = probability self.individualFactory: FloatingPointIndividualFactory = FloatingPointIndividualFactory.getFactory( )
def test_FloatingPointIndividualFactory(): # FloatingPointIndividualFactory.createFactory("FitnessFunctionQuadratic", isModivied = True) factory = FloatingPointIndividualFactory.getFactory() # assert factory.fitnessFunction == FitnessFunctionQuadratic() assert factory.isModivied == True
def __init__(self): self.individualFactory: FloatingPointIndividualFactory = FloatingPointIndividualFactory.getFactory( )
def __init__(self, populationSize: int, dimensions: int, rangeMin: int, rangeMax: int): self.populationSize = populationSize self.dimensions = dimensions self.rangeMax = rangeMax self.rangeMin = rangeMin self.individualFactory: FloatingPointIndividualFactory = FloatingPointIndividualFactory.getFactory()
def __init__(self, sigma: float, probabilityOfMutation: float): self.sigma = sigma self.probabilityOfMutation = probabilityOfMutation self.individualFactory: FloatingPointIndividualFactory = FloatingPointIndividualFactory.getFactory( )