Skip to content

Commit 334c0e7

Browse files
committed
Solved import between different experiments bug
* The ev and preev modules are unloaded and loaded before every experiment #57
1 parent 712c93b commit 334c0e7

File tree

7 files changed

+137
-278
lines changed

7 files changed

+137
-278
lines changed

MLC/Common/PreevaluationManager.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,26 @@ def get_callback():
1818
"""
1919
# Check if the preevaluation is activated
2020
if Config.get_instance().getboolean('EVALUATOR', 'preevaluation'):
21-
module_name = Config.get_instance().get('EVALUATOR', 'preev_function')
21+
function_name = Config.get_instance().get('EVALUATOR', 'preev_function')
22+
module_name = "Preevaluation.{0}".format(function_name)
23+
24+
try:
25+
# WARNING: I am unloading manually the evaluation function module. I need to do this
26+
# because Python does not support module unloading and my evaluation functions are
27+
# all the same, so when one experiment loads his module, other project with the same
28+
# name of module won't be able to load yours
29+
preev_module = sys.modules["Preevaluation"]
30+
del sys.modules['Preevaluation']
31+
del preev_module
32+
lg.logger_.debug("[EV_FACTORY] Module {0} was removed"
33+
.format(sys.modules["Preevaluation"]))
34+
except KeyError, err:
35+
# If the module cannot be unload because it does not exists, continue
36+
pass
37+
2238
lg.logger_.debug('[PREEV_MANAGER] Importing module {0}'.format(module_name))
2339
try:
24-
module = importlib.import_module('Preevaluation.' + module_name)
40+
module = importlib.import_module(module_name)
2541
reload(module)
2642
return module
2743
except ImportError:

MLC/GUI/Experiment/FirstIndividualsManager.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,3 @@ def _load_table(self):
226226
table_model.set_editable_columns([1])
227227
table_model.set_data_changed_callback(self.modify_individual)
228228
table_model.sort_by_col(0)
229-
230-
def _test_individual_value(self, indiv_value):
231-
"""
232-
Evaluate an individual in order to check its correctness. If the evaluation
233-
throw any exception, it won't be handled in this method
234-
"""
235-
LispTreeExpr.check_expression(indiv_value)
236-
individual = Individual.generate(config=Config.get_instance(),
237-
rhs_value=indiv_value)
238-
callback = EvaluatorFactory.get_callback()
239-
return callback.cost(individual)

MLC/Population/Evaluation/EvaluatorFactory.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,31 @@
99
class EvaluatorFactory(object):
1010
@staticmethod
1111
def get_callback():
12-
module_name = Config.get_instance().get('EVALUATOR', 'evaluation_function')
12+
function_name = Config.get_instance().get('EVALUATOR', 'evaluation_function')
13+
module_name = 'Evaluation.{0}'.format(function_name)
14+
15+
try:
16+
# WARNING: I am unloading manually the evaluation function module. I need to do this
17+
# because Python does not support module unloading and my evaluation functions are
18+
# all the same, so when one experiment loads his module, other project with the same
19+
# name of module won't be able to load yours
20+
ev_module = sys.modules["Evaluation"]
21+
del sys.modules['Evaluation']
22+
del ev_module
23+
lg.logger_.debug("[EV_FACTORY] Module {0} was removed".format(sys.modules["Evaluation"]))
24+
except KeyError, err:
25+
# If the module cannot be unload because it does not exists, continue
26+
pass
27+
1328
lg.logger_.debug('[EV_FACTORY] Importing module {0}'.format(module_name))
1429
try:
15-
module = importlib.import_module('Evaluation.' + module_name)
30+
# lg.logger_.debug("[EV_FACTORY] Sys.path: {0}".format(sys.path))
31+
module = importlib.import_module(module_name)
1632
reload(module)
1733
return module
18-
except ImportError:
19-
lg.logger_.debug("[EV_FACTORY] Evaluation function doesn't exists. " +
20-
"Aborting program...")
34+
except ImportError, err:
35+
lg.logger_.debug("[EV_FACTORY] Evaluation function doesn't exists. "
36+
"Aborting program. Error Msg: {0}".format(err))
2137
sys.exit(-1)
2238

2339
@staticmethod

MLC/Scripts/Evaluation/toy_problem.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

MLC/Scripts/Evaluation/toy_problem_python_ev.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)