使用这个简单的模型,你可以说三个人的三位一体是“平衡的”或“不平衡的”,这取决于三合一边缘的产品是正面的还是负面的.
所以最后我要做的是实现一个ising类型模型.即如果新网络具有比翻转之前的网络更平衡的三边形(更低的能量),则随机边缘被翻转并保持新的关系,如果不是这种情况那么新关系仅以一定的概率保持.
好的,最后我的问题:我编写了以下代码,但是我的数据集包含~120k三元组,因此需要4天才能运行!
任何人都可以提供有关如何优化代码的任何提示吗?
谢谢,
理查德.
#importing required librarystry: import matplotlib.pyplot as pltexcept: raiseimport networkx as nximport csvimport randomimport mathdef prod(iterable): p= 1 for n in iterable: p *= n return pdef Sum(iterable): p= 0 for n in iterable: p += n[3] return pdef 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).
总结以上是内存溢出为你收集整理的python – 帮助优化我的社交网络演化模型全部内容,希望文章能够帮你解决python – 帮助优化我的社交网络演化模型所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)