fragile.algorithms.fmc
Contents
fragile.algorithms.fmc
#
Module Contents#
Classes#
The |
|
The policy is in charge of calculating the interactions with the |
|
The Environment is in charge of stepping the walkers, acting as a state transition function. |
|
The WalkersAPI class defines the base functionality for managing walkers in a swarm simulation. |
|
The Swarm implements the iteration logic to make the |
- class fragile.algorithms.fmc.BoundaryInitAction(only_first_epoch=True, **kwargs)[source]#
Bases:
fragile.core.api_classes.Callback
The
Walkers
is a data structure that takes care of all the data involved in making a Swarm evolve.- Parameters
only_first_epoch (bool) –
- name = boundary_action#
- default_outputs = ['actions']#
- class fragile.algorithms.fmc.FMCPolicy(inner_swarm, follow_best=False, **kwargs)[source]#
Bases:
fragile.core.api_classes.PolicyAPI
The policy is in charge of calculating the interactions with the
Environment
.The PolicyAPI class is responsible for defining the policy that determines the actions for interacting with the
Environment
in a swarm simulation. This is an abstract base class, and specific policy implementations should inherit from this class and implement the ‘select_actions’ method.- Parameters
follow_best (bool) –
- default_inputs#
- property param_dict#
Return the dictionary defining all the data attributes that the component requires.
- Return type
fragile.core.typing.StateDict
- select_actions(**kwargs)[source]#
Select actions for each walker in the swarm based on the current state.
This method must be implemented by subclasses.
- Parameters
**kwargs – Additional keyword arguments required for selecting actions.
- Returns
The selected actions as a Tensor or a StateData dictionary.
- Return type
Union[Tensor, StateData]
- class fragile.algorithms.fmc.EnvSwarm(swarm)[source]#
Bases:
fragile.core.api_classes.EnvironmentAPI
The Environment is in charge of stepping the walkers, acting as a state transition function.
For every different problem, a new Environment needs to be implemented following the
EnvironmentAPI
interface.- property inputs#
Return a dictionary containing the data that this component needs to function.
- Return type
fragile.core.typing.InputDict
- property outputs#
Return a tuple containing the names of the data attribute that the component outputs.
- Return type
Tuple[str, Ellipsis]
- property param_dict#
Return the dictionary defining all the data attributes that the component requires.
- Return type
fragile.core.typing.StateDict
- make_transitions(inplace=True, **kwargs)[source]#
Return the data corresponding to the new state of the environment after using the input data to make the corresponding state transition.
- Parameters
inplace (bool) – If
False
return the new data. IfTrue
, update the state of the Swarm.inactives – Whether to update the walkers marked as inactive.
**kwargs – Keyword arguments passed if the returned value from the
states_to_data
function of the class was a dictionary.
- Returns
Dictionary containing the data representing the state of the environment after the state transition. The keys of the dictionary are the names of the data attributes and its values are arrays representing a batch of new values for that attribute.
The
StatesEnv
returned bystep
will contain the returned data.- Return type
Union[None, fragile.core.typing.StateData]
- class fragile.algorithms.fmc.WalkersSwarm(inner_swarm, **kwargs)[source]#
Bases:
fragile.core.api_classes.WalkersAPI
The WalkersAPI class defines the base functionality for managing walkers in a swarm simulation. This class inherits from the SwarmComponent class.
- property param_dict#
Return the dictionary defining all the data attributes that the component requires.
- Return type
fragile.core.typing.StateDict
- run_epoch(inplace=True, **kwargs)[source]#
Implement the functionality for running an epoch in the derived class. This method is called during the balance operation.
- Parameters
inplace (bool, optional) – If True, updates the swarm state with the data generated during the epoch. If False, returns the data. Defaults to True.
**kwargs – Additional keyword arguments required for running the epoch.
- Returns
- A dictionary containing the data generated during the epoch if inplace is
False. Otherwise, returns None.
- Return type
StateData
- class fragile.algorithms.fmc.FMCSwarm(swarm, policy=None, env=None, callbacks=None, minimize=False, max_epochs=1e+100, store_init_action=None)[source]#
Bases:
fragile.core.swarm.Swarm
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 theevolution_end
flag toTrue
.- 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
- 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.
swarm (fragile.core.swarm.Swarm) –
store_init_action (fragile.core.api_classes.Callback) –
- walkers_last = False#
- property swarm#
- Return type