def run_iteration(self, task, c, fpop, xb, fxb, ki, *args, **dparams): r"""Core function of EvolutionStrategyMpL algorithm. Args: task (Task): Optimization task. c (numpy.ndarray): Current population. fpop (numpy.ndarray): Current populations fitness/function values. xb (numpy.ndarray): Global best individual. fxb (float): Global best individuals fitness/function value. ki (int): Number of successful mutations. args (list): Additional arguments. dparams (dict): Additional keyword arguments. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, float, list, dict]: 1. New population. 2. New populations function/fitness values. 3. New global best solution. 4. New global best solutions fitness/objective value. 5. Additional arguments. 6. Additional keyword arguments: * ki (int): Number of successful mutations. """ if task.Iters % self.k == 0: _, ki = self.update_rho(c, ki), 0 cn = objects2array([ IndividualES(x=self.mutate_rand(c, task), task=task, rnd=self.Rand) for _ in range(self.lam) ]) cn = np.append(cn, c) cn = objects2array( [cn[i] for i in np.argsort([i.f for i in cn])[:self.mu]]) ki += self.change_count(c, cn) fcn = np.asarray([x.f for x in cn]) xb, fxb = self.get_best(cn, fcn, xb, fxb) return cn, fcn, xb, fxb, args, {'ki': ki}
def run_iteration(self, task, caravan, fcaravan, cb, fcb, *args, **dparams): r"""Core function of Camel Algorithm. Args: task (Task): Optimization task. caravan (numpy.ndarray[Camel]): Current population of Camels. fcaravan (numpy.ndarray[float]): Current population fitness/function values. cb (Camel): Current best Camel. fcb (float): Current best Camel fitness/function value. args (list): Additional arguments. dparams (dict): Additional keyword arguments. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, float, list, dict]: 1. New population. 2. New population function/fitness value. 3. New global best solution. 4. New global best fitness/objective value. 5. Additional arguments. 6. Additional keyword arguments. """ ncaravan = objects2array([self.walk(c, cb, task) for c in caravan]) ncaravan = objects2array([self.oasis(c, self.rand(), self.alpha) for c in ncaravan]) ncaravan = objects2array([self.life_cycle(c, self.mu, task) for c in ncaravan]) fncaravan = np.asarray([c.f for c in ncaravan]) cb, fcb = self.get_best(ncaravan, fncaravan, cb, fcb) return ncaravan, fncaravan, cb, fcb, args, dparams
def run_iteration(self, task, c, fpop, xb, fxb, *args, **dparams): r"""Core function of EvolutionStrategyML algorithm. Args: task (Task): Optimization task. c (numpy.ndarray): Current population. fpop (numpy.ndarray): Current population fitness/function values. xb (numpy.ndarray): Global best individual. fxb (float): Global best individuals fitness/function value. args (list): Additional arguments. dparams (dict): Additional keyword arguments. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, float, list, dict]: 1. New population. 2. New populations fitness/function values. 3. New global best solution. 4. New global best solutions fitness/objective value. 5. Additional arguments. 6. Additional keyword arguments. """ cn = objects2array([ IndividualES(x=self.mutate_rand(c, task), task=task, rand=self.Rand) for _ in range(self.lam) ]) c = self.new_pop(cn) fc = np.asarray([x.f for x in c]) xb, fxb = self.get_best(c, fc, xb, fxb) return c, fc, xb, fxb, args, dparams
def run_iteration(self, task, c, fpop, xb, fxb, ki, *args, **dparams): r"""Core function of EvolutionStrategy(1+1) algorithm. Args: task (Task): Optimization task. pop (Individual): Current position. fpop (float): Current position function/fitness value. xb (Individual): Global best position. fxb (float): Global best function/fitness value. ki (int): Number of successful updates before rho update. args (list): Additional arguments. dparams (dict): Additional keyword arguments. Returns: Tuple[Individual, float, Individual, float, list, dict]: 1, Initialized individual. 2, Initialized individual fitness/function value. 3. New global best solution. 4. New global best solutions fitness/objective value. 5. Additional arguments. 6. Additional keyword arguments: * ki (int): Number of successful rho update. """ if task.Iters % self.k == 0: c.rho, ki = self.update_rho(c.rho, ki), 0 cn = objects2array([ task.repair(self.mutate(c.x, c.rho), self.Rand) for _i in range(self.mu) ]) cn_f = np.asarray([task.eval(cn[i]) for i in range(len(cn))]) ib = np.argmin(cn_f) if cn_f[ib] < c.f: c.x, c.f, ki = cn[ib], cn_f[ib], ki + 1 if cn_f[ib] < fxb: xb, fxb = self.get_best(cn[ib], cn_f[ib], xb, fxb) return c, c.f, xb, fxb, args, {'ki': ki}
def new_pop(self, pop): r"""Return new population. Args: pop (numpy.ndarray): Current population. Returns: numpy.ndarray: New population. """ pop_s = np.argsort([i.f for i in pop]) if self.mu < self.lam: return objects2array([pop[i] for i in pop_s[:self.mu]]) npop = list() for i in range(int(ceil(float(self.mu) / self.lam))): npop.extend( pop[:self.lam if (self.mu - i * self.lam) >= self.lam else self.mu - i * self.lam]) return objects2array(npop)
def init_pop(self, task, n, rnd, itype, *args, **kwargs): r"""Initialize starting population. Args: task (Task): Optimization task. n (int): Number of camels in population. rnd (mtrand.RandomState): Random number generator. itype (Individual): Individual type. wargs (list): Additional arguments. kwargs (dict): Additional keyword arguments. Returns: Tuple[numpy.ndarray, numpy.ndarray, list, dict]: 1. Initialize population of camels. 2. Initialized populations function/fitness values. 3. Additional arguments. 4. Additional keyword arguments. """ caravan = objects2array([itype(E_init=self.E_init, S_init=self.S_init, task=task, rnd=rnd, e=True) for _ in range(n)]) return caravan, np.asarray([c.f for c in caravan]), args, kwargs
def default_individual_init(task, n, rnd=rand, itype=None, *args, **kwargs): r"""Initialize `n` individuals of type `itype`. Args: task (Task): Optimization task. n (int): Number of individuals in population. rnd (Optional[rand.RandomState]): Random number generator. itype (Optional[Individual]): Class of individual in population. args (list): Additional arguments. kwargs (dict): Additional keyword arguments. Returns: Tuple[numpy.ndarray, numpy.ndarray]: 1. Initialized individuals. 2. Initialized individuals function/fitness values. 3. Additional arguments. 4. Additional keyword arguments. """ pop = objects2array([itype(task=task, rnd=rnd, e=True) for _ in range(n)]) return pop, np.asarray([x.f for x in pop]), args, kwargs
def init_school(self, task): """Initialize fish school with uniform distribution.""" curr_step_individual = self.step_individual_init * (task.Upper - task.Lower) curr_step_volitive = self.step_volitive_init * (task.Upper - task.Lower) curr_weight_school = 0.0 school = [] positions = self.generate_uniform_coordinates(task) for idx in range(self.NP): fish = self.init_fish(positions[idx], task) school.append(fish) curr_weight_school += fish.weight prev_weight_school = curr_weight_school return curr_step_individual, curr_step_volitive, curr_weight_school, prev_weight_school, objects2array( school)