fragile.algorithms.jump_best#

Module Contents#

Classes#

DummyFMCPolicy

The policy is in charge of calculating the interactions with the Environment.

EnvSwarm

The Environment is in charge of stepping the walkers, acting as a state transition function.

JumpToBestTree

Tree data structure that keeps track of the visited states.

JumpToBestEnv

The Environment is in charge of stepping the walkers, acting as a state transition function.

WalkersSwarm

The WalkersAPI class defines the base functionality for managing walkers in a swarm simulation.

JumpToBest

The WalkersAPI class defines the base functionality for managing walkers in a swarm simulation.

FMCSwarm

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

class fragile.algorithms.jump_best.DummyFMCPolicy(inner_swarm, **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.

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]

act(inplace=True, **kwargs)[source]#

Calculate SwarmState containing the data needed to interact with the environment.

Parameters
  • inplace (bool, optional) – If True, updates the swarm state with the selected actions. If False, returns the selected actions. Defaults to True.

  • **kwargs – Additional keyword arguments required for selecting actions.

Returns

None if inplace is True. Otherwise, a StateData dictionary

containing the selected actions.

Return type

Union[None, StateData]

class fragile.algorithms.jump_best.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.

__getattr__(item)[source]#
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. If True, 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 by step will contain the returned data.

Return type

Union[None, fragile.core.typing.StateData]

step(**kwargs)[source]#

Return the data corresponding to the new state of the environment after using the input data to make the corresponding state transition.

Return type

fragile.core.typing.StateData

class fragile.algorithms.jump_best.JumpToBestTree(names=None, prune=False, root_id=DEFAULT_ROOT_ID, node_names=None, edge_names=('actions', 'dt'), next_prefix=NetworkxTree.DEFAULT_NEXT_PREFIX, **kwargs)[source]#

Bases: fragile.callbacks.tree.HistoryTree

Tree data structure that keeps track of the visited states.

It allows to save the Swarm data after every iteration and methods to recover the sampled data after the algorithm run.

The data that will be stored in the graph it’s defined in the names parameter. For example:

  • If names is ["observs", "actions"], the observations of every visited state will be stored as node attributes, and actions will be stored as edge attributes.

  • If names if ["observs", "actions", "next_observs"] the same data will be stored, but when the data generator methods are called the observation corresponding to the next state will also be returned.

The attribute names also defines the order of the data returned by the generator.

As long as the data is stored in the graph (passing a valid names list at initialization, the order of the data can be redefined passing the names parameter to the generator method.

For example, if the names passed at initialization is ["states", "rewards"], you can call the generator methods with names=["rewards", "states", "next_states"] and the returned data will be a tuple containing (rewards, states, next_states).

Parameters
  • names (fragile.core.typing.NamesData) –

  • prune (bool) –

  • root_id (fragile.core.typing.NodeId) –

  • node_names (fragile.core.typing.NamesData) –

  • edge_names (fragile.core.typing.NamesData) –

  • next_prefix (str) –

__len__()[source]#
after_evolve()[source]#
after_env()[source]#
update_tree()[source]#
class fragile.algorithms.jump_best.JumpToBestEnv(swarm)[source]#

Bases: EnvSwarm

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.

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. If True, 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 by step will contain the returned data.

Return type

Union[None, fragile.core.typing.StateData]

reset(inplace=True, root_walker=None, states=None, **kwargs)[source]#

Reset the internal state of the SwarmComponent.

Parameters
  • inplace (bool, optional) – If False return the new data. If True, update the state of the Swarm. Defaults to True.

  • root_walker (Optional[StateData], optional) – _description_. Defaults to None.

  • states (Optional[StateData], optional) – _description_. Defaults to None.

  • inactives (bool, optional) – Whether to update the walkers marked as inactive. Defaults to True.

  • kwargs – Other arguments passed to make_transitions.

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 by step will contain the returned data.

Return type

Union[None, StateData]

class fragile.algorithms.jump_best.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

__getattr__(item)[source]#
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

reset(inplace=True, **kwargs)[source]#

Reset the internal state of the Walkers class. This method should be implemented in the derived class.

Parameters
  • inplace (bool, optional) – Unused. Defaults to True.

  • **kwargs – Additional keyword arguments required for resetting the component.

Returns

None

class fragile.algorithms.jump_best.JumpToBest(inner_swarm, **kwargs)[source]#

Bases: WalkersSwarm

The WalkersAPI class defines the base functionality for managing walkers in a swarm simulation. This class inherits from the SwarmComponent class.

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

reset(inplace=True, **kwargs)[source]#

Reset the internal state of the Walkers class. This method should be implemented in the derived class.

Parameters
  • inplace (bool, optional) – Unused. Defaults to True.

  • **kwargs – Additional keyword arguments required for resetting the component.

Returns

None

class fragile.algorithms.jump_best.FMCSwarm(swarm, policy=None, env=None, callbacks=None, minimize=False, max_epochs=1e+100)[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 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.

  • swarm (fragile.core.swarm.Swarm) –

walkers_last = False#
property swarm#
Return type

fragile.core.swarm.Swarm

property n_walkers#

Return the number of walkers in the swarm.

Return type

int

inputs_from_swarm(name)[source]#