opytimizer.optimizers.swarm.kh

Krill Herd.

class opytimizer.optimizers.swarm.kh.KH(params: Optional[Dict[str, Any]] = None)

A KH class, inherited from Optimizer.

This is the designed class to define KH-related variables and methods.

References

A. Gandomi and A. Alavi. Krill herd: A new bio-inspired optimization algorithm. Communications in Nonlinear Science and Numerical Simulation (2012).

__init__(params: Optional[Dict[str, Any]] = None) → None

Initialization method.

Parameters:params – Contains key-value parameters to the meta-heuristics.
N_max

Maximum induced speed.

w_n

Inertia weight of the neighbours’ motion.

NN

Number of neighbours.

V_f

Foraging speed.

w_f

Inertia weight of the foraging motion.

D_max

Maximum diffusion speed.

C_t

Position constant.

Cr

Crossover probability.

Mu

Mutation probability.

motion

Array of motions.

foraging

Array of foragings.

compile(space: opytimizer.core.space.Space) → None

Compiles additional information that is used by this optimizer.

Parameters:space – A Space object containing meta-information.
_food_location(agents: List[opytimizer.core.agent.Agent], function: opytimizer.core.function.Function) → opytimizer.core.agent.Agent

Calculates the food location.

Parameters:
  • agents – List of agents.
  • function – A Function object that will be used as the objective function.
Returns:

A new food location.

Return type:

(Agent)

_sensing_distance(agents: List[opytimizer.core.agent.Agent], idx: int) → Tuple[float, float]

Calculates the sensing distance for an individual krill (eq. 7).

Parameters:
  • agents – List of agents.
  • idx – Selected agent.
Returns:

The sensing distance for an individual krill.

Return type:

(Tuple[float, float])

_get_neighbours(agents: List[opytimizer.core.agent.Agent], idx: int, sensing_distance: float, eucl_distance: List[float]) → List[opytimizer.core.agent.Agent]

Gathers the neighbours based on the sensing distance.

Parameters:
  • agents – List of agents.
  • idx – Selected agent.
  • sensing_distance – Sensing distanced used to gather the krill’s neighbours.
  • eucl_distance – List of euclidean distances.
Returns:

A list containing the krill’s neighbours.

Return type:

(List[Agent])

_local_alpha(agent: opytimizer.core.agent.Agent, worst: opytimizer.core.agent.Agent, best: opytimizer.core.agent.Agent, neighbours: List[opytimizer.core.agent.Agent]) → float

Calculates the local alpha (eq. 4).

Parameters:
  • agent – Selected agent.
  • worst – Worst agent.
  • best – Best agent.
  • neighbours – List of neighbours.
Returns:

The local alpha.

Return type:

(float)

_target_alpha(agent: opytimizer.core.agent.Agent, worst: opytimizer.core.agent.Agent, best: opytimizer.core.agent.Agent, C_best: float) → float

Calculates the target alpha (eq. 8).

Parameters:
  • agent – Selected agent.
  • worst – Worst agent.
  • best – Best agent.
  • C_best – Effectiveness coefficient.
Returns:

The target alpha.

Return type:

(float)

_neighbour_motion(agents: List[opytimizer.core.agent.Agent], idx: int, iteration: int, n_iterations: int, motion: numpy.ndarray) → numpy.ndarray

Performs the motion induced by other krill individuals (eq. 2).

Parameters:
  • agents – List of agents.
  • idx – Selected agent.
  • iteration – Current iteration.
  • n_iterations – Maximum number of iterations.
  • motion – Array of motions.
Returns:

The krill’s neighbour motion.

Return type:

(np.ndarray)

_food_beta(agent: opytimizer.core.agent.Agent, worst: opytimizer.core.agent.Agent, best: opytimizer.core.agent.Agent, food: numpy.ndarray, C_food: float) → numpy.ndarray

Calculates the food attraction (eq. 13).

Parameters:
  • agent – Selected agent.
  • worst – Worst agent.
  • best – Best agent.
  • food – Food location.
  • C_food – Food coefficient.
Returns:

The food attraction.

Return type:

(np.ndarray)

_best_beta(agent: opytimizer.core.agent.Agent, worst: opytimizer.core.agent.Agent, best: opytimizer.core.agent.Agent) → numpy.ndarray

Calculates the best attraction (eq. 15).

Parameters:
  • agent – Selected agent.
  • worst – Worst agent.
  • best – Best agent.
Returns:

The best attraction.

Return type:

(np.ndarray)

_foraging_motion(agents: List[opytimizer.core.agent.Agent], idx: int, iteration: int, n_iterations: int, food: numpy.ndarray, foraging: numpy.ndarray) → numpy.ndarray

Performs the foraging induced by the food location (eq. 10).

Parameters:
  • agents – List of agents.
  • idx – Selected agent.
  • iteration – Current iteration.
  • n_iterations – Maximum number of iterations.
  • food – Food location.
  • foraging – Array of foraging motions.
Returns:

The krill’s foraging motion.

Return type:

(np.ndarray)

_physical_diffusion(n_variables: int, n_dimensions: int, iteration: int, n_iterations: int) → float

Performs the physical diffusion of individual krills (eq. 16-17).

Parameters:
  • n_variables – Number of decision variables.
  • n_dimensions – Number of dimensions.
  • iteration – Current iteration.
  • n_iterations – Maximum number of iterations.
Returns:

The physical diffusion.

Return type:

(float)

_update_position(agents: List[opytimizer.core.agent.Agent], idx: int, iteration: int, n_iterations: int, food: numpy.ndarray, motion: numpy.ndarray, foraging: numpy.ndarray) → numpy.ndarray

Updates a single krill position (eq. 18-19).

Parameters:
  • agents – List of agents.
  • idx – Selected agent.
  • iteration – Current iteration.
  • n_iterations – Maximum number of iterations.
  • food – Food location.
  • motion – Array of motions.
  • foraging – Array of foraging motions.
Returns:

The updated position.

Return type:

(np.ndarray)

_crossover(agents: List[opytimizer.core.agent.Agent], idx: int) → opytimizer.core.agent.Agent

Performs the crossover between selected agent and a randomly agent (eq. 21).

Parameters:
  • agents – List of agents.
  • idx – Selected agent.
Returns:

An agent after suffering a crossover operator.

Return type:

(Agent)

_mutation(agents: List[opytimizer.core.agent.Agent], idx: int) → opytimizer.core.agent.Agent

Performs the mutation between selected agent and randomly agents (eq. 22).

Parameters:
  • agents – List of agents.
  • idx – Selected agent.
Returns:

An agent after suffering a mutation operator.

Return type:

(Agent)

update(space: opytimizer.core.space.Space, function: opytimizer.core.function.Function, iteration: int, n_iterations: int) → None

Wraps motion and genetic updates over all agents and variables.

Parameters:
  • space – Space containing agents and update-related information.
  • function – A Function object that will be used as the objective function.
  • iteration – Current iteration.
  • n_iterations – Maximum number of iterations.