def test_preevaluated(self): evaluator = Evaluator() pop = Population().new("X", X) evaluator.eval(problem, pop) pop[range(30)].set("F", None) evaluator = Evaluator() evaluator.eval(problem, pop) np.testing.assert_allclose(F, pop.get("F")) self.assertTrue(evaluator.n_eval == 30)
class ACO(Algorithm): def __init__(self, ant, pheromones, n_ants=10, global_update='best', local_update=True, evaluate_each_ant=True, display=ACODisplay(), **kwargs): """ Parameters ---------- ant : class An objective defining the behavior of each ant pheromones : class The pheromone implementation storing the amount of pheromones on each edge, the evaporation and update. n_ants : int Number of ants to be used each iteration global_update : {'all', 'it-best', 'best'} """ super().__init__(display=display, **kwargs) # make the ant always to be a function being able to call if not callable(ant): proto = deepcopy(ant) def new_ant(): return deepcopy(proto) ant = new_ant self.ant = ant self.pheromones = pheromones self.n_ants = n_ants self.global_update = global_update self.local_update = local_update self.evaluate_each_ant = evaluate_each_ant def _initialize(self): self.evaluator = Evaluator( skip_already_evaluated=False ) if self.evaluate_each_ant else MockEvaluator() self.opt = Population() self._next() def _next(self): # initialize all ants to be used in this iteration ants = [] for k in range(self.n_ants): ant = self.ant() ant.initialize(self.problem, self.pheromones) ants.append(ant) active = list(range(self.n_ants)) while len(active) > 0: for k in active: ant = ants[k] if ant.has_next(): ant.next() if self.local_update: e = ant.last() if e is None or e.pheromone is None: raise Exception( "For a local update the ant has to set the pheromones when notified." ) else: self.pheromones.set( e.key, self.pheromones.get(e.key) * ant.alpha + e.pheromone * ant.alpha) # self.pheromones.update(e.key, e.pheromone * ant.alpha) else: ant.finalize() active = [i for i in active if i != k] colony = Population.create(*ants) # this evaluation can be disabled or faked if evaluate_each_ant is false - then the finalize method of the # ant has to set the objective and/or constraint values accordingly self.evaluator.eval(self.problem, colony) set_cv(colony) set_feasibility(colony) # set the current best including the new colony opt = FitnessSurvival().do(problem, Population.merge(colony, self.opt), 1) # do the evaporation after this iteration self.pheromones.evaporate() # select the ants to be used for the global pheromone update if self.global_update == "all": ants_to_update = colony elif self.global_update == "it-best": ants_to_update = FitnessSurvival().do(problem, colony, 1) elif self.global_update == "best": ants_to_update = self.opt else: raise Exception( "Unknown value for global updating the pheromones!") # now spread the pheromones for each ant depending on performance for ant in ants_to_update: for e in ant.path: if e.pheromone is None: raise Exception( "The ant has to set the pheromone of each entry in the path." ) else: self.pheromones.update(e.key, e.pheromone * pheromones.rho) self.pop, self.off = colony, colony self.opt = opt def _set_optimum(self, **kwargs): pass
def test_evaluate_pop(self): evaluator = Evaluator() pop = Population().new("X", X) evaluator.eval(problem, pop) np.testing.assert_allclose(F, pop.get("F")) self.assertTrue(evaluator.n_eval == len(X))
def test_evaluate_individual(self): evaluator = Evaluator() ind = evaluator.eval(problem, Individual(X=X[0])) np.testing.assert_allclose(F[0], ind.get("F")) self.assertTrue(evaluator.n_eval == 1)
def test_evaluate_array_single(self): evaluator = Evaluator(evaluate_values_of=["F", "CV"]) _F, _CV = evaluator.eval(problem, X[0]) np.testing.assert_allclose(F[0], _F) self.assertTrue(evaluator.n_eval == 1)
def test_evaluate_array(self): evaluator = Evaluator(evaluate_values_of=["F", "CV"]) _F, _CV = evaluator.eval(problem, X) np.testing.assert_allclose(F, _F) self.assertTrue(evaluator.n_eval == len(X))