def aging(self, task, pop): r"""Apply aging to individuals. Args: task (Task): Optimization task. pop (numpy.ndarray[Individual]): Current population. Returns: numpy.ndarray[Individual]: New population. """ fpop = np.asarray([x.f for x in pop]) x_b, x_w = pop[np.argmin(fpop)], pop[np.argmax(fpop)] avg, npop = np.mean(fpop[fpop != np.inf]), [] for x in pop: x.age += 1 Lt = round( self.age(Lt_min=self.Lt_min, Lt_max=self.Lt_max, mu=self.mu, x_f=x.f, avg=avg, x_gw=x_w.f, x_gb=x_b.f)) if x.age <= Lt: npop.append(x) if len(npop) == 0: npop = objects2array([ self.itype(task=task, rnd=self.Rand, e=True) for _ in range(self.NP) ]) return npop
def pop_increment(self, pop, task): r"""Increment population. Args: pop (numpy.ndarray): Current population. task (Task): Optimization task. Returns: numpy.ndarray: Increased population. """ deltapop = int(round(max(1, self.NP * self.delta_pop_e(task.Iters)))) return objects2array([ self.itype(task=task, rnd=self.Rand, e=True) for _ in range(deltapop) ])
def evolve(self, pop, xb, task, **kwargs): r"""Evolve population with the help multiple mutation strategies. Args: pop (numpy.ndarray): Current population. xb (numpy.ndarray): Current best individual. task (Task): Optimization task. kwargs (Dict[str, Any]): Additional arguments. Returns: numpy.ndarray: New population of individuals. """ return objects2array([ self.CrossMutt(pop, i, xb, self.F, self.CR, self.Rand, task, self.itype, self.strategies) for i in range(len(pop)) ])
def pop_decrement(self, pop, task): r"""Decrement population. Args: pop (numpy.ndarray): Current population. task (Task): Optimization task. Returns: numpy.ndarray: Decreased population. """ deltapop = int(round(max(1, self.NP * self.delta_pop_c(task.Iters)))) if len(pop) - deltapop <= 0: return pop ni = self.Rand.choice(len(pop), deltapop, replace=False) npop = [] for i, e in enumerate(pop): if i not in ni: npop.append(e) elif self.rand() >= self.omega: npop.append(e) return objects2array(npop)
def evolve(self, pop, xb, task, **ukwargs): r"""Evolve current population. Args: pop (numpy.ndarray[Individual]): Current population. xb (Individual): Global best individual. task (Task): Optimization task. ukwargs (Dict[str, Any]): Additional arguments. Returns: numpy.ndarray: New population. """ npop = objects2array([self.AdaptiveGen(e) for e in pop]) for i, e in enumerate(npop): npop[i].x = self.CrossMutt(npop, i, xb, e.F, e.CR, rnd=self.Rand) for e in npop: e.evaluate(task, rnd=self.rand) return npop
def init_pop_individual(task, n, itype, **kwargs): r"""Custom population initialization function for numpy individual type. Args: task (Task): Optimization task. n (int): Population size. itype (Individual): Type of individual in population. kwargs (Dict[str, Any]): Additional arguments. Returns: Tuple[numpy.ndarray, numpy.ndarray, list, dict): 1. Initialized population. 2. Initialized populations fitness/function values. 3. Additional arguments. 4. Additional keyword arguments. """ pop = objects2array( [itype(x=np.full(task.D, 0.0), task=task) for _ in range(n)]) return pop, np.asarray([x.f for x in pop]), [], {}
def evolve(self, pop, xb, task, **kwargs): r"""Evolve population. Arg: pop (numpy.ndarray): Current population. xb (Individual): Current best individual. task (Task): Optimization task. kwargs (Dict[str, Any]): Additional arguments. Returns: numpy.ndarray: New evolved populations. """ return objects2array([ self.itype(x=self.CrossMutt(pop, i, xb, self.F, self.CR, self.Rand), task=task, rnd=self.Rand, e=True) for i in range(len(pop)) ])
def selection(self, pop, npop, xb, fxb, task, **kwargs): r"""Operator for selection. Args: pop (numpy.ndarray): Current population. npop (numpy.ndarray): New Population. xb (numpy.ndarray): Current global best solution. fxb (float): Current global best solutions fitness/objective value. task (Task): Optimization task. kwargs (Dict[str, Any]): Additional arguments. Returns: Tuple[numpy.ndarray, numpy.ndarray, float]: 1. New selected individuals. 2. New global best solution. 3. New global best solutions fitness/objective value. """ arr = objects2array( [e if e.f < pop[i].f else pop[i] for i, e in enumerate(npop)]) xb, fxb = self.get_best(arr, np.asarray([e.f for e in arr]), xb, fxb) return arr, xb, fxb
def post_selection(self, pop, task, xb, fxb, **kwargs): r"""Post selection operator. In this algorithm the post selection operator decrements the population at specific iterations/generations. Args: pop (numpy.ndarray): Current population. task (Task): Optimization task. kwargs (Dict[str, Any]): Additional arguments. Returns: Tuple[numpy.ndarray, numpy.ndarray, float]: 1. Changed current population. 2. New global best solution. 3. New global best solutions fitness/objective value. """ Gr = task.nFES // (self.pmax * len(pop)) + self.rp nNP = len(pop) // 2 if task.Iters == Gr and len(pop) > 3: pop = objects2array([ pop[i] if pop[i].f < pop[i + nNP].f else pop[i + nNP] for i in range(nNP) ]) return pop, xb, fxb