-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwaste+food+management.py
410 lines (389 loc) · 10.5 KB
/
waste+food+management.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# coding: utf-8
# # Waste Food Management
# ---
# Despite the overwhelming food supply, people still go hungry. This project is a way to come up with a part of the solution to a global problem.
#
# Restaurants produce food in abundance to meet their demands. Some of this food gets wasted or thrown. We are modelling a scenario where we take the food from these restaurants and give it to the ones in need through our tie-ups. The tie-ups are organisations (NGOs) or religious places where the poor come to be served free food.
#
# ## Problem Set-up
# ---
# We model this as a sequential decision modelling problem where we implement 3 policies.
# The span of this problem occurs over a 12 hour day where we are the food bank in charge of allocating food resources to multiple tie-ups. Each tie-up has it's own demand ($D_t$) which we know the forecast of.
#
# Over the 12 hour day, we send out food to one tie-up in one hour. Therefore, we have 12 tie-ups to send food out to. We have $R_0$ amount of food in our storage initially that keeps decreasing as we keep sending the food out.
# Problem sequence:
# 1. Collect food amount $R_0$ from restaurants at night and store in food bank storage units.
# 2. In the morning, the 12 hour day starts.
# 3. At $t=0$, allot food amount $x_t$ to the first tie-up '$t_1$'where food is distributed throughout the hour.
# (The demand is not known until the end of the hour)
# 4. Once the first hour is up, at $t=t+1$, send out $x_{t+1}$ to the next tie-up and so on....
#
# Note: Keep updating the amount of food you have in storage as $R_{t+1}$ = $R_t$ - $x_t$
#
# But there is a catch to this problem!! You can only send out food that is **fresh**.
# To quantify freshness, we introduce a freshness factor $f_F$ where $F= A,B$
# We assume there to be 2 kinds of food in the storage.
# 1. Type A is the kind of food that deteriorates faster with time.
# 2. Type B is the kind of food that deteriorates with time but at a rate that is much slower than type A.
#
# $f_{F,t+1}$ = $f_{F,t}$ - $E_{F,t+1}$
#
# where $E_{F,t+1}$ is a decreasing function.
# The rates at which type A and B deteriorate are:
#
# $E_{A,t+1}$ = {1,5,10}
#
# $E_{B,t+1}$ = {1,2,3}
#
# The decreasing function $E_{F,t+1}$ can take on any of the 3 values with equal probability.
# We start off by assuming the food in storage initially is $f_F$ = 500 for both type A and B. This value decreases as every hour passes.
#
# ## Modelling the problem
# ---
# *State variables*: $R_A$ , $R_B$ , $f_A$ , $f_B$
#
# *Exogenous information*: $E_A$ , $E_B$ , $D$
#
# *Transition function*:
#
# $R_{F,t+1}$ = $R_{F,t}$ - $x_{F,t}$
#
# $f_{F,t+1}$ = $f_{F,t}$ - $E_{F,t+1}$
#
# where F = A,B
#
# *Decision variables*:
#
# Decision 1: How much quantitiy of food to send to one tie-up? $x_t$ = ?
#
# Decision 2: How much of it should be type A and type B? $x_{A,t}$ = ? $x_{B,t}$ = ?
#
# *Constraints*:
#
# For F= A,B
#
# $x_F$ <= $R_F$........*you can only send as much as you have*
#
# $f_F$ > 0 ............*you can only serve fresh food, i.e, if freshness factor becomes 0, the food must be thrown away*
#
#
# ## Policy 1
#
# **Decision 1:**
# Divide the amount of food to every tie-up equally irrespective of the demand. Send the demand needed if below equal or send equal.
#
# **Decision 2**
# Type A should be 60% of the demand and type B should be 40% of demand.
# In[3]:
global fa
global fb
global x
global Ra
global Rb
global R
global T
global t
global d
import random
T=12
fa=50
fb=50
Ra=500
Rb=500
d=0
R = Ra+Rb
def demandcalc(T):
global D
D= random.normalvariate(100,20) #random.choice(50,100)
equal=(R//T) #use t as start index while T comes from the other way
Dmu= 100
if equal-D>=0:
x=D
else:
x=equal #what if actual demand is much greater than equal
print("The calculated demand is : %r" %x)
return x
def send(x, t):
global fa
global fb
global D
global d
global Ra
global Rb
global R
fa = fa
fb = fb
D = D
d = d
Ra = Ra
Rb = Rb
R = R
Eaa= [1,5,10];
Ea= random.choice(Eaa)
Ebb= [1,2,3];
Eb= random.choice(Ebb)
fa= fa - Ea
fb= fb - Eb
print("The freshness is : %r / %r" %(fa,fb))
xa= int(0.6*x)
xb= int(0.4*x)
print("The expected amount of A and B to be sent : %r / %r" %(xa,xb))
if fa>0:
if Ra>=xa:
Ra= Ra-xa
else:
d= xa-Ra #d is deficit
xa=Ra
Ra=0
else:
Ra=0
xa=0
xb=x
print("The amount of A sent: %r" %xa)
print("The amount of A left: %r" %(Ra))
if fb>0:
xb=xb+d
d=0
if Rb>=xb:
Rb= Rb-xb
else:
xb=Rb
Rb=0
else:
Rb=0
xb=0
print("The amount of B sent: %r" %xb)
print("The amount of B left: %r" %(Rb))
x= xa+xb
print("The total amount sent : %r" %x)
R= Ra+Rb
print("The Total amount left is : %r" %(R))
T = 12
Sum = 0
for i in range(1,13):
print("For tie-up %r" %i)
x = demandcalc(T)
Sum = int(Sum + x)
send(x, i)
T = T-1
print("The total demand in all the tie-ups is %r" %Sum)
# ## Policy 2:
#
# **Decision 1**:
# Send whatever the forecasted demand is
#
# **Decision 2**:
# Send 60% A and 40% B
# In[43]:
global fa
global fb
global x
global Ra
global Rb
global R
global T
global t
global d
import random
T=12
fa=50
fb=50
Ra=500
Rb=500
d=0
R = Ra+Rb
def demandcalc(T):
global D
D= random.normalvariate(100,20) #random.choice(50,100)
x=D
print("The calculated demand is or the amount to be sent : %r" %x)
return x
def send(x, t):
global fa
global fb
global D
global d
global Ra
global Rb
global R
fa = fa
fb = fb
D = D
d = d
Ra = Ra
Rb = Rb
R = R
Eaa= [1,5,10];
Ea= random.choice(Eaa)
Ebb= [1,2,3];
Eb= random.choice(Ebb)
fa= fa - Ea
fb= fb - Eb
print("The freshness is : %r / %r" %(fa,fb))
xa= int(0.6*x)
xb= int(0.4*x)
print("The expected amount of A and B to be sent : %r / %r" %(xa,xb))
if fa>0:
if Ra>=xa:
Ra= Ra-xa
else:
d= xa-Ra #d is deficit
xa=Ra
Ra=0
else:
Ra=0
xa=0
xb=x
print("The amount of A sent: %r" %xa)
print("The amount of A left: %r" %(Ra))
if fb>0:
xb=xb+d
d=0
if Rb>=xb:
Rb= Rb-xb
else:
xb=Rb
Rb=0
else:
Rb=0
xb=0
print("The amount of B sent: %r" %xb)
print("The amount of B left: %r" %(Rb))
x= xa+xb
print("The total amount sent : %r" %x)
R= Ra+Rb
print("The Total amount left is : %r" %(R))
T = 12
Sum = 0
for i in range(1,13):
print("For tie-up %r" %i)
x = demandcalc(T)
Sum = int(Sum + x)
send(x, i)
T = T-1
print("The total demand in all the tie-ups is %r" %Sum)
# ## Policy 3:
# *Optimization over the freshness (We maximize the number of people having fresh food where fresh food is limited to a value of 35 and above)*
#
# **Decision 1**:
# Send whatever the forecasted demand is
#
# **Decision 2**:
# Send the type A quantity as j% of demand such that maximum number of people get fresh food
# In[86]:
global fa
global fb
global x
global Ra
global Rb
global R
global T
global t
global d
global D
global aa
global bb
global ff
global s
global j
global v
global sum
import random
import matplotlib.pyplot as plt
fa=50
fb=50
Ra=500
Rb=500
d=0
aa=0
bb=0
sum=0
s=[]
j=[]
R = Ra+Rb
for j in range(0,101,1):
fa=50
fb=50
Ra=500
Rb=500
d=0
aa=0
bb=0
for T in range(0,13):
D=random.normalvariate(100,20) #random.choice(50,100)
x=D
#print("The calculated demand is or the amount to be sent : %r" %x)
Eaa= [1,5,10];
Ea= random.choice(Eaa)
Ebb= [1,2,3];
Eb= random.choice(Ebb)
fa= fa - Ea
fb= fb - Eb
#print("The freshness is : %r / %r" %(fa,fb))
xa= int((j/100)*x)
xb= int(((100-j)/100)*x)
#print("The expected amount of A and B to be sent : %r / %r" %(xa,xb))
if fa>=0:
if Ra>=xa:
Ra= Ra-xa
else:
d= xa-Ra #d is deficit
xa=Ra
Ra=0
if fa>=35:
aa=aa+xa
else:
aa=aa
else:
Ra=0
xa=0
xb=x
#print("The amount of A sent: %r" %xa)
#print("The amount of A left: %r" %(Ra))
if fb>0:
xb=xb+d
d=0
if Rb>=xb:
Rb= Rb-xb
else:
xb=Rb
Rb=0
if fb>=35:
bb=bb+xb
else:
bb=bb
else:
Rb=0
xb=0
#print("The amount of B sent: %r" %xb)
#print("The amount of B left: %r" %(Rb))
x= xa+xb
#print("The total amount sent : %r" %x)
R= Ra+Rb
#print("The Total amount left is : %r" %(R))
ff=aa+bb
s.append(ff)
#print("The number of people eating fresh food is:%r" %ff)
v=max(s)
print("The maximum number of people that can get fresh food is: %r" %v)
jj=s.index(v)
print("The percentage of A that should be sent for every demand is: %r"%jj)
j= range(101)
plt.plot(s,j)
plt.ylabel('Percentage of demand that is fulfiled by A')
plt.xlabel('Number of people eating fresh food')
plt.show()
# ## Inferences
#
# ### Policy 1:
# 1. It is not a very good policy if the goal is to maximize the number of people fed.
# 2. Should a scenario arise, where the demand is consistently way above the equal amount that is allocated, there will be a food deficit at the tie-ups when the we have the food at the storage.
# 3. There is a high possibility of food being wasted due to the freshness falling to 0 before it could get allocated.
# 4. The food bank has to make multiple trips to various tie-ups when it could have allocated as much as the tie-up needed and finished the food faster.
#
# ### Policy 2:
# 1. This policy is good if we want to meet the demand but there is difficulty when we go to decide the % of demand that should be of type A and B.
# 2. The stock finishes up faster which also means lesser number of trips to tie-ups compared to policy 1.
#
# ### Policy 3:
# 1. This policy gives the optimum percent of demand that should be fulfilled by type A and thus, type B (Type B% = 100-type A %) if the goal in mind is to maximize the amount of fresh food consumed.