-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDemo_BloodBankAnimation.py
186 lines (145 loc) · 6.44 KB
/
Demo_BloodBankAnimation.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import time
import numpy as np
from VISolver.Domains.BloodBank import BloodBank, CreateNetworkExample
from VISolver.Solvers.AdamsBashforthEuler import ABEuler
from VISolver.Projection import BoxProjection
from VISolver.Solver import Solve
from VISolver.Options import (
DescentOptions, Miscellaneous, Reporting, Termination, Initialization)
from VISolver.Log import PrintSimResults, PrintSimStats
import matplotlib.animation as animation
import matplotlib.cm as cm
import matplotlib.pyplot as plt
def Demo():
#__BLOOD_BANK__##################################################
#############################################################
# Phase 1 - Left hospital has relatively low demand for blood
#############################################################
# Define Network and Domain
Network = CreateNetworkExample(ex=1)
Domain = BloodBank(Network=Network,alpha=2)
# Set Method
Method = ABEuler(Domain=Domain,P=BoxProjection(lo=0),Delta0=1e-5)
# Initialize Starting Point
Start = np.zeros(Domain.Dim)
# Calculate Initial Gap
gap_0 = Domain.gap_rplus(Start)
# Set Options
Init = Initialization(Step=-1e-10)
Term = Termination(MaxIter=10000,Tols=[(Domain.gap_rplus,1e-3*gap_0)])
Repo = Reporting(Requests=[Domain.gap_rplus, 'Step', 'F Evaluations',
'Projections','Data'])
Misc = Miscellaneous()
Options = DescentOptions(Init,Term,Repo,Misc)
# Print Stats
PrintSimStats(Domain,Method,Options)
# Start Solver
tic = time.time()
BloodBank_Results_Phase1 = Solve(Start,Method,Domain,Options)
toc = time.time() - tic
# Print Results
PrintSimResults(Options,BloodBank_Results_Phase1,Method,toc)
#########################################################
# Phase 2 - Left hospital has comparable demand for blood
#########################################################
# Define Network and Domain
Network = CreateNetworkExample(ex=2)
Domain = BloodBank(Network=Network,alpha=2)
# Set Method
Method = ABEuler(Domain=Domain,P=BoxProjection(lo=0),Delta0=1e-5)
# Initialize Starting Point
Start = BloodBank_Results_Phase1.PermStorage['Data'][-1]
# Calculate Initial Gap
gap_0 = Domain.gap_rplus(Start)
# Set Options
Init = Initialization(Step=-1e-10)
Term = Termination(MaxIter=10000,Tols=[(Domain.gap_rplus,1e-3*gap_0)])
Repo = Reporting(Requests=[Domain.gap_rplus, 'Step', 'F Evaluations',
'Projections','Data'])
Misc = Miscellaneous()
Options = DescentOptions(Init,Term,Repo,Misc)
# Print Stats
PrintSimStats(Domain,Method,Options)
# Start Solver
tic = time.time()
BloodBank_Results_Phase2 = Solve(Start,Method,Domain,Options)
toc = time.time() - tic
# Print Results
PrintSimResults(Options,BloodBank_Results_Phase2,Method,toc)
########################################################
# Phase 3 - Left hospital has excessive demand for blood
########################################################
# Define Network and Domain
Network = CreateNetworkExample(ex=3)
Domain = BloodBank(Network=Network,alpha=2)
# Set Method
Method = ABEuler(Domain=Domain,P=BoxProjection(lo=0),Delta0=1e-5)
# Initialize Starting Point
Start = BloodBank_Results_Phase2.PermStorage['Data'][-1]
# Calculate Initial Gap
gap_0 = Domain.gap_rplus(Start)
# Set Options
Init = Initialization(Step=-1e-10)
Term = Termination(MaxIter=10000,Tols=[(Domain.gap_rplus,1e-3*gap_0)])
Repo = Reporting(Requests=[Domain.gap_rplus, 'Step', 'F Evaluations',
'Projections','Data'])
Misc = Miscellaneous()
Options = DescentOptions(Init,Term,Repo,Misc)
# Print Stats
PrintSimStats(Domain,Method,Options)
# Start Solver
tic = time.time()
BloodBank_Results_Phase3 = Solve(Start,Method,Domain,Options)
toc = time.time() - tic
# Print Results
PrintSimResults(Options,BloodBank_Results_Phase3,Method,toc)
########################################################
# Animate Network
########################################################
# Construct MP4 Writer
fps = 15
FFMpegWriter = animation.writers['ffmpeg']
metadata = dict(title='BloodBank', artist='Matplotlib')
writer = FFMpegWriter(fps=fps, metadata=metadata)
# Collect Frames
frame_skip = 5
freeze = 5
Dyn_1 = BloodBank_Results_Phase1.PermStorage['Data']
Frz_1 = [Dyn_1[-1]]*fps*frame_skip*freeze
Dyn_2 = BloodBank_Results_Phase2.PermStorage['Data']
Frz_2 = [Dyn_2[-1]]*fps*frame_skip*freeze
Dyn_3 = BloodBank_Results_Phase3.PermStorage['Data']
Frz_3 = [Dyn_3[-1]]*fps*frame_skip*freeze
Frames = np.concatenate((Dyn_1,Frz_1,Dyn_2,Frz_2,Dyn_3,Frz_3),
axis=0)[::frame_skip]
# Normalize Colormap by Flow at each Network Level
Domain.FlowNormalizeColormap(Frames,cm.Reds)
# Mark Annotations
t1 = 0
t2 = t1 + len(BloodBank_Results_Phase1.PermStorage['Data']) // frame_skip
t3 = t2 + fps*freeze
t4 = t3 + len(BloodBank_Results_Phase2.PermStorage['Data']) // frame_skip
t5 = t4 + fps*freeze
t6 = t5 + len(BloodBank_Results_Phase3.PermStorage['Data']) // frame_skip
Dyn_1_ann = '$Demand_{1}$ $<$ $Demand_{2}$ & $Demand_{3}$\n(Equilibrating)'
Frz_1_ann = '$Demand_{1}$ $<$ $Demand_{2}$ & $Demand_{3}$\n(Converged)'
Dyn_2_ann = '$Demand_{1}$ ~= $Demand_{2}$ & $Demand_{3}$\n(Equilibrating)'
Frz_2_ann = '$Demand_{1}$ ~= $Demand_{2}$ & $Demand_{3}$\n(Converged)'
Dyn_3_ann = '$Demand_{1}$ $>$ $Demand_{2}$ & $Demand_{3}$\n(Equilibrating)'
Frz_3_ann = '$Demand_{1}$ $>$ $Demand_{2}$ & $Demand_{3}$\n(Converged)'
anns = sorted([(t1, plt.title, Dyn_1_ann),
(t2, plt.title, Frz_1_ann),
(t3, plt.title, Dyn_2_ann),
(t4, plt.title, Frz_2_ann),
(t5, plt.title, Dyn_3_ann),
(t6, plt.title, Frz_3_ann)],
key=lambda x:x[0], reverse=True)
# Save Animation to File
fig, ax = plt.subplots()
BloodBank_ani = animation.FuncAnimation(fig, Domain.UpdateVisual,
init_func=Domain.InitVisual,
frames=len(Frames),
fargs=(ax, Frames, anns), blit=True)
BloodBank_ani.save('BloodBank.mp4', writer=writer)
if __name__ == '__main__':
Demo()