Skip to content

Commit 1c42b3f

Browse files
committed
create constants for frequently used labels
1 parent 1c7ef13 commit 1c42b3f

File tree

7 files changed

+40
-26
lines changed

7 files changed

+40
-26
lines changed

golem/core/optimisers/genetic/gp_optimizer.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from copy import deepcopy
22
from random import choice
3-
from typing import Sequence, Union, Any
3+
from typing import Any, Sequence, Union
44

55
from golem.core.constants import MAX_GRAPH_GEN_ATTEMPTS
66
from golem.core.dag.graph import Graph
@@ -9,15 +9,17 @@
99
from golem.core.optimisers.genetic.operators.elitism import Elitism
1010
from golem.core.optimisers.genetic.operators.inheritance import Inheritance
1111
from golem.core.optimisers.genetic.operators.mutation import Mutation
12-
from golem.core.optimisers.genetic.operators.operator import PopulationT, EvaluationOperator
12+
from golem.core.optimisers.genetic.operators.operator import EvaluationOperator, PopulationT
1313
from golem.core.optimisers.genetic.operators.regularization import Regularization
1414
from golem.core.optimisers.genetic.operators.reproduction import ReproductionController
1515
from golem.core.optimisers.genetic.operators.selection import Selection
1616
from golem.core.optimisers.genetic.parameters.graph_depth import AdaptiveGraphDepth
1717
from golem.core.optimisers.genetic.parameters.operators_prob import init_adaptive_operators_prob
18-
from golem.core.optimisers.genetic.parameters.population_size import init_adaptive_pop_size, PopulationSize
18+
from golem.core.optimisers.genetic.parameters.population_size import PopulationSize, init_adaptive_pop_size
1919
from golem.core.optimisers.objective.objective import Objective
2020
from golem.core.optimisers.opt_history_objects.individual import Individual
21+
from golem.core.optimisers.opt_history_objects.opt_history import EXTENDED_INITIAL_ASSUMPTIONS_LABEL, \
22+
INITIAL_ASSUMPTIONS_LABEL
2123
from golem.core.optimisers.optimization_parameters import GraphRequirements
2224
from golem.core.optimisers.optimizer import GraphGenerationParams
2325
from golem.core.optimisers.populational_optimizer import PopulationalOptimizer
@@ -64,13 +66,13 @@ def __init__(self,
6466
def _initial_population(self, evaluator: EvaluationOperator):
6567
""" Initializes the initial population """
6668
# Adding of initial assumptions to history as zero generation
67-
self._update_population(evaluator(self.initial_individuals), 'initial_assumptions')
69+
self._update_population(evaluator(self.initial_individuals), INITIAL_ASSUMPTIONS_LABEL)
6870
pop_size = self.graph_optimizer_params.pop_size
6971

7072
if len(self.initial_individuals) < pop_size:
7173
self.initial_individuals = self._extend_population(self.initial_individuals, pop_size)
7274
# Adding of extended population to history
73-
self._update_population(evaluator(self.initial_individuals), 'extended_initial_assumptions')
75+
self._update_population(evaluator(self.initial_individuals), EXTENDED_INITIAL_ASSUMPTIONS_LABEL)
7476

7577
def _extend_population(self, pop: PopulationT, target_pop_size: int) -> PopulationT:
7678
verifier = self.graph_generation_params.verifier

golem/core/optimisers/meta/surrogate_optimizer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from golem.core.optimisers.meta.surrogate_evaluator import SurrogateDispatcher
77
from golem.core.optimisers.meta.surrogate_model import RandomValuesSurrogateModel
88
from golem.core.optimisers.objective import Objective, ObjectiveFunction
9+
from golem.core.optimisers.opt_history_objects.opt_history import EVOLUTION_RESULTS_LABEL
910
from golem.core.optimisers.optimization_parameters import GraphRequirements
1011
from golem.core.optimisers.optimizer import GraphGenerationParams
1112
from golem.core.optimisers.populational_optimizer import EvaluationAttemptsError, _try_unfit_graph
@@ -17,6 +18,7 @@ class SurrogateEachNgenOptimizer(EvoGraphOptimizer):
1718
1819
Additionally, we need to pass surrogate_model object
1920
"""
21+
2022
def __init__(self,
2123
objective: Objective,
2224
initial_graphs: Sequence[OptGraph],
@@ -54,5 +56,5 @@ def optimise(self, objective: ObjectiveFunction) -> Sequence[OptGraph]:
5456
break
5557
# Adding of new population to history
5658
self._update_population(new_population)
57-
self._update_population(self.best_individuals, 'evolution_results')
59+
self._update_population(self.best_individuals, EVOLUTION_RESULTS_LABEL)
5860
return [ind.graph for ind in self.best_individuals]

golem/core/optimisers/opt_history_objects/opt_history.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
if TYPE_CHECKING:
1919
from golem.core.optimisers.opt_history_objects.individual import Individual
2020

21+
INITIAL_ASSUMPTIONS_LABEL = 'initial_assumptions'
22+
EXTENDED_INITIAL_ASSUMPTIONS_LABEL = 'extended_initial_assumptions'
23+
EVOLUTION_RESULTS_LABEL = 'evolution_results'
24+
TUNING_START_LABEL = 'tuning_start'
25+
TUNING_RESULT_LABEL = 'tuning_result'
26+
2127

2228
class OptHistory:
2329
"""
@@ -212,7 +218,7 @@ def initial_assumptions(self) -> Optional[Generation]:
212218
if not self.generations:
213219
return None
214220
for gen in self.generations:
215-
if gen.label == 'initial_assumptions':
221+
if gen.label == INITIAL_ASSUMPTIONS_LABEL:
216222
return gen
217223

218224
@property
@@ -224,23 +230,23 @@ def evolution_results(self) -> Optional[Generation]:
224230
if not self.generations:
225231
return None
226232
for gen in reversed(self.generations):
227-
if gen.label == 'evolution_results':
233+
if gen.label == EVOLUTION_RESULTS_LABEL:
228234
return gen
229235

230236
@property
231237
def tuning_start(self) -> Optional[Generation]:
232238
if not self.generations:
233239
return None
234240
for gen in reversed(self.generations):
235-
if gen.label == 'tuning_start':
241+
if gen.label == TUNING_START_LABEL:
236242
return gen
237243

238244
@property
239245
def tuning_result(self) -> Optional[Generation]:
240246
if not self.generations:
241247
return None
242248
for gen in reversed(self.generations):
243-
if gen.label == 'tuning_result':
249+
if gen.label == TUNING_RESULT_LABEL:
244250
return gen
245251

246252
@property

golem/core/optimisers/populational_optimizer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from abc import abstractmethod
22
from random import choice
3-
from typing import Any, Optional, Sequence, Dict
3+
from typing import Any, Dict, Optional, Sequence
44

55
from golem.core.constants import MIN_POP_SIZE
66
from golem.core.dag.graph import Graph
77
from golem.core.optimisers.archive import GenerationKeeper
88
from golem.core.optimisers.genetic.evaluation import MultiprocessingDispatcher, SequentialDispatcher
9-
from golem.core.optimisers.genetic.operators.operator import PopulationT, EvaluationOperator
9+
from golem.core.optimisers.genetic.operators.operator import EvaluationOperator, PopulationT
1010
from golem.core.optimisers.objective import GraphFunction, ObjectiveFunction
1111
from golem.core.optimisers.objective.objective import Objective
1212
from golem.core.optimisers.opt_history_objects.individual import Individual
13+
from golem.core.optimisers.opt_history_objects.opt_history import EVOLUTION_RESULTS_LABEL
1314
from golem.core.optimisers.optimization_parameters import GraphRequirements
14-
from golem.core.optimisers.optimizer import GraphGenerationParams, GraphOptimizer, AlgorithmParameters
15+
from golem.core.optimisers.optimizer import AlgorithmParameters, GraphGenerationParams, GraphOptimizer
1516
from golem.core.optimisers.timer import OptimisationTimer
1617
from golem.core.utilities.grouped_condition import GroupedCondition
1718

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

111112
@property

golem/core/optimisers/random/random_mutation_optimizer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from typing import Union, Optional, Sequence
1+
from typing import Optional, Sequence, Union
22

33
from golem.core.dag.graph import Graph
44
from golem.core.optimisers.genetic.gp_params import GPAlgorithmParameters
55
from golem.core.optimisers.genetic.operators.mutation import Mutation
66
from golem.core.optimisers.genetic.operators.operator import EvaluationOperator, PopulationT
77
from golem.core.optimisers.objective import Objective
88
from golem.core.optimisers.opt_history_objects.individual import Individual
9+
from golem.core.optimisers.opt_history_objects.opt_history import EXTENDED_INITIAL_ASSUMPTIONS_LABEL, \
10+
INITIAL_ASSUMPTIONS_LABEL
911
from golem.core.optimisers.optimization_parameters import GraphRequirements
1012
from golem.core.optimisers.optimizer import GraphGenerationParams
1113
from golem.core.optimisers.populational_optimizer import PopulationalOptimizer
@@ -38,13 +40,13 @@ def _evolve_population(self, evaluator: EvaluationOperator) -> PopulationT:
3840
return new_population
3941

4042
def _initial_population(self, evaluator: EvaluationOperator):
41-
self._update_population(evaluator(self.initial_individuals), 'initial_assumptions')
43+
self._update_population(evaluator(self.initial_individuals), INITIAL_ASSUMPTIONS_LABEL)
4244
pop_size = self.graph_optimizer_params.pop_size
4345

4446
if len(self.initial_individuals) < pop_size:
4547
self.initial_individuals = self._extend_population(self.initial_individuals, pop_size)
4648
# Adding of extended population to history
47-
self._update_population(evaluator(self.initial_individuals), 'extended_initial_assumptions')
49+
self._update_population(evaluator(self.initial_individuals), EXTENDED_INITIAL_ASSUMPTIONS_LABEL)
4850

4951

5052
class RandomMutationOptimizer(RandomSearchOptimizer):

golem/core/optimisers/random/random_search.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from golem.core.optimisers.graph import OptGraph
1010
from golem.core.optimisers.objective import Objective, ObjectiveFunction
1111
from golem.core.optimisers.opt_history_objects.individual import Individual
12+
from golem.core.optimisers.opt_history_objects.opt_history import EVOLUTION_RESULTS_LABEL, INITIAL_ASSUMPTIONS_LABEL
1213
from golem.core.optimisers.optimization_parameters import GraphRequirements
13-
from golem.core.optimisers.optimizer import GraphOptimizer, GraphGenerationParams
14+
from golem.core.optimisers.optimizer import GraphGenerationParams, GraphOptimizer
1415
from golem.core.optimisers.timer import OptimisationTimer
1516
from golem.core.utilities.grouped_condition import GroupedCondition
1617

@@ -34,7 +35,7 @@ def __init__(self,
3435
'Optimisation stopped: Time limit is reached'
3536
).add_condition(
3637
lambda: requirements.num_of_generations is not None and
37-
self.current_iteration_num >= requirements.num_of_generations,
38+
self.current_iteration_num >= requirements.num_of_generations,
3839
'Optimisation stopped: Max number of iterations reached')
3940

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

4748
with self.timer, self._progressbar as pbar:
4849
self.best_individual = self._eval_initial_individual(evaluator)
49-
self._update_best_individual(self.best_individual, 'initial_assumptions')
50+
self._update_best_individual(self.best_individual, INITIAL_ASSUMPTIONS_LABEL)
5051
while not self.stop_optimization():
5152
new_individual = self._generate_new_individual()
5253
evaluator([new_individual])
5354
self.current_iteration_num += 1
5455
self._update_best_individual(new_individual)
5556
pbar.update()
56-
self._update_best_individual(self.best_individual, 'evolution_results')
57+
self._update_best_individual(self.best_individual, EVOLUTION_RESULTS_LABEL)
5758
pbar.close()
5859
return [self.best_individual.graph]
5960

golem/core/tuning/tuner_interface.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from golem.core.optimisers.graph import OptGraph
1515
from golem.core.optimisers.objective import ObjectiveEvaluate, ObjectiveFunction
1616
from golem.core.optimisers.opt_history_objects.individual import Individual
17-
from golem.core.optimisers.opt_history_objects.opt_history import OptHistory
17+
from golem.core.optimisers.opt_history_objects.opt_history import OptHistory, TUNING_RESULT_LABEL, TUNING_START_LABEL
1818
from golem.core.optimisers.opt_history_objects.parent_operator import ParentOperator
1919
from golem.core.tuning.search_space import SearchSpace, convert_parameters
2020
from golem.core.utilities.data_structures import ensure_wrapped_in_sequence
@@ -96,7 +96,7 @@ def init_check(self, graph: OptGraph) -> None:
9696
graph = deepcopy(graph)
9797
fitness = self.objective_evaluate(graph)
9898
self.init_individual = self._create_individual(graph, fitness)
99-
self._add_to_history([self.init_individual], label='tuning_start')
99+
self._add_to_history([self.init_individual], label=TUNING_START_LABEL)
100100

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

156156
self.obtained_individual = final_individual
157-
self._add_to_history([self.obtained_individual], label='tuning_result')
157+
self._add_to_history([self.obtained_individual], label=TUNING_RESULT_LABEL)
158158

159159
return self.obtained_individual.graph
160160

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

182-
self._add_to_history(self.obtained_individual, label='tuning_result')
182+
self._add_to_history(self.obtained_individual, label=TUNING_RESULT_LABEL)
183183

184184
return final_graphs
185185

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

285285
if label is None:
286286
label = f'tuning_iteration_{self.evaluations_count}'
287-
if label not in ('tuning_start', 'tuning_result'):
287+
if label not in (TUNING_START_LABEL, TUNING_RESULT_LABEL):
288288
individuals = list(individuals)
289289
individuals.append(self.init_individual) # add initial individual to maintain consistency of inheritance
290290
history.add_to_history(individuals=individuals,

0 commit comments

Comments
 (0)