fragile.core.swarm#

Module Contents#

Classes#

Swarm

The Swarm implements the iteration logic to make the Walkers evolve.

FunctionMapper

It is a swarm adapted to minimize mathematical functions.

class fragile.core.swarm.Swarm(n_walkers, env, policy, walkers, callbacks=None, minimize=False, max_epochs=1e+100)[source]#

Bases: fragile.core.api_classes.SwarmAPI

The Swarm implements the iteration logic to make the Walkers evolve.

It contains the necessary logic to use an Environment, a Model, and a Walkers instance to create the algorithm execution loop.

This class defines a method called run() that receives two optional arguments, root_walker and state, and has no return value. This method runs the fractal AI Swarm evolution process until a stop condition is met.

In its implementation, it calls several other methods: (before_reset(), reset(), after_reset(), evolve(), before_evolve(), after_evolve(), evolution_end(), before_env(), after_env(), before_policy(), after_policy(), before_walkers(), after_walkers(), run_end()) defined within the same class, which are mainly used to manage different aspects of the search process or to invoke user-defined callbacks.

The evolve() method updates the states of the search environment and model, makes the walkers undergo a perturbation process, and balances them. It also invokes several other methods that trigger user-defined callbacks.

The evolution_end() method returns True if any of the following conditions is met: 1. The current epoch exceeds the maximum allowed epochs. 2. All walkers are out of the problem domain. 3. Any callback of the class has set the evolution_end flag to True.

walkers_last#

If True indicates that the Walkers class runs after acting on the environment. If Fase, the walkers run before acting on the environment.

Type

bool

Parameters
  • n_walkers (int) – The number of walkers in the swarm.

  • env (EnvironmentAPI) – An environment that simulates the objective function.

  • policy (PolicyAPI) – A policy that defines how the individuals evolve.

  • walkers (WalkersAPI) – A set of motion rules to control a population of walkers.

  • callbacks (Optional[Iterable[Callback]]) – A list of functions to call at each iteration.

  • minimize (bool) – If True, take the minimum value of fitness, else take the maximum.

  • max_epochs (int) – Maximum number of epochs allowed before the swarm search is stopped.

__repr__()[source]#

Return repr(self).

to_html()[source]#
_state_stats_df()[source]#
_setup_inputs()[source]#
_setup_clone_names()[source]#
_setup_components()[source]#
class fragile.core.swarm.FunctionMapper(minimize=True, start_same_pos=False, **kwargs)[source]#

Bases: Swarm

It is a swarm adapted to minimize mathematical functions.

Parameters
  • minimize (bool) –

  • start_same_pos (bool) –

classmethod from_function(function, bounds, **kwargs)[source]#

Initialize a FunctionMapper using a python callable and a Bounds instance.

Parameters
  • function (Callable) – Callable representing an arbitrary function to be optimized.

  • bounds (judo.data_structures.bounds.Bounds) – Represents the domain of the function to be optimized.

  • **kwargs – Passed to FunctionMapper __init__.

Returns

Instance of FunctionMapper that optimizes the target function.

Return type

FunctionMapper

__repr__()[source]#

Return repr(self).

reset(root_walker=None, state=None)[source]#

Reset the fragile.Walkers, the Function environment, the Model and clear the internal data to start a new search process.

Parameters
  • root_walker (Optional[OneWalker]) – Walker representing the initial state of the search. The walkers will be reset to this walker, and it will be added to the root of the StateTree if any.

  • state (Optional[fragile.core.state.SwarmState]) – StateData dictionary that define the initial state of the Swarm.