def compile(self, space: Space) -> None: """Compiles additional information that is used by this optimizer. Args: space: A Space object containing meta-information. """ # Replaces the current agents with a derived Lion structure space.agents = [ Lion( agent.n_variables, agent.n_dimensions, agent.lb, agent.ub, agent.position, agent.fit, ) for agent in space.agents ] # Calculates the number of nomad lions and their genders n_nomad = int(self.N * space.n_agents) nomad_gender = d.generate_bernoulli_distribution(1 - self.S, n_nomad) # Iterates through all possible nomads for i, agent in enumerate(space.agents[:n_nomad]): # Toggles to `True` the nomad property agent.nomad = True # Defines the gender according to Bernoulli distribution agent.female = bool(nomad_gender[i]) # Calculates the gender of pride lions pride_gender = d.generate_bernoulli_distribution( self.S, space.n_agents - n_nomad) # Iterates through all possible prides for i, agent in enumerate(space.agents[n_nomad:]): # Defines the gender according to Bernoulli distribution agent.female = bool(pride_gender[i]) # Allocates to the corresponding pride agent.pride = i % self.P
def _parasitism_phase( self, space: Space, n_crows: int, n_cuckoos: int, iteration: int, n_iterations: int, ): """Performs the parasitism phase using the current number of cuckoos. Args: space: Space containing agents and update-related information. n_crows: Number of crows. n_cuckoos: Number of cuckoos. iteration: Current iteration. n_iterations: Maximum number of iterations. """ # Gathers the cuckoos cuckoos = space.agents[n_crows:n_crows + n_cuckoos] # Calculates a list of cuckoos' fitness fitness = [cuckoo.fit for cuckoo in cuckoos] # Calculates the probability of selection p = iteration / n_iterations # Iterates through all cuckoos for cuckoo in cuckoos: # Selects a cuckoo through tournament selection s = g.tournament_selection(fitness, 1)[0] # Selects two random agents from the space i = r.generate_integer_random_number(high=space.n_agents) j = r.generate_integer_random_number(high=space.n_agents, exclude_value=i) # Creates a bernoulli distribution to preserve or not variables (eq. 12) k = d.generate_bernoulli_distribution(1 - p, cuckoo.n_variables) k = np.expand_dims(k, -1) # Calculates the gaussian-based step distribution (eq. 11) rand = r.generate_uniform_random_number() S_g = (space.agents[i].position - space.agents[j].position) * rand # Updates the cuckoo's position and clips its limits (eq. 10) cuckoo.position = space.agents[s].position + S_g * k cuckoo.clip_by_bound()
def _generate_abandoned_nests(self, agents: List[Agent], prob: float) -> List[Agent]: """Generate a fraction of nests to be replaced. Args: agents: List of agents. prob: Probability of replacing worst nests. Returns: (List[Agent]): A new list of agents which can be seen as the new nests to be replaced. """ # Makes a temporary copy of current agents new_agents = copy.deepcopy(agents) # Generates a bernoulli distribution array # It will be used to replace or not a certain nest b = d.generate_bernoulli_distribution(1 - prob, len(agents)) # Iterates through every new agent for j, new_agent in enumerate(new_agents): # Generates a uniform random number r1 = r.generate_uniform_random_number() # Then, we select two random nests k = r.generate_integer_random_number(0, len(agents) - 1) l = r.generate_integer_random_number(0, len(agents) - 1, exclude_value=k) # Calculates the random walk between these two nests step_size = r1 * (agents[k].position - agents[l].position) # Finally, we replace the old nest # Note it will only be replaced if 'b' is 1 new_agent.position += step_size * b[j] return new_agents
def _generate_abandoned_nests(self, agents, prob): """Generate a fraction of nests to be replaced according to Yang's implementation. Args: agents (list): List of agents. prob (float): Probability of replacing worst nests. Returns: A new list of agents which can be seen as the new nests to be replaced. """ # Makes a temporary copy of current agents new_agents = copy.deepcopy(agents) # Generates a bernoulli distribution array # It will be used to replace or not a certain nest b = d.generate_bernoulli_distribution(prob, len(agents)) # Iterating through every new agent for j, new_agent in enumerate(new_agents): # Generates a uniform random number r1 = r.generate_uniform_random_number(0, 1) # Then, we select two random nests k = int(r.generate_uniform_random_number(0, len(agents) - 1)) l = int(r.generate_uniform_random_number(0, len(agents) - 1)) # Calculating the random walk between these two nests step_size = r1 * (agents[k].position - agents[l].position) # Finally, we replace the old nest # Note it will only be replaced if 'b' is 1 new_agent.position += (step_size * b[j]) return new_agents
import opytimizer.math.distribution as d # Generates a Bernoulli distribution b = d.generate_bernoulli_distribution(prob=0.5, size=10) print(b) # Generates a choice distribution c = d.generate_choice_distribution(n=10, probs=None, size=10) print(c) # Generates a Lévy distribution l = d.generate_levy_distribution(beta=0.5, size=10) print(l)
def test_generate_bernoulli_distribution(): bernoulli_array = distribution.generate_bernoulli_distribution(prob=0.5, size=10) assert bernoulli_array.shape == (10,)