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

Using max_time in test population #195

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion experiment/test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
python -m pytest -v test_algorithm.py --ml ${ALGORITHM}
python -m pytest -v test_population.py --ml ${ALGORITHM}
python -m pytest -s -v test_population.py --ml ${ALGORITHM}
42 changes: 33 additions & 9 deletions experiment/test_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
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'
random_state = 42

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

features, labels, feature_names = read_file(
Expand All @@ -49,21 +48,46 @@ def test_population(ml):
y_train = y_train[sample_idx]
X_train = X_train.iloc[sample_idx]

##################################################
# fit with max_time
##################################################
MAXTIME = 3600 # in seconds
if hasattr(algorithm.est, 'max_time'):
algorithm.est.max_time = MAXTIME
print('max time:',MAXTIME)
else:
print('max time not set')

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

if 'get_population' not in dir(algorithm):
algorithm.get_population = lambda est: [est]
##################################################
# get best solution
##################################################
if 'get_best_solution' not in dir(algorithm):
algorithm.get_best_solution = lambda est: est
print(f"{ml} does not implement get_best_solution")
else:
print(f"{ml} has get_best_solution method. Using implemented method")

population = algorithm.get_population(algorithm.est)
best_model = algorithm.get_best_solution(algorithm.est)
print('Best model')
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"
##################################################
# get population
##################################################
if 'get_population' not in dir(algorithm):
algorithm.get_population = lambda est: [est]
print(f"{ml} does not implement get_population")
else:
print(f"{ml} has get_population method. Using implemented method")

population = algorithm.get_population(algorithm.est)
assert 1 <= len(population) <= 100, \
"Population size is not within the expected range"

for p in population:
for i, p in enumerate(population):
print(f"Individual {i}")
print(algorithm.model(p, X_train))
print(p.predict(X_train.values))
Loading