dacbench.envs.onell_env¶
Module Contents¶
Classes¶
An abstract class for an individual in binary representation |
|
An individual for OneMax problem |
|
An individual for LeadingOne problem |
|
Environment for (1+(lbd, lbd))-GA |
- class dacbench.envs.onell_env.BinaryProblem(n, val=None, rng=np.random.default_rng())¶
An abstract class for an individual in binary representation
- is_optimal(self)¶
- get_optimal(self)¶
- eval(self)¶
- abstract get_fitness_change_after_mutation(self, locs)¶
Calculate the change in fitness after flipping the bits at positions locs
- Parameters
locs (1d-array) – positions where bits are flipped
objective after mutation - objective before mutation
- abstract get_fitness_after_crossover(self, xprime, locs_x, locs_xprime)¶
Calculate fitness of the child when crossover with xprime, without doing the actual crossover
- Parameters
xprime (1d boolean array) – the individual to crossover with
locs_x (1d boolean/integer array) – positions where we keep current bits of self
locs_xprime (: 1d boolean/integer array) – positions where we change to xprime’s bits
fitness of the new individual after crossover
- mutate(self, p, n_offsprings, rng=np.random.default_rng())¶
Draw l ~ binomial(n, p), l>0 Generate n_offsprings children by flipping exactly l bits and select the best one
- Parameters
p (float) – mutation probability, in range of [0,1]
n_offsprings (int) – number of mutated children
- Returns
- Return type
the best child (maximum fitness), its fitness and number of evaluations used
- crossover(self, xprime, p, n_offsprings, include_xprime=True, count_different_inds_only=True, rng=np.random.default_rng())¶
- Generate n_offsprings children using crossover on self and xprime, and return the best one
Crossover operator: for each bit, taking value from xprime with probability p and from self with probability 1-p
- Parameters
xprime (1d boolean numpy array) – the individual to crossover with
p (float) – crossover bias, in range of [0,1]
include_xprime (boolean) – if True, include xprime in the selection for the best individual after all crossovers
count_different_inds_only (boolean) – if True, only count an evaluation of a child if it is different from both of its parents (self and xprime)
rng (numpy random generator) –
- Returns
- Return type
the best child (maximum fitness), its fitness and number of evaluations used
- class dacbench.envs.onell_env.OneMax(n, val=None, rng=np.random.default_rng())¶
Bases:
dacbench.envs.onell_env.BinaryProblemAn individual for OneMax problem The aim is to maximise the number of 1 bits
- eval(self)¶
- is_optimal(self)¶
- get_optimal(self)¶
- get_fitness_change_after_mutation(self, locs)¶
Calculate the change in fitness after flipping the bits at positions locs
- Parameters
locs (1d-array) – positions where bits are flipped
objective after mutation - objective before mutation
- get_fitness_after_crossover(self, xprime, locs_x, locs_xprime)¶
Calculate fitness of the child when crossover with xprime, without doing the actual crossover
- Parameters
xprime (1d boolean array) – the individual to crossover with
locs_x (1d boolean/integer array) – positions where we keep current bits of self
locs_xprime (: 1d boolean/integer array) – positions where we change to xprime’s bits
fitness of the new individual after crossover
- class dacbench.envs.onell_env.LeadingOne(n, val=None, rng=np.random.default_rng())¶
Bases:
dacbench.envs.onell_env.BinaryProblemAn individual for LeadingOne problem The aim is to maximise the number of leading (and consecutive) 1 bits in the string
- eval(self)¶
- is_optimal(self)¶
- get_optimal(self)¶
- get_fitness_change_after_mutation(self, locs)¶
Calculate the change in fitness after flipping the bits at positions locs
- Parameters
locs (1d-array) – positions where bits are flipped
objective after mutation - objective before mutation
- get_fitness_after_crossover(self, xprime, locs_x, locs_xprime)¶
this implementation should be improved
- dacbench.envs.onell_env.HISTORY_LENGTH = 5¶
- class dacbench.envs.onell_env.OneLLEnv(config)¶
Bases:
dacbench.AbstractEnvEnvironment for (1+(lbd, lbd))-GA for both OneMax and LeadingOne problems
- reset(self)¶
Resets env
- Returns
Environment state
- Return type
numpy.array
- get_state(self)¶
- get_onell_params(self, action)¶
Get OneLL-GA parameters (lbd1, lbd2, p and c) from an action
- Returns: lbd1, lbd2, p, c
- lbd1: float (will be converted to int in step())
number of mutated off-springs: in range [1,n]
- lbd2: float (will be converted to int in step())
number of crossovered off-springs: in range [1,n]
- p: float
mutation probability
- c: float
crossover bias
- step(self, action)¶
Execute environment step
- Parameters
action (Box) – action to execute
- Returns
state, reward, done, info
np.array, float, bool, dict
- plot_agent_prediction(self, agent, dirname)¶
Plot agent progress for this particular environment
- close(self) bool¶
Close Env
No additional cleanup necessary
- Returns
Closing confirmation
- Return type
bool