Python 具有双向定向边可能性的igraph barabasi模型(将现有节点连接到未来节点的生成中)

Python 具有双向定向边可能性的igraph barabasi模型(将现有节点连接到未来节点的生成中),python,igraph,directed-graph,Python,Igraph,Directed Graph,我需要创建一个网络,其中生成的图形可以将现有节点(正在生成的节点)链接到一个或多个未来节点(即将生成的节点,即node_ID_future>node_ID_current),而不仅仅链接到已经生成的节点 import igraph N = 50 degree_mean = 5 m = list(np.random.poisson(degree_mean,N)) g = Graph.Barabasi(N, m, outpref=True, directed=True, power=1, zero_

我需要创建一个网络,其中生成的图形可以将现有节点(正在生成的节点)链接到一个或多个未来节点(即将生成的节点,即node_ID_future>node_ID_current),而不仅仅链接到已经生成的节点

import igraph
N = 50
degree_mean = 5
m = list(np.random.poisson(degree_mean,N))
g = Graph.Barabasi(N, m, outpref=True, directed=True, power=1, zero_appeal=1, implementation="psumtree", start_from=None)
下面是我的原始代码/模型,但我无法让它运行,因为我的python.exe每次都崩溃。我试图得到一个类似Barabasi模型的模型,但并不局限于只从数量较大的节点到数量较低的节点(即节点2只能有到节点1或0{2->1或2->0}的边,节点20到低于20的节点,但我需要一个来自eg 2->19或4->35的定向节点)

N=20
in_k=3
out_k=in_k
indegree=np.random.poisson(单位:k,N)#每个节点的度数
outdegree=np.random.poisson(out_k,N)#每个节点的度数
#让我们为ID和col列创建一个存储阵列
列=3
形状=(N,列)
a=零(形状)
ID=列表(范围(0,N,1))
#将ID分配给列1,将度分配给列2
对于范围(0,len(ID))中的k:
a[k,0]=ID[k]
a[k,1]=超度数[k]
a[k,2]=indegree[k]
def return_顶点(deg1_索引、deg2_索引、未使用的_索引、必需的_out):#函数根据度和概率选择边
输出索引=零(必需输出)
pvals=[0.42,0.33,0.25]#加入到1deg、2deg或无度数节点的概率
迭代次数=整数(必需)
对于范围内的h(0,迭代次数):
所选类型=np.其中(np.随机.多项式(1,pvals))[0][0]#从概率中选择类型
如果(所选类型<1):
类型选项=长度(deg1指数)
和_胜者_=np.random.randint(类型_选项)#选择所选类型的随机顶点
out_索引[h]=(deg1_索引[np.random.randint(类型选项)])
如果(选择的类型<2)和(选择的类型>=1):
类型选项=长度(deg2指数)
而且,获胜者是=np.random.randint(类型选项)
out_索引[h]=(deg2_索引[np.random.randint(类型选项)])
如果(选择类型>1):
类型\u选项=len(未使用的\u索引)
而且,获胜者是=np.random.randint(类型选项)
out_索引[h]=(未使用的_索引[np.random.randint(类型选项)])
回归指数
#现在,我们需要建立一个具有进出度的邻接矩阵
#另外,如果存在边缘Eij,我想增加存在边缘Eji的可能性
#存储用空白矩阵
A=零([N,N])
对于范围(0,N)中的i:#使用行作为索引循环遍历顶点
#对于范围(0,N)内的j:
if(A[i].sum()=1):#如果顶点正在接收边,则基于度返回边的概率更高
边_to _add=a[i,1]-a[i].sum()
deg1_指数=非零(A[:,i])#查找一阶顶点
对于范围内的l(0,len(deg1_指数)):#查找2度顶点
deg2_指数=非零(A[:,deg1_指数[l])
#非度计数=N-len(deg1_指数)-len(deg2_指数)
打印(类型(deg1_索引[np.random.randint(1)])
#V_选项=数组([[zeros(len(deg1_索引))],[zeros(non_degree_count)])
使用的索引=数组([deg1\u索引+deg2\u索引])
使用的装配工=[无]*(N+1)
装配工ID=[None]*(N+1)
对于范围(0,len(使用的索引))内的x:
fitter_used[x]=used_索引[x]
对于范围(0,len(ID))中的y:
装配工ID[y]=ID[y]
未使用的\u索引=setxor1d(装配工ID,装配工使用)
#未使用的_指数=[2,5,9]
所选的顶点=返回顶点(deg1索引、deg2索引、未使用的索引、边添加)
对于范围内的m(0,len(所选的数)):
A[i,所选的[m]=1
#印刷品(A)
连接指数=np,其中(A)
边=zip(*连接索引)

我将在下周对此进行研究,因此希望我能在接下来的几天内发布我自己的答案。

到目前为止你都做了哪些尝试?请包括您为解决此问题而创建的任何代码。还包括当前输入、期望输出和当前输出。如果您定义自己的模型,您可能需要自己实现它。我现在明白了,我将不得不设计一个新模型,但我最初的尝试有很多问题,因为我仍在学习范围是什么。谢谢你的洞察力
N = 20
in_k = 3
out_k = in_k
indegree = np.random.poisson(in_k,N) #degrees for each node
outdegree = np.random.poisson(out_k,N) #degrees for each node
#lets create a storage array for the ID and col columns
columns = 3
shape = (N,columns)
a = zeros(shape)
ID = list(range(0, N, 1))

#Assigning the ID to col 1 and the degree to col 2

for k in range(0,len(ID)):
    a[k,0] = ID[k]
    a[k,1] = outdegree[k]
    a[k,2] = indegree[k]
def return_vertices(deg1_indices,deg2_indices,unused_indices,required_out): #function to choose edges based on degree and probability
    out_indices = zeros(required_out)
    pvals = [0.42,0.33,0.25] #probability of joining to 1deg, 2deg, or no degree node 
    iterations = int(required_out) 
    for h in range(0,iterations):
        chosen_type = np.where(np.random.multinomial(1,pvals))[0][0] #choose type from probabilities
        if (chosen_type < 1):
            type_options = len(deg1_indices)
            and_the_winner_is = np.random.randint(type_options) #choose random vertex of type selected
            out_indices[h] = (deg1_indices[np.random.randint(type_options)])

        if (chosen_type < 2) & (chosen_type >= 1):
            type_options = len(deg2_indices)
            and_the_winner_is = np.random.randint(type_options)
            out_indices[h] = (deg2_indices[np.random.randint(type_options)])

        if (chosen_type > 1):
            type_options = len(unused_indices)
            and_the_winner_is = np.random.randint(type_options)
            out_indices[h] = (unused_indices[np.random.randint(type_options)])

    return out_indices
#now we need to set up an adjacency matrix with the in and out degrees
#also i would like to add a higher probability of an edge Eji existing if there is an edge Eij

#Blank matrix for storage
A = zeros([N,N])

for i in range(0,N): #loop through vertices using rows as index
    #for j in range(0,N):
     if (A[i].sum() < a[i,1]): #if vertex needs outgoing edges

            if (A[:,i].sum() < 1): #if vertex is not receiving any edges, pick vertices at random
                edges_to_add = a[i,1] - A[i].sum()             
                Column_selected = np.random.randint(N,size=edges_to_add)
                A[i,Column_selected] = 1       

            if (A[:,i].sum() >= 1): #if vertex is receiving edges, higher probability of returning an edge based on degree
                edges_to_add = a[i,1] - A[i].sum()
                deg1_indices = nonzero(A[:,i]) #find 1st degree verts

                for l in range(0,len(deg1_indices)): #find 2nd degree verts
                    deg2_indices = nonzero(A[:,deg1_indices[l]])

                #non_degree_count = N - len(deg1_indices) - len(deg2_indices)
                print(type(deg1_indices[np.random.randint(1)]))
                #V_options = array([[zeros(len(deg1_indices))],[zeros(non_degree_count)]])
                used_indices = array([deg1_indices + deg2_indices])

                fitter_used = [None] * (N + 1)
                fitter_ID = [None] * (N + 1)
                for x in range(0, len(used_indices)):
                    fitter_used[x] = used_indices[x]
                for y in range(0, len(ID)):
                    fitter_ID[y] = ID[y]


                unused_indices = setxor1d(fitter_ID,fitter_used)
                #unused_indices = [2,5,9]

                the_chosen_ones = return_vertices(deg1_indices,deg2_indices,unused_indices,edges_to_add)
                for m in range(0,len(the_chosen_ones)):
                    A[i,the_chosen_ones[m]] = 1

#print(A)

conn_indices = np.where(A)
edges = zip(*conn_indices)