Skip to content

Commit

Permalink
Bump GeneticEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
alcides committed Oct 13, 2024
1 parent 7347f42 commit d0cb2c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion algorithms/geneticengine/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dependencies:
- pip=24.2
- dill=0.3.9
- pip:
- geneticengine @ git+https://github.com/alcides/GeneticEngine.git@c7b7a770c9d408cf6aeea1fdaec79696b4595434
- geneticengine @ git+https://github.com/alcides/GeneticEngine.git@5e7c4ec8807b8299f92e0347443714e16f22e40f
10 changes: 10 additions & 0 deletions experiment/methods/geneticengine/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
authors: # the participants
- Alcides Fonseca, Guilherme Espada, Leon Ingelse, Eduardo Madeira
email: me@alcidesfonseca.com, gjespada@fc.ul.pt, leoningelse@gmail.com, jmadeira@lasige.di.fc.ul.pt
name: Genetic Engine
description:
GeneticEngine is an extensible framework for implementing evolutionary algorithms. Users can write their own class structure and select the algorithm they want to run. Each algorithm will generate random trees following the class specification.
Currently, GeneticEngine supports several representations (tree-based, grammatical evolution, structured grammatical evolution, stack-based), several algorithms (hill-climbing, Genetic Programming, 1+1) and several operators (Tournament, Lexicase, different mutations and crossovers).
A distinctive feature of this framework is that it supports dependent types that semantically constraint the tree generation.

url: "https://github.com/alcides/GeneticEngine/"
55 changes: 29 additions & 26 deletions experiment/test_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,65 @@

root_dir = d(abspath(__file__))
sys.path.append(root_dir)
print('appended',root_dir,'to sys.path')
print("appended", root_dir, "to sys.path")

import importlib
from read_file import read_file

if 'OMP_NUM_THREADS' not in os.environ.keys():
os.environ['OMP_NUM_THREADS'] = '1'
if 'OPENBLAS_NUM_THREADS' not in os.environ.keys():
os.environ['OPENBLAS_NUM_THREADS'] = '1'
if 'MKL_NUM_THREADS' not in os.environ.keys():
os.environ['MKL_NUM_THREADS'] = '1'
if "OMP_NUM_THREADS" not in os.environ.keys():
os.environ["OMP_NUM_THREADS"] = "1"
if "OPENBLAS_NUM_THREADS" not in os.environ.keys():
os.environ["OPENBLAS_NUM_THREADS"] = "1"
if "MKL_NUM_THREADS" not in os.environ.keys():
os.environ["MKL_NUM_THREADS"] = "1"


def test_population(ml):
"""Sympy compatibility of model string"""

dataset = 'test/192_vineyard_small.tsv.gz'
dataset = "test/192_vineyard_small.tsv.gz"
random_state = 42

algorithm = importlib.__import__(f'methods.{ml}.regressor',globals(),
locals(),
['est','hyper_params','complexity'])

features, labels, feature_names = read_file(
dataset,
use_dataframe=True
algorithm = importlib.__import__(
f"methods.{ml}.regressor",
globals(),
locals(),
["est", "hyper_params", "complexity"],
)
print('feature_names:',feature_names)

features, labels, feature_names = read_file(dataset, use_dataframe=True)
print("feature_names:", feature_names)

# generate train/test split
X_train, X_test, y_train, y_test = train_test_split(features, labels,
train_size=0.75,
test_size=0.25,
random_state=random_state)

X_train, X_test, y_train, y_test = train_test_split(
features, labels, train_size=0.75, test_size=0.25, random_state=random_state
)

# Few samples to try to make it quick
sample_idx = np.random.choice(np.arange(len(X_train)), size=10)

y_train = y_train[sample_idx]
X_train = X_train.iloc[sample_idx]

algorithm.est.fit(X_train.values, y_train)

if 'get_population' not in dir(algorithm):
if "get_population" not in dir(algorithm):
algorithm.get_population = lambda est: [est]
if 'get_best_solution' not in dir(algorithm):
if "get_best_solution" not in dir(algorithm):
algorithm.get_best_solution = lambda est: est

population = algorithm.get_population(algorithm.est)
print(population, "pop", algorithm.est.get_population())
best_model = algorithm.get_best_solution(algorithm.est)
print(algorithm.model(best_model, X_train))
print(algorithm.est.predict(X_train.values))

# assert that population has at least 1 and no more than 100 individuals
assert 1 <= len(population) <= 100, "Population size is not within the expected range"

assert (
1 <= len(population) <= 100
), "Population size is not within the expected range"

for p in population:
print(population, algorithm.est, type(algorithm.est))
print(algorithm.model(p, X_train))
print(p.predict(X_train.values))

0 comments on commit d0cb2c4

Please sign in to comment.