Skip to content

Commit

Permalink
fix GraphOptimizerParameters import
Browse files Browse the repository at this point in the history
  • Loading branch information
gkirgizov committed Sep 12, 2022
1 parent 626a373 commit 45664fa
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion fedot/core/optimisers/gp_comp/gp_params.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from dataclasses import dataclass
from typing import Sequence, Union, Any

from fedot.core.optimisers.optimizer import GraphOptimizerParameters
from fedot.core.optimisers.gp_comp.operators.crossover import CrossoverTypesEnum
from fedot.core.optimisers.gp_comp.operators.elitism import ElitismTypesEnum
from fedot.core.optimisers.gp_comp.operators.inheritance import GeneticSchemeTypesEnum
from fedot.core.optimisers.gp_comp.operators.mutation import MutationTypesEnum, MutationStrengthEnum
from fedot.core.optimisers.gp_comp.operators.regularization import RegularizationTypesEnum
from fedot.core.optimisers.gp_comp.operators.selection import SelectionTypesEnum
from fedot.core.optimisers.optimizer import GraphOptimizerParameters


@dataclass
Expand Down
5 changes: 4 additions & 1 deletion fedot/core/optimisers/gp_comp/operators/crossover.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from copy import deepcopy
from random import choice, random
from typing import Callable, List, Union, Iterable, Tuple
from typing import Callable, Union, Iterable, Tuple, TYPE_CHECKING

from fedot.core.optimisers.gp_comp.pipeline_composer_requirements import PipelineComposerRequirements
from fedot.core.optimisers.gp_comp.gp_operators import equivalent_subtree, replace_subtrees
Expand All @@ -10,6 +10,9 @@
from fedot.core.optimisers.optimizer import GraphGenerationParams
from fedot.core.utilities.data_structures import ComparableEnum as Enum

if TYPE_CHECKING:
from fedot.core.optimisers.gp_comp.gp_params import GPGraphOptimizerParameters


class CrossoverTypesEnum(Enum):
subtree = 'subtree'
Expand Down
4 changes: 4 additions & 0 deletions fedot/core/optimisers/gp_comp/operators/elitism.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from random import shuffle
from typing import TYPE_CHECKING

from fedot.core.optimisers.gp_comp.operators.operator import PopulationT, Operator
from fedot.core.utilities.data_structures import ComparableEnum as Enum

if TYPE_CHECKING:
from fedot.core.optimisers.gp_comp.gp_params import GPGraphOptimizerParameters


class ElitismTypesEnum(Enum):
keep_n_best = 'keep_n_best'
Expand Down
5 changes: 4 additions & 1 deletion fedot/core/optimisers/gp_comp/operators/inheritance.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from functools import partial
from typing import (Callable)
from typing import (Callable, TYPE_CHECKING)

from fedot.core.optimisers.gp_comp.operators.operator import PopulationT, Operator
from fedot.core.optimisers.gp_comp.operators.selection import Selection
from fedot.core.utilities.data_structures import ComparableEnum as Enum

if TYPE_CHECKING:
from fedot.core.optimisers.gp_comp.gp_params import GPGraphOptimizerParameters


class GeneticSchemeTypesEnum(Enum):
steady_state = 'steady_state'
Expand Down
8 changes: 6 additions & 2 deletions fedot/core/optimisers/gp_comp/operators/mutation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from copy import deepcopy
from functools import partial
from random import choice, randint, random, sample
from typing import Callable, List, Union, Tuple
from typing import Callable, List, Union, Tuple, TYPE_CHECKING

import numpy as np

Expand All @@ -17,6 +17,10 @@
from fedot.core.optimisers.gp_comp.pipeline_composer_requirements import PipelineComposerRequirements


if TYPE_CHECKING:
from fedot.core.optimisers.gp_comp.gp_params import GPGraphOptimizerParameters


class MutationStrengthEnum(Enum):
weak = 0.2
mean = 1.0
Expand All @@ -38,7 +42,7 @@ class MutationTypesEnum(Enum):

class Mutation(Operator):
def __init__(self,
parameters: 'GPGraphGenerationParameters',
parameters: 'GPGraphOptimizerParameters',
requirements: PipelineComposerRequirements,
graph_generation_params: GraphGenerationParams,
# TODO: move these 2 to gp_parameters
Expand Down
8 changes: 5 additions & 3 deletions fedot/core/optimisers/gp_comp/operators/operator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from abc import ABC, abstractmethod
from typing import Sequence, Callable, Optional
from typing import Sequence, Optional, TYPE_CHECKING, Callable

from fedot.core.log import default_log
from fedot.core.optimisers.gp_comp.individual import Individual
from fedot.core.optimisers.gp_comp.pipeline_composer_requirements import PipelineComposerRequirements
# from fedot.core.optimisers.optimizer import GraphOptimizerParameters # TODO: fix import loop

PopulationT = Sequence[Individual] # TODO: provisional
if TYPE_CHECKING:
from fedot.core.optimisers.optimizer import GraphOptimizerParameters

PopulationT = Sequence[Individual] # TODO: provisional EvaluationOperator = Callable[[PopulationT], PopulationT]
EvaluationOperator = Callable[[PopulationT], PopulationT]


Expand Down
4 changes: 4 additions & 0 deletions fedot/core/optimisers/gp_comp/operators/regularization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import deepcopy
from typing import TYPE_CHECKING

from fedot.core.optimisers.gp_comp.individual import Individual, ParentOperator
from fedot.core.optimisers.gp_comp.operators.operator import PopulationT, EvaluationOperator, Operator
Expand All @@ -7,6 +8,9 @@
from fedot.core.pipelines.node import Node
from fedot.core.utilities.data_structures import ComparableEnum as Enum

if TYPE_CHECKING:
from fedot.core.optimisers.gp_comp.gp_params import GPGraphOptimizerParameters


class RegularizationTypesEnum(Enum):
none = 'none'
Expand Down
10 changes: 6 additions & 4 deletions fedot/core/optimisers/gp_comp/operators/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ def spea2_selection(individuals: PopulationT, pop_size: int) -> PopulationT:
for j in range(i + 1, inds_len):
dist = 0.0
for idx in range(fitness_len):
val = individuals[i].fitness.values[idx] - \
individuals[j].fitness.values[idx]
val = \
individuals[i].fitness.values[idx] - \
individuals[j].fitness.values[idx]
dist += val * val
distances[j] = dist
kth_dist = _randomized_select(distances, 0, inds_len - 1, inds_len_sqrt)
Expand All @@ -151,8 +152,9 @@ def spea2_selection(individuals: PopulationT, pop_size: int) -> PopulationT:
for j in range(i + 1, inds_len):
dist = 0.0
for idx in range(fitness_len):
val = individuals[chosen_indices[i]].fitness.values[idx] - \
individuals[chosen_indices[j]].fitness.values[idx]
val = \
individuals[chosen_indices[i]].fitness.values[idx] - \
individuals[chosen_indices[j]].fitness.values[idx]
dist += val * val
distances[i][j] = dist
distances[j][i] = dist
Expand Down
6 changes: 5 additions & 1 deletion test/unit/optimizer/test_elitism_operator.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from typing import TYPE_CHECKING

import pytest

from fedot.core.optimisers.adapters import PipelineAdapter
from fedot.core.optimisers.gp_comp.evaluation import SimpleDispatcher
from fedot.core.optimisers.gp_comp.gp_params import GPGraphOptimizerParameters
from fedot.core.optimisers.gp_comp.individual import Individual
from fedot.core.optimisers.gp_comp.operators.elitism import Elitism, ElitismTypesEnum
from test.unit.optimizer.test_evaluation import prepared_objective
from test.unit.pipelines.test_node_cache import pipeline_first, pipeline_second, pipeline_third, pipeline_fourth, \
pipeline_fifth

if TYPE_CHECKING:
from fedot.core.optimisers.gp_comp.gp_params import GPGraphOptimizerParameters


@pytest.fixture()
def set_up():
Expand Down

0 comments on commit 45664fa

Please sign in to comment.