Skip to content

Commit 691de61

Browse files
committed
update
1 parent cbf4180 commit 691de61

File tree

3 files changed

+322
-0
lines changed

3 files changed

+322
-0
lines changed

16-mcm-b/NetFlow.ipynb

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from ShortPath import Floyd,inf\n",
10+
"import numpy as np \n",
11+
"import pandas as pd \n",
12+
"import networkx as nx \n",
13+
"from matplotlib import pyplot as plt "
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 2,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"def netflow(OD, T0, C, n=100, alpha=1, beta=.5):\n",
23+
" m = len(T0)\n",
24+
"\n",
25+
" # 初始化\n",
26+
" T = np.array(T0)\n",
27+
" Q = np.zeros_like(T0)\n",
28+
"\n",
29+
" def updateQ(COD, P):\n",
30+
" P = np.array(P)\n",
31+
" for i in range(m):\n",
32+
" for j in range(m):\n",
33+
" path = P[i, j]\n",
34+
" for k in range(len(path)-1):\n",
35+
" s, t = path[k], path[k+1]\n",
36+
" Q[s, t] += COD[i, j]\n",
37+
"\n",
38+
" def updateT():\n",
39+
" return T0*(1+alpha*((Q/C)**beta))\n",
40+
"\n",
41+
" for i in range(n):\n",
42+
" _, P = Floyd(T)\n",
43+
" updateQ(OD/n, P)\n",
44+
" T = updateT()\n",
45+
"\n",
46+
" return Q, T, T/T0 - 1"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 3,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"def evalue(Q, T, J):\n",
56+
" RT = T[(T != inf)]\n",
57+
" QT = (Q*T).flatten()\n",
58+
" QT = QT[~np.isnan(QT)]\n",
59+
" J = J.flatten()\n",
60+
" J = J[~np.isnan(J)]\n",
61+
" return {\n",
62+
" '平均道路通行时间': np.mean(RT),\n",
63+
" '加权路网通行时耗': np.sum(QT),\n",
64+
" '路网拥堵指数': np.mean(J)\n",
65+
" }"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 4,
71+
"metadata": {
72+
"tags": []
73+
},
74+
"outputs": [],
75+
"source": [
76+
"def run():\n",
77+
" Q, T, J = netflow(OD, T0, C)\n",
78+
" print('Q=', Q)\n",
79+
" print('J=', J)\n",
80+
" eval0 = evalue(Q, T, J)\n",
81+
" print('evalue=', eval0)\n",
82+
" return Q, T, J"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 5,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"def draw_graph(cmap=plt.cm.RdYlGn):\n",
92+
" g = nx.Graph()\n",
93+
" colors = []\n",
94+
" for i in range(m):\n",
95+
" for j in range(m):\n",
96+
" if T0[i,j] < inf:\n",
97+
" g.add_edge(i,j,weight=T0[i,j])\n",
98+
" colors.append(-np.log(J[i,j]))\n",
99+
" options = {\n",
100+
" \"node_color\": \"#A0CBE2\",\n",
101+
" \"edge_color\": colors,\n",
102+
" \"width\": 4,\n",
103+
" \"edge_cmap\": cmap,\n",
104+
" \"with_labels\": True\n",
105+
" }\n",
106+
" nx.draw_kamada_kawai(g,**options)"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 12,
112+
"metadata": {
113+
"tags": []
114+
},
115+
"outputs": [
116+
{
117+
"output_type": "stream",
118+
"name": "stdout",
119+
"text": "增加道路1-4之前:\nQ= [[ 0. 510. 690. 0.]\n [ 0. 0. 0. 310.]\n [ 0. 0. 0. 890.]\n [ 0. 0. 0. 0.]]\nJ= [[ nan 1.00995049 2.62678511 nan]\n [ nan nan nan 0.78740079]\n [ nan nan nan 2.98328678]\n [ nan nan nan nan]]\nevalue= {'平均道路通行时间': 3.801193611955694, '加权路网通行时耗': 9205.944948398836, '路网拥堵指数': 1.8518557916463467}\n增加道路1-4之后:\nQ= [[ 0. 240. 300. 660.]\n [ 0. 0. 0. 40.]\n [ 0. 0. 0. 500.]\n [ 0. 0. 0. 0.]]\nJ= [[ nan 0.69282032 1.73205081 2.96647939]\n [ nan nan nan 0.28284271]\n [ nan nan nan 2.23606798]\n [ nan nan nan nan]]\nevalue= {'平均道路通行时间': 3.5738327896660804, '加权路网通行时耗': 7279.645003961635, '路网拥堵指数': 1.5820522430818205}\n"
120+
},
121+
{
122+
"output_type": "error",
123+
"ename": "AttributeError",
124+
"evalue": "module 'matplotlib.cm' has no attribute 'PiYlGn'",
125+
"traceback": [
126+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
127+
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
128+
"\u001b[1;32m<ipython-input-12-7b2be1b639b0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 31\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msubplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m122\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 32\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtitle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'after join the network of community:'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 33\u001b[1;33m \u001b[0mdraw_graph\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPiYlGn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
129+
"\u001b[1;31mAttributeError\u001b[0m: module 'matplotlib.cm' has no attribute 'PiYlGn'"
130+
]
131+
}
132+
],
133+
"source": [
134+
"T0 = np.array([\n",
135+
" [inf, 2, 1, inf],\n",
136+
" [inf, inf, inf, 2],\n",
137+
" [inf, inf, inf, 1],\n",
138+
" [inf, inf, inf, inf]\n",
139+
" ])\n",
140+
"OD = np.array([\n",
141+
" [0, 200, 0, 1000],\n",
142+
" [0, 0, 0, 0],\n",
143+
" [0, 0, 0, 200],\n",
144+
" [0, 0, 0, 0]\n",
145+
" ])\n",
146+
"C = np.array([\n",
147+
" [1, 500, 100, 1],\n",
148+
" [1, 1, 1, 500],\n",
149+
" [1, 1, 1, 100],\n",
150+
" [1, 1, 1, 1]\n",
151+
" ])\n",
152+
"m = len(T0)\n",
153+
"\n",
154+
"print('增加道路1-4之前:')\n",
155+
"Q, T, J = run()\n",
156+
"plt.subplot(121)\n",
157+
"plt.title('before join the network of community:')\n",
158+
"draw_graph()\n",
159+
"\n",
160+
"print('增加道路1-4之后:')\n",
161+
"T0[0, 3] = 1.5\n",
162+
"C[0, 3] = 75\n",
163+
"Q, T, J = run()\n",
164+
"plt.subplot(122)\n",
165+
"plt.title('after join the network of community:')\n",
166+
"draw_graph(plt.cm.YlGn)\n"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": null,
172+
"metadata": {},
173+
"outputs": [],
174+
"source": []
175+
}
176+
],
177+
"metadata": {
178+
"language_info": {
179+
"codemirror_mode": {
180+
"name": "ipython",
181+
"version": 3
182+
},
183+
"file_extension": ".py",
184+
"mimetype": "text/x-python",
185+
"name": "python",
186+
"nbconvert_exporter": "python",
187+
"pygments_lexer": "ipython3",
188+
"version": "3.6.8-final"
189+
},
190+
"orig_nbformat": 2,
191+
"kernelspec": {
192+
"name": "python36864bit3f53e4684f024fc68f038ddcedf7a612",
193+
"display_name": "Python 3.6.8 64-bit"
194+
}
195+
},
196+
"nbformat": 4,
197+
"nbformat_minor": 2
198+
}

16-mcm-b/NetFlow.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def evalue(Q, T, J):
6565
'路网拥堵指数': np.mean(np.log(J))
6666
}
6767

68+
69+
6870
print('增加道路1-4之前:')
6971
Q, T, J = netflow(OD, T0, C)
7072
print('Q=', Q)

16-mcm-b/NetFlow2.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# To add a new cell, type '# %%'
2+
# To add a new markdown cell, type '# %% [markdown]'
3+
# %%
4+
from ShortPath import Floyd,inf
5+
import numpy as np
6+
import pandas as pd
7+
import networkx as nx
8+
from matplotlib import pyplot as plt
9+
import matplotlib as mpl
10+
11+
12+
# %%
13+
def netflow(OD, T0, C, n=100, alpha=1, beta=.5):
14+
m = len(T0)
15+
16+
# 初始化
17+
T = np.array(T0)
18+
Q = np.zeros_like(T0)
19+
20+
def updateQ(COD, P):
21+
P = np.array(P)
22+
for i in range(m):
23+
for j in range(m):
24+
path = P[i, j]
25+
for k in range(len(path)-1):
26+
s, t = path[k], path[k+1]
27+
Q[s, t] += COD[i, j]
28+
29+
def updateT():
30+
return T0*(1+alpha*((Q/C)**beta))
31+
32+
for i in range(n):
33+
_, P = Floyd(T)
34+
updateQ(OD/n, P)
35+
T = updateT()
36+
37+
return Q, T, T/T0 - 1
38+
39+
40+
# %%
41+
def evalue(Q, T, J):
42+
RT = T[(T != inf)]
43+
QT = (Q*T).flatten()
44+
QT = QT[~np.isnan(QT)]
45+
J = J.flatten()
46+
J = J[~np.isnan(J)]
47+
return {
48+
'平均道路通行时间': np.mean(RT),
49+
'加权路网通行时耗': np.sum(QT),
50+
'路网拥堵指数': np.mean(J)
51+
}
52+
53+
54+
# %%
55+
def run():
56+
Q, T, J = netflow(OD, T0, C)
57+
print('Q=', Q)
58+
print('J=', J)
59+
eval0 = evalue(Q, T, J)
60+
print('evalue=', eval0)
61+
return Q, T, J
62+
63+
64+
# %%
65+
def draw_graph(cmap=plt.cm.RdYlGn):
66+
g = nx.Graph()
67+
colors = []
68+
for i in range(m):
69+
for j in range(m):
70+
if T0[i,j] < inf:
71+
g.add_edge(i,j,weight=T0[i,j])
72+
colors.append(-np.log(J[i,j]))
73+
options = {
74+
"node_color": "#A0CBE2",
75+
"edge_color": colors,
76+
"width": 4,
77+
"edge_cmap": cmap,
78+
"with_labels": True
79+
}
80+
nx.draw_kamada_kawai(g,**options)
81+
82+
# %%
83+
T0 = np.array([
84+
[inf, 2, 1, inf],
85+
[inf, inf, inf, 2],
86+
[inf, inf, inf, 1],
87+
[inf, inf, inf, inf]
88+
])
89+
OD = np.array([
90+
[0, 200, 0, 1000],
91+
[0, 0, 0, 0],
92+
[0, 0, 0, 200],
93+
[0, 0, 0, 0]
94+
])
95+
C = np.array([
96+
[1, 500, 100, 1],
97+
[1, 1, 1, 500],
98+
[1, 1, 1, 100],
99+
[1, 1, 1, 1]
100+
])
101+
m = len(T0)
102+
103+
print('增加道路1-4之前:')
104+
Q, T, J = run()
105+
plt.subplot(121)
106+
plt.title('before join the network of community:')
107+
draw_graph()
108+
109+
print('增加道路1-4之后:')
110+
T0[0, 3] = 1.5
111+
C[0, 3] = 75
112+
Q, T, J = run()
113+
plt.subplot(122)
114+
plt.title('after join the network of community:')
115+
draw_graph(plt.cm.YlGn)
116+
117+
plt.show()
118+
119+
120+
# %%
121+
122+

0 commit comments

Comments
 (0)