Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adding mutation as parent operator #262

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def partial_fit(self, experience: ExperienceBuffer):
def _get_experience(self, experience: ExperienceBuffer):
""" Get experience from ExperienceBuffer, process rewards and log. """
obs, actions, rewards = experience.retrieve_experience()
arms = [self._arm_by_action[action] for action in actions]
arms = [self._arm_by_action[action.__name__] for action in actions]
# there is no need to process rewards as in MAB, since this processing unifies rewards for all contexts
self._dbg_log(obs, actions, rewards)
return obs, arms, rewards
Expand Down
16 changes: 14 additions & 2 deletions golem/core/optimisers/adaptive/mab_agents/mab_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import _pickle as pickle
import random
import re
from typing import Union, Sequence, Optional
from functools import partial
from typing import Union, Sequence, Optional, Callable

from mabwiser.mab import MAB, LearningPolicy
from scipy.special import softmax
Expand All @@ -25,7 +26,8 @@ def __init__(self,
super().__init__(actions=actions, enable_logging=enable_logging)
self.actions = list(actions)
self._indices = list(range(len(actions)))
self._arm_by_action = dict(zip(actions, self._indices))
# str because parent operator for mutation is stored as string for custom mutations serialisation
self._arm_by_action = dict(map(lambda x, y: (self._get_callable_name(x), y), actions, self._indices))
self._agent = MAB(arms=self._indices,
learning_policy=LearningPolicy.EpsilonGreedy(epsilon=0.4),
n_jobs=n_jobs)
Expand All @@ -34,6 +36,16 @@ def __init__(self,
self._initial_fit()
self._path_to_save = path_to_save

@staticmethod
def _get_callable_name(action: Callable):
if isinstance(action, partial):
return action.func.__name__
else:
try:
return action.__name__
except AttributeError:
return str(action)

def _initial_fit(self):
n = len(self.actions)
uniform_rewards = [1. / n] * n
Expand Down
4 changes: 4 additions & 0 deletions golem/core/optimisers/genetic/operators/base_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class MutationTypesEnum(Enum):

none = 'none'

@property
def __name__(self):
return self.name


def get_mutation_prob(mut_id: MutationStrengthEnum, node: Optional[GraphNode],
default_mutation_prob: float = 0.7) -> float:
Expand Down
3 changes: 2 additions & 1 deletion golem/core/optimisers/genetic/operators/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def _mutation(self, individual: Individual) -> Tuple[Individual, bool]:
new_graph = self._apply_mutations(new_graph, mutation_type)
is_correct_graph = self.graph_generation_params.verifier(new_graph)
if is_correct_graph:
# str for custom mutations serialisation
parent_operator = ParentOperator(type_='mutation',
operators=mutation_type,
operators=mutation_type.__name__,
parent_individuals=individual)
individual = Individual(new_graph, parent_operator,
metadata=self.requirements.static_individual_metadata)
Expand Down
Loading