Skip to content

Commit

Permalink
create constants for frequently used labels
Browse files Browse the repository at this point in the history
  • Loading branch information
MorrisNein committed Oct 30, 2023
1 parent 1c7ef13 commit 1c42b3f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 26 deletions.
12 changes: 7 additions & 5 deletions golem/core/optimisers/genetic/gp_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from copy import deepcopy
from random import choice
from typing import Sequence, Union, Any
from typing import Any, Sequence, Union

from golem.core.constants import MAX_GRAPH_GEN_ATTEMPTS
from golem.core.dag.graph import Graph
Expand All @@ -9,15 +9,17 @@
from golem.core.optimisers.genetic.operators.elitism import Elitism
from golem.core.optimisers.genetic.operators.inheritance import Inheritance
from golem.core.optimisers.genetic.operators.mutation import Mutation
from golem.core.optimisers.genetic.operators.operator import PopulationT, EvaluationOperator
from golem.core.optimisers.genetic.operators.operator import EvaluationOperator, PopulationT
from golem.core.optimisers.genetic.operators.regularization import Regularization
from golem.core.optimisers.genetic.operators.reproduction import ReproductionController
from golem.core.optimisers.genetic.operators.selection import Selection
from golem.core.optimisers.genetic.parameters.graph_depth import AdaptiveGraphDepth
from golem.core.optimisers.genetic.parameters.operators_prob import init_adaptive_operators_prob
from golem.core.optimisers.genetic.parameters.population_size import init_adaptive_pop_size, PopulationSize
from golem.core.optimisers.genetic.parameters.population_size import PopulationSize, init_adaptive_pop_size
from golem.core.optimisers.objective.objective import Objective
from golem.core.optimisers.opt_history_objects.individual import Individual
from golem.core.optimisers.opt_history_objects.opt_history import EXTENDED_INITIAL_ASSUMPTIONS_LABEL, \
INITIAL_ASSUMPTIONS_LABEL
from golem.core.optimisers.optimization_parameters import GraphRequirements
from golem.core.optimisers.optimizer import GraphGenerationParams
from golem.core.optimisers.populational_optimizer import PopulationalOptimizer
Expand Down Expand Up @@ -64,13 +66,13 @@ def __init__(self,
def _initial_population(self, evaluator: EvaluationOperator):
""" Initializes the initial population """
# Adding of initial assumptions to history as zero generation
self._update_population(evaluator(self.initial_individuals), 'initial_assumptions')
self._update_population(evaluator(self.initial_individuals), INITIAL_ASSUMPTIONS_LABEL)
pop_size = self.graph_optimizer_params.pop_size

if len(self.initial_individuals) < pop_size:
self.initial_individuals = self._extend_population(self.initial_individuals, pop_size)
# Adding of extended population to history
self._update_population(evaluator(self.initial_individuals), 'extended_initial_assumptions')
self._update_population(evaluator(self.initial_individuals), EXTENDED_INITIAL_ASSUMPTIONS_LABEL)

def _extend_population(self, pop: PopulationT, target_pop_size: int) -> PopulationT:
verifier = self.graph_generation_params.verifier
Expand Down
4 changes: 3 additions & 1 deletion golem/core/optimisers/meta/surrogate_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from golem.core.optimisers.meta.surrogate_evaluator import SurrogateDispatcher
from golem.core.optimisers.meta.surrogate_model import RandomValuesSurrogateModel
from golem.core.optimisers.objective import Objective, ObjectiveFunction
from golem.core.optimisers.opt_history_objects.opt_history import EVOLUTION_RESULTS_LABEL
from golem.core.optimisers.optimization_parameters import GraphRequirements
from golem.core.optimisers.optimizer import GraphGenerationParams
from golem.core.optimisers.populational_optimizer import EvaluationAttemptsError, _try_unfit_graph
Expand All @@ -17,6 +18,7 @@ class SurrogateEachNgenOptimizer(EvoGraphOptimizer):
Additionally, we need to pass surrogate_model object
"""

def __init__(self,
objective: Objective,
initial_graphs: Sequence[OptGraph],
Expand Down Expand Up @@ -54,5 +56,5 @@ def optimise(self, objective: ObjectiveFunction) -> Sequence[OptGraph]:
break
# Adding of new population to history
self._update_population(new_population)
self._update_population(self.best_individuals, 'evolution_results')
self._update_population(self.best_individuals, EVOLUTION_RESULTS_LABEL)
return [ind.graph for ind in self.best_individuals]
14 changes: 10 additions & 4 deletions golem/core/optimisers/opt_history_objects/opt_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
if TYPE_CHECKING:
from golem.core.optimisers.opt_history_objects.individual import Individual

INITIAL_ASSUMPTIONS_LABEL = 'initial_assumptions'
EXTENDED_INITIAL_ASSUMPTIONS_LABEL = 'extended_initial_assumptions'
EVOLUTION_RESULTS_LABEL = 'evolution_results'
TUNING_START_LABEL = 'tuning_start'
TUNING_RESULT_LABEL = 'tuning_result'


class OptHistory:
"""
Expand Down Expand Up @@ -212,7 +218,7 @@ def initial_assumptions(self) -> Optional[Generation]:
if not self.generations:
return None
for gen in self.generations:
if gen.label == 'initial_assumptions':
if gen.label == INITIAL_ASSUMPTIONS_LABEL:
return gen

@property
Expand All @@ -224,23 +230,23 @@ def evolution_results(self) -> Optional[Generation]:
if not self.generations:
return None
for gen in reversed(self.generations):
if gen.label == 'evolution_results':
if gen.label == EVOLUTION_RESULTS_LABEL:
return gen

@property
def tuning_start(self) -> Optional[Generation]:
if not self.generations:
return None
for gen in reversed(self.generations):
if gen.label == 'tuning_start':
if gen.label == TUNING_START_LABEL:
return gen

@property
def tuning_result(self) -> Optional[Generation]:
if not self.generations:
return None
for gen in reversed(self.generations):
if gen.label == 'tuning_result':
if gen.label == TUNING_RESULT_LABEL:
return gen

@property
Expand Down
9 changes: 5 additions & 4 deletions golem/core/optimisers/populational_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from abc import abstractmethod
from random import choice
from typing import Any, Optional, Sequence, Dict
from typing import Any, Dict, Optional, Sequence

from golem.core.constants import MIN_POP_SIZE
from golem.core.dag.graph import Graph
from golem.core.optimisers.archive import GenerationKeeper
from golem.core.optimisers.genetic.evaluation import MultiprocessingDispatcher, SequentialDispatcher
from golem.core.optimisers.genetic.operators.operator import PopulationT, EvaluationOperator
from golem.core.optimisers.genetic.operators.operator import EvaluationOperator, PopulationT
from golem.core.optimisers.objective import GraphFunction, ObjectiveFunction
from golem.core.optimisers.objective.objective import Objective
from golem.core.optimisers.opt_history_objects.individual import Individual
from golem.core.optimisers.opt_history_objects.opt_history import EVOLUTION_RESULTS_LABEL
from golem.core.optimisers.optimization_parameters import GraphRequirements
from golem.core.optimisers.optimizer import GraphGenerationParams, GraphOptimizer, AlgorithmParameters
from golem.core.optimisers.optimizer import AlgorithmParameters, GraphGenerationParams, GraphOptimizer
from golem.core.optimisers.timer import OptimisationTimer
from golem.core.utilities.grouped_condition import GroupedCondition

Expand Down Expand Up @@ -105,7 +106,7 @@ def optimise(self, objective: ObjectiveFunction) -> Sequence[Graph]:
# Adding of new population to history
self._update_population(new_population)
pbar.close()
self._update_population(self.best_individuals, 'evolution_results')
self._update_population(self.best_individuals, EVOLUTION_RESULTS_LABEL)
return [ind.graph for ind in self.best_individuals]

@property
Expand Down
8 changes: 5 additions & 3 deletions golem/core/optimisers/random/random_mutation_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Union, Optional, Sequence
from typing import Optional, Sequence, Union

from golem.core.dag.graph import Graph
from golem.core.optimisers.genetic.gp_params import GPAlgorithmParameters
from golem.core.optimisers.genetic.operators.mutation import Mutation
from golem.core.optimisers.genetic.operators.operator import EvaluationOperator, PopulationT
from golem.core.optimisers.objective import Objective
from golem.core.optimisers.opt_history_objects.individual import Individual
from golem.core.optimisers.opt_history_objects.opt_history import EXTENDED_INITIAL_ASSUMPTIONS_LABEL, \
INITIAL_ASSUMPTIONS_LABEL
from golem.core.optimisers.optimization_parameters import GraphRequirements
from golem.core.optimisers.optimizer import GraphGenerationParams
from golem.core.optimisers.populational_optimizer import PopulationalOptimizer
Expand Down Expand Up @@ -38,13 +40,13 @@ def _evolve_population(self, evaluator: EvaluationOperator) -> PopulationT:
return new_population

def _initial_population(self, evaluator: EvaluationOperator):
self._update_population(evaluator(self.initial_individuals), 'initial_assumptions')
self._update_population(evaluator(self.initial_individuals), INITIAL_ASSUMPTIONS_LABEL)
pop_size = self.graph_optimizer_params.pop_size

if len(self.initial_individuals) < pop_size:
self.initial_individuals = self._extend_population(self.initial_individuals, pop_size)
# Adding of extended population to history
self._update_population(evaluator(self.initial_individuals), 'extended_initial_assumptions')
self._update_population(evaluator(self.initial_individuals), EXTENDED_INITIAL_ASSUMPTIONS_LABEL)


class RandomMutationOptimizer(RandomSearchOptimizer):
Expand Down
9 changes: 5 additions & 4 deletions golem/core/optimisers/random/random_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from golem.core.optimisers.graph import OptGraph
from golem.core.optimisers.objective import Objective, ObjectiveFunction
from golem.core.optimisers.opt_history_objects.individual import Individual
from golem.core.optimisers.opt_history_objects.opt_history import EVOLUTION_RESULTS_LABEL, INITIAL_ASSUMPTIONS_LABEL
from golem.core.optimisers.optimization_parameters import GraphRequirements
from golem.core.optimisers.optimizer import GraphOptimizer, GraphGenerationParams
from golem.core.optimisers.optimizer import GraphGenerationParams, GraphOptimizer
from golem.core.optimisers.timer import OptimisationTimer
from golem.core.utilities.grouped_condition import GroupedCondition

Expand All @@ -34,7 +35,7 @@ def __init__(self,
'Optimisation stopped: Time limit is reached'
).add_condition(
lambda: requirements.num_of_generations is not None and
self.current_iteration_num >= requirements.num_of_generations,
self.current_iteration_num >= requirements.num_of_generations,
'Optimisation stopped: Max number of iterations reached')

def optimise(self, objective: ObjectiveFunction) -> Sequence[OptGraph]:
Expand All @@ -46,14 +47,14 @@ def optimise(self, objective: ObjectiveFunction) -> Sequence[OptGraph]:

with self.timer, self._progressbar as pbar:
self.best_individual = self._eval_initial_individual(evaluator)
self._update_best_individual(self.best_individual, 'initial_assumptions')
self._update_best_individual(self.best_individual, INITIAL_ASSUMPTIONS_LABEL)
while not self.stop_optimization():
new_individual = self._generate_new_individual()
evaluator([new_individual])
self.current_iteration_num += 1
self._update_best_individual(new_individual)
pbar.update()
self._update_best_individual(self.best_individual, 'evolution_results')
self._update_best_individual(self.best_individual, EVOLUTION_RESULTS_LABEL)
pbar.close()
return [self.best_individual.graph]

Expand Down
10 changes: 5 additions & 5 deletions golem/core/tuning/tuner_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from golem.core.optimisers.graph import OptGraph
from golem.core.optimisers.objective import ObjectiveEvaluate, ObjectiveFunction
from golem.core.optimisers.opt_history_objects.individual import Individual
from golem.core.optimisers.opt_history_objects.opt_history import OptHistory
from golem.core.optimisers.opt_history_objects.opt_history import OptHistory, TUNING_RESULT_LABEL, TUNING_START_LABEL
from golem.core.optimisers.opt_history_objects.parent_operator import ParentOperator
from golem.core.tuning.search_space import SearchSpace, convert_parameters
from golem.core.utilities.data_structures import ensure_wrapped_in_sequence
Expand Down Expand Up @@ -96,7 +96,7 @@ def init_check(self, graph: OptGraph) -> None:
graph = deepcopy(graph)
fitness = self.objective_evaluate(graph)
self.init_individual = self._create_individual(graph, fitness)
self._add_to_history([self.init_individual], label='tuning_start')
self._add_to_history([self.init_individual], label=TUNING_START_LABEL)

init_metric = self._fitness_to_metric_value(fitness)
self.log.message(f'Initial graph: {graph_structure(graph)} \n'
Expand Down Expand Up @@ -154,7 +154,7 @@ def _single_obj_final_check(self, tuned_graph: OptGraph):
self.log.message('Final metric is None')

self.obtained_individual = final_individual
self._add_to_history([self.obtained_individual], label='tuning_result')
self._add_to_history([self.obtained_individual], label=TUNING_RESULT_LABEL)

return self.obtained_individual.graph

Expand All @@ -179,7 +179,7 @@ def _multi_obj_final_check(self, tuned_graphs: Sequence[OptGraph]) -> Sequence[O
self.obtained_individual = [self.init_individual]
final_graphs = [self.init_individual.graph]

self._add_to_history(self.obtained_individual, label='tuning_result')
self._add_to_history(self.obtained_individual, label=TUNING_RESULT_LABEL)

return final_graphs

Expand Down Expand Up @@ -284,7 +284,7 @@ def _add_to_history(self, individuals: Sequence[Individual], label: Optional[str

if label is None:
label = f'tuning_iteration_{self.evaluations_count}'
if label not in ('tuning_start', 'tuning_result'):
if label not in (TUNING_START_LABEL, TUNING_RESULT_LABEL):
individuals = list(individuals)
individuals.append(self.init_individual) # add initial individual to maintain consistency of inheritance
history.add_to_history(individuals=individuals,
Expand Down

0 comments on commit 1c42b3f

Please sign in to comment.