-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
94 lines (79 loc) · 4.01 KB
/
display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3
from market import *
import os
import copy
import subprocess
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
G=nx.MultiDiGraph()
wrld = World(g=G)
zList = wrld >> ("Alice", "Bob", "Carol", "Dylan") # returns list of zygotes
miZList = list(map(lambda obj: iNode.iZygote(obj,wrld), zList))
nList = wrld << ("USA", "China", "Russia", "Ukraine") # returns list of nations
usa = nList[0]
subGov = usa >> ("NY","CA","TX")
subGov[0] << miZList # make iZygotes citizen of NY
wrld.geometrize()
institution = Institution(govList=nList,nm="WorldBank")
nyInstitution = Institution(govList=subGov, nm="PATH")
UseValue.resetUV(uvList= { 0 : "Fear", 1 : "Power",
2 : "Friendship", 3 : "Loyalty", 4 : "Love",
5 : "$$$", 6 : "Labor", 7 : "Food", 8 : "Housing" ,
9 : "Genes", 10: "Land"})
### We illustrate a couple of derived UseValues.
#laborer = UseValue(name="Laborer", uVector=[("Food" , -1) , ("Housing",-1), ("Labor", 1)])
#farm = UseValue(name="Farm", uVector=[("Labor", -1), ("Food", 1),("Land", 1),("$$$", -.1)])
### The model (World) uses
### (1) the laborer UseValue to initialize the possessions of each iZygote and
### (2) the farm UseValue to illustrate an enterprise which transforms labor and $$$s
worker = cNode(name="Worker", owner=wrld,
uvList=[(UseValue.UV("Food"), -1) , (UseValue.UV("Housing"), -1), (UseValue.UV("Labor"), 3)],
factory=True, saleable=False)
farmer = cNode(name="Farm", owner=wrld,
uvList=[(UseValue.UV("Labor"), -1) , (UseValue.UV("Land"), -1) , (UseValue.UV("$$$"), -1)],
factory=True, saleable=True)
contractor = cNode(name="Shelter", owner=wrld,
uvList=[(UseValue.UV("Labor"), -1) , (UseValue.UV("Land"), -1) , (UseValue.UV("$$$"), -1)],
factory=True, saleable=True)
wrld.transfer(nd=worker, newOwner=miZList[3])
wrld.transfer(nd=farmer, newOwner=miZList[0])
wrld.transfer(nd=contractor, newOwner=miZList[1])
moneySupply = cNode(name="u$a", owner=usa, uvList=[(UseValue.UV("$$$"),1)],
factory=False, saleable=False)
usa_mkt = Market(money=moneySupply, govList=[usa])
workerNeeded = Offer(who=miZList[0], itemList=UseValue.UV("Labor"), transWhere="*",
transWhen="7", offer=False, price=2, until="@14*13")
usa_mkt.submit(offer=workerNeeded)
tStrip = Offer(who=subGov[0], itemList=UseValue.UV("$$$"), transWhere="*", offer=True, price="1", until="@10*10")
tBill = Offer(who=subGov[0], itemList=UseValue.UV("$$$"), transWhere="*", offer=True, price="1", until="@10")
# create display
nodeColor = []
for node in G.nodes():
cls = Node.nodes[node].__class__
G.nodes[node] ["color"] = Node.nodeColors[cls]
G.nodes[node] ["style"] = Node.nodeStyle[cls]
G.nodes[node] ["penwidth"] = 3.0
nodeColor.append(Node.nodeColors[cls])
edgeColor = []
for edge in G.edges():
edgeData = G.get_edge_data(edge[0],edge[1])
for data in edgeData.items():# key is first element of data
edgeColor.append(Edge.edgeColors[data[0]])
G.edges[edge[0],edge[1], data[0]]["color"] = Edge.edgeColors[data[0]]
G.edges[edge[0],edge[1], data[0]]["style"] = Edge.edgeStyles[data[0]]
G.edges[edge[0],edge[1], data[0]]["penwidth"] = 3.0
# Need to create a layout when doing separate calls to draw nodes and edges
#pos=nx.shell_layout(G)
pos=nx.kamada_kawai_layout(G)
pos=nx.spiral_layout(G)
nx.draw_networkx_nodes(G, pos, cmap=plt.get_cmap('jet'), linewidths=2.0, node_shape='o',
node_color = nodeColor, node_size=3000)
nx.draw_networkx_labels(G, pos, font_size=24)
nx.draw_networkx_edges(G, pos, edge_color=edgeColor, width=3, style="dotted",
arrows=True, arrowsize=30, node_size=3000)
nx.nx_agraph.write_dot(G,"multi.dot")
#dot -T png multi.dot > multi.png or #cat multi.dot | dot -T png > multi.dot.png
os.system("cat multi.dot | dot -T png > multi.dot.png")
subprocess.run(["firefox", "multi.dot.png"])
#plt.show()