def getCost(WG): global L, posis graphCost = Graph() graphCost.DG = WG.copy() graphCost.WG = WG.copy() #need positive weigh values to minimize weights = nx.get_edge_attributes(graphCost.WG, "weight") absweights = np.absolute(list(weights.values())) weights.update(zip(weights, absweights)) nx.set_edge_attributes(graphCost.WG, weights, "weight") #make undirected graph becouse directions dosnt matter and the adjancency matrix needs to be symetric ( Laplacian depends on adjancency) undirectedWG = graphCost.WG.to_undirected() L = nx.laplacian_matrix(undirectedWG) L = L.todense() position = nx.get_node_attributes(graphCost.WG, "pos") position = list(position.values()) posis = np.float_(np.array(position)) #starting condition for optimizer pos0 = posis[:, 0] ## we only optimze x-value pos0 = pos0[np.newaxis, :] #set conditions con1 = {'type': 'eq', 'fun': constraint1} con2 = {'type': 'eq', 'fun': constraint2} con3 = {'type': 'ineq', 'fun': constraint3} cons = [con1, con2] #only first two for first optimization, following the paper #run first optimisation sol = minimize(objective, pos0, method='SLSQP', constraints=cons) #set new position posis[:, 0] = sol.x posis = posis.round(2) graphCost.setNodeAttribute(posis, "pos") pos = nx.get_node_attributes(graphCost.WG, "pos") #graphCost.printGraph() #find nodes at identical pos unique, index, count = np.unique(posis, axis=0, return_index=True, return_counts=True) # add 0.3 to x-coord if at same pos, as long as nodes at same pos while (True): for idx, i in enumerate(count): if i > 1: need = posis[index[idx]] rowIdx = np.argwhere( (posis[:, 0] == need[0]) * (posis[:, 1] == need[1])) posis[rowIdx[0], 0] += 0.3 unique, index, count = np.unique(posis, axis=0, return_index=True, return_counts=True) if all(count == 1): break # 2nd optimisation to get optimzied values after manuel changes in c-coord cons = [con1, con2, con3] pos0 = posis[:, 0] pos0 = pos0[np.newaxis, :] sol = minimize(objective, pos0, method='SLSQP', constraints=cons) posis[:, 0] = sol.x posis = posis.round(2) graphCost.setNodeAttribute(posis, "pos") pos = nx.get_node_attributes(graphCost.WG, "pos") #graphCost.printGraph() return sol.fun / MAXCC
def main(): global nn, data, result, ME dataPaths = ["/home/ed/Dokumente/Codes/Classification_Network_Deap/data2.txt"] titles = ["Neural Network Classification"] data, result = loadData(dataPaths[0]) #build Graphs DG = nx.DiGraph() #setlayers DG.add_nodes_from([1, 2, 3, 4], ActFunc = activations.noFunc, active = True, pos = (0,0),bias=0) DG.add_nodes_from([5, 6, 7, 8], ActFunc = activations.modSigmoid, active = True, pos = (0,0),bias=1) DG.add_nodes_from([9, 10, 11,12], ActFunc = activations.modSign, active = True, pos = (0,0),bias=1) #set edges DG.add_edges_from([(1, 5), (1, 6), (1, 7), (1, 8), (2, 5), (2, 6), (2, 7), (2, 8),(3, 5), (3, 6), (3, 7), (3, 8),(4, 5), (4, 6), (4, 7), (4, 8)], weight = 10, active = False) DG.add_edges_from([(5, 9), (5, 10), (5, 11), (5, 12),(6, 9), (6, 10), (6, 11), (6, 12),(7, 9), (7, 10), (7, 11), (7, 12),(8, 9), (8, 10), (8, 11), (8, 12)], weight = 10, active = False) pos= [None] *12 nn = Graph() nn.DG = DG nn.WG = DG.copy() c=0 # set positions of neurons for i in range(NUMLAYER): for j in range(NUMNEURONS): pos[c]= np.array([j,i]) c+=1 nn.setNodeAttribute(pos,"pos") #set active nodes, especially remove not needed In- and Outputneurons activeNodes = nx.get_node_attributes(nn.WG, "active") setactivieNodes = [True, False, False, True, True, True, True, True,False, True, False, False] activeNodes.update(zip(activeNodes,setactivieNodes)) nx.set_node_attributes(nn.WG, activeNodes, "active") nx.set_node_attributes(nn.DG, activeNodes, "active") nn.removeNodes() nn.removeParentNodes() #get biases for prediction biases = nx.get_node_attributes(nn.WG,"bias") nn.biases = list(biases.values()) #size = np.array([10,10]) size = np.array([50,50]) ME = MapElite(size) #initialzePopulation(200) #run(15000) #Analyze ''' ME.readFile(1) nn.WG = ME.getBest(1) #nn.printGraph() ME.plotGrid() modularity = nn.getCommunitiesModularity() ccost = getCost(nn.WG) print("Fit :",evaluate2(nn.predict(data)), " Mod: ", modularity," ccost: ", ccost) print(nx.get_edge_attributes(nn.WG,"weight")) print(nx.get_node_attributes(nn.WG,"bias")) plotNN(nn, nn.WG) plotResults(data, result, nn.predict(data)) plt.show() nn.printGraph() ''' '''