Actions

Multiplex Network Pseudocode: Difference between revisions

From Santa Fe Institute Events Wiki

 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Running The Simulation =
<tt>
#Set parameters
N=100
UN = 10
L=200
B=2
T=1000
# Initialize Network
G=nx.DiGraph()
G=initNodes(G,N)
G=initEdge(G,N,L)
# run the game
[G, S] = AsyncFermiUpdateNet(G,N,UN,B,T)
</tt>
= Generating Network =
= Generating Network =
== Node Creation ==
== Node Creation ==
=== Basic Node ===
This code takes graph G and populates it with N nodes with generic state and payoff.
<tt>
def initNodes(G,N):
    for n in range(N):
        G.add_node(n,state=rng.randrange(2),payoff=0)   
    return G
</tt>


== Edge Creation ==
== Edge Creation ==
=== Basic Edge With Complex Attributes ===
=== Basic Edge With Complex Attributes ===
This code take digraph object G with N nodes and L links and generate links with 2x2 matrix representing the payoffs.
<tt>  
<tt>  
def initEdge(G,N,L):
def initEdge(G,N,L):
     for l in range(L):
     for l in range(L):
Line 10: Line 41:
         j=rng.randrange(N)
         j=rng.randrange(N)
         payoff = np.matrix([[rng.random() for e in range(2)] for e in range(2)])
         payoff = np.matrix([[rng.random() for e in range(2)] for e in range(2)])
        #payoff = payoff/payoff.sum().sum()
         G.add_edge(i,j,w=payoff)
         G.add_edge(i,j,w=payoff)
         G.add_edge(j,i,w=payoff.transpose())
         G.add_edge(j,i,w=payoff.transpose())
Line 19: Line 49:
== Network Update ==
== Network Update ==
=== Asynchronous Update ===
=== Asynchronous Update ===
<tt>
def AsyncFermiUpdateNet(G,N,UN,B,T):
    S =[getNodeStates(G,N)]
 
   
    for t in range(T):
        idx = rng.sample(range(N),UN)
        for i in idx:
            G=AsyncFermiUpdateNode(G,i,B)
            S.extend([getNodeStates(G,N)])
    return [G,S]
</tt>


== Node Update ==
== Node Update ==
=== Fermi Rule ===
=== Fermi Rule ===
<tt>
def AsyncFermiUpdateNode(G,i,B):
    if G.degree(i) == 0:
        return G
   
    others = G.neighbors(i)
    otherspay = []
    mypay = []
    for j in others:
        mypay=G.edge[i][j]['w'][G.node[i]['state'],G.node[j]['state']]
        otherpay = G.edge[j][i]['w'][G.node[i]['state'],G.node[j]['state']]
        dpay = mypay - otherpay
       
        prob = 1/(1+math.exp(B*dpay))
        #print(prob)
        if rng.random() < prob:
            G.node[i]['state']=G.node[j]['state']
            return G
    return G
</tt>

Latest revision as of 19:36, 18 June 2014

Running The Simulation

  1. Set parameters

N=100 UN = 10 L=200 B=2 T=1000

  1. Initialize Network

G=nx.DiGraph() G=initNodes(G,N) G=initEdge(G,N,L)

  1. run the game

[G, S] = AsyncFermiUpdateNet(G,N,UN,B,T)

Generating Network

Node Creation

Basic Node

This code takes graph G and populates it with N nodes with generic state and payoff. def initNodes(G,N):

   for n in range(N):
       G.add_node(n,state=rng.randrange(2),payoff=0)    
   return G

Edge Creation

Basic Edge With Complex Attributes

This code take digraph object G with N nodes and L links and generate links with 2x2 matrix representing the payoffs.

def initEdge(G,N,L):

   for l in range(L):
       i=rng.randrange(N)
       j=rng.randrange(N)
       payoff = np.matrix([[rng.random() for e in range(2)] for e in range(2)])
       G.add_edge(i,j,w=payoff)
       G.add_edge(j,i,w=payoff.transpose())
   return G

Process on Network

Network Update

Asynchronous Update

def AsyncFermiUpdateNet(G,N,UN,B,T):

   S =[getNodeStates(G,N)]
  
   
   for t in range(T):
       idx = rng.sample(range(N),UN)
       for i in idx:
           G=AsyncFermiUpdateNode(G,i,B)
           S.extend([getNodeStates(G,N)])
   return [G,S]

Node Update

Fermi Rule

def AsyncFermiUpdateNode(G,i,B):

   if G.degree(i) == 0:
       return G
   
   others = G.neighbors(i)
   otherspay = []
   mypay = []
   for j in others:
       mypay=G.edge[i][j]['w'][G.node[i]['state'],G.node[j]['state']]
       otherpay = G.edge[j][i]['w'][G.node[i]['state'],G.node[j]['state']]
       dpay = mypay - otherpay
       
       prob = 1/(1+math.exp(B*dpay))
       #print(prob)
       if rng.random() < prob:
           G.node[i]['state']=G.node[j]['state']
           return G
   return G