Skip to content

Commit 6bf088f

Browse files
committed
Added importlib to load evaluation and preevaluation scritps
* #29
1 parent c1daf13 commit 6bf088f

File tree

5 files changed

+31
-45
lines changed

5 files changed

+31
-45
lines changed

MLC/Application.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,17 @@
1111
from MLC.Population.Creation.CreationFactory import CreationFactory
1212
from MLC.Population.Evaluation.EvaluatorFactory import EvaluatorFactory
1313
from MLC.Population.Population import Population
14-
from MLC.Scripts.Evaluation import toy_problem
15-
from MLC.Scripts.Evaluation import toy_problem_python_ev
16-
from MLC.Scripts.Evaluation import arduino
17-
from MLC.Scripts.Evaluation import simulink_ev
18-
from MLC.Scripts.Preevaluation import default
19-
from MLC.Scripts.Preevaluation import simulink_preev
2014
from MLC.Simulation import Simulation
2115

2216

2317
class Application(object):
2418

2519
def __init__(self, simulation, log_mode='console'):
2620
self._config = Config.get_instance()
27-
self._set_ev_callbacks()
28-
self._set_preev_callbacks()
2921

3022
# Set logger mode of the App
3123
set_logger(log_mode)
24+
self._project_validations()
3225
self._simulation = simulation
3326

3427
# Gen creator
@@ -170,16 +163,10 @@ def show_best(self, population):
170163
stop_no_graph = self._config.getboolean('BEHAVIOUR', 'stopongraph')
171164
EvaluatorFactory.get_ev_callback().show_best(best_index, best_indiv, stop_no_graph)
172165

173-
def _set_ev_callbacks(self):
174-
# Set the callbacks to be called at the moment of the evaluation
175-
# FIXME: Dinamically get instances from "MLC.Scripts import *"
176-
EvaluatorFactory.set_ev_callback('toy_problem', toy_problem)
177-
EvaluatorFactory.set_ev_callback('toy_problem_python_ev', toy_problem_python_ev)
178-
EvaluatorFactory.set_ev_callback('arduino', arduino)
179-
EvaluatorFactory.set_ev_callback('simulink_ev', simulink_ev)
180-
181-
def _set_preev_callbacks(self):
182-
# Set the callbacks to be called at the moment of the preevaluation
183-
# FIXME: To this dynamically searching .pys in the directory
184-
PreevaluationManager.set_callback('default', default)
185-
PreevaluationManager.set_callback('simulink_preev', simulink_preev)
166+
167+
def _project_validations(self):
168+
# Check that the evaluation and preevaluation modules can be loaded
169+
EvaluatorFactory.get_ev_callback()
170+
PreevaluationManager.get_callback()
171+
172+
# TODO: Add another validations

MLC/Common/PreevaluationManager.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import MLC.Log.log as lg
2+
import importlib
23
import sys
34
from MLC.mlc_parameters.mlc_parameters import Config
45

@@ -8,11 +9,6 @@
89

910

1011
class PreevaluationManager(object):
11-
callback = {}
12-
13-
@staticmethod
14-
def set_callback(callback_name, ev_callback):
15-
PreevaluationManager.callback[callback_name] = ev_callback
1612

1713
@staticmethod
1814
def get_callback():
@@ -22,10 +18,14 @@ def get_callback():
2218
"""
2319
# Check if the preevaluation is activated
2420
if Config.get_instance().getboolean('EVALUATOR', 'preevaluation'):
25-
callback_name = Config.get_instance().get('EVALUATOR', 'preev_function')
21+
module_name = Config.get_instance().get('EVALUATOR', 'preev_function')
22+
lg.logger_.debug('[PREEV_MANAGER] Importing module {0}'.format(module_name))
2623
try:
27-
return PreevaluationManager.callback[callback_name]
28-
except KeyError:
29-
lg.logger_.debug("Preevaluation function doesn't exists. Aborting program...")
24+
callback = importlib.import_module('MLC.Scripts.Preevaluation.' + module_name)
25+
return callback
26+
except ImportError:
27+
lg.logger_.debug("[PREEV_MANAGER] Preevaluation function doesn't exists. " +
28+
"Aborting program...")
29+
sys.exit(-1)
30+
3031

31-
return None
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
import MLC.Log.log as lg
2+
import importlib
23
import sys
34

45
from MLC.mlc_parameters.mlc_parameters import Config
56
from MLC.Population.Evaluation.StandaloneEvaluator import StandaloneEvaluator
67

78

89
class EvaluatorFactory(object):
9-
callback = {}
10-
11-
@staticmethod
12-
def set_ev_callback(callback_name, ev_callback):
13-
EvaluatorFactory.callback[callback_name] = ev_callback
14-
1510
@staticmethod
1611
def get_ev_callback():
17-
callback_name = Config.get_instance().get('EVALUATOR', 'evaluation_function')
12+
module_name = Config.get_instance().get('EVALUATOR', 'evaluation_function')
13+
lg.logger_.debug('[EV_FACTORY] Importing module {0}'.format(module_name))
1814
try:
19-
return EvaluatorFactory.callback[callback_name]
20-
except KeyError:
21-
lg.logger_.debug("Evaluation function doesn't exists. " +
15+
callback = importlib.import_module('MLC.Scripts.Evaluation.' + module_name)
16+
return callback
17+
except ImportError:
18+
lg.logger_.debug("[EV_FACTORY] Evaluation function doesn't exists. " +
2219
"Aborting program...")
20+
sys.exit(-1)
2321

2422
@staticmethod
2523
def make(strategy):
2624
if strategy == "mfile_standalone":
2725
ev_callback = EvaluatorFactory.get_ev_callback()
2826
return StandaloneEvaluator(ev_callback)
2927
else:
30-
lg.logger_.error("[CREATION_FACTORY] Evaluation method " +
28+
lg.logger_.error("[EV_FACTORY] Evaluation method " +
3129
strategy + " is not valid. Aborting program")
3230
sys.exit(-1)

conf/configuration.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ badvalue = 1e36
6969
badvalues_elim = first
7070
%badvalues_elim = none
7171
%badvalues_elim = all
72-
preevaluation = false
73-
preev_function = simulink_preev
72+
preevaluation = true
73+
preev_function = default
7474
problem_variables.gamma = 0.1
7575

7676
[BEHAVIOUR]

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/python2.7
22
import numpy as np
3+
import importlib
34
from MLC.Application import Application
45
from MLC.mlc_parameters.mlc_parameters import Config
56
from MLC.matlab_engine import MatlabEngine
@@ -22,11 +23,11 @@ def main():
2223

2324
eng = MatlabEngine.engine()
2425
config = initialize_config()
25-
MatlabEngine.load_random_values("./tests/integration_tests/matlab_randoms.txt")
2626

2727
# Create the MLC2 object and store it in the workspace. With this
2828
# feature we will be able to call every function of the MATLAB code
2929
# from any part of the code where the engine is available
30+
# MatlabEngine.load_random_values("./tests/integration_tests/matlab_randoms.txt")
3031
eng.workspace['wmlc'] = eng.MLC2()
3132

3233
simulation = Simulation()

0 commit comments

Comments
 (0)