Skip to content

Final elite solution is sometimes incorrect #39

@JFKakaJFK

Description

@JFKakaJFK

It can be the case that the values for the best solution are reported incorrectly (best solution reported has worse fitness compared to the best fitness reported). Here is a minimal reproduction example (should work as long as the gomea library and numpy are installed using e.g. pip install git+https://github.com/CWI-EvolutionaryIntelligence/GOMEA.git@dev#egg=gomea):

import gomea
import numpy as np

from ast import literal_eval

class CustomRosenbrockFunction(gomea.fitness.BBOFitnessFunctionRealValued):
    def objective_function( self, objective_index, variables ):
        f = 0
        for i in range(len(variables)-1):
            x = variables[i]
            y = variables[i+1]
            f += 100*(y-x*x)*(y-x*x) + (1.0-x)*(1.0-x)
        return f

def run_gomea():
    dim = 10
    frv = CustomRosenbrockFunction(dim,value_to_reach=1e-6)
    lm = gomea.linkage.Univariate()
    rvgom = gomea.RealValuedGOMEA(
        fitness=frv,
        linkage_model=lm,
        lower_init_range=-115,
        upper_init_range=-100,
        max_number_of_populations=1,
        base_population_size=100,
        max_number_of_evaluations=10_000
    )
    result = rvgom.run()
    result.printAllStatistics()


    stats = result.getFinalStatistics()
    best_solution = np.array(literal_eval(stats["best_solution"]))

    best_fitness = stats["best_obj_val"]
    actual_fitness = frv.objective_function(0, best_solution)
    print("Best solution: ", best_solution)
    print("Expected fitness: ", best_fitness)
    print("Actual fitness: ", actual_fitness)
    assert np.allclose(actual_fitness, best_fitness)

if __name__ == "__main__":
    for _ in range(100):
        run_gomea()

Truncated output:

...
Best solution:  [ 0.4489464   0.11328248  1.10134688  0.86169825  0.72128235  0.33731554
 -1.02180791 -1.44331219 -3.22583439 14.04847148]
Expected fitness:  4601.251681722282
Actual fitness:  5057.507173058592
Traceback (most recent call last):
   assert np.allclose(actual_fitness, best_fitness)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions