-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_functions.py
935 lines (766 loc) · 41.4 KB
/
helper_functions.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
# -*- coding: utf-8 -*-
# @Time : 28/07/22 11:29 PM
# @Author : nkpanda
# @FileName: helper_functions.py
# @Git :https://github.com/nkpanda97
import numpy as np
from pyomo.environ import *
import matplotlib.pyplot as plt
from tabulate import tabulate
import mpisppy.utils.sputils as sputils
import pandas as pd
from itertools import product
from numpy.random import default_rng
from tqdm import tqdm
# https://mpi-sppy.readthedocs.io/en/latest/examples.html
# ------------------------------ Models for Q(a) MOD1 ------------------------------------------------------------------
# Create deeterministic model instance
def create_model_qa(capacity_per_mode=[5, 3, 2], capacity_max=[7, 4, 3]):
"""
This function creates a diterminestic model for the MOD1 model given in the report and jupyter
notebook
"""
model = ConcreteModel()
# Declaring SETS
model.n = Set(initialize=np.arange(4)) # Number of technologies
model.k = Set(initialize=np.arange(3)) # Number of modes
# Declaring Variables
model.x = Var(model.n, within=NonNegativeReals) # Decision variable for capacity of each techology
model.y = Var(model.n, model.k,
within=NonNegativeReals) # Capacity of technology i={0,1,2,3} effectively used in mode j={0,1,2}
# Defining Parameters
model.capacity_max = Param(model.k, within=NonNegativeReals,
mutable=True) # Maximum possible value of \xi_j, j={0,1,2}
model.capacity_per_mode = Param(model.k, within=NonNegativeReals,
mutable=True) # Stochastic load demand for model j but value is known as the expected value
model.tech_cost = Param(model.n, within=NonNegativeReals, mutable=True) # Cost of each technology per unit capacity
model.operation_cost = Param(model.n, within=NonNegativeReals,
mutable=True) # Cost of each technology for production of per unit capacity
model.max_investment = 120 # Max budget
model.T = Param(model.k, initialize=[10, 6, 1], mutable=False)
# Filling parameter
operating_costs = [4, 4.5, 3.2, 5.5]
capital_cost = [10, 7, 16, 6]
for i in model.n:
model.operation_cost[i] = operating_costs[i]
model.tech_cost[i] = capital_cost[i]
for j in model.k:
model.capacity_per_mode[j] = capacity_per_mode[j]
model.capacity_max[j] = capacity_max[j]
def max_demand(model):
return sum(model.x[i] for i in model.n) >= sum(model.capacity_max[j] for j in model.k)
def budget_constraint(model):
return sum(model.tech_cost[i] * model.x[i] for i in model.n) <= model.max_investment
def capacity_per_mode(model, i):
return sum(model.y[i, j] for j in model.k) <= model.x[i]
def demand_bal(model, j):
return sum(model.y[i, j] for i in model.n) >= model.capacity_per_mode[j]
model.con1 = Constraint(rule=max_demand)
model.con2 = Constraint(rule=budget_constraint)
model.con3 = Constraint(model.n, rule=capacity_per_mode)
model.con4 = Constraint(model.k, rule=demand_bal)
def obj_second_stage(model):
expr = sum(model.operation_cost[i] * sum(model.T[j] * model.y[i, j] for j in model.k) for i in model.n)
return expr
def obj_first_stage(model):
expr = sum(model.tech_cost[i] * model.x[i] for i in model.n)
return expr
model.ObjCost_first_stage = Expression(rule=obj_first_stage)
model.ObjCost_second_stage = Expression(rule=obj_second_stage)
def total_cost_rule(model):
return model.ObjCost_first_stage + model.ObjCost_second_stage
model.Total_Cost_Objective = Objective(rule=total_cost_rule, sense=minimize)
return model
# Createmodel to calculate EEV
def create_eev_model_qa(x_bar=[1, 2, 3, 4], capacity_per_mode=[5, 3, 2], capacity_max=[7, 4, 3]):
"""
This function creates a diterminestic model for the MOD1.1 model to solve for the EEEV value given in the report and
jupyter notebook
"""
model = ConcreteModel()
# Declaring SETS
model.n = Set(initialize=np.arange(4)) # Number of technologies
model.k = Set(initialize=np.arange(3)) # Number of modes
# Declaring Variables
model.y = Var(model.n, model.k,
within=NonNegativeReals) # Capacity of technology i={0,1,2,3} effectively used in mode j={0,1,2}
# Defining Parameters
model.x_bar = Param(model.n, within=NonNegativeReals, mutable=True) # Expected value solution
model.capacity_max = Param(model.k, within=NonNegativeReals,
mutable=True) # Maximum possible value of \xi_j, j={0,1,2}
model.capacity_per_mode = Param(model.k, within=NonNegativeReals,
mutable=True) # Stochastic load demand for model j but value is known as the expected value
model.tech_cost = Param(model.n, within=NonNegativeReals, mutable=True) # Cost of each technology per unit capacity
model.operation_cost = Param(model.n, within=NonNegativeReals,
mutable=True) # Cost of each technology for production of per unit capacity
model.max_investment = 120 # Max budget
model.T = Param(model.k, initialize=[10, 6, 1], mutable=False)
# Filling parameter
operating_costs = [4, 4.5, 3.2, 5.5]
for i in model.n:
model.operation_cost[i] = operating_costs[i]
model.x_bar[i] = x_bar[i]
for j in model.k:
model.capacity_per_mode[j] = capacity_per_mode[j]
def capacity_per_mode(model, i):
return sum(model.y[i, j] for j in model.k) <= model.x_bar[i]
def demand_bal(model, j):
return sum(model.y[i, j] for i in model.n) >= model.capacity_per_mode[j]
model.con3 = Constraint(model.n, rule=capacity_per_mode)
model.con4 = Constraint(model.k, rule=demand_bal)
def obj_second_stage(model):
expr = sum(model.operation_cost[i] * sum(model.T[j] * model.y[i, j] for j in model.k) for i in model.n)
return expr
model.obj = Objective(expr=sum(model.operation_cost[i] * sum(model.T[j] * model.y[i, j] for j in model.k)
for i in model.n), sense=minimize)
return model
# ------------------------------ Models for Q(b) MOD2------------------------------------------------------------------
# Create deterministic model instance for Q(b)
def create_model_qb(random_parameters=np.array([1, 1, 1, 1, 1, 1, 1]), capacity_max=[7, 4, 3]):
"""
This function creates a diterminestic model for the MOD2 model given in the report and jupyter
notebook
"""
# random_parameters=[a1,a2,a3,a4,xi1,xi2,xi3]
alpha_i = list(random_parameters[0:4]) + [1]
capacity_per_mode = list(random_parameters[4:7])
model = ConcreteModel()
# Declaring SETS------------------------------------------------------------------------------
model.n = Set(initialize=np.arange(5)) # Number of technologies
model.k = Set(initialize=np.arange(3)) # Number of modes
# Declaring Variables------------------------------------------------------------------------------
model.x = Var(model.n, within=NonNegativeReals) # Decision variable for capacity of each techology
model.y = Var(model.n, model.k,
within=NonNegativeReals) # Capacity of technology i={0,1,2,3} effectively used in mode j={0,1,2}
# Defining Parameters------------------------------------------------------------------------------
model.capacity_max = Param(model.k, within=NonNegativeReals,
mutable=True) # Maximum possible value of \xi_j, j={0,1,2}
model.capacity_per_mode = Param(model.k, within=NonNegativeReals,
mutable=True) # Stochastic load demand for model j but value is known as the expected value
model.tech_cost = Param(model.n, within=NonNegativeReals, mutable=True) # Cost of each technology per unit capacity
model.operation_cost = Param(model.n, within=NonNegativeReals,
mutable=True) # Cost of each technology for production of per unit capacity
model.max_investment = 120 # Max budget
model.T = Param(model.k, initialize=[10, 6, 1], mutable=False)
model.alpha = Param(model.n, mutable=True) # Random parameter for operational availability
# Filling parameter-----------------------------------------------------------------------------------
operating_costs = [4, 4.5, 3.2, 5.5, 10]
capital_cost = [10, 7, 16, 6, 0]
for i in model.n:
model.operation_cost[i] = operating_costs[i]
model.tech_cost[i] = capital_cost[i]
model.alpha[i] = alpha_i[i]
for j in model.k:
model.capacity_per_mode[j] = capacity_per_mode[j]
model.capacity_max[j] = capacity_max[j]
def max_demand(model):
return sum(model.x[i] for i in model.n) >= sum(model.capacity_max[j] for j in model.k)
def budget_constraint(model):
return sum(model.tech_cost[i] * model.x[i] for i in model.n) <= model.max_investment
def capacity_per_mode(model, i):
return sum(model.y[i, j] for j in model.k) <= model.alpha[i] * model.x[i]
def demand_bal(model, j):
return sum(model.y[i, j] for i in model.n) >= model.capacity_per_mode[j]
# model.con1 = Constraint(rule=max_demand)
model.con2 = Constraint(rule=budget_constraint)
model.con3 = Constraint(model.n, rule=capacity_per_mode)
model.con4 = Constraint(model.k, rule=demand_bal)
def obj_second_stage(model):
expr = sum(model.operation_cost[i] * sum(model.T[j] * model.y[i, j] for j in model.k) for i in model.n)
return expr
def obj_first_stage(model):
expr = sum(model.tech_cost[i] * model.x[i] for i in model.n)
return expr
model.ObjCost_first_stage = Expression(rule=obj_first_stage)
model.ObjCost_second_stage = Expression(rule=obj_second_stage)
def total_cost_rule(model):
return model.ObjCost_first_stage + model.ObjCost_second_stage
model.Total_Cost_Objective = Objective(rule=total_cost_rule, sense=minimize)
return model
def create_model_qb_eev(xbar=[], random_parameters=np.array([1, 1, 1, 1, 1, 1, 1])):
"""
This function creates a diterminestic model for the MOD2 model given in the report and jupyter
notebook
"""
# random_parameters=[a1,a2,a3,a4,xi1,xi2,xi3]
alpha_i = list(random_parameters[0:4]) + [1]
capacity_per_mode = list(random_parameters[4:7])
model = ConcreteModel()
# Declaring SETS------------------------------------------------------------------------------
model.n = Set(initialize=np.arange(5)) # Number of technologies
model.k = Set(initialize=np.arange(3)) # Number of modes
# Declaring Variables------------------------------------------------------------------------------
model.y = Var(model.n, model.k,
within=NonNegativeReals) # Capacity of technology i={0,1,2,3} effectively used in mode j={0,1,2}
# Defining Parameters------------------------------------------------------------------------------
model.xbar = Param(model.n, within=NonNegativeReals, mutable=True) # Expected value solutions
model.capacity_per_mode = Param(model.k, within=NonNegativeReals,
mutable=True) # Stochastic load demand for model j but value is known as the expected value
model.tech_cost = Param(model.n, within=NonNegativeReals, mutable=True) # Cost of each technology per unit capacity
model.operation_cost = Param(model.n, within=NonNegativeReals,
mutable=True) # Cost of each technology for production of per unit capacity
model.max_investment = 120 # Max budget
model.T = Param(model.k, initialize=[10, 6, 1], mutable=False)
model.alpha = Param(model.n, mutable=True) # Random parameter for operational availability
# Filling parameter-----------------------------------------------------------------------------------
operating_costs = [4, 4.5, 3.2, 5.5, 10]
capital_cost = [10, 7, 16, 6, 0]
for i in model.n:
model.operation_cost[i] = operating_costs[i]
model.tech_cost[i] = capital_cost[i]
model.alpha[i] = alpha_i[i]
model.xbar[i] = xbar[i]
for j in model.k:
model.capacity_per_mode[j] = capacity_per_mode[j]
def capacity_per_mode(model, i):
return sum(model.y[i, j] for j in model.k) <= model.alpha[i]*model.xbar[i]
def demand_bal(model, j):
return sum(model.y[i, j] for i in model.n) >= model.capacity_per_mode[j]
model.con3 = Constraint(model.n, rule=capacity_per_mode)
model.con4 = Constraint(model.k, rule=demand_bal)
model.obj = Objective(expr = sum(model.operation_cost[i] * sum(model.T[j] * model.y[i, j] for j in model.k)
for i in model.n), sense=minimize)
return model
# Createmodel to calculate EEV for Qb
def create_eev_model_qb(x_bar=[1, 2, 3, 4, 5], random_parameters=np.array([1, 1, 1, 1, 1, 1, 1])):
"""
This function creates a diterminestic model for the MOD1.1 model to solve for the EEEV value given in the report and
jupyter notebook
"""
# random_parameters=[a1,a2,a3,a4,xi1,xi2,xi3]
alpha_i = list(random_parameters[0:4]) + [1]
capacity_per_mode = list(random_parameters[4:7])
model = ConcreteModel()
# Declaring SETS
model.n = Set(initialize=np.arange(5)) # Number of technologies
model.k = Set(initialize=np.arange(3)) # Number of modes
# Declaring Variables
model.y = Var(model.n, model.k,
within=NonNegativeReals) # Capacity of technology i={0,1,2,3} effectively used in mode j={0,1,2}
# Defining Parameters
model.x_bar = Param(model.n, within=NonNegativeReals, mutable=True) # Expected value solution
model.capacity_per_mode = Param(model.k, within=NonNegativeReals,
mutable=True) # Stochastic load demand for model j but value is known as the expected value
model.tech_cost = Param(model.n, within=NonNegativeReals, mutable=True) # Cost of each technology per unit capacity
model.operation_cost = Param(model.n, within=NonNegativeReals,
mutable=True) # Cost of each technology for production of per unit capacity
model.max_investment = 120 # Max budget
model.T = Param(model.k, initialize=[10, 6, 1], mutable=False)
model.alpha = Param(model.n, mutable=True) # Random parameter for operational availability
# Filling parameter
operating_costs = [4, 4.5, 3.2, 5.5, 10]
for i in model.n:
model.operation_cost[i] = operating_costs[i]
model.x_bar[i] = x_bar[i]
model.alpha[i] = alpha_i[i]
for j in model.k:
model.capacity_per_mode[j] = capacity_per_mode[j]
def capacity_per_mode(model, i):
return sum(model.y[i, j] for j in model.k) <= model.alpha[i]*model.x_bar[i]
def demand_bal(model, j):
return sum(model.y[i, j] for i in model.n) >= model.capacity_per_mode[j]
model.con3 = Constraint(model.n, rule=capacity_per_mode)
model.con4 = Constraint(model.k, rule=demand_bal)
model.obj = Objective(expr=sum(model.operation_cost[i] * sum(model.T[j] * model.y[i, j] for j in model.k)
for i in model.n), sense=minimize)
return model
def create_model_qc(random_parameters=np.array([1, 1, 1, 1, 1, 1, 1, 1, 1])):
"""
This function creates a diterminestic model for the MOD2 model given in the report and jupyter
notebook
"""
# random_parameters=[tau1, tau2, a1,a2,a3,a4,xi1,xi2,xi3]
tau_i = [10] + list(random_parameters[0:2])
alpha_i = list(random_parameters[2:6]) + [1]
capacity_per_mode = list(random_parameters[6:9])
model = ConcreteModel()
# Declaring SETS------------------------------------------------------------------------------
model.n = Set(initialize=np.arange(5)) # Number of technologies
model.k = Set(initialize=np.arange(3)) # Number of modes
# Declaring Variables------------------------------------------------------------------------------
model.x = Var(model.n, within=NonNegativeReals) # Decision variable for capacity of each techology
model.y = Var(model.n, model.k,
within=NonNegativeReals) # Capacity of technology i={0,1,2,3} effectively used in mode j={0,1,2}
# Defining Parameters------------------------------------------------------------------------------
model.capacity_per_mode = Param(model.k, within=NonNegativeReals,
mutable=True) # Stochastic load demand for model j but value is known as the expected value
model.tech_cost = Param(model.n, within=NonNegativeReals, mutable=True) # Cost of each technology per unit capacity
model.operation_cost = Param(model.n, within=NonNegativeReals,
mutable=True) # Cost of each technology for production of per unit capacity
model.max_investment = 120 # Max budget
model.T = Param(model.k,mutable=True)
model.alpha = Param(model.n, mutable=True) # Random parameter for operational availability
# Filling parameter-----------------------------------------------------------------------------------
operating_costs = [4, 4.5, 3.2, 5.5, 10]
capital_cost = [10, 7, 16, 6, 0]
for i in model.n:
model.operation_cost[i] = operating_costs[i]
model.tech_cost[i] = capital_cost[i]
model.alpha[i] = alpha_i[i]
for j in model.k:
model.capacity_per_mode[j] = capacity_per_mode[j]
model.T[j] = tau_i[j]
def budget_constraint(model):
return sum(model.tech_cost[i] * model.x[i] for i in model.n) <= model.max_investment
def capacity_per_mode(model, i):
return sum(model.y[i, j] for j in model.k) <= model.alpha[i] * model.x[i]
def demand_bal(model, j):
return sum(model.y[i, j] for i in model.n) >= model.capacity_per_mode[j]
model.con2 = Constraint(rule=budget_constraint)
model.con3 = Constraint(model.n, rule=capacity_per_mode)
model.con4 = Constraint(model.k, rule=demand_bal)
def obj_second_stage(model):
expr = sum(model.operation_cost[i] * sum(model.T[j] * model.y[i, j] for j in model.k) for i in model.n)
return expr
def obj_first_stage(model):
expr = sum(model.tech_cost[i] * model.x[i] for i in model.n)
return expr
model.ObjCost_first_stage = Expression(rule=obj_first_stage)
model.ObjCost_second_stage = Expression(rule=obj_second_stage)
def total_cost_rule(model):
return model.ObjCost_first_stage + model.ObjCost_second_stage
model.Total_Cost_Objective = Objective(rule=total_cost_rule, sense=minimize)
return model
# Createmodel to calculate EEV for Qc
def create_eev_model_qc(x_bar=[1, 2, 3, 4, 5], random_parameters=np.array([1, 1, 1, 1, 1, 1, 1, 1, 1])):
"""
This function creates a diterminestic model for the MOD1.1 model to solve for the EEEV value given in the report and
jupyter notebook
"""
# random_parameters=[tau2, tau3, a1,a2,a3,a4,xi1,xi2,xi3]
tau_i = [10] + list(random_parameters[0:2])
alpha_i = list(random_parameters[2:6]) + [1]
capacity_per_mode = list(random_parameters[6:9])
model = ConcreteModel()
# Declaring SETS
model.n = Set(initialize=np.arange(5)) # Number of technologies
model.k = Set(initialize=np.arange(3)) # Number of modes
# Declaring Variables
model.y = Var(model.n, model.k,
within=NonNegativeReals) # Capacity of technology i={0,1,2,3} effectively used in mode j={0,1,2}
# Defining Parameters
model.x_bar = Param(model.n, within=NonNegativeReals, mutable=True) # Expected value solution
model.capacity_per_mode = Param(model.k, within=NonNegativeReals,
mutable=True) # Stochastic load demand for model j but value is known as the expected value
model.tech_cost = Param(model.n, within=NonNegativeReals, mutable=True) # Cost of each technology per unit capacity
model.operation_cost = Param(model.n, within=NonNegativeReals,
mutable=True) # Cost of each technology for production of per unit capacity
model.max_investment = 120 # Max budget
model.T = Param(model.k, mutable=True)
model.alpha = Param(model.n, mutable=True) # Random parameter for operational availability
# Filling parameter
operating_costs = [4, 4.5, 3.2, 5.5, 10]
for i in model.n:
model.operation_cost[i] = operating_costs[i]
model.x_bar[i] = x_bar[i]
model.alpha[i] = alpha_i[i]
for j in model.k:
model.capacity_per_mode[j] = capacity_per_mode[j]
model.T[j] = tau_i[j]
def capacity_per_mode(model, i):
return sum(model.y[i, j] for j in model.k) <= model.alpha[i]*model.x_bar[i]
def demand_bal(model, j):
return sum(model.y[i, j] for i in model.n) >= model.capacity_per_mode[j]
model.con3 = Constraint(model.n, rule=capacity_per_mode)
model.con4 = Constraint(model.k, rule=demand_bal)
model.obj = Objective(expr=sum(model.operation_cost[i] * sum(model.T[j] * model.y[i, j] for j in model.k)
for i in model.n), sense=minimize)
return model
# ------------------------------ Additional Utility Functions---------------------------------------------------------
def print_res(model, question='qa'):
"""
This function is used to print the results of the expected value solution for all the models.
"""
if question == 'qb':
res_x = [['Technology-1', model.x[0].value, model.y[0, 0].value, model.y[0, 1].value, model.y[0, 2].value],
['Technology-2', model.x[1].value, model.y[1, 0].value, model.y[1, 1].value, model.y[1, 2].value],
['Technology-3', model.x[2].value, model.y[2, 0].value, model.y[2, 1].value, model.y[2, 2].value],
['Technology-4', model.x[3].value, model.y[3, 0].value, model.y[3, 1].value, model.y[3, 2].value],
['Technology-5', model.x[4].value, model.y[4, 0].value, model.y[4, 1].value, model.y[4, 2].value]
]
total_investment = value(model.ObjCost_first_stage)
total_operation_cost = value(model.ObjCost_second_stage)
print(tabulate(res_x, headers=['Technologies', 'Installed Capacity (x̄)',
'Usage (T1)', 'Usage (T2)', 'Usage (T3)'], tablefmt="pretty"))
print('Total investment cost =', total_investment)
print('Total operation cost =', total_operation_cost)
print('Total objective cost (EV) =', total_operation_cost + total_investment)
elif question == 'qa':
res_x = [['Technology-1', model.x[0].value, model.y[0, 0].value, model.y[0, 1].value, model.y[0, 2].value],
['Technology-2', model.x[1].value, model.y[1, 0].value, model.y[1, 1].value, model.y[1, 2].value],
['Technology-3', model.x[2].value, model.y[2, 0].value, model.y[2, 1].value, model.y[2, 2].value],
['Technology-4', model.x[3].value, model.y[3, 0].value, model.y[3, 1].value, model.y[3, 2].value],
]
total_investment = value(model.ObjCost_first_stage)
total_operation_cost = value(model.ObjCost_second_stage)
print(tabulate(res_x, headers=['Technologies',
'Installed Capacity (x̄)',
'Usage (T1)', 'Usage (T2)',
'Usage (T3)'], tablefmt="pretty"))
print('Total investment cost =', total_investment)
print('Total operation cost =', total_operation_cost)
print('Total objective cost (EV) =', total_operation_cost + total_investment)
def print_res_ts(model, question='qa'):
"""
This function is used to print the results of the stochastic solution for all the models.
"""
if question == 'qa':
res_x = [['Technology-1', model.x[0].value],
['Technology-2', model.x[1].value],
['Technology-3', model.x[2].value],
['Technology-4', model.x[3].value],
]
print(tabulate(res_x, headers=['Technologies', 'Installed Capacity (x̄)'], tablefmt="pretty"))
elif question == 'qb':
res_x = [['Technology-1', model.x[0].value],
['Technology-2', model.x[1].value],
['Technology-3', model.x[2].value],
['Technology-4', model.x[3].value],
['Technology-5', model.x[4].value]]
print(tabulate(res_x, headers=['Technologies', 'Installed Capacity (x̄)'], tablefmt="pretty"))
def generate_scenario_qa():
# This function is used to generate 27 scenarios based on the provoded discrete distributions for \xi_i
scenario_names = []
prob_dict = {'xi_1_3': 0.3,
'xi_1_5': 0.4,
'xi_1_7': 0.3,
'xi_2_2': 0.3,
'xi_2_3': 0.4,
'xi_2_4': 0.3,
'xi_3_1': 0.3,
'xi_3_2': 0.4,
'xi_3_3': 0.3,
}
for i in range(27):
scenario_names.append('scenario-' + str(i + 1))
all_scenarios = pd.DataFrame(
data={'xi_1': [3, 5, 7], 'xi_2': [2, 3, 4], 'xi_3': [1, 2, 3]}) # Scenario matrix as a data frame
all_scenarios = pd.DataFrame(index=scenario_names, data=list(product(*all_scenarios.values.T)))
all_scenarios_prob = []
for s in all_scenarios.iterrows():
all_scenarios_prob.append(prob_dict['xi_1_' + str(s[1][0])] *
prob_dict['xi_2_' + str(s[1][1])] *
prob_dict['xi_3_' + str(s[1][2])])
fig = plt.figure(figsize=(15, 8))
plt.stem(np.arange(27) + 1, all_scenarios_prob)
for i in range(27):
plt.text(i + 1, all_scenarios_prob[i],
'[' + str(all_scenarios[0][i]) + ','
+ str(all_scenarios[1][i]) + ','
+ str(all_scenarios[2][i]) + ']',
rotation=45)
plt.xlabel('Scenarios')
plt.ylabel('Probability')
plt.xticks(np.arange(27) + 1)
plt.grid()
plt.savefig('results/mod1_discretization.png', dpi=300)
plt.show()
scen_prob_df = pd.DataFrame(index=scenario_names, data={'Probability': all_scenarios_prob})
return scenario_names, all_scenarios, scen_prob_df
def scenario_creator_qa(scenario_name, S = '', prob_df='' ):
# This function is necessary to solve model1 and obtain stochastic (TS) solution using the MPI-SSPY package
# compatible with pyomo
try:
mode_demand = S.loc[scenario_name].to_numpy()
except:
raise ValueError("Unrecognized scenario name")
model = create_model_qa(capacity_per_mode=mode_demand,capacity_max = [5,3,2])
sputils.attach_root_node(model, model.ObjCost_first_stage, [model.x]) # It calls the attach_root_node function.
# We tell this function which part of the objective function (model.ObjCost_first_stage) and
# which set of variables (model.x) belong to the first stage.
# In this case, the problem is only two stages, so we need only specify the root node and the
# first-stage information–MPI-SPPy assumes the remainder of the model belongs to the second stage.
model._mpisppy_probability = prob_df.loc[scenario_name].to_numpy() # Equal probability
return model
def generate_combinations(arr, total_combinations, column_names):
"""
This function returns all the permutations of choosing one one eleme nt from each given arays of list.
Function to produce all possible combuination from each individual arrays
"""
# number of arrays
n = len(arr)
# to keep track of next element
# in each of the n arrays
indices = [0 for i in range(n)]
arr_fin_ = []
s_names = []
for i in range(total_combinations):
s_names.append('scenario_' + str(i))
while (1):
# print current combination
arr_ = []
for i in range(n):
arr_.append(arr[i][indices[i]])
arr_fin_.append(arr_)
next = n - 1
while (next >= 0 and
(indices[next] + 1 >= len(arr[next]))):
next -= 1
if (next < 0):
df_ = pd.DataFrame(index=s_names, data=arr_fin_)
df_.columns = column_names
return df_, s_names
indices[next] += 1
for i in range(next + 1, n):
indices[i] = 0
def generate_scenario_qb(number_of_samples, plot=True, seed = None):
# This functiion generates the scenarios based on \xi_i and alpha_i. The total number of scenarios generated
# is: 27*number_of_samples**4
rng = default_rng(seed)
xi1 = np.array([3, 5, 7])
xi2 = np.array([2, 3, 4])
xi3 = np.array([1, 2, 3])
prob_dict = {'xi_1_3': 0.3,
'xi_1_5': 0.4,
'xi_1_7': 0.3,
'xi_2_2': 0.3,
'xi_2_3': 0.4,
'xi_2_4': 0.3,
'xi_3_1': 0.3,
'xi_3_2': 0.4,
'xi_3_3': 0.3,
}
# Sampling from a uniform distribution
alpha_1 = rng.uniform(0.6, 0.9, number_of_samples) # Generating points between lower and higher limits.
alpha_2 = rng.uniform(0.7, 0.8, number_of_samples) # Generating points between lower and higher limits.
alpha_3 = rng.uniform(0.5, 0.8, number_of_samples) # Generating points between lower and higher limits.
alpha_4 = rng.uniform(0.9, 1, number_of_samples) # Generating points between lower and higher limits.
s_all, scenario_names__ = generate_combinations([alpha_1, alpha_2, alpha_3, alpha_4, xi1, xi2, xi3],
(number_of_samples ** 4) * 27,
['alpha_1', 'alpha_2', 'alpha_3', 'alpha_4', 'xi_1', 'xi_2',
'xi_3'])
s_prob = []
for s in s_all.iterrows():
s_prob.append(((1 / number_of_samples) ** 4) * prob_dict['xi_1_' + str(int(s[1][4]))] *
prob_dict['xi_2_' + str(int(s[1][5]))] *
prob_dict['xi_3_' + str(int(s[1][6]))])
s_prob_df = pd.DataFrame(index=scenario_names__, data=s_prob)
if plot:
fig, ax = plt.subplot_mosaic([['1', '2'], ['3', '4']], constrained_layout=True)
ax['1'].hist(s_all['alpha_1'], 40, facecolor='green', density=True, )
ax['2'].hist(s_all['alpha_2'], 40, facecolor='red')
ax['3'].hist(s_all['alpha_3'], 40, facecolor='blue')
ax['4'].hist(s_all['alpha_4'], 40, facecolor='brown')
ax['1'].set_xlabel(r'$\alpha_1:=\mathcal{U}(0.6,0.9)$')
ax['2'].set_xlabel(r'$\alpha_2:=\mathcal{U}(0.7,0.8)$')
ax['3'].set_xlabel(r'$\alpha_3:=\mathcal{U}(0.5,0.8)$')
ax['4'].set_xlabel(r'$\alpha_4:=\mathcal{U}(0.9,1)$')
ax['1'].set_ylabel('count')
ax['2'].set_ylabel('count')
ax['3'].set_ylabel('count')
ax['4'].set_ylabel('count')
ax['1'].grid(True)
ax['2'].grid(True)
ax['3'].grid(True)
ax['4'].grid(True)
# plt.tight_layout()
plt.suptitle(r'Histograms for $\alpha_i$')
plt.savefig('results/mod2_discretization_m2.png', dpi=300)
plt.show()
return s_all, s_prob_df
def generate_scenario_qc(number_of_samples, plot=True, seed = None):
# This functiion generates the scenarios based on \xi_i and alpha_i. The total number of scenarios generated
# is: 27*number_of_samples**4
rng = default_rng(seed)
xi1 = np.array([3, 5, 7])
xi2 = np.array([2, 3, 4])
xi3 = np.array([1, 2, 3])
tau_2 = np.array([5, 7.5])
tau_3 = np.array([0.5, 1.75])
prob_dict = {'xi_1_3': 0.3,
'xi_1_5': 0.4,
'xi_1_7': 0.3,
'xi_2_2': 0.3,
'xi_2_3': 0.4,
'xi_2_4': 0.3,
'xi_3_1': 0.3,
'xi_3_2': 0.4,
'xi_3_3': 0.3,
'tau_2_5.0': 0.6,
'tau_2_7.5': 0.4,
'tau_3_0.5': 0.6,
'tau_3_1.75': 0.4
}
# Sampling from a uniform distribution
alpha_1 = rng.uniform(0.6, 0.9, number_of_samples) # Generating points between lower and higher limits.
alpha_2 = rng.uniform(0.7, 0.8, number_of_samples) # Generating points between lower and higher limits.
alpha_3 = rng.uniform(0.5, 0.8, number_of_samples) # Generating points between lower and higher limits.
alpha_4 = rng.uniform(0.9, 1, number_of_samples) # Generating points between lower and higher limits.
s_all, scenario_names__ = generate_combinations([tau_2,tau_3, alpha_1, alpha_2, alpha_3, alpha_4, xi1, xi2, xi3],
(number_of_samples ** 4) * 27*4,
['tau_2','tau_3','alpha_1', 'alpha_2', 'alpha_3', 'alpha_4', 'xi_1', 'xi_2',
'xi_3'])
s_prob = []
for s in s_all.iterrows():
s_prob.append(((1 / number_of_samples) ** 4) * prob_dict['xi_1_' + str(int(s[1][6]))] *
prob_dict['xi_2_' + str(int(s[1][7]))] *
prob_dict['xi_3_' + str(int(s[1][8]))]*
prob_dict['tau_2_' + str(float(s[1][0]))]*
prob_dict['tau_3_' + str(float(s[1][1]))])
s_prob_df = pd.DataFrame(index=scenario_names__, data=s_prob)
if plot:
fig, ax = plt.subplot_mosaic([['1', '2'], ['3', '4']], constrained_layout=True)
ax['1'].hist(s_all['alpha_1'], 40, facecolor='green', density=True, )
ax['2'].hist(s_all['alpha_2'], 40, facecolor='red')
ax['3'].hist(s_all['alpha_3'], 40, facecolor='blue')
ax['4'].hist(s_all['alpha_4'], 40, facecolor='brown')
ax['1'].set_xlabel(r'$\alpha_1:=\mathcal{U}(0.6,0.9)$')
ax['2'].set_xlabel(r'$\alpha_2:=\mathcal{U}(0.7,0.8)$')
ax['3'].set_xlabel(r'$\alpha_3:=\mathcal{U}(0.5,0.8)$')
ax['4'].set_xlabel(r'$\alpha_4:=\mathcal{U}(0.9,1)$')
ax['1'].set_ylabel('count')
ax['2'].set_ylabel('count')
ax['3'].set_ylabel('count')
ax['4'].set_ylabel('count')
ax['1'].grid(True)
ax['2'].grid(True)
ax['3'].grid(True)
ax['4'].grid(True)
# plt.tight_layout()
plt.suptitle(r'Histograms for $\alpha_i$')
plt.savefig('mod2_discretization_m2.png', dpi=300)
plt.show()
return s_all, s_prob_df
def scenario_creator_qb(scenario_name, S = '', prob_df='' ):
"""
This function is necessary to solve model2 and obtain stochastic (TS) solution using the MPI-SSPY package
compatible with pyomo
"""
try:
mode_demand = S.loc[scenario_name].to_numpy()
except:
raise ValueError("Unrecognized scenario name")
model = create_model_qb(random_parameters = mode_demand)
sputils.attach_root_node(model, model.ObjCost_first_stage, [model.x])
model._mpisppy_probability = prob_df.loc[scenario_name].to_numpy()
return model
def scenario_creator_qc(scenario_name, S = '', prob_df='' ):
"""
This function is necessary to solve model2 and obtain stochastic (TS) solution using the MPI-SSPY package
compatible with pyomo
"""
try:
mode_demand = S.loc[scenario_name].to_numpy()
except:
raise ValueError("Unrecognized scenario name")
model = create_model_qc(random_parameters = mode_demand)
sputils.attach_root_node(model, model.ObjCost_first_stage, [model.x])
model._mpisppy_probability = prob_df.loc[scenario_name].to_numpy()
return model
# --------------------- Solving Wait-and-See Problem -----------------------------------------------
# Solving wait and see
def solve_ws_qa(demandmax=[],all_scenarios=pd.DataFrame(), create_model=ConcreteModel()):
x1_ws = []
x2_ws = []
x3_ws = []
x4_ws = []
total_investment_ws = []
total_operation_cost_ws = []
for s in all_scenarios.iterrows():
model = create_model(capacity_per_mode=[s[1][0], s[1][1], s[1][2]],capacity_max = demandmax)
SolverFactory('gurobi').solve(model)
x1_ws.append(model.x[0].value)
x2_ws.append(model.x[1].value)
x3_ws.append(model.x[2].value)
x4_ws.append(model.x[3].value)
total_investment_ws.append(value(model.ObjCost_first_stage))
total_operation_cost_ws.append(value(model.ObjCost_second_stage))
res_pd = pd.DataFrame(data={'Scenarios':all_scenarios.index ,
'Technology-1':x1_ws ,
'Technology-2':x2_ws ,
'Technology-3':x3_ws ,
'Technology-4':x4_ws ,
'Total Investment':total_investment_ws ,
'Total Operation Cost':total_operation_cost_ws ,
'Total objective cost':np.array(total_investment_ws) +
np.array(total_operation_cost_ws)})
res=[['Technology-1',np.mean(x1_ws)] ,
['Technology-2',np.mean(x2_ws)] ,
['Technology-3',np.mean(x3_ws)] ,
['Technology-4',np.mean(x4_ws)] ,
['Total Investment',np.mean(total_investment_ws)] ,
['Total Operation Cost',np.mean(total_operation_cost_ws)] ,
['Total objective cost (WS)', np.mean(total_operation_cost_ws)+np.mean(total_investment_ws)]]
print(res_pd)
print('------------------------- Average value of wait and see -------------------------------')
print(tabulate(res, headers =['Items', 'Average Values']))
def solve_ws_qb(all_scenarios=pd.DataFrame()):
x1_ws_qb = []
x2_ws_qb = []
x3_ws_qb = []
x4_ws_qb = []
x5_ws_qb = []
total_investment_ws_qb = []
total_operation_cost_ws_qb = []
for s in tqdm(all_scenarios.iterrows()):
model = create_model_qb(random_parameters=
[s[1][0], s[1][1], s[1][2],s[1][3], s[1][4], s[1][5], s[1][6]])
SolverFactory('gurobi').solve(model)
x1_ws_qb.append(model.x[0].value)
x2_ws_qb.append(model.x[1].value)
x3_ws_qb.append(model.x[2].value)
x4_ws_qb.append(model.x[3].value)
x5_ws_qb.append(model.x[4].value)
total_investment_ws_qb.append(value(model.ObjCost_first_stage))
total_operation_cost_ws_qb.append(value(model.ObjCost_second_stage))
res_pd = pd.DataFrame(data={'Scenarios':all_scenarios.index ,
'Technology-1':x1_ws_qb ,
'Technology-2':x2_ws_qb ,
'Technology-3':x3_ws_qb ,
'Technology-4':x4_ws_qb ,
'Technology-5':x5_ws_qb ,
'Total Investment':total_investment_ws_qb ,
'Total Operation Cost':total_operation_cost_ws_qb ,
'Total objective cost': np.array(total_investment_ws_qb)+
np.array(total_operation_cost_ws_qb)})
res=[['Technology-1',np.mean(x1_ws_qb)] ,
['Technology-2',np.mean(x2_ws_qb)] ,
['Technology-3',np.mean(x3_ws_qb)] ,
['Technology-4',np.mean(x4_ws_qb)] ,
['Technology-5',np.mean(x5_ws_qb)],
['Total Investment',np.mean(total_investment_ws_qb)] ,
['Total Operation Cost',np.mean(total_operation_cost_ws_qb)] ,
['Total objective cost (WS)', np.mean(total_operation_cost_ws_qb)+np.mean(total_investment_ws_qb)]]
print(res_pd)
print('------------------------- Average value of wait and see -------------------------------')
print(tabulate(res, headers =['Items', 'Average Values']))
def solve_ws_qc(all_scenarios=pd.DataFrame(), create_model=ConcreteModel()):
x1_ws_qc = []
x2_ws_qc = []
x3_ws_qc = []
x4_ws_qc = []
x5_ws_qc = []
total_investment_ws_qc = []
total_operation_cost_ws_qc = []
for s in tqdm(all_scenarios.iterrows()):
model = create_model(random_parameters=
[s[1][0], s[1][1], s[1][2],s[1][3], s[1][4], s[1][5], s[1][6], s[1][7], s[1][8]])
SolverFactory('gurobi').solve(model)
x1_ws_qc.append(model.x[0].value)
x2_ws_qc.append(model.x[1].value)
x3_ws_qc.append(model.x[2].value)
x4_ws_qc.append(model.x[3].value)
x5_ws_qc.append(model.x[4].value)
total_investment_ws_qc.append(value(model.ObjCost_first_stage))
total_operation_cost_ws_qc.append(value(model.ObjCost_second_stage))
res_pd = pd.DataFrame(data={'Scenarios':all_scenarios.index ,
'Technology-1':x1_ws_qc ,
'Technology-2':x2_ws_qc ,
'Technology-3':x3_ws_qc ,
'Technology-4':x4_ws_qc ,
'Technology-5':x5_ws_qc ,
'Total Investment':total_investment_ws_qc ,
'Total Operation Cost':total_operation_cost_ws_qc ,
'Total objective cost': np.array(total_investment_ws_qc)+
np.array(total_operation_cost_ws_qc)})
res=[['Technology-1',np.mean(x1_ws_qc)] ,
['Technology-2',np.mean(x2_ws_qc)] ,
['Technology-3',np.mean(x3_ws_qc)] ,
['Technology-4',np.mean(x4_ws_qc)] ,
['Technology-5',np.mean(x5_ws_qc)],
['Total Investment',np.mean(total_investment_ws_qc)] ,
['Total Operation Cost',np.mean(total_operation_cost_ws_qc)] ,
['Total objective cost (WS)', np.mean(total_operation_cost_ws_qc)+np.mean(total_investment_ws_qc)]]
print(res_pd)
print('------------------------- Average value of wait and see -------------------------------')
print(tabulate(res, headers =['Items', 'Average Values']))