Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder changes phenotype for individuals with parameters #261

Closed
jakobj opened this issue Dec 27, 2020 · 0 comments · Fixed by #265
Closed

Reorder changes phenotype for individuals with parameters #261

jakobj opened this issue Dec 27, 2020 · 0 comments · Fixed by #265
Assignees
Labels
bug Something isn't working high priority Let's fix this quickly!
Milestone

Comments

@jakobj
Copy link
Member

jakobj commented Dec 27, 2020

Since parameter values are stored according to their position in the cartesian graph, reordering nodes without updating the (position, value) pairs will change the phenotype of individuals.

Here is a modified version of the reorder test that reproduces the bug:

def test_genome_reordering_empirically(rng):
    # empirically test that reordering does not change the output function of a genome

    pytest.importorskip("sympy")

    genome_params = {
        "n_inputs": 2,
        "n_outputs": 1,
        "n_columns": 14,
        "n_rows": 1,
        "levels_back": None,
        "primitives": (cgp.Mul, cgp.Sub, cgp.Add, cgp.ConstantFloat, cgp.Parameter),
    }

    genome = cgp.Genome(**genome_params)

    # f(x_0, x_1) = x_0 ** 2 - x_1 + 0.5
    dna_fixed = [
        ID_INPUT_NODE,  # x_1 (address 0)
        ID_NON_CODING_GENE,
        ID_NON_CODING_GENE,
        ID_INPUT_NODE,  # x_1 (address 1)
        ID_NON_CODING_GENE,
        ID_NON_CODING_GENE,
        0,  # Mul ->  x_0^2 (address 2)
        0,  # x_0
        0,  # x_0
        1,  # Sub -> x_0^2 - x_1 (address 3)
        2,  # x_0^2
        1,  # x_1
        1,  # Sub -> 0 (address 4)
        0,  # x_0
        0,  # x_0
        3,  # const -> 1 (address 5)
        2,
        3,
        3,  # const -> 1 (address 6)
        0,
        0,
        4,  # param -> 0.9 (address 7)
        4,
        3,
        4,  # param -> 0.4 (address 8)
        7,
        2,
        2,  # Add -> x_0^2 - x_1 + 1 (address 9)
        3,  # x_0^2 - x_1
        5,  # 1
        1,  # Sub -> x_0^2 - x_1 + 1 - 0.9 (address 10)
        9,  # x_0^2 - x_1 + 1
        7,  # 0.9
        2,  # Add -> x_0^2 - x_1 + 1 - 0.9 + 0.4 (address 11)
        10,  # x_0^2 - x_1 + 1 - 0.9
        8,  # 0.4
        3,  # const (address 8)
        0,
        1,
        3,  # const (address 9)
        0,
        1,
        3,  # const (address 10)
        0,
        1,
        3,  # const (address 11)
        0,
        1,
        ID_OUTPUT_NODE,  # out -> x_0^2 - x_1 + 0.5
        11,
        ID_NON_CODING_GENE,
    ]

    genome.dna = dna_fixed
    genome._parameter_names_to_values["<p7>"] = 0.9
    genome._parameter_names_to_values["<p8>"] = 0.4

    sympy_expression = cgp.CartesianGraph(genome).to_sympy()
    n_reorderings = 100
    for _ in range(n_reorderings):
        genome.reorder(rng)
        sympy_expression_after_reorder = cgp.CartesianGraph(genome).to_sympy()
        assert sympy_expression_after_reorder == sympy_expression

@HenrikMettler can you please take a look at this, you're most familiar with the reorder implementation. I guess one would need to go through the _parameter_names_to_values of the Genome class after reordering to update the parameter names to reflect the new node positions.

once this is fixed we should consider a bugfix release, 0.2.1 @mschmidt87 do you want to take care of this again?

@jakobj jakobj added bug Something isn't working high priority Let's fix this quickly! labels Dec 27, 2020
@jakobj jakobj added this to the 0.2.1 milestone Dec 27, 2020
jakobj added a commit that referenced this issue Jan 6, 2021
Bug-fix: Reorder changes phenotype for individuals with parameters (#261)
@jakobj jakobj modified the milestones: 0.2.1, 0.3.0 Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority Let's fix this quickly!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants