-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_extraction_stochastic.py
283 lines (255 loc) · 9.89 KB
/
data_extraction_stochastic.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import numpy as np
import pandas as pd
import os
from stochastic_generation import Generate_stochastic_data
import warnings
warnings.filterwarnings('ignore')
def Read_data(file_dir, filenames):
graph_file = os.path.join(file_dir, filenames[0])
pre_prod_file = os.path.join(file_dir, filenames[1])
infec_file = os.path.join(file_dir, filenames[2])
relaxpara_file = os.path.join(file_dir, filenames[3])
data_dict = {}
# graph = pd.read_csv("data_graph.csv") #read csv with data related to regions, their businesses and possible trade
graph = pd.read_csv(graph_file) #read csv with data related to regions, their businesses and possible trade
# prod = pd.read_csv(prod_file) #read csv with data related to regions and their businesses information
infec = pd.read_csv(infec_file) #read csv with data related to region information
relaxpara = pd.read_csv(relaxpara_file) # read cvs with parameter used in relaxed soft constraints model
prod = Generate_stochastic_data(pre_prod_file)
data_dict['graph'] = graph
data_dict['prod'] = prod
data_dict['infec'] = infec
data_dict['relaxpara'] = relaxpara
I = graph.region.unique().size #Compute number of regions
J = graph.business.unique().size #Compute number of businesses
data_dict['I'] = I
data_dict['J'] = J
trade = []
trade_orig_dest = np.zeros((I,I,J))
trade_orig = []
#Loop to determine businesses that trade
for r in range(graph.business.size):
if graph['region'][r] != graph['trade_region'][r]:
trade.append(graph['business'][r])
trade_orig.append(graph['region'][r])
trade_orig_dest[graph['region'][r],graph['trade_region'][r],graph['business'][r]] = 1
trade_orig_ar = list( dict.fromkeys(trade_orig) )
trade_orig_ar.sort()
trade_ar = list( dict.fromkeys(trade) )
trade_ar.sort()
data_dict['trade_ar'] = trade_ar
J1 = len(trade_ar) #Number of businesses that involve trade
J2 = J-J1
T = 6 #Inserted by hand, but could then be automated...
num_scenario = 5
data_dict['T'] = T
data_dict['num_scenario'] = num_scenario
#Set supply and demand graph
#Here define all the edges between state for each business
#Loop to determine all the possible edges that involve trade
Edge = [None]*J #I use J and not J1 since the order of the businesses matter
for e in trade_ar:
Edge[e] = []
for r in range(graph.business.size):
if graph['business'][r] == e:
if graph['region'][r] != graph['trade_region'][r]:
Edge[e].append((graph['region'][r],graph['trade_region'][r]))
data_dict['Edge'] = Edge
#Loop to determine the profit of the above registered Edges
Profit = [None]*J
for j in range(J):
Profit[j] = {}
prof_lab = []
for i in range(T):
prof_lab.append(f'Profit{i+1}')
for x in trade_ar:
for z in range(len(Edge[x])):
Profit[x][Edge[x][z]] = graph[(graph['region']==Edge[x][z][0]) & (graph['business']==x) & (graph['trade_region']==Edge[x][z][1])][prof_lab].values
Profit[x][Edge[x][z]] = Profit[x][Edge[x][z]][0].tolist()
data_dict['Profit'] = Profit
#Loop to extract profit of local business
Q = np.zeros((I,J,T))
for x in range(I):
for y in range(J):
Q[x,y,:] = graph[(graph['region']==x) & (graph['business']==y) & (graph['trade_region']==x)][prof_lab].values
data_dict['Q'] = Q
prod_lab = []
for i in range(T):
prod_lab.append(f'prod_cap{i+1}')
#Loop to extract production capacity (uncertain)
R = np.zeros((num_scenario,I,J,T))
for xi in range(num_scenario):
for x in range(I):
for y in range(J):
R[xi,x,y,:] = prod[(prod['scenario']==xi) & (prod['region']==x) & (prod['business']==y)][prod_lab].values
data_dict['R'] = R
dem_lab = []
for i in range(T):
dem_lab.append(f'demand{i+1}')
#Loop to extract demand
D = np.zeros((num_scenario,I,J,T))
for xi in range(num_scenario):
for x in range(I):
for y in range(J):
D[xi,x,y,:] = prod[(prod['scenario']==xi) & (prod['region']==x) & (prod['business']==y)][dem_lab].values
data_dict['D'] = D
unemp_lab = []
for i in range(T):
unemp_lab.append(f'unemp{i+1}')
#Loop to extract Unemployed people
S = np.zeros((I,J,T))
for x in range(I):
for y in range(J):
S[x,y,:] = prod[(prod['scenario']==0) & (prod['region']==x) & (prod['business']==y)][unemp_lab].values
data_dict['S'] = S
imp_lab = []
for i in range(T):
imp_lab.append(f'importance{i+1}')
# importance of opening business
delta = np.zeros((I,J,T))
for x in range(I):
for y in range(J):
delta[x,y,:] = prod[(prod['scenario']==0) & (prod['region']==x) & (prod['business']==y)][imp_lab].values
data_dict['delta'] = delta
acc_lab = []
for i in range(T):
acc_lab.append(f'acc_unemp{i+1}')
#Loop to extract accepted unemployed people
v = np.zeros((I,T))
for x in range(I):
v[x,:] = infec[(infec['region']==x)][acc_lab].values
data_dict['v'] = v
weight_lab = []
for i in range(T):
weight_lab.append(f'weight_inf{i+1}')
#Loop to extract weight of infected people
W = np.zeros((I,T))
for x in range(I):
W[x,:] = infec[(infec['region']==x)][weight_lab].values
data_dict['W'] = W
infr_lab = []
for i in range(T):
infr_lab.append(f'inf_rate{i+1}')
#Loop to extract infection rate
alpha = np.zeros((I,T))
for x in range(I):
alpha[x,:] = infec[(infec['region']==x)][infr_lab].values
data_dict['alpha'] = alpha
rec_lab = []
for i in range(T):
rec_lab.append(f'rec_rate{i+1}')
#Loop to extract recovery rate
beta = np.zeros((I,T))
for x in range(I):
beta[x,:] = infec[(infec['region']==x)][rec_lab].values
data_dict['beta'] = beta
#Recovery period
T0 = 3 #For now entered by hand
data_dict['T0'] = T0
infb_lab = []
for i in range(T):
infb_lab.append(f'inf_bus{i+1}')
#Loop to extract infected people for each business
B = np.zeros((num_scenario,I,J,T))
for xi in range(num_scenario):
for x in range(I):
for y in range(J):
B[xi,x,y,:] = prod[(prod['scenario']==xi) & (prod['region']==x) & (prod['business']==y)][infb_lab].values
data_dict['B'] = B
#Loop to extract initial infected people
a0 = np.zeros(I)
for x in range(I):
a0[x] = infec[(infec['region']==x)][['initial_inf']].values
data_dict['a0'] = a0
acci_lab = []
for i in range(T):
acci_lab.append(f'acc_inf{i+1}')
#Loop to extract acceptable infected people
c = np.zeros((I,T))
for x in range(I):
c[x,:] = infec[(infec['region']==x)][acci_lab].values
data_dict['c'] = c
#Loop to extract intensive rate
int_lab = []
for i in range(T):
int_lab.append(f'int_rate{i+1}')
h = np.zeros((I,T))
for x in range(I):
h[x,:] = infec[(infec['region']==x)][int_lab].values
data_dict['h'] = h
#Loop to extract ICU capacity
icu_lab = []
for i in range(T):
icu_lab.append(f'icu_cap{i+1}')
k = np.zeros((I,T))
for x in range(I):
k[x,:] = infec[(infec['region']==x)][icu_lab].values
data_dict['k'] = k
# medical equipment parameters
#Loop for testing kits if no business opened
tkno_lab = []
for i in range(T):
tkno_lab.append(f'tk_nobus{i+1}')
f = np.zeros((I,T))
for x in range(I):
f[x,:] = infec[(infec['region']==x)][tkno_lab].values
data_dict['f'] = f
#Loop for PPE if no business opened
ppeno_lab = []
for i in range(T):
ppeno_lab.append(f'ppe_nobus{i+1}')
m = np.zeros((I,T))
for x in range(I):
m[x,:] = infec[(infec['region']==x)][ppeno_lab].values
data_dict['m'] = m
#Loop for tk requiered of business j opens
tkbus_lab = []
for i in range(T):
tkbus_lab.append(f'tk_bus{i+1}')
g = np.zeros((I,J,T))
for x in range(I):
for y in range(J):
g[x,y,:] = prod[(prod['scenario']==0) & (prod['region']==x) & (prod['business']==y)][tkbus_lab].values
data_dict['g'] = g
#Loop for ppe requiered of business j opens
ppebus_lab = []
for i in range(T):
ppebus_lab.append(f'ppe_bus{i+1}')
n = np.zeros((I,J,T))
for x in range(I):
for y in range(J):
n[x,y,:] = prod[(prod['scenario']==0) & (prod['region']==x) & (prod['business']==y)][ppebus_lab].values
data_dict['n'] = n
#Loop for maximum tk available in region i
tkmax_lab = []
for i in range(T):
tkmax_lab.append(f'tk_max{i+1}')
l = np.zeros((I,T))
for x in range(I):
l[x,:] = infec[(infec['region']==x)][tkmax_lab].values
data_dict['l'] = l
#Loop for maximum ppe available in region i
ppemax_lab = []
for i in range(T):
ppemax_lab.append(f'ppe_max{i+1}')
u = np.zeros((I,T))
for x in range(I):
u[x,:] = infec[(infec['region']==x)][ppemax_lab].values
data_dict['u'] = u
#Loop for initial state
x0 = np.zeros((I,J))
for x in range(I):
for y in range(J):
x0[x,y] = prod[(prod['scenario']==0) & (prod['region']==x) & (prod['business']==y)][['init_state']].values
data_dict['x0'] = x0
#Loop for parameters used in relaxed soft constraints model
time_lab = []
for i in range(1,T):
time_lab.append(f'time{i+1}')
num_cons = 5 # number of soft constraints
gamma = np.zeros((num_cons,I,T-1))
for cons in range(num_cons):
for x in range(I):
gamma[cons,x,:] = relaxpara[(relaxpara['constraint']==cons) & (relaxpara['region']==x)][time_lab].values
data_dict['gamma'] = gamma
return data_dict