Skip to content

Commit

Permalink
Added targets to benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
alcides committed Oct 22, 2024
1 parent ee947f3 commit 6c9b276
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 15 deletions.
3 changes: 1 addition & 2 deletions examples/benchmarks/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from geml.grammars.ruleset_classification import make_grammar



class ClassificationBenchmark(Benchmark):

def __init__(self, X, y, feature_names):
Expand All @@ -34,7 +33,7 @@ def fitness_function(ruleset):
except ValueError:
return -1

self.problem = SingleObjectiveProblem(minimize=False, fitness_function=fitness_function)
self.problem = SingleObjectiveProblem(minimize=False, fitness_function=fitness_function, target=1)

def setup_grammar(self, y, feature_names):
# Grammar
Expand Down
9 changes: 7 additions & 2 deletions examples/benchmarks/classification_lexicase.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from examples.benchmarks.benchmark import Benchmark, example_run
from examples.benchmarks.datasets import get_banknote
from geneticengine.grammar.grammar import extract_grammar, Grammar
from geneticengine.problems import LazyMultiObjectiveProblem, Problem
from geneticengine.problems import MultiObjectiveProblem, Problem
from geml.common import forward_dataset
from geml.grammars.ruleset_classification import make_grammar

Expand All @@ -24,7 +24,12 @@ def fitness_function_lexicase(ruleset):
except ValueError:
return [0 for _ in y]

self.problem = LazyMultiObjectiveProblem(minimize=False, fitness_function=fitness_function_lexicase, target=1)
n_objectives = len(y)
self.problem = MultiObjectiveProblem(
fitness_function=fitness_function_lexicase,
minimize=[False for _ in range(n_objectives)],
target=[1 for _ in range(n_objectives)],
)

def setup_grammar(self, y, feature_names):
options = [int(v) for v in np.unique(y)]
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmarks/game_of_life_vectorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def fitness_function(i: Condition):
ypred = [_clf(line) for line in np.rollaxis(X, 0)]
return f1_score(y, ypred)

self.problem = SingleObjectiveProblem(minimize=False, fitness_function=fitness_function)
self.problem = SingleObjectiveProblem(minimize=False, fitness_function=fitness_function, target=1)

def setup_grammar(self):
self.grammar = extract_grammar(
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmarks/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def fitness_function(ruleset):
except ValueError:
return -1

self.problem = SingleObjectiveProblem(minimize=False, fitness_function=fitness_function)
self.problem = SingleObjectiveProblem(minimize=False, fitness_function=fitness_function, target=0)

def setup_grammar(self, feature_names):
# Grammar
Expand Down
9 changes: 7 additions & 2 deletions examples/benchmarks/regression_lexicase.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from examples.benchmarks.datasets import get_vladislavleva
from geneticengine.grammar.grammar import extract_grammar
from geneticengine.grammar.grammar import Grammar
from geneticengine.problems import LazyMultiObjectiveProblem, Problem
from geneticengine.problems import MultiObjectiveProblem, Problem

from geml.common import forward_dataset
from geml.grammars.symbolic_regression import Expression, components, make_var
Expand Down Expand Up @@ -61,7 +61,12 @@ def lexicase_fitness_function(n: Expression):

return grouped_errors

self.problem = LazyMultiObjectiveProblem(minimize=True, fitness_function=lexicase_fitness_function)
n_objectives = len(y)
self.problem = MultiObjectiveProblem(
fitness_function=lexicase_fitness_function,
minimize=[True for _ in range(n_objectives)],
target=[0 for _ in range(n_objectives)],
)

def setup_grammar(self, feature_names):
# Grammar
Expand Down
3 changes: 2 additions & 1 deletion examples/benchmarks/santafe.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def setup_problem(self, map):
def fitness_function(i):
return simulate(i, map)

self.problem = SingleObjectiveProblem(minimize=False, fitness_function=fitness_function)
target_food = map.count("#")
self.problem = SingleObjectiveProblem(minimize=False, fitness_function=fitness_function, target=target_food)

def setup_grammar(self):
self.grammar = extract_grammar([ActionBlock, Action, IfFood, Move, Right, Left], ActionMain)
Expand Down
5 changes: 1 addition & 4 deletions examples/benchmarks/string_match.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations




from examples.benchmarks.benchmark import Benchmark, example_run
from geml.grammars.letter import Char, Consonant, LetterString, String, Vowel
from geneticengine.grammar.grammar import extract_grammar
Expand All @@ -11,7 +9,6 @@
from geneticengine.problems import SingleObjectiveProblem



class StringMatchBenchmark(Benchmark):
def __init__(self, target: str = "Hellow world!"):
self.setup_problem(target)
Expand All @@ -33,7 +30,7 @@ def fitness_function(individual):
fitness -= 1 / (1 + (abs(ord(t_p) - ord(g_p))))
return fitness

self.problem = SingleObjectiveProblem(minimize=True, fitness_function=fitness_function)
self.problem = SingleObjectiveProblem(minimize=True, fitness_function=fitness_function, target=0)

def setup_grammar(self):
self.grammar = extract_grammar([LetterString, Char, Vowel, Consonant], String)
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmarks/vectorialgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3112,7 +3112,7 @@ def regressor(l):
y_pred,
)

self.problem = SingleObjectiveProblem(minimize=True, fitness_function=fitness_function)
self.problem = SingleObjectiveProblem(minimize=True, fitness_function=fitness_function, target=0)

def setup_grammar(self):
"""See fitness/vectorialgp.py for docs.
Expand Down
3 changes: 3 additions & 0 deletions geml/classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def get_grammar(self, feature_names: list[str], data, target) -> Grammar:
Var.to_numpy = lambda s: f"dataset[:,{index_of[s.name]}]" # type:ignore
return extract_grammar(components, RuleSet)

def get_goal(self) -> tuple[bool, float]:
return True, 1


class GeneticProgrammingClassifier(GeneticEngineClassifier):

Expand Down
7 changes: 6 additions & 1 deletion geml/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import abstractmethod
import abc
from typing import Any

import numpy as np
Expand Down Expand Up @@ -108,6 +109,9 @@ def predict(self, X):
feature_names, data = self.prepare_inputs(X)
return forward_dataset(self._best_individual[0], data)

@abc.abstractmethod
def get_goal(self) -> tuple[bool, float]: ...

@_fit_context(prefer_skip_nested_validation=True)
def fit(self, X, y):

Expand All @@ -129,7 +133,8 @@ def fitness_function(x: Expression) -> float:
except ValueError:
return -10000000

problem = SingleObjectiveProblem(fitness_function)
minimize, target = self.get_goal()
problem = SingleObjectiveProblem(fitness_function, minimize, target)

population_recorder = PopulationRecorder()

Expand Down
3 changes: 3 additions & 0 deletions geml/regressors.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def get_grammar(self, feature_names: list[str], data, target) -> Grammar:
complete_components = components + [Var]
return extract_grammar(complete_components, Expression)

def get_goal(self) -> tuple[bool, float]:
return False, 0


class GeneticProgrammingRegressor(GeneticEngineRegressor):

Expand Down

0 comments on commit 6c9b276

Please sign in to comment.