gflownet.envs.composite.setflex
Classes implementing the family of Set meta-environments, which allow to combine multiple sub-environments without any specific order.
Classes
Module Contents
- class gflownet.envs.composite.setflex.SetFlex(max_elements, envs_unique=None, subenvs=None, do_random_subenvs=False, **kwargs)[source]
Bases:
gflownet.envs.composite.setbase.BaseSet- Parameters:
max_elements (int) – The maximum number of enviroments that can be included in the set. Note that this number does not refer to the number of unique environments, but the number of elements (instances of a sub-environment) that can form a set. For example, a SetFlex of up to 10 2D points would contain a single unique environment (2D ContinuousCube) with max_elements equal to 10.
envs_unique (iterable) – An iterable containing the set of unique environments that can make part of the set. This iterable is meant to contain unique environments, unique meaning that the both the type and the action space are unique. “Repeated” sub-environmens are discarded. If it is None, the unique environments may be determined from the argument subenvs.
subenvs (iterable) – An iterable used to initialize the SetFlex with a specific set of sub-environments. This list of environments plays the role of a condition for a specific trajectory. If it is None, the set of sub-environments for a trajectory may be set via
set_subenvs().do_random_subenvs (bool) – If True, the environment is initialized with a set of random sub-environments. If True, also the reset method will resample the set of sub-environments. First, the number of elements is sampled uniformly from 1 to max_elements, then the set of sub-environments is sampled with replacement from the set of unique environments. This is practical for testing purposes.
- set_subenvs(subenvs)[source]
Sets the sub-environments of the Set and applies the rest of necessary changes to the environment.
Sets self.subenvs
Determines the indices of the unique environments for each subenv.
Sets self.state by setting the correct dones, unique indices source states of the sub-envs.
The sub-environments can be thought as the conditioning variables of a specific trajectory, since the specific sub-environments are expected to be variable in a SetFlex, unlike in the (simpler) Set.
- Parameters:
subenvs (iterable) – The list of sub-environments to condition a trajectory.
- reset(env_id=None)[source]
Resets the environment by resetting the sub-environments.
If self.do_random_subenvs is True, the set of sub-environments is re-sampled.
- Parameters:
env_id (Union[int, str])
- get_env_instances_by_unique_indices(unique_indices)[source]
Returns a list of env instances corresponding to the requested unique environments. The instances have already been reset and their ID set.
- Parameters:
unique_indices (list) – Indices of the unique environments
- Returns:
A list containing instances of the requested environments
- set_state(state, done=False)[source]
Sets a state and done.
It also sets the sub-environments as specified in the unique indices of the state by making copies of the unique environments.
- Parameters:
state (dict) – A state of the Set environment.
done (bool) – Whether the trajectory of the environment is done or not.
- states2policy(states)[source]
Prepares a batch of states in environment format for the policy model.
The default policy representation is the concatenation of the following elements: - One-hot encoding of the active sub-environment - Toggle flag - Done flag of each sub-environment - A vector indicating the number of sub-environments of each unique environment
present in the state.
- For each unique environment:
A concatenation of the policy-format states of the sub-environments, padded up to self.max_elements with the policy representation of the source states.
- Parameters:
states (list) – A batch of states in environment format.
- Returns:
A tensor containing all the states in the batch.
- Return type:
torchtyping.TensorType[batch, state_policy_dim]
- states2proxy(states)[source]
Prepares a batch of states in environment format for a proxy.
The default proxy format is similar to the environment format, except that the states of the sub-enviroments in the dictionary are converted into their corresponding proxy formats.
- Parameters:
states (list) – A batch of states in environment format.
- Returns:
A tensor containing all the states in the batch.
- Return type:
torchtyping.TensorType[batch, state_oracle_dim]
- state2readable(state=None)[source]
Converts a state into human-readable representation.
It concatenates the readable representations of each sub-environment preceded by the index of its unique environment (idx: …), separated by “; “ and all preceded by Set meta-data: active sub-environment and toggle flag. If a sub-environment is done, it is indicanted with “ | done” after the state.
- Parameters:
state (dict) – A state in environment format.
- Returns:
str – The state in readable format.
- Return type:
str
- readable2state(readable)[source]
Converts a human-readable representation of a state into the standard format.
- Parameters:
readable (str)
- Return type:
List[int]
- is_source(state=None)[source]
Returns True if the environment’s state or the state passed as parameter (if not None) is the source state of the environment.
This method is overriden to check the meta-data about non-present states, which is special of the SetFlex..
- Parameters:
state (dict) – None, or a state in environment format.
- Returns:
bool – Whether the state is the source state of the environment
- Return type:
bool
- __eq__(other, ignored_keys=[])[source]
Checks whether the current environment instance is equal to the input environment instance.
- This method is overriden to ignore the keys:
subenvsifself.do_random_subenvsis True, because in this case resetting the environment will resample the sub-environments.stateifself.do_random_subenvsis True, because in this case resetting the environment will resample the sub-environments and set the state accordingly.
- Parameters:
other (GFlowNetEnv) – The environment instance to be compared.
ignored_keys (list) – A list of keys (strings) to be ignored in the comparison. This parameter may be used by subclasses that may need to ignore certain keys.
- Returns:
bool – True if the environments’s attributes are considered equal; False otherwise.
- Return type:
bool