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

Fix to get Test and Train Fitness for final solution of Mult-objective Optimization. Fixes #166 #167

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 grammars/supervised_learning/supervised_learning.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(<e>-<e>) |
(<e>*<e>) |
aq(<e>,<e>) |
x[:, <varidx>] |
x[<varidx>] |
<c>
<varidx> ::= GE_RANGE:dataset_n_vars
<c> ::= <d>.<d> | -<d>.<d>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def __init__(self):
# if we want a separate test set, generate a set of fitness
# cases for it
if params['DATASET_TEST']:
self.training_test = True
self.test_in = np.random.random((n_vars, n_samples))
self.test_exp = p.eval(self.training_in)

Expand Down
3 changes: 0 additions & 3 deletions src/fitness/supervised_learning/supervised_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ def __init__(self):
# Find number of variables.
self.n_vars = np.shape(self.training_in)[1] # sklearn convention

# Regression/classification-style problems use training and test data.
if params['DATASET_TEST']:
self.training_test = True

def evaluate(self, ind, **kwargs):
"""
Expand Down
14 changes: 8 additions & 6 deletions src/stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def get_soo_stats(individuals, end):
stdout.flush()

# Generate test fitness on regression problems
if hasattr(params['FITNESS_FUNCTION'], "training_test") and end:
if params["DATASET_TEST"] and end:
# Save training fitness.
trackers.best_ever.training_fitness = copy(trackers.best_ever.fitness)

Expand Down Expand Up @@ -240,7 +240,7 @@ def get_moo_stats(individuals, end):
stdout.flush()

# Generate test fitness on regression problems
if hasattr(params['FITNESS_FUNCTION'], "training_test") and end:
if params["DATASET_TEST"] and end:

for ind in trackers.best_ever:
# Iterate over all individuals in the first front.
Expand All @@ -249,7 +249,10 @@ def get_moo_stats(individuals, end):
ind.training_fitness = copy(ind.fitness)

# Evaluate test fitness.
ind.test_fitness = params['FITNESS_FUNCTION'](ind, dist='test')
ind.test_fitness = [
func(ind, dist="test")
for func in params["FITNESS_FUNCTION"].fitness_functions
]

# Set main fitness as training fitness.
ind.fitness = ind.training_fitness
Expand Down Expand Up @@ -364,9 +367,8 @@ def print_final_stats():
:return: Nothing.
"""

if hasattr(params['FITNESS_FUNCTION'], "training_test"):
print("\n\nBest:\n Training fitness:\t",
trackers.best_ever.training_fitness)
if params["DATASET_TEST"]:
print("\n\nBest:\n Training fitness:\t", trackers.best_ever.training_fitness)
print(" Test fitness:\t\t", trackers.best_ever.test_fitness)
else:
print("\n\nBest:\n Fitness:\t", trackers.best_ever.fitness)
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/stats/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def save_best_ind_to_file(stats, ind, end=False, name="best"):
savefile.write("Phenotype:\n" + str(ind.phenotype) + "\n\n")
savefile.write("Genotype:\n" + str(ind.genome) + "\n")
savefile.write("Tree:\n" + str(ind.tree) + "\n")
if hasattr(params['FITNESS_FUNCTION'], "training_test"):
if params["DATASET_TEST"]:
if end:
savefile.write("\nTraining fitness:\n" + str(ind.training_fitness))
savefile.write("\nTest fitness:\n" + str(ind.test_fitness))
Expand Down