-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eco-LCA_input_alpha.py
401 lines (224 loc) · 7.97 KB
/
Eco-LCA_input_alpha.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
# coding: utf-8
# In[ ]:
import sys
import numpy as np
print ("Welcome to Eco-LCA")
rcond = None
"""Starting Counter for number of processes/network (can be changed with the length of any full list)"""
counter = 1
"""The count of levels in the chain being checked for input"""
level = 1
"""The list of processes still being checked for input"""
check = []
"""Starting a list of ( process, output name, output value)"""
out =[]
"""Starting a list of list of all (processes , output names, output values)"""
allPros =[]
"""Starting a list of lists of all (processes, input names, input values)"""
allIn = []
"""Environmental impact vector"""
F = []
allEnv = []
v_allEnv = []
res =[]
p = []
# In[2]:
print("Enter Model complexity/price limit")
epsilon = float(input())
print("Enter tolerance of iteration")
zeta = float(input())
pro = input ("Enter name of the final demand process/network:\n")
"""Adding the process into a list"""
out.append(pro)
print ("For %s, Enter:" % pro)
outName = input("Name of output:\n")
"""Adding the name of the put of the process to the list"""
out.append(outName)
"""Prompting the user to input the name of the final demand process/network then name of the output the value of the output"""
f_dem = float(input("Enter final demand:\n"))
dirt = {pro : counter}
print ("Flow of %s : " % outName)
outVal = float(input())
print ("Variance of flow: ")
v_outVal = float(input())
"""Adding the value of the output of the process"""
tec = np.zeros((1,1))
v_tec = np.zeros((1,1))
tec[(0,0)] = outVal
v_tec[(0,0)] = v_outVal
F.append(f_dem)
"""Adding the list made into a list of all lists"""
allPros.append(out)
"""Resetting the list for the next process"""
out = []
"""Asking for environmenal impact"""
print ("Environmental impact : ")
env = float(input())
allEnv.append(env)
tot_env = env
print ("Variance of environmental impact :")
v_env = float(input())
v_allEnv.append(v_env)
print ("Price of process :" )
price = float(input())
p.append(price)
Mc = np.sum(p)
check.append(pro)
"""@@@@@@@@@@@@@@@New edits@@@@@@@@@@@@@@@"""
S = np.linalg.solve(tec,F)
X_inv = np.linalg.inv(tec)
lamda = np.matmul(allEnv,X_inv)
alpha = np.matmul(X_inv,F)
unc1 = 0
unc2 = 0
for i in range(0,counter):
unc1 = (lamda[i]** 2) * v_allEnv[i] + unc1
for i in range(0,counter):
for j in range(counter):
unc2 = ((alpha[i] * lamda[j]))** 2 * v_tec[i,j] + unc2
res.append((unc1 + unc2)**(1/2))
print ("res = %f \n" % res[counter-1])
RSD = res[counter-1]/tot_env
curenv = env * S[-counter]
print ("The enveronmental impact of %s process is %f \n" % (pro,curenv))
RSD_old = RSD;
"""@@@@@@@@@@@@@@@End of new edits@@@@@@@@@@@@@@@"""
"""Looping through inputs of the processes"""
fail_counter = 0;
while level != 0 :
old_X =tec
old_M = allEnv
old_F = F
old_varX = v_tec
old_varM = v_allEnv
old_res = res
old_p = p
RSD_old = RSD
old_Mc = Mc
old_E = tot_env
"""Prompting user to enter the inputs for the process"""
print ("Name of input to %s or leave blank to proceed : " % check[level - 1])
inName = input()
if inName != "":
counter = counter + 1
level = level + 1
"""Prompting the user for the value of the input"""
print ("Flow of %s as input: " % inName)
inVal = float(input())
print ("Variance of flow of %s input: " % inName)
v_inVal = float(input())
print ("Price of process input is from:" )
price = float(input())
p.append(price)
Mc = np.sum(p)
if Mc > epsilon:
tec = old_X
allEnv = old_M
F = old_F
v_tec = old_varX
v_allEnv = old_varM
res = old_res
p = old_p
RSD = RSD_old
Mc = old_Mc
tot_env = old_E
print("Model complexity has hit limit. Quitting algorithm")
break;
else:
continue;
"""Asking for the name of the process the input is from"""
print ("Enter name of the process outputing %s :" % inName)
pro = input()
dirt[pro] = (counter)
"""Asking for the output value of the current process """
print ("For %s, enter: " % inName)
print ("Flow of %s output:" % inName)
outVal = float(input())
print ("Variance of flow of %s output:" % inName)
v_outVal = float(input())
print ("Environmental impact :")
env = float(input())
allEnv.append(env)
tot_env = env + tot_env
print ("Variance of environmental impact :")
v_env = float(input())
v_allEnv.append(v_env)
A = np.zeros((counter,counter))
A[-tec.shape[0]:, -tec.shape[1]:] = tec
tec = A
tec[(0,0)] = outVal
tec[(0,-dirt[check[level-2]])] = (- inVal)
v_A = np.zeros((counter,counter))
v_A[-v_tec.shape[0]:, -v_tec.shape[1]:] = v_tec
v_tec = v_A
v_tec[(0,0)] = v_outVal
v_tec[(0,-dirt[check[level-2]])] = v_inVal
F = np.append(0,F)
"""@@@@@@@@@@@@@@@@@@@@@@Enter New edits Here@@@@@@@@@@@@@@@@@@@@@@@@@@@"""
S = np.linalg.solve(tec,F)
X_inv = np.linalg.inv(tec)
lamda = np.matmul(allEnv,X_inv)
alpha = np.matmul(X_inv,F)
unc1 = 0
unc2 = 0
for i in range(0,counter):
unc1 = (lamda[i]** 2) * v_allEnv[i] + unc1
for i in range(0,counter):
for j in range(0,counter):
unc2 = ((alpha[i] * lamda[j]))** 2 * v_tec[i,j] + unc2
res.append((unc1 + unc2)**(1/2))
print ("res = %f \n" % res[counter-1])
RSD = res[counter-1]/tot_env
check.append(pro)
if abs(RSD - RSD_old) > zeta:
continue;
else:
print("Change in RSD is lower than tolerance\n")
fail_counter = fail_counter+1;
if fail_counter > 3:
tec = old_X
allEnv = old_M
F = old_F
v_tec = old_varX
v_allEnv = old_varM
res = old_res
p = old_p
RSD = RSD_old
Mc = old_Mc
tot_env = old_E
break;
else:
continue
if RSD < RSD_old:
print("RSD check as has passed")
continue
else:
print("RSD check has failed\n")
print("Do you want to keep currently entered data?\n")
choice = input()
if choice == "yes":
continue;
else:
tec = old_X
allEnv = old_M
F = old_F
v_tec = old_varX
v_allEnv = old_varM
res = old_res
p = old_p
RSD = RSD_old
Mc = old_Mc
tot_env = old_E
print("Do you want to continue?")
choice = input()
fail_counter = fail_counter - 1;
if choice == "yes":
check.pop()
level = level - 1
counter = counter - 1
continue;
else:
break;
else:
check.pop()
level = level - 1