python – 帮助优化我的社交网络演化模型
发布时间:2020-12-20 12:18:35 所属栏目:Python 来源:网络整理
导读:我正在编写一段代码来模拟社交网络的演变.这个想法是每个人被分配到一个节点,并且人与人之间的关系(网络上的边缘)被赋予权重1或-1,这取决于该关系是友好的还是不友好的. 使用这个简单的模型,你可以说三个人的三位一体是“平衡的”或“不平衡的”,这取决于三
我正在编写一段代码来模拟社交网络的演变.这个想法是每个人被分配到一个节点,并且人与人之间的关系(网络上的边缘)被赋予权重1或-1,这取决于该关系是友好的还是不友好的.
使用这个简单的模型,你可以说三个人的三位一体是“平衡的”或“不平衡的”,这取决于三合一边缘的产品是正面的还是负面的. 所以最后我要做的是实现一个ising类型模型.即如果新网络具有比翻转之前的网络更平衡的三边形(更低的能量),则随机边缘被翻转并保持新的关系,如果不是这种情况那么新关系仅以一定的概率保持. 好的,最后我的问题:我编写了以下代码,但是我的数据集包含~120k三元组,因此需要4天才能运行! 任何人都可以提供有关如何优化代码的任何提示吗? 谢谢, #Importing required librarys try: import matplotlib.pyplot as plt except: raise import networkx as nx import csv import random import math def prod(iterable): p= 1 for n in iterable: p *= n return p def Sum(iterable): p= 0 for n in iterable: p += n[3] return p def CalcTriads(n): firstgen=G.neighbors(n) Edges=[] Triads=[] for i in firstgen: Edges.append(G.edges(i)) for i in xrange(len(Edges)): for j in range(len(Edges[i])):# For node n go through the list of edges (j) for the neighboring nodes (i) if set([Edges[i][j][1]]).issubset(firstgen):# If the second node on the edge is also a neighbor of n (its in firstgen) then keep the edge. t=[n,Edges[i][j][0],Edges[i][j][1]] t.sort() Triads.append(t)# Add found nodes to Triads. new_Triads = []# Delete duplicate triads. for elem in Triads: if elem not in new_Triads: new_Triads.append(elem) Triads = new_Triads for i in xrange(len(Triads)):# Go through list of all Triads finding the weights of their edges using G[node1][node2]. Multiply the three weights and append value to each triad. a=G[Triads[i][0]][Triads[i][1]].values() b=G[Triads[i][1]][Triads[i][2]].values() c=G[Triads[i][2]][Triads[i][0]].values() Q=prod(a+b+c) Triads[i].append(Q) return Triads ###### Import sorted edge data ###### li=[] with open('Sorted Data.csv','rU') as f: reader = csv.reader(f) for row in reader: li.append([float(row[0]),float(row[1]),float(row[2])]) G=nx.Graph() G.add_weighted_edges_from(li) for i in xrange(800000): e = random.choice(li) # Choose random edge TriNei=[] a=CalcTriads(e[0]) # Find triads of first node in the chosen edge for i in xrange(0,len(a)): if set([e[1]]).issubset(a[i]): # Keep triads which contain the whole edge (i.e. both nodes on the edge) TriNei.append(a[i]) preH=-Sum(TriNei) # Save the "energy" of all the triads of which the edge is a member e[2]=-1*e[2]# Flip the weight of the random edge and create a new graph with the flipped edge G.clear() G.add_weighted_edges_from(li) TriNei=[] a=CalcTriads(e[0]) for i in xrange(0,len(a)): if set([e[1]]).issubset(a[i]): TriNei.append(a[i]) postH=-Sum(TriNei)# Calculate the post flip "energy". if postH<preH:# If the post flip energy is lower then the pre flip energy keep the change continue elif random.random() < 0.92: # If the post flip energy is higher then only keep the change with some small probability. (0.92 is an approximate placeholder for exp(-DeltaH)/exp(1) at the moment) e[2]=-1*e[2] 解决方法
以下建议不会提高您的性能,因为它们不在算法级别,即不是非常特定于您的问题.但是,它们是略微改进性能的通用建议:
除非您使用的是Python 3,否则请进行更改 for i in range(800000): 至 for i in xrange(800000): 后者只是迭代0到800000之间的数字,第一个创建一个巨大的数字列表,然后迭代该列表.使用范围为其他循环执行类似的操作. 另外,改变 j=random.choice(range(len(li))) e=li[j] # Choose random edge 至 e = random.choice(li) 并随后使用e代替li [j].如果您确实需要索引号,请使用random.randint(0,len(li)-1). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |