def __fill_and_assert(self, fill_creator, expected_pop_indexes, expected_individuals): with saved(Config.get_instance()) as config: Config.get_instance().set("POPULATION", "size", "5") Config.get_instance().set("BEHAVIOUR", "save", "false") from MLC.Log.log import set_logger set_logger('testing') population = Population(5, 0, Config.get_instance(), MLCRepository.make("")) population.fill(fill_creator) MLCRepository.get_instance().add_population(population) # Assert that one Population was added self.assertEqual(MLCRepository.get_instance().count_population(), 1) # Assert that the individuals are in the expected position inside the Population for position, i in enumerate(expected_pop_indexes): expected_individual = expected_individuals[i] inserted_individual_id = population.get_individuals()[position] inserted_individual = MLCRepository.get_instance( ).get_individual(inserted_individual_id) self.assertEqual(expected_individual.get_value(), inserted_individual.get_value())
def __init__(self, simulation, callbacks={}, gen_creator=None): self._config = Config.get_instance() # Reload the Operations supported Operations.get_instance(reload_operations=True) self._simulation = simulation self._mlc_repository = MLCRepository.get_instance() # Set logger mode of the App set_logger(self._config.get('LOGGING', 'logmode')) self._simulation = simulation self._project_validations() # callbacks configuration self.__callbacks_manager = MLCCallbacksManager() # bad values and duplicates self.__badvalues_elim = self._config.get('EVALUATOR', 'badvalues_elim') # Gen creator if gen_creator is None: gen_method = self._config.get('GP', 'generation_method') self._gen_creator = CreationFactory.make(gen_method) else: self._gen_creator = gen_creator # Gen evaluator ev_method = self._config.get('EVALUATOR', 'evaluation_method') self._evaluator = EvaluatorFactory.make(ev_method, self.__callbacks_manager) self._look_for_duplicates = self._config.getboolean( 'OPTIMIZATION', 'lookforduplicates') # callbacks for the MLC application if MLC_CALLBACKS.ON_START in callbacks: self.__callbacks_manager.subscribe( MLC_CALLBACKS.ON_START, callbacks[MLC_CALLBACKS.ON_START]) if MLC_CALLBACKS.ON_EVALUATE in callbacks: self.__callbacks_manager.subscribe( MLC_CALLBACKS.ON_EVALUATE, callbacks[MLC_CALLBACKS.ON_EVALUATE]) if MLC_CALLBACKS.ON_NEW_GENERATION in callbacks: self.__callbacks_manager.subscribe( MLC_CALLBACKS.ON_NEW_GENERATION, callbacks[MLC_CALLBACKS.ON_NEW_GENERATION]) if MLC_CALLBACKS.ON_FINISH in callbacks: self.__callbacks_manager.subscribe( MLC_CALLBACKS.ON_FINISH, callbacks[MLC_CALLBACKS.ON_FINISH]) # add callback to show best individual self.__callbacks_manager.subscribe(MLC_CALLBACKS.ON_NEW_GENERATION, self.show_best) self.__display_best = True
def get_simulation(self): if Experiment.__last_simulation is None or Experiment.__last_simulation != self._simulation: MLCRepository._instance = None Config._instance = None Config.get_instance().read(self._config_file) set_logger(Config.get_instance().get('LOGGING', 'logmode')) if Experiment.__last_simulation: Experiment.__last_simulation.close() self._simulation = Simulation(self._name) Experiment.__last_simulation = self._simulation return self._simulation
def main(): # MATLAB random numbers, used in integration tests RandomManager.load_random_values( "./tests/integration_tests/matlab_randoms.txt") # load configuration config = initialize_config() # set logger log_mode = config.get('LOGGING', 'logmode') set_logger(log_mode) simulation = Simulation() mlc = Application(simulation) mlc.go(to_generation=3, display_best=False) raw_input("Press enter to continue...")
def setUp(self): set_logger("testing") # Load randoms from file random_file = './mlc/unit_matlab_randoms.txt' RandomManager.clear_random_values() RandomManager.load_random_values(random_file) config = Config.get_instance() config.read( os.path.join(mlc_config_path.get_test_path(), 'mlc/individual/configuration.ini')) self._individual_l0 = Individual("(root (cos 5.046))") self._individual_l1 = Individual( "(root (log (sin (exp (tanh 3.6284)))))") self._individual_l2 = Individual( "(root (cos (* (+ (* -1.912 -9.178) (cos S0)) 3.113)))") self._individual_l3 = Individual( "(root (log (/ (* (sin 4.37) (- -8.815 -3.902)) (log (+ 2.025 -8.685)))))" ) self._individual_l4 = Individual("(root S0)")
def setUp(self): try: os.makedirs(MLCWorkspaceTest.WORKSPACE_DIR) except OSError: shutil.rmtree(MLCWorkspaceTest.WORKSPACE_DIR) os.makedirs(MLCWorkspaceTest.WORKSPACE_DIR) mlc = MLCLocal(working_dir=MLCWorkspaceTest.WORKSPACE_DIR) # general settings for MLC set_logger('console') set_working_directory(MLCWorkspaceTest.WORKSPACE_DIR) this_dir = os.path.dirname(os.path.abspath(__file__)) MLCWorkspaceTest.ORIGINAL_CONFIGURATION = os.path.join(this_dir, MLCWorkspaceTest.ORIGINAL_EXPERIMENT + ".conf") MLCWorkspaceTest.NEW_CONFIGURATION = os.path.join(this_dir, MLCWorkspaceTest.NEW_EXPERIMENT + ".conf") mlc.new_experiment(MLCWorkspaceTest.ORIGINAL_EXPERIMENT, MLCWorkspaceTest.ORIGINAL_CONFIGURATION) # random file for simulations MLCWorkspaceTest.FILE_WITH_RANDOMS = os.path.join(this_dir, "matlab_randoms.txt")
import MLC.Log.log as lg from MLC.Common.LispTreeExpr.LispTreeExpr import LispTreeExpr from MLC.Log.log import set_logger from MLC.mlc_parameters.mlc_parameters import Config def initialize_config(): config = Config.get_instance() config.read('/home/htomar/MLC-0.0.5/Clone_18/Clone_18.conf') return config # Set printable resolution (don't alter numpy interval resolution) np.set_printoptions(precision=9) # Show full arrays, no matter what size do they have np.set_printoptions(threshold=np.inf) # Don't show scientific notation np.set_printoptions(suppress=True) initialize_config() set_logger('console') # Full expression expr6 = "(root (/ (+ 636.4469 (cos S6)) (log (* 1069.5890 (/ (/ (/ (/ (/ (sin (- -1491.0946 (- S7 1541.5198))) (- (exp (sin (- -701.6797 (- 394.8346 (- S5 0.5484))))) -0.1508)) (exp (- (sin (sin (/ S3 1053.8509))) -0.0004))) (tanh (exp (log S6)))) (exp (sin (sin (tanh (sin (sin (- -701.6797 (- 394.8346 (- S5 0.5484)))))))))) (tanh (exp (- (tanh (- (log S3) (exp (sin (- -701.6797 (- 394.8346 (- S5 0.5484))))))) -0.0004))))))))" expr61 = "(root (exp (- -6.3726 (* -7.1746 S0))))" expr612 = "(root (- -6.3726 (* -7.1746 S0)))" tree = LispTreeExpr(expr6) out = LispTreeExpr.formal(tree) print out
def setUpClass(cls): set_logger("file") config = Config.get_instance() config.read( os.path.join(config_path.get_test_path(), 'mlc/individual/configuration.ini'))
S1 = np.array(g_data[1]) # Temperature S2 = np.array(g_data[2]) # Wind S3 = np.array(g_data[3]) # Solar S4 = np.array(g_data[4]) # Humidity S5 = np.array(g_data[5]) # IsHoliday S6 = np.array(g_data[6]) # Day of the Week S7 = np.array(g_data[7]) y = np.array(g_data[8]) # Whole Building Energy # func = (((636.4469 + np.cos(S6)))/(np.log((1069.5890 * ((((((((((np.sin(((-1491.0946) - (S7 - 1541.5198))))/((np.exp(np.sin(((-701.6797) - (394.8346 - (S5 - 0.5484))))) - (-0.1508)))))/(np.exp((np.sin(np.sin(((S3)/(1053.8509)))) - (-0.0004))))))/(np.tanh(np.exp(np.log(S6))))))/(np.exp(np.sin(np.sin(np.tanh(np.sin(np.sin(((-701.6797) - (394.8346 - (S5 - 0.5484))))))))))))/(np.tanh(np.exp((np.tanh((np.log(S3) - np.exp(np.sin(((-701.6797) - (394.8346 - (S5 - 0.5484))))))) - (-0.0004)))))))))) func = "(root (/ (+ 636.4469 (cos S6)) (log (* 1069.5890 (/ (/ (/ (/ (/ (sin (- -1491.0946 (- S7 1541.5198))) (- (exp (sin (- -701.6797 (- 394.8346 (- S5 0.5484))))) -0.1508)) (exp (sin (sin (tanh (sin (tanh (/ S3 1053.8509)))))))) (tanh (exp (/ (tanh (/ (/ S3 1053.8509) 0.1508)) 0.1508)))) (exp (sin (sin (/ (sin (tanh (sin (tanh (/ S3 1053.8509))))) (- (/ S3 1053.8509) -0.1508)))))) (tanh (exp (/ (/ (/ (/ S3 1053.8509) (- (- (sin (tanh (/ S3 1053.8509))) -0.0004) -0.1508)) (- (cos (/ -1994.7186 S5)) -0.1508)) (tanh (exp (sin S6)))))))))))" initialize_config() # Don't log anything set_logger('testing') # Evaluate the expression tree = LispTreeExpr(func) b = tree.calculate_expression([S0, S1, S2, S3, S4, S5, S6, S7]) # Compare the expressions #============================================================================== # z = (np.vstack((b, y, S5, S6, S7))).T # for index in xrange(len(z)): # print ("Index: {}\t func: {}\t y: {}\t S3: {}\t S5: {}\t S6: {}\t S7: {}" # .format(index, z[index][0], z[index][1], S3[index], S5[index], S6[index], S7[index])) #============================================================================== y_mean = np.mean(y) sq_diff = np.dot((y-b), (y-b))