def _assert_expressions(self, expression, function_name): # expected = self._eng.simplify_my_LISP(expression) expected = SimplificationTest.simplified_expressions[function_name] tree = LispTreeExpr(expression) tree.simplify_tree() obtained = tree.get_simplified_tree_as_string() self.assertEquals(obtained, expected)
def _draw_individual_tree(self, pop_index, indiv_index, simplify, evaluate, value): fig = plt.figure() # Put figure window on top of all other windows fig.canvas.manager.window.setWindowModality(Qt.ApplicationModal) fig.canvas.manager.window.setWindowTitle( "Individual Tree Representation") # Simplify the tree if neccesary tree = LispTreeExpr(value) if simplify == True: tree.simplify_tree() graph = tree.construct_graph() # Evaluate the Individual if necessary cost = None if evaluate == True: cost = test_individual_value(parent=self, experiment_name=self._experiment_name, log_prefix=self._log_prefix, indiv_value=value, config=Config.get_instance()) # Remove image axes ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') # Networkx use the node id as label. We have to relabel every node labels = {} for node_id in graph: labels[node_id] = graph.node[node_id]['value'] # Use Pygraph to visualize the graph as a hierarchical tree pos = nx.nx_pydot.graphviz_layout(graph, prog='dot') # Draw nodes and edges in separated nodes to be able to change # the perimter color of the nodes nodes = nx.draw_networkx_nodes(graph, pos, node_size=1000, node_color='#D3D3D3') nodes.set_edgecolor('k') nx.draw_networkx_edges(graph, pos, arrows=False) nx.draw_networkx_labels(graph, pos, labels, font_size=12) # Set the figure Title plt.rc('font', family='serif') title = ( u"Generation N°{0} - Individual N°{1} - Simplified: {2}".format( self._current_gen, indiv_index, simplify)) if evaluate == True: title += " - Cost: {0}".format(cost) plt.suptitle(title, fontsize=12) plt.show() self.close()
def test_do_not_raise_exception_when_numpy_warning_appear(self): # This expression raise a numpy warning expression = '(root (cos (exp 1e20)))' tree = LispTreeExpr(expression) try: tree.calculate_expression(sensor_replacement_list=[]) self.assertEquals(True, True) except FloatingPointError: self.assertEquals(True, False)
def test_tree_depth_root(self): expression = '(root S0)' tree = LispTreeExpr(expression) # root root = tree.get_root_node() self.assertNode(root, depth=1, childs=1, expr_index=0) # S0 self.assertNode(root._nodes[0], depth=1, childs=0, expr_index=6)
def test_tree_depth_leaf(self): expression = '(root (tanh S0))' tree = LispTreeExpr(expression) # root root = tree.get_root_node() self.assertNode(root, depth=1, childs=1, expr_index=0) # (tanh S0) sub_expression = root._nodes[0] self.assertNode(sub_expression, depth=2, childs=1, expr_index=6) # S0 self.assertNode(sub_expression._nodes[0], depth=2, childs=0, expr_index=12)
def check_individual_value(parent, experiment_name, log_prefix, indiv_value, nodialog=False): try: """ Evaluate an individual in order to check its correctness. Handle Exceptions """ LispTreeExpr.check_expression(indiv_value) return True except ExprException, err: # Print the error message returned in the exception, # removing the prefix ([EXPR_EXCEPTION]]) if not nodialog: QMessageBox.critical(parent, "Invalid Individsual", "Individual inserted is not well-formed. " "Error Msg: {0}" .format(err.message[err.message.find(']') + 2:])) logger.error("{0} Experiment {1} - " "Individual inserted is not well-formed. " "Error Msg: {2}" .format(log_prefix, experiment_name, err.message[err.message.find(']') + 2:]))
def test_tree_subtree(self): expression = '(root (+ (tanh S0) (cos S1))))' tree = LispTreeExpr(expression) # root root = tree.get_root_node() self.assertNode(root, 1, 1, expr_index=0) # (+ (tanh S0) (cos S1))) sum_node = root._nodes[0] self.assertNode(sum_node, depth=2, childs=2, expr_index=6) # (tanh S0) subtree_0 = sum_node._nodes[0] self.assertNode(subtree_0, depth=3, childs=1, expr_index=9) self.assertNode(subtree_0._nodes[0], depth=3, childs=0, expr_index=15) # (cos S1) subtree_1 = sum_node._nodes[1] self.assertNode(subtree_1, depth=3, childs=1, expr_index=19) self.assertNode(subtree_1._nodes[0], depth=3, childs=0, expr_index=24)
def test_individual_value(parent, experiment_name, log_prefix, indiv_value, config): try: """ Evaluate an individual in order to check its correctness. Handle Exceptions """ LispTreeExpr.check_expression(indiv_value) individual = Individual.generate(config=config, rhs_value=indiv_value) callback = EvaluatorFactory.get_callback() return callback.cost(individual) except ExprException, err: # Print the error message returned in the exception, # removing the prefix ([EXPR_EXCEPTION]]) QMessageBox.critical(parent, "Invalid Individual", "Individual inserted is not well-formed. " "Error Msg: {0}" .format(err.message[err.message.find(']') + 2:])) logger.error("{0} Experiment {1} - " "Individual inserted is not well-formed. " "Error Msg: {2}" .format(log_prefix, experiment_name, err.message[err.message.find(']') + 2:]))
def __simplify_and_sensors_tree(value, config): sensor_list = () replace_list = () if config.getboolean('POPULATION', 'sensor_spec'): config_sensor_list = sorted( config.get_list('POPULATION', 'sensor_list')) sensor_list = ['S' + str(x) for x in config_sensor_list] replace_list = [ 'z' + str(x) for x in range(len(config_sensor_list)) ] else: amount_sensors = config.getint('POPULATION', 'sensors') # Replace the available sensors in the individual expression sensor_list = ['S' + str(x) for x in range(amount_sensors)] replace_list = ['z' + str(x) for x in range(amount_sensors)] for i in range(len(replace_list)): value = value.replace(replace_list[i], sensor_list[i]) if config.getboolean('OPTIMIZATION', 'simplify'): return LispTreeExpr(value).get_simplified_tree_as_string() return value
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 __mutate_tree(self, mutation_type): mutmindepth = self._config.getint("GP", "mutmindepth") maxdepth = self._config.getint("GP", "maxdepth") sensor_spec = self._config.getboolean("POPULATION", "sensor_spec") sensors = self._config.getint("POPULATION", 'sensors') mutation_types = self._config.get_list("GP", 'mutation_types') # equi probability for each mutation type selected. if mutation_type == Individual.MutationType.ANY: rand_number = RandomManager.rand() mutation_type = mutation_types[int( np.floor(rand_number * len(mutation_types)))] if mutation_type in [ Individual.MutationType.REMOVE_SUBTREE_AND_REPLACE, Individual.MutationType.SHRINK ]: new_individual_value = None preevok = False while not preevok: # remove subtree and grow new subtree try: subtree, _, _ = self.__extract_subtree( self._tree, mutmindepth, maxdepth, maxdepth) if mutation_type == Individual.MutationType.REMOVE_SUBTREE_AND_REPLACE: next_individual_type = 0 else: next_individual_type = 4 new_individual_value = Individual.__generate_indiv_regressive_tree( subtree, self._config, next_individual_type) if new_individual_value: if sensor_spec: config_sensor_list = sorted( self._config.get_list('POPULATION', 'sensor_list')) else: config_sensor_list = range(sensors - 1, -1, -1) for i in range(len(config_sensor_list)): new_individual_value = new_individual_value.replace( "z%d" % i, "S%d" % config_sensor_list[i]) # Preevaluate the Individual preevok = self._preevaluate_individual( new_individual_value) else: raise TreeException() except TreeException: raise TreeException( "[MUTATE_TREE] A non subtractable Individual was generated. " "Individual: {0}".format( self._tree.get_expanded_tree_as_string())) return new_individual_value elif mutation_type == Individual.MutationType.REPARAMETRIZATION: new_individual_value = None preevok = False while not preevok: new_individual_value = self.__reparam_tree( LispTreeExpr(self.get_value())) preevok = self._preevaluate_individual(new_individual_value) return new_individual_value elif mutation_type == Individual.MutationType.HOIST: preevok = False counter = 0 maxtries = self._config.getint("GP", "maxtries") while not preevok and counter < maxtries: counter += 1 controls = self._config.getint("POPULATION", "controls") prob_threshold = 1 / float(controls) cl = [ stree.to_string() for stree in self.get_tree().get_root_node()._nodes ] changed = False k = 0 for nc in RandomManager.randperm(controls): k += 1 # control law is cropped if it is the last one and no change happend before if (RandomManager.rand() < prob_threshold) or (k == controls and not changed): try: _, sm, _ = self.__extract_subtree( LispTreeExpr('(root ' + cl[nc - 1] + ')'), mutmindepth + 1, maxdepth, maxdepth + 1) cl[nc - 1] = sm changed = True except TreeException: changed = False new_individual_value = "(root %s)" % " ".join(cl[:controls]) preevok = self._preevaluate_individual(new_individual_value) if counter == maxtries: raise TreeException( "[MUTATE HOIST] Candidate could not found a " "substitution {0} tests".format(maxtries)) return new_individual_value else: raise NotImplementedError("Mutation type %s not implemented" % mutation_type)
def _tree(self): if self._lazy_tree is None: self._lazy_tree = LispTreeExpr(self.get_value()) return self._lazy_tree
# 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 (cos (exp (- -6.3726 (* -7.1746 S0)))))" expr61 = "(root (exp (- -6.3726 (* -7.1746 S0))))" expr612 = "(root (- -6.3726 (* -7.1746 S0)))" tree = LispTreeExpr(expr6) x = np.linspace(-10.0, 10.0, num=201) mlc_y = tree.calculate_expression([x]) # Calculate the Mean squared error y = np.tanh(x**3 - x**2 - 1) evaluation = float(np.sum((mlc_y - y)**2)) # print mlc_y with open("./costs_python.txt", "w") as f: for elem in mlc_y: f.write("%50.50f\n" % elem) print evaluation print np.sum(mlc_y)
def assert_check_expression_without_exception(self, expression): try: LispTreeExpr.check_expression(expression) self.assertTrue(True, True) except ExprException: self.assertTrue(True, False)
def assert_check_expression_with_exception(self, expression, exception_class): try: LispTreeExpr.check_expression(expression) self.assertTrue(True, False) except exception_class: self.assertTrue(True, True)
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)) CV = np.sqrt(sq_diff/len(b))/y_mean print CV