Exemplo n.º 1
0
def test_drop_mutation_for_linear_graph():
    """
    Tests single_drop mutation can remove node
    """

    linear_two_nodes = OptGraph(OptNode('logit', [OptNode('scaling')]))

    linear_one_node = OptGraph(OptNode('logit'))

    composer_requirements = GPComposerRequirements(primary=['scaling'],
                                                   secondary=['logit'],
                                                   mutation_prob=1)

    graph_params = GraphGenerationParams(
        adapter=DirectAdapter(), rules_for_constraint=DEFAULT_DAG_RULES)
    successful_mutation_drop = False
    for _ in range(100):
        graph_after_mutation = mutation(types=[MutationTypesEnum.single_drop],
                                        params=graph_params,
                                        ind=Individual(linear_two_nodes),
                                        requirements=composer_requirements,
                                        log=default_log(__name__),
                                        max_depth=2).graph
        if not successful_mutation_drop:
            successful_mutation_drop = \
                graph_after_mutation.root_node.descriptive_id == linear_one_node.root_node.descriptive_id
        else:
            break
    assert successful_mutation_drop
Exemplo n.º 2
0
def test_edge_mutation_for_graph():
    """
    Tests edge mutation can add edge between nodes
    """
    graph_without_edge = \
        OptGraph(OptNode('logit', [OptNode('one_hot_encoding', [OptNode('scaling')])]))

    primary = OptNode('scaling')
    graph_with_edge = \
        OptGraph(OptNode('logit', [OptNode('one_hot_encoding', [primary]), primary]))

    composer_requirements = GPComposerRequirements(
        primary=['scaling', 'one_hot_encoding'],
        secondary=['logit', 'scaling'],
        mutation_prob=1)

    graph_params = GraphGenerationParams(
        adapter=DirectAdapter(), rules_for_constraint=DEFAULT_DAG_RULES)
    successful_mutation_edge = False
    for _ in range(100):
        graph_after_mutation = mutation(types=[MutationTypesEnum.single_edge],
                                        params=graph_params,
                                        ind=Individual(graph_without_edge),
                                        requirements=composer_requirements,
                                        log=default_log(__name__),
                                        max_depth=graph_with_edge.depth).graph
        if not successful_mutation_edge:
            successful_mutation_edge = \
                graph_after_mutation.root_node.descriptive_id == graph_with_edge.root_node.descriptive_id
        else:
            break
    assert successful_mutation_edge
Exemplo n.º 3
0
def test_intermediate_add_mutation_for_linear_graph():
    """
    Tests single_add mutation can add node between two existing nodes
    """

    linear_two_nodes = OptGraph(OptNode('logit', [OptNode('scaling')]))
    linear_three_nodes_inner = \
        OptGraph(OptNode('logit', [OptNode('one_hot_encoding', [OptNode('scaling')])]))

    composer_requirements = GPComposerRequirements(
        primary=['scaling'], secondary=['one_hot_encoding'], mutation_prob=1)

    graph_params = GraphGenerationParams(
        adapter=DirectAdapter(), rules_for_constraint=DEFAULT_DAG_RULES)
    successful_mutation_inner = False

    for _ in range(100):
        graph_after_mutation = mutation(types=[MutationTypesEnum.single_add],
                                        params=graph_params,
                                        ind=Individual(linear_two_nodes),
                                        requirements=composer_requirements,
                                        log=default_log(__name__),
                                        max_depth=3).graph
        if not successful_mutation_inner:
            successful_mutation_inner = \
                graph_after_mutation.root_node.descriptive_id == linear_three_nodes_inner.root_node.descriptive_id
        else:
            break

    assert successful_mutation_inner
Exemplo n.º 4
0
def validate(graph: Graph, rules: List[Callable] = None):
    if not rules:
        rules = default_rules
    for rule_func in rules:
        if rule_func in DEFAULT_DAG_RULES and isinstance(graph, OptGraph):
            graph = DirectAdapter(base_graph_class=Graph,
                                  base_node_class=GraphNode).restore(graph)
        rule_func(graph)

    return True
Exemplo n.º 5
0
class GraphGenerationParams:
    """
    This dataclass is for defining the parameters using in graph generation process

    :param adapter: the function for processing of external object that should be optimized
    :param rules_for_constraint: set of constraints
    """
    adapter: BaseOptimizationAdapter = DirectAdapter()
    rules_for_constraint: Optional[List[Callable]] = None
    advisor: Optional[DefaultChangeAdvisor] = DefaultChangeAdvisor()
Exemplo n.º 6
0
def run_custom_example(
        timeout: datetime.timedelta = datetime.timedelta(minutes=0.2)):
    data = pd.read_csv(
        os.path.join(fedot_project_root(), 'examples', 'data',
                     'custom_encoded.csv'))
    nodes_types = ['V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V10']
    rules = [has_no_self_cycled_nodes, has_no_cycle, _has_no_duplicates]

    initial = CustomGraphModel(nodes=[
        CustomGraphNode(nodes_from=None, content=node_type)
        for node_type in nodes_types
    ])

    requirements = GPComposerRequirements(primary=nodes_types,
                                          secondary=nodes_types,
                                          max_arity=10,
                                          max_depth=10,
                                          pop_size=5,
                                          num_of_generations=5,
                                          crossover_prob=0.8,
                                          mutation_prob=0.9,
                                          timeout=timeout)

    optimiser_parameters = GPGraphOptimiserParameters(
        genetic_scheme_type=GeneticSchemeTypesEnum.steady_state,
        mutation_types=[custom_mutation],
        crossover_types=[CrossoverTypesEnum.none],
        regularization_type=RegularizationTypesEnum.none)

    graph_generation_params = GraphGenerationParams(adapter=DirectAdapter(
        base_graph_class=CustomGraphModel, base_node_class=CustomGraphNode),
                                                    rules_for_constraint=rules)

    optimizer = GPGraphOptimiser(
        graph_generation_params=graph_generation_params,
        metrics=[],
        parameters=optimiser_parameters,
        requirements=requirements,
        initial_graph=initial,
        log=default_log(logger_name='Bayesian', verbose_level=1))

    optimized_network = optimizer.optimise(partial(custom_metric, data=data))

    optimized_network.show()
Exemplo n.º 7
0
def test_custom_graph_opt():
    nodes_types = ['A', 'B', 'C', 'D']
    rules = [has_no_self_cycled_nodes]

    requirements = GPComposerRequirements(primary=nodes_types,
                                          secondary=nodes_types,
                                          max_arity=3,
                                          max_depth=3,
                                          pop_size=5,
                                          num_of_generations=5,
                                          crossover_prob=0.8,
                                          mutation_prob=0.9)

    optimiser_parameters = GPGraphOptimiserParameters(
        genetic_scheme_type=GeneticSchemeTypesEnum.steady_state,
        mutation_types=[
            MutationTypesEnum.simple, MutationTypesEnum.reduce,
            MutationTypesEnum.growth, MutationTypesEnum.local_growth
        ],
        regularization_type=RegularizationTypesEnum.none)

    graph_generation_params = GraphGenerationParams(adapter=DirectAdapter(
        CustomModel, CustomNode),
                                                    rules_for_constraint=rules)

    optimizer = GPGraphOptimiser(
        graph_generation_params=graph_generation_params,
        metrics=[],
        parameters=optimiser_parameters,
        requirements=requirements,
        initial_graph=None)

    optimized_network = optimizer.optimise(custom_metric)

    assert optimized_network is not None
    assert isinstance(optimized_network, CustomModel)
    assert isinstance(optimized_network.nodes[0], CustomNode)

    assert 'custom_A' in [str(_) for _ in optimized_network.nodes]