Skip to content

Commit

Permalink
Search now returns multiple individuals, for multi-objective support
Browse files Browse the repository at this point in the history
  • Loading branch information
alcides committed Oct 22, 2024
1 parent 2e0c5b0 commit d090562
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ def target(x):


alg_gp = GeneticProgramming(problem=problem, budget=budget, representation=representation)
ind = alg_gp.search()
ind = alg_gp.search()[0]
print("\n======\nGP\n======\n")
print(f"{ind.get_fitness(problem)} - {ind}")


alg_hc = HC(problem=problem, budget=budget, representation=representation)
ind = alg_hc.search()
ind = alg_hc.search()[0]
print("\n======\nHC\n======\n")
print(f"{ind.get_fitness(problem)} - {ind}")

alg_rs = RandomSearch(problem=problem, budget=budget, representation=representation)
ind = alg_rs.search()
ind = alg_rs.search()[0]
print("\n======\nRS\n======\n")
print(f"{ind.get_fitness(problem)} - {ind}")
2 changes: 1 addition & 1 deletion examples/extra_columns_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ def fitness_test(p) -> float:
seed=SEED,
)

ind = gp.search()
ind = gp.search()[0]
print(simplify(ind.get_phenotype()))
3 changes: 2 additions & 1 deletion tests/core/reproducibility_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def test_random_gp(representation):
representation=repr,
random=r,
)
e = gp.search()
es = gp.search()
e = es[0]
v = e.get_phenotype().i, e.get_phenotype().j
vals.append(v)
assert vals[0] == vals[1]
2 changes: 1 addition & 1 deletion tests/geml/symbolic_regression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ def fitness_function(x: Expression) -> float:
random=r,
budget=EvaluationBudget(100),
)
ind = gp.search()
ind = gp.search()[0]
assert ind.get_fitness(p).fitness_components[0] <= 1
2 changes: 1 addition & 1 deletion tests/gp/parallel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_parallel(self):
population_initializer=FullInitializer(max_depth=max_depth),
tracker=tracker,
)
ind = gp.search()
ind = gp.search()[0]
tree = ind.get_phenotype()
assert isinstance(tree, UnderTest)
assert isinstance(tree.a, Leaf)
8 changes: 4 additions & 4 deletions tests/gp/probabilistic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_probabilistic_grammar_tree_based(self):
population_size=1000,
budget=EvaluationBudget(50 * 1000),
)
ind = gp.search()
ind = gp.search()[0]
tree = ind.get_phenotype()
assert isinstance(tree, OptionA)

Expand All @@ -65,7 +65,7 @@ def test_probabilistic_grammar_ge(self):
population_size=1000,
budget=EvaluationBudget(50 * 1000),
)
ind = gp.search()
ind = gp.search()[0]
tree = ind.get_phenotype()
assert isinstance(tree, OptionA)

Expand All @@ -83,7 +83,7 @@ def test_probabilistic_grammar_sge(self):
population_size=1000,
budget=EvaluationBudget(50 * 1000),
)
ind = gp.search()
ind = gp.search()[0]
tree = ind.get_phenotype()
assert isinstance(tree, OptionA)

Expand All @@ -102,6 +102,6 @@ def test_probabilistic_grammar_dsge(self):
random=r,
population_size=1000,
)
ind = gp.search()
ind = gp.search()[0]
tree = ind.get_phenotype()
assert isinstance(tree, OptionA)
2 changes: 1 addition & 1 deletion tests/gp/type_safety_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_safety(self):
GenericMutationStep(1),
),
)
ind = gp.search()
ind = gp.search()[0]
tree = ind.get_phenotype()
assert isinstance(tree, UnderTest)
assert isinstance(tree.a, Leaf)
Expand Down
4 changes: 2 additions & 2 deletions tests/gp/unknown_objectives_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_unknown_objectives_and_target(self):
GenericMutationStep(1),
),
)
ind = gp.search()
ind = gp.search()[0]
tree = ind.get_phenotype()
assert isinstance(tree, Root)
assert isinstance(tree, Leaf)
Expand All @@ -75,7 +75,7 @@ def test_unknown_objectives_and_unkown_target(self):
GenericMutationStep(1),
),
)
ind = gp.search()
ind = gp.search()[0]
tree = ind.get_phenotype()
assert isinstance(tree, Root)
assert isinstance(tree, Leaf)
Expand Down
11 changes: 6 additions & 5 deletions tests/representations/basic_representation_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
from typing import Any
from geneticengine.algorithms.hill_climbing import HC
from geneticengine.algorithms.one_plus_one import OnePlusOne
from geneticengine.algorithms.random_search import RandomSearch
Expand All @@ -18,8 +19,8 @@ def create_genotype(self, random: RandomSource, **kwargs) -> list[int]:
def genotype_to_phenotype(self, internal: list[int]) -> int:
return sum(internal)

def mutate(self, random: RandomSource, internal: list[int]) -> list[int]:
nc = copy.deepcopy(internal)
def mutate(self, random: RandomSource, genotype: list[int], **kwargs: Any) -> list[int]:
nc = copy.deepcopy(genotype)
ind = random.randint(0, len(nc) - 1)
nc[ind] = random.randint(0, MAX_NUMBER)
return nc
Expand All @@ -29,20 +30,20 @@ class TestBasicRepresentation:
def test_random_search(self):
p = SingleObjectiveProblem(fitness_function=lambda x: abs(2024 - x), minimize=True)
rs = RandomSearch(problem=p, budget=EvaluationBudget(100), representation=LinearRepresentation())
v = rs.search()
v = rs.search()[0]
assert isinstance(v.get_phenotype(), int)
assert 0 <= v.get_phenotype() <= MAX_NUMBER * MAX_ELEMENTS

def test_one_plus_one(self):
p = SingleObjectiveProblem(fitness_function=lambda x: abs(2024 - x), minimize=True)
rs = OnePlusOne(problem=p, budget=EvaluationBudget(100), representation=LinearRepresentation())
v = rs.search()
v = rs.search()[0]
assert isinstance(v.get_phenotype(), int)
assert 0 <= v.get_phenotype() <= MAX_NUMBER * MAX_ELEMENTS

def test_hill_climbing(self):
p = SingleObjectiveProblem(fitness_function=lambda x: abs(2024 - x), minimize=True)
rs = HC(problem=p, budget=EvaluationBudget(100), representation=LinearRepresentation())
v = rs.search()
v = rs.search()[0]
assert isinstance(v.get_phenotype(), int)
assert 0 <= v.get_phenotype() <= MAX_NUMBER * MAX_ELEMENTS
2 changes: 1 addition & 1 deletion tests/representations/dependent_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def fitness_function(x: SimplePair) -> float:
random=r,
budget=EvaluationBudget(10),
)
ind = gp.search()
ind = gp.search()[0]
p = ind.get_phenotype()
assert p.a <= p.b

Expand Down

0 comments on commit d090562

Please sign in to comment.