Skip to content

Commit

Permalink
Add test for graphs in history (#872)
Browse files Browse the repository at this point in the history
* add test for graphs in loaded history;
* add test for `Individual` properties.
  • Loading branch information
MorrisNein authored Sep 12, 2022
1 parent 44fce9d commit 3db5464
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions test/unit/composer/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest

from fedot.api.main import Fedot
from fedot.core.optimisers.gp_comp.pipeline_composer_requirements import PipelineComposerRequirements
from fedot.core.dag.graph import Graph
from fedot.core.dag.verification_rules import DEFAULT_DAG_RULES
from fedot.core.data.data import InputData
Expand All @@ -18,6 +17,7 @@
from fedot.core.optimisers.gp_comp.individual import Individual, ParentOperator
from fedot.core.optimisers.gp_comp.operators.crossover import CrossoverTypesEnum, Crossover
from fedot.core.optimisers.gp_comp.operators.mutation import MutationTypesEnum, Mutation
from fedot.core.optimisers.gp_comp.pipeline_composer_requirements import PipelineComposerRequirements
from fedot.core.optimisers.objective import PipelineObjectiveEvaluate
from fedot.core.optimisers.objective.data_source_splitter import DataSourceSplitter
from fedot.core.optimisers.objective.objective import Objective
Expand Down Expand Up @@ -125,6 +125,15 @@ def test_newly_generated_history():
loaded_history = OptHistory.load(dumped_history).save()
assert dumped_history is not None
assert dumped_history == loaded_history, 'The history is not equal to itself after reloading!'
for ind in chain(*history.individuals):
# All individuals in `history.individuals` must have a native generation.
assert ind.has_native_generation
if ind.native_generation == 0:
continue
# All individuals must have parents, except for the initial assumptions.
assert ind.parents
# The first of `operators_from_prev_generation` must point to `parents_from_prev_generation`.
assert ind.parents_from_prev_generation == list(ind.operators_from_prev_generation[0].parent_individuals)


def assert_intermediate_metrics(pipeline: Graph):
Expand Down Expand Up @@ -195,8 +204,9 @@ def test_history_backward_compatibility():
# Assert that all history pipelines have fitness
assert len(historical_pipelines) == len(all_historical_fitness)
assert np.shape(history.individuals) == np.shape(historical_fitness)
# Assert that fitness, parent_individuals, and objective are valid
# Assert that fitness, graph, parent_individuals, and objective are valid
assert all(isinstance(ind.fitness, SingleObjFitness) for ind in chain(*history.individuals))
assert all(ind.graph.nodes for ind in chain(*history.individuals))
assert all(isinstance(parent_ind, Individual)
for ind in chain(*history.individuals)
for parent_op in ind.operators_from_prev_generation
Expand Down

0 comments on commit 3db5464

Please sign in to comment.