def compress(individual): nodes, edges, labels = gp.graph(individual) from gp_utils import recompute_tree_graph graph = recompute_tree_graph(nodes, edges) root = visit_node(graph, 0, labels) optimized = optimize(root) return wrap_children(optimized)
def level_data(expr): """ This method review each node in the individual and it indicate the level, arity, the number of node and the number of node of the father :param expr: Individual to take the information :return: a list [node_num, level, arity, father] """ nodes, edges, labels = gp.graph(expr) edge = sorted(edges) contador = 1 nod = 0 level=list() if len(expr)<2: level.append([0, contador, 0, None]) else: level.append([edge[0][0], contador, expr[0].arity, None]) # This is for the root. for i in range(max(nodes)): restart=True contador=1 nod=i+1 while restart: restart=False for j in range(max(nodes)): if edge[j][1]==nod: contador+=1 nod=edge[j][0] if(nod>0 and contador>1): restart=True for elem in edge: if elem[1]==(i+1): nod_p=elem[0] level.append([i+1, contador, expr[i+1].arity, nod_p]) return level
def draw_individual(individual, datafile, run_type): g = pgv.AGraph() g.graph_attr['label'] = "{}:{}".format(datafile, run_type).upper() for i, expr in enumerate(individual): nodes, edges, labels = gp.graph(expr) node_map = dict() for j in range(len(nodes)): current = nodes[j] new_node = int(str(i + 1) + str(current)) nodes[j] = new_node node_map[current] = new_node for j in range(len(edges)): edges[j] = (node_map[edges[j][0]], node_map[edges[j][1]]) g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for j in range(len(nodes)): n = g.get_node(node_map[j]) # round constants to 4dp for aesthetics :-) try: labels[j] = float(labels[j]) labels[j] = str(round(labels[j], 4)) except: pass n.attr["label"] = labels[j] return g
def level_node(expr): nodes, edges, labels = gp.graph(expr) #outfile = open('edges.txt', 'a') edge=sorted(edges) #outfile.write('\n edges expre %s %s ' % (edge,expr)) #outfile.close() contador=1 nod=0 level=list() restart=True #numnodo #numnivel #aridad if len(expr)<2: level.append([0, contador, 0]) else: level.append([edge[0][0], contador, expr[0].arity]) for i in range(max(nodes)): restart=True contador=1 nod=i+1 while restart: restart=False for j in range(max(nodes)): if edge[j][1]==nod: contador+=1 nod=edge[j][0] if(nod>0 and contador>1): restart=True level.append([i+1, contador, expr[i+1].arity]) return level
def plot_trees(best): """ create tree plots from best array """ for i in range(len(best)): nodes, edges, labels = gp.graph(best[i]) matplotlib.rcParams['figure.figsize'] = (10.0, 10.0) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = graphviz_layout(g, prog="dot") f = plt.figure() nx.draw_networkx_nodes(g, pos, node_size=1500, font_size=8, node_color='lightblue') nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels, font_size=8) if (i < 10): plotfile = "tree_0" + str(i) + ".pdf" else: plotfile = "tree_" + str(i) + ".pdf" plt.title(str(best[i])) f.savefig(plotfile)
def evalSymbReg(individual, points): # Transform the tree expression in a callable function func = toolbox.compile(expr=individual) # traversing the syntax tree to discover options and interactions nodes, edges, labels = gp.graph(expr=individual) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show() degrees = g.degree() primitives = nx.dfs_successors(g) L = g.size() #for i in range(L): # if degrees[i] == 1: sqerrors = [] for point in points: x1 = point[0] x2 = point[1] sqerrors.append((func(x1,x2) - x1 ** 4 - x1 ** 3 - x2 ** 2 - x2) ** 2) return math.fsum(sqerrors) / len(points),
def level_node(expr): """ This method review each node in the individual and it indicate the level, arity and number of node. :param expr: Individual to take the information :return: a list [node_num, level, arity] """ nodes, edges, labels = gp.graph(expr) edge = sorted(edges) contador = 1 nod = 0 level = list() if len(expr) < 2: level.append([0, contador, 0]) else: level.append([edge[0][0], contador, expr[0].arity]) for i in range(max(nodes)): restart = True contador = 1 nod = i + 1 while restart: restart = False for j in range(max(nodes)): if edge[j][1] == nod: contador += 1 nod = edge[j][0] if (nod > 0) and (contador > 1): restart = True level.append([i + 1, contador, expr[i + 1].arity]) return level
def level_node(expr): nodes, edges, labels = gp.graph(expr) #outfile = open('edges.txt', 'a') edge = sorted(edges) #outfile.write('\n edges expre %s %s ' % (edge,expr)) #outfile.close() contador = 1 nod = 0 level = list() restart = True #numnodo #numnivel #aridad if len(expr) < 2: level.append([0, contador, 0]) else: level.append([edge[0][0], contador, expr[0].arity]) for i in range(max(nodes)): restart = True contador = 1 nod = i + 1 while restart: restart = False for j in range(max(nodes)): if edge[j][1] == nod: contador += 1 nod = edge[j][0] if (nod > 0 and contador > 1): restart = True level.append([i + 1, contador, expr[i + 1].arity]) return level
def level_node(expr): """ This method review each node in the individual and it indicate the level, arity and number of node. :param expr: Individual to take the information :return: a list [node_num, level, arity] """ nodes, edges, labels = gp.graph(expr) edge = sorted(edges) contador = 1 nod = 0 level=list() if len(expr) < 2: level.append([0, contador, 0]) else: level.append([edge[0][0], contador, expr[0].arity]) # This is for the root. for i in range(max(nodes)): restart = True contador = 1 nod = i+1 while restart: restart=False for j in range(max(nodes)): if edge[j][1]==nod: contador += 1 nod = edge[j][0] if(nod > 0) and (contador > 1): restart = True level.append([i+1, contador, expr[i+1].arity]) return level
def fiteval(individual, treeorret): global k penalty = 0 nodes, edges, evals = gp.graph(individual) print(evals) print(len(evals)) keys = get_key('gt', evals) keys1 = get_key('ls', evals) keys2 = get_key('ls1', evals) keys3 = get_key('gt1', evals) print(keys) print(keys1) f = gp.compile(individual, pset) def f1(row): x = np.array(row) try: return f(*x) except Exception as e: return 0 def pencalc(keys): penalty = 0 for i in keys: subject = [evals[i + 1], evals[i + 2]] print(subject) cond = (operator.or_(*[isinstance(j, float) for j in subject]) and (any(i in pset.arguments for i in subject))) print(cond) if (cond == True): if ((subject[0] in pset.arguments) and isinstance(subject[1], float)): penalty = penalty + 10 penalty = penalty + 10 return penalty presgt = 'gt' not in evals.values() presls = 'ls' not in evals.values() pres = presgt and presls presgt = 'gt1' not in evals.values() presls = 'ls1' not in evals.values() pres1 = presgt and presls print(pres) peninc = -100 if pres else 10 peninc1 = -100 if pres1 else 10 penaltyfin = pencalc(keys) + pencalc(keys1) + pencalc(keys2) + pencalc( keys3) + peninc + peninc1 - len(evals) if (treeorret): k['vals'] = k.drop(['targret', 'Symbol', 'ranchoice'], axis=1).apply(f1, axis=1) retfit = (k.vals * k.targret).sum() - (k.ranchoice * k.targret).sum() k = k.drop('vals', axis=1) return (penaltyfin, retfit) else: return (penaltyfin, )
def print_population(pop, hof): expr = 0 for data in [pop, hof]: for ind in data: # print(str(ind)) expr = ind # plotting tree graph nodes, edges, labels = gp.graph(expr)
def printIndTree(ind, label): nodes, edges, labels = gp.graph(ind) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw("%s.pdf" % (label, ))
def printIndTree(ind, label): nodes, edges, labels = gp.graph(ind) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw("%s.pdf" % (label,))
def plot(individual): nodes, edges, labels = gp.graph(individual) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = nx.drawing.nx_agraph.graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show()
def main(): random.seed() NGEN = 40 MU = 300 LAMBDA = 200 CXPB = 0.5 MUTPB = 0.4 pop = toolbox.population(n=MU) hof = tools.HallOfFame(1) stats_fit = tools.Statistics(lambda ind: ind.fitness.values) stats_size = tools.Statistics(len) mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size) mstats.register("avg", numpy.mean) mstats.register("std", numpy.std) mstats.register("min", numpy.min) mstats.register("max", numpy.max) #pop, log = algorithms.eaSimple(pop, toolbox, CXPB, MUTPB, NGEN, stats=mstats, halloffame=hof, verbose=True) pop, log = algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats=mstats, halloffame=hof) #print graph print(str(hof[0])) print(hof[0].height) print(hof[0].fitness.values) ##### #plot graph expr = hof[0] nodes, edges, labels = gp.graph(expr) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = nx.nx_pydot.graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show() ##### # print log return pop, log, hof
def evalSymbReg(individual): # Transform the tree expression in a callable function func = toolbox.compile(expr=individual) # Evaluate the sum of squared difference between the expression # and the real function values : x**4 + x**3 + x**2 + x diff = numpy.sum((func(samples_x, samples_z) - values)**2) nodes, edges, labels = gp.graph(individual) if len(nodes) > 16: # 18 diff += 100000000000000.0 return diff,
def individual_pdf(file_path, individual, pset): nodes, edges, labels = gp.graph(individual) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog='dot') for i in nodes: n = g.get_node(i) n.attr['label'] = labels[i] g.draw(file_path + '.pdf') return
def main(): pop, log, hof = gp_algo() ind = hof.items[0] print(ind) # expr = toolbox.individual() nodes, edges, labels = gp.graph(ind) dot = create_dot(edges, labels) plot_tree(dot=dot) validate(ind) test(ind)
def printTree(individual): nodes, edges, labels = gp.graph(individual) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = nx.graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show()
def visualize_tree(expression, filename): import pygraphviz as pgv nodes, edges, labels = gp.graph(expression) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw(f"{filename}.png", "png")
def create_needfield(individual): #输出因子文件中的needfield dic = gp.graph(individual)[2] needfield = "[" for key in dic.keys(): if "needData" in dic[key]: needfield += dic[key][9:-1] + "," needfield = needfield[0:-1] + "]" for x in locals().keys(): #清除局部变量 del locals()[x] gc.collect() return needfield
def graph(best_individual, filename): ind = creator.Individual.from_string(best_individual, pset) # Generate and Store graph nodes, edges, labels = gp.graph(ind) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw("../gp_trees/best/" + filename + ".png")
def draw(individual, path): nodes, edges, labels = gp.graph(individual) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw(path)
def draw_tree(tree): nodes, edges, labels = gp.graph(tree) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = nx.nx_pydot.graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show()
def show_results(self, gtitle): ''' Display the results of executing the DEAP GP object; best individual is displayed. Params: gtitle - string of the graph title. Returns: N/A ''' if self.rlist: # Show the squence required result = ", ".join(map(str, self.slist)) print("\nRequired sequence: {}".format(result)) # Show the resultant integer sequence result = ", ".join(map(str, self.rlist)) print("\nCalculated result: {}".format(result)) # Let the user know how it went. print("-" * 80) if self.rlist == self.slist: print("\nSuccessfully calculated the Integer Sequence.") else: print("\nUnsuccessfull in calculating the Integer Sequence.") # Display the individual print('\nBest individual : ', self.expr) # Display the resultant equation from the best individual tree = gp.PrimitiveTree(self.expr) str(tree) # Display the best individual => graph and equation. # Only works reliably on Linux Ubuntu. if sys.platform == 'linux' or sys.platform == 'linux2': print("Running on Linux OS.") nodes, edges, labels = gp.graph(self.expr) # Create tree diagram g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) # Remove the filename extension gtitle = os.path.splitext(gtitle)[0] plt.title(gtitle, None, 'center', None) plt.savefig(gtitle + '.png') # plt.show() else: print("Graphical output only available on Linux.") else: print("\nError: hof variable is emtpy.")
def draw_graph(expr): plt.figure(figsize=(14, 4)) nodes, edges, labels = gp.graph(expr) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.savefig('output/graph.png')
def plotEquationStructure(individual, output_name): nodes, edges, labels = gp.graph(individual) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw(output_name)
def getGrapf(): expr = toolbox.individual() nodes, edges, labels = gp.graph(expr) g = nx.DiGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = pgv(g, prog="dot") nx.draw(g, pos) nx.draw(g, pos) nx.draw(g, pos, labels) plt.show()
def plotExpressionTree(toolbox, individual): nodes, edges, labels = gp.graph(individual) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show()
def view_results(self, results): lab_dict = { 'cat_prim': '+', 'q_constr': '?', 'plus_constr': '+', 'or_prim': '|', 'star_constr': 'x', 'looka_constr': '(?=)', 'lookb_constr': '(?<=)', 'nch_constr': '[^ ]', 'ch_constr': '[ ]', 'term_constr': 't()' } hof, log = results avgs = [l['avg'] for l in log] plt.plot(avgs) plt.xlabel('Generations') plt.ylabel('Fitness Averages') plt.title('Capture Fitness Avgs over Generations (0 is perfect)') plt.show() expr = hof.__dict__['items'][0] tree = gp.PrimitiveTree(expr) str(tree) + ' = ' + gp.compile(tree, pset=self.pset).s # for i in range(3): nodes, edges, labels = gp.graph(expr) print(labels.values()) new_labs = {} for i, lab in enumerate(labels.values()): if self.has_digit(lab): new_labs[i] = self.pset.context[lab].s if new_labs[i] == ' ': new_labs[i] = '\' \'' else: new_labs[i] = lab_dict[lab] print(labels) print(new_labs) graph = networkx.Graph() graph.add_nodes_from(nodes) graph.add_edges_from(edges) pos = graphviz_layout(graph, prog="dot") plt.figure(figsize=(10, 10)) networkx.draw_networkx_nodes(graph, pos, node_size=400, node_color='w') networkx.draw_networkx_edges(graph, pos, edge_color="blue") networkx.draw_networkx_labels(graph, pos, new_labs) plt.axis("off") plt.show()
def graphIndividual(self,expr,fname): """ creates a file that contains the graph of an individual """ nodes, edges, labels = gp.graph(expr) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw(fname)
def write_graph(ind, filename): nodes, edges, labels = gp.graph(ind) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw(filename)
def drawTree(expr): nodes, edges, labels = gp.graph(expr) # Plot the tree g = pgv.AGraph(nodesep=1.0) g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw("tree.pdf")
def plot_tree(individual): import pygraphviz as pgv nodes, edges, labels = gp.graph(individual) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw("tree.png")
def plot_tree2(individual): import matplotlib.pyplot as plt import networkx as nx nodes, edges, labels = gp.graph(individual) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = nx.random_layout(g) nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show()
def display_individual_graph(expr): nodes, edges, labels = gp.graph(expr) g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) # On recupere la position des noeuds pos = nx.drawing.nx_agraph.graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show()
def plot_graph(expr): nodes, edges, labels = gp.graph(expr) import matplotlib.pyplot as plt import networkx as nx g = nx.Graph() g.add_nodes_from(nodes) g.add_edges_from(edges) pos = nx.drawing.nx_agraph.graphviz_layout(g, prog="dot") nx.draw_networkx_nodes(g, pos) nx.draw_networkx_edges(g, pos) nx.draw_networkx_labels(g, pos, labels) plt.show()
def main(): random.seed(318) pop = toolbox.population(n=300) hof = tools.HallOfFame(1) stats_fit = tools.Statistics(lambda ind: ind.fitness.values) stats_size = tools.Statistics(len) mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size) mstats.register("avg", numpy.mean) mstats.register("std", numpy.std) mstats.register("min", numpy.min) mstats.register("max", numpy.max) pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 40, stats=mstats, halloffame=hof, verbose=True) # print log best = tools.selBest(pop, 1)[0] return pop, log, hof, best, gp.graph(best)
def main(): p = 1 g = 1 c = 0.6 m = 0.2 pop = toolbox.population(n=p) hof = tools.HallOfFame(1) stats_fit = tools.Statistics(lambda ind: ind.fitness.values) stats_size = tools.Statistics(len) mstats = tools.MultiStatistics(fitness=stats_fit, size=stats_size) mstats.register("avg", numpy.mean) mstats.register("std", numpy.std) mstats.register("min", numpy.min) mstats.register("max", numpy.max) pop, log = algorithms.eaSimple(pop, toolbox, c, m, g, stats=mstats, halloffame=hof, verbose=True) f = toolbox.compile(expr=hof[0]) l = [f(i) for i in range(len(target))] print(target, l) f = evaluate(hof[0]) print(f) # print log #return pop, log, hof nodes, edges, labels = gp.graph(hof[0]) import pygraphviz as pgv g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for i in nodes: n = g.get_node(i) n.attr["label"] = labels[i] g.draw("tree.pdf")
toolbox.register("compile", gp.compile, pset=pset) def evalCircuit(individual): func = toolbox.compile(expr=individual) fitness = sum(func(*in_) == out for in_, out in zip(inputs, outputs)), height = individual.height length = len(individual) if fitness[0] == 2**NO_OF_INPUTS and height != 0: fitness = list(fitness) fitness[0] += float(1/height) fitness = tuple(fitness) return fitness toolbox.register("evaluate", evalCircuit) toolbox.register("select", tools.selDoubleTournament, fitness_size=300, parsimony_size=2, fitness_first=True) toolbox.register("mate", gp.cxOnePoint) toolbox.register("expr_mut", gp.genGrow, min_=0, max_=5) toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset) ind = creator.Individual(gp.PrimitiveTree.from_string("m(1,m(B,A,0),i(m((A,B,1)))",pset)) nodes,edges,labels = gp.graph(ind) g = pgv.AGraph() g.add_nodes_from(nodes) g.add_edges_from(edges) g.layout(prog="dot") for j in nodes: n = g.get_node(j) n.attr["label"] = labels[j] name = "ind.fig" g.draw(name)