def decrement_population(self, pop, task): r"""Decrement population. Args: pop (numpy.ndarray): Current population. task (Task): Optimization task. Returns: numpy.ndarray[Individual]: Decreased population. """ delta_population = int( round( max( 1, self.population_size * self.delta_pop_created( (task.iters + 1))))) if len(pop) - delta_population <= 0: return pop ni = self.rng.choice(len(pop), delta_population, replace=False) new_population = [] for i, e in enumerate(pop): if i not in ni: new_population.append(e) elif self.random() >= self.omega: new_population.append(e) return objects_to_array(new_population)
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 = np.mean(fpop[np.isfinite(fpop)]) new_population = [] for x in pop: x.age += 1 lifetime = round( self.age(min_lifetime=self.min_lifetime, max_lifetime=self.max_lifetime, mu=self.mu, x_f=x.f, avg=avg, x_gw=x_w.f, x_gb=x_b.f)) if x.age <= lifetime: new_population.append(x) if len(new_population) == 0: new_population = objects_to_array([ self.individual_type(task=task, rng=self.rng, e=True) for _ in range(self.population_size) ]) return new_population
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. xb (numpy.ndarray): Global best individual coordinates. fxb (float): Global best fitness. 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.max_evals // (self.p_max * len(pop)) + self.rp new_np = len(pop) // 2 if (task.iters + 1) == gr and len(pop) > 3: pop = objects_to_array([ pop[i] if pop[i].f < pop[i + new_np].f else pop[i + new_np] for i in range(new_np) ]) return pop, xb, fxb
def selection(self, population, new_population, best_x, best_fitness, task, **kwargs): r"""Operator for selection. Args: population (numpy.ndarray): Current population. new_population (numpy.ndarray): New Population. best_x (numpy.ndarray): Current global best solution. best_fitness (float): Current global best solutions fitness/objective value. task (Task): Optimization task. 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 = objects_to_array([ e if e.f < population[i].f else population[i] for i, e in enumerate(new_population) ]) best_x, best_fitness = self.get_best(arr, np.asarray([e.f for e in arr]), best_x, best_fitness) return arr, best_x, best_fitness
def default_individual_init(task, population_size, rng, individual_type=None, **_kwargs): r"""Initialize `population_size` individuals of type `individual_type`. Args: task (Task): Optimization task. population_size (int): Number of individuals in population. rng (numpy.random.Generator): Random number generator. individual_type (Optional[Individual]): Class of individual in population. Returns: Tuple[numpy.ndarray[Individual], numpy.ndarray[float]: 1. Initialized individuals. 2. Initialized individuals function/fitness values. """ pop = objects_to_array([individual_type(task=task, rng=rng, e=True) for _ in range(population_size)]) return pop, np.asarray([x.f for x in pop])
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. Returns: numpy.ndarray: New population of individuals. """ return objects_to_array([ self.strategy(pop, i, xb, self.differential_weight, self.crossover_probability, self.rng, task, self.individual_type, self.strategies) for i in range(len(pop)) ])
def increment_population(self, task): r"""Increment population. Args: task (Task): Optimization task. Returns: numpy.ndarray[Individual]: Increased population. """ delta_pop = int( round( max( 1, self.population_size * self.delta_pop_eliminated( (task.iters + 1))))) return objects_to_array([ self.individual_type(task=task, rng=self.rng, e=True) for _ in range(delta_pop) ])
def evolve(self, pop, xb, task, **kwargs): r"""Evolve population. Args: pop (numpy.ndarray): Current population. xb (numpy.ndarray): Current best individual. task (Task): Optimization task. Returns: numpy.ndarray: New evolved populations. """ return objects_to_array([ self.individual_type(x=self.strategy(pop, i, self.differential_weight, self.crossover_probability, self.rng, x_b=xb), task=task, rng=self.rng, e=True) for i in range(len(pop)) ])