-
Notifications
You must be signed in to change notification settings - Fork 1
/
Results.py
executable file
·1512 lines (1216 loc) · 74 KB
/
Results.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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
from pandas import ExcelWriter
from numpy import interp
import math
def Load_results1(instance,i,n,type_generator):
'''
This function loads the results that depend of the periods in to a dataframe and creates a excel file with it.
:param instance: The instance of the project resolution created by PYOMO.
:return: A dataframe called Time_series with the values of the variables that depend of the periods.
'''
path = 'Results/Results' + '_' + str(i) + '_' + str(n) + '.xls'
writer = ExcelWriter(path, engine='xlsxwriter')
# Load the variables that does not depend of the periods in python dyctionarys
Number_Scenarios = int(instance.Scenarios.extract_values()[None])
Number_Periods = int(instance.Periods.extract_values()[None])
Number_Renewable_Source = int(instance.Renewable_Source.extract_values()[None])
Number_Generator = int(instance.Generator_Type.extract_values()[None])
Renewable_Nominal_Capacity = instance.Renewable_Nominal_Capacity.extract_values()
Inverter_Efficiency_Renewable = instance.Renewable_Inverter_Efficiency.extract_values()
Renewable_Invesment_Cost = instance.Renewable_Invesment_Cost.extract_values()
OyM_Renewable = instance.Maintenance_Operation_Cost_Renewable.extract_values()
Renewable_Units = instance.Renewable_Units.get_values()
Fix_Invesment = instance.Fix_Invesment_PV.extract_values()
Integer_PV = instance.Integer_PV.get_values()
Data_Renewable = pd.DataFrame()
for r in range(1, Number_Renewable_Source + 1):
Name = 'Source ' + str(r)
Data_Renewable.loc['Units', Name] = Renewable_Units[r]
Data_Renewable.loc['Nominal Capacity (W)', Name] = Renewable_Nominal_Capacity[r]
Data_Renewable.loc['Inverter Efficiency', Name] = Inverter_Efficiency_Renewable[r]
Data_Renewable.loc['Investment Cost (USD/W)', Name] = Renewable_Invesment_Cost[r]
Data_Renewable.loc['OyM', Name] = OyM_Renewable[r]
Data_Renewable.loc['Fix invesment', Name] = Fix_Invesment[r]
Data_Renewable.loc['Investment Decision', Name] = Integer_PV[r]
Data_Renewable.loc['Invesment (USD)', Name] = Fix_Invesment[r]*Integer_PV[r] + Renewable_Units[r]*Renewable_Nominal_Capacity[r]*Renewable_Invesment_Cost[r]
Data_Renewable.loc['OyM Cost (USD)', Name] = Renewable_Units[r]*Renewable_Nominal_Capacity[r]*Renewable_Invesment_Cost[r]*OyM_Renewable[r]
Data_Renewable.loc['Total Nominal Capacity (W)', Name] = Data_Renewable.loc['Nominal Capacity (W)', Name]*Data_Renewable.loc['Units', Name]
Data_Renewable.to_excel(writer, sheet_name='Data Renewable')
columns = []
for i in range(1, Number_Scenarios+1):
columns.append('Scenario_'+str(i))
# Energy Time Series
Scenarios = pd.DataFrame()
Number = 7
if instance.Lost_Load_Probability > 0:
Lost_Load = instance.Lost_Load.get_values()
Number += 1
Renewable_Energy_1 = instance.Renewable_Energy_Production.extract_values()
Renewable_Units = instance.Renewable_Units.get_values()
Renewable_Energy = {}
for s in range(1, Number_Scenarios + 1):
for t in range(1, Number_Periods+1):
foo = []
for r in range(1,Number_Renewable_Source+1 ):
foo.append((s,r,t))
Renewable_Energy[s,t] = sum(Renewable_Energy_1[s,r,t]*Data_Renewable.loc['Inverter Efficiency', 'Source ' + str(r)]
*Data_Renewable.loc['Units', 'Source ' + str(r)] for s,r,t in foo)
Battery_Flow_Out = instance.Energy_Battery_Flow_Out.get_values()
Battery_Flow_in = instance.Energy_Battery_Flow_In.get_values()
Curtailment = instance.Energy_Curtailment.get_values()
Energy_Demand = instance.Energy_Demand.extract_values()
SOC = instance.State_Of_Charge_Battery.get_values()
Generator_Energy = instance.Generator_Energy.get_values()
Total_Generator_Energy = {}
for s in range(1, Number_Scenarios + 1):
for t in range(1, Number_Periods+1):
foo = []
for g in range(1,Number_Generator+1):
foo.append((s,g,t))
Total_Generator_Energy[s,t] = sum(Generator_Energy[i] for i in foo)
Scenarios_Periods = [[] for i in range(Number_Scenarios)]
for i in range(0,Number_Scenarios):
for j in range(1, Number_Periods+1):
Scenarios_Periods[i].append((i+1,j))
foo=0
for i in columns:
Information = [[] for i in range(Number)]
for j in Scenarios_Periods[foo]:
Information[0].append(Renewable_Energy[j])
Information[1].append(Battery_Flow_Out[j])
Information[2].append(Battery_Flow_in[j])
Information[3].append(Curtailment[j])
Information[4].append(Energy_Demand[j])
Information[5].append(SOC[j])
Information[6].append(Total_Generator_Energy[j])
if instance.Lost_Load_Probability > 0:
Information[7].append(Lost_Load[j])
Scenarios=Scenarios.append(Information)
foo+=1
index=[]
for j in range(1, Number_Scenarios+1):
index.append('Renewable Energy '+str(j) + ' (Wh)')
index.append('Battery Flow Out '+str(j) + ' (Wh)')
index.append('Battery Flow in '+str(j) + ' (Wh)')
index.append('Curtailment '+str(j) + ' (Wh)')
index.append('Energy Demand '+str(j) + ' (Wh)')
index.append('SOC '+str(j) + ' (Wh)')
index.append('Gen energy '+str(j) + ' (Wh)')
if instance.Lost_Load_Probability > 0:
index.append('Lost Load '+str(j) + ' (Wh)')
Scenarios.index= index
# Creation of an index starting in the 'model.StartDate' value with a frequency step equal to 'model.Delta_Time'
if instance.Delta_Time() >= 1 and type(instance.Delta_Time()) == type(1.0) : # if the step is in hours and minutes
foo = str(instance.Delta_Time()) # trasform the number into a string
hour = foo[0] # Extract the first character
minutes = str(int(float(foo[1:3])*60)) # Extrac the last two character
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(hour + 'h'+ minutes + 'min')) # Creation of an index with a start date and a frequency
elif instance.Delta_Time() >= 1 and type(instance.Delta_Time()) == type(1): # if the step is in hours
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(str(instance.Delta_Time()) + 'h')) # Creation of an index with a start date and a frequency
else: # if the step is in minutes
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(str(int(instance.Delta_Time()*60)) + 'min'))# Creation of an index with a start date and a frequency
Scenarios.columns = columns
Scenarios = Scenarios.transpose()
Scenarios.to_excel(writer, sheet_name='Time Series') # Creating an excel file with the values of the variables that are in function of the periods
columns = [] # arreglar varios columns
for i in range(1, Number_Scenarios+1):
columns.append('Scenario '+str(i))
Scenario_information =[[] for i in range(Number_Scenarios)]
Scenario_Weight = instance.Scenario_Weight.extract_values()
for i in range(1, Number_Scenarios+1):
Scenario_information[i-1].append(Scenario_Weight[i])
Scenario_Information = pd.DataFrame(Scenario_information,index=columns)
Scenario_Information.columns=['Scenario Weight']
Scenario_Information = Scenario_Information.transpose()
Scenario_Information.to_excel(writer, sheet_name='Scenario Information')
Renewable_Energy = pd.DataFrame()
for s in range(1, Number_Scenarios + 1):
for r in range(1, Number_Renewable_Source + 1):
column = 'Renewable ' + str(s) + ' ' + str(r) + ' (Wh)'
column2 = 'Renewable unit ' + str(s) + ' ' + str(r) + ' (Wh)'
Energy = []
Unit_Energy = []
for t in range(1, Number_Periods + 1):
Source = 'Source ' + str(r)
Energy.append(Renewable_Energy_1[s,r,t]*Data_Renewable.loc['Inverter Efficiency', Source]
*Data_Renewable.loc['Units', Source])
Unit_Energy.append(Renewable_Energy_1[s,r,t])
Renewable_Energy[column] = Energy
Renewable_Energy[column2] = Unit_Energy
Renewable_Energy.index = Scenarios.index
Renewable_Energy.to_excel(writer, sheet_name='Renewable Energy Time Series')
Generator_Data = pd.DataFrame()
if instance.formulation == 'LP':
Generator_Efficiency = instance.Generator_Efficiency.extract_values()
Low_Heating_Value = instance.Low_Heating_Value.extract_values()
Fuel_Cost = instance.Fuel_Cost.extract_values()
Generator_Invesment_Cost = instance.Generator_Invesment_Cost.extract_values()
Generator_Nominal_Capacity = instance.Generator_Nominal_Capacity.get_values()
Maintenance_Operation_Cost_Generator = instance.Maintenance_Operation_Cost_Generator.extract_values()
for g in range(1, Number_Generator + 1):
Name = 'Generator ' + str(g)
Generator_Data.loc['Generator Efficiency',Name] = Generator_Efficiency[g]
Generator_Data.loc['Low Heating Value (Wh/l)',Name] = Low_Heating_Value[g]
Generator_Data.loc['Fuel Cost (USD/l)',Name] = Fuel_Cost[g]
Generator_Data.loc['Generator Invesment Cost (USD/W)',Name] = Generator_Invesment_Cost[g]
Generator_Data.loc['Generator Nominal Capacity (W)',Name] = Generator_Nominal_Capacity[g]
Generator_Data.loc['OyM Generator', Name] = Maintenance_Operation_Cost_Generator[g]
Generator_Data.loc['Invesment Generator (USD)', Name] = Generator_Invesment_Cost[g]*Generator_Nominal_Capacity[g]
Generator_Data.loc['OyM Cost (USD)', Name] = Generator_Data.loc['Invesment Generator (USD)', Name]*Generator_Data.loc['OyM Generator', Name]
Generator_Data.loc['Marginal Cost (USD/Wh)', Name] = (Generator_Data.loc['Fuel Cost (USD/l)',Name]/
(Generator_Data.loc['Generator Efficiency',Name]*Generator_Data.loc['Low Heating Value (Wh/l)',Name]))
Generator_Data.loc['Marginal Cost (USD/Wh)', Name] = round(Generator_Data.loc['Marginal Cost (USD/Wh)', Name],3)
if instance.formulation == 'MILP':
Generator_Min_Out_Put = instance.Generator_Min_Out_Put.extract_values()
Generator_Efficiency = instance.Generator_Efficiency.extract_values()
Low_Heating_Value = instance.Low_Heating_Value.extract_values()
Fuel_Cost = instance.Fuel_Cost.extract_values()
Generator_Invesment_Cost = instance.Generator_Invesment_Cost.extract_values()
Cost_Increase = instance.Cost_Increase.extract_values()
Generator_Nominal_Capacity = instance.Generator_Nominal_Capacity.extract_values()
if type_generator == 'Fix':
Integer_generator = instance.Integer_generator
else:
Integer_generator = instance.Integer_generator.get_values()
Maintenance_Operation_Cost_Generator = instance.Maintenance_Operation_Cost_Generator.extract_values()
for g in range(1, Number_Generator + 1):
Name = 'Generator ' + str(g)
Generator_Data.loc['Generator Nominal Capacity (W)',Name] = Generator_Nominal_Capacity[g]
Generator_Data.loc['Generator Min Out Put',Name] = Generator_Min_Out_Put[g]
Generator_Data.loc['Generator Efficiency',Name] = Generator_Efficiency[g]
Generator_Data.loc['Low Heating Value (Wh/l)',Name] = Low_Heating_Value[g]
Generator_Data.loc['Fuel Cost (USD/l)',Name] = Fuel_Cost[g]
Generator_Data.loc['Generator Invesment Cost (USD/W)',Name] = Generator_Invesment_Cost[g]
Generator_Data.loc['Cost Increase',Name] = Cost_Increase[g]
M_1 = Fuel_Cost[g]/(Generator_Efficiency[g]*Low_Heating_Value[g])
M_1 = round(M_1,3)
Generator_Data.loc['Marginal cost Full load (USD/Wh)',Name] = M_1
Generator_Data.loc['Start Cost Generator (USD)',Name] = M_1*Generator_Nominal_Capacity[g]*Cost_Increase[g]
Generator_Data.loc['Start Cost Generator (USD)',Name] = round(Generator_Data.loc['Start Cost Generator (USD)',Name],3)
M_2 = (M_1*Generator_Nominal_Capacity[g]-Generator_Data.loc['Start Cost Generator (USD)',Name])/ \
Generator_Nominal_Capacity[g]
Generator_Data.loc['Marginal cost Partial load (USD/Wh)',Name] = round(M_2,3)
Generator_Data.loc['Number of Generators', Name] = Integer_generator[g]
Generator_Data.loc['Maintenance Operation Cost Generator', Name] = Maintenance_Operation_Cost_Generator[g]
Generator_Data.loc['Invesment Generator (USD)', Name] = (Generator_Nominal_Capacity[g]
*Integer_generator[g]*Generator_Invesment_Cost[g])
Generator_Data.loc['OyM Cost (USD)', Name] = (Generator_Nominal_Capacity[g]*Integer_generator[g]
*Generator_Invesment_Cost[g]
*Maintenance_Operation_Cost_Generator[g])
Generator_Data.to_excel(writer, sheet_name='Generator Data')
Project_Data = pd.Series()
Project_Data['Net Present Cost (USD)'] = instance.ObjectiveFuntion.expr()
Project_Data['Discount Rate'] = instance.Discount_Rate.value
Project_Data['Proyect Life Time (years)'] = instance.Years.value
Project_Data['Value of lost load (USD/Wh)'] = instance.Value_Of_Lost_Load.value
a = Project_Data['Discount Rate']*((1+Project_Data['Discount Rate'])**Project_Data['Proyect Life Time (years)'])
b = ((1 + Project_Data['Discount Rate'])**Project_Data['Proyect Life Time (years)']) - 1
Project_Data['Capital Recovery Factor'] = round(a/b,3)
if instance.Curtailment_Unitary_Cost > 0:
Project_Data['Curtailment Unitary Cost (USD/Wh)'] = instance.Curtailment_Unitary_Cost
Project_Data.to_excel(writer, sheet_name='Project Data')
Battery_Nominal_Capacity = instance.Battery_Nominal_Capacity.get_values()[None]
PriceBattery = instance.Battery_Invesment_Cost.value
Battery_Electronic_Invesmente_Cost = instance.Battery_Electronic_Invesmente_Cost.value
OM_Bat = instance.Maintenance_Operation_Cost_Battery.value
SOC_1 = instance.Battery_Initial_SOC.value
Ch_bat_eff = instance.Charge_Battery_Efficiency.value
Dis_bat_eff = instance.Discharge_Battery_Efficiency.value
Deep_of_Discharge = instance.Deep_of_Discharge.value
Battery_Cycles = instance.Battery_Cycles.value
Fix_Invesment_Battery = instance.Fix_Invesment_Battery.extract_values()[None]
Integer_Battery = instance.Integer_Battery.get_values()[None]
Unitary_Battery_Cost = PriceBattery - Battery_Electronic_Invesmente_Cost
Battery_Repostion_Cost = Unitary_Battery_Cost/(Battery_Cycles*2*(1-Deep_of_Discharge))
Battery_Repostion_Cost = round(Battery_Repostion_Cost, 3)
Battery_Data = pd.DataFrame()
Battery_Data.loc['Nominal Capacity (Wh)','Battery'] = Battery_Nominal_Capacity
Battery_Data.loc['Unitary Invesment Cost (USD/Wh)','Battery'] = PriceBattery
Battery_Data.loc['Unitary invesment cost electronic equipment (USD/Wh)','Battery'] = Battery_Electronic_Invesmente_Cost
Battery_Data.loc['OyM','Battery'] = OM_Bat
Battery_Data.loc['Initial State of Charge','Battery'] = SOC_1
Battery_Data.loc['Charge efficiency','Battery'] = Ch_bat_eff
Battery_Data.loc['Discharge efficiency','Battery'] = Dis_bat_eff
Battery_Data.loc['Deep of Discharge','Battery'] = Deep_of_Discharge
Battery_Data.loc['Battery Cycles','Battery'] = Battery_Cycles
Battery_Data.loc['Unitary Battery Reposition Cost (USD/Wh)','Battery'] = Battery_Repostion_Cost
Battery_Data.loc['Fix invesment','Battery'] = Fix_Invesment_Battery
Battery_Data.loc['Investment Decision','Battery'] = Integer_Battery
Battery_Data.loc['Invesment Cost (USD)','Battery'] = Fix_Invesment_Battery*Integer_Battery + Battery_Nominal_Capacity*PriceBattery
Battery_Data.loc['OyM Cost (USD)', 'Battery'] = Battery_Nominal_Capacity*PriceBattery*OM_Bat
Battery_Data.to_excel(writer, sheet_name='Battery Data')
Generator_Time_Series = pd.DataFrame()
if instance.formulation == 'LP':
for s in range(1, Number_Scenarios + 1):
for g in range(1, Number_Generator + 1):
column_1 = 'Energy Generator ' + str(s) + ' ' + str(g) + ' (Wh)'
column_2 = 'Fuel Cost ' + str(s) + ' ' + str(g) + ' (USD)'
Name = 'Generator ' + str(g)
for t in range(1, Number_Periods + 1):
Generator_Time_Series.loc[t,column_1] = Generator_Energy[s,g,t]
Generator_Time_Series.loc[t,column_2] = (Generator_Time_Series.loc[t,column_1]
*Generator_Data.loc['Marginal Cost (USD/Wh)', Name])
if instance.formulation == 'MILP':
Generator_Integer = instance.Generator_Energy_Integer.get_values()
for s in range(1, Number_Scenarios + 1):
for g in range(1, Number_Generator + 1):
column_1 = 'Energy Generator ' + str(s) + ' ' + str(g) + ' (Wh)'
column_2 = 'Integer Generator ' + str(s) + ' ' + str(g)
column_3 = 'Fuel Cost ' + str(s) + ' ' + str(g) + ' (USD)'
Name = 'Generator ' + str(g)
for t in range(1, Number_Periods + 1):
Generator_Time_Series.loc[t,column_1] = Generator_Energy[s,g,t]
Generator_Time_Series.loc[t,column_2] = Generator_Integer[s,g,t]
Generator_Time_Series.loc[t,column_3] = (Generator_Integer[s,g,t]*Generator_Data.loc['Start Cost Generator (USD)',Name]
+ Generator_Energy[s,g,t]*Generator_Data.loc['Marginal cost Partial load (USD/Wh)',Name] )
Generator_Time_Series.index = Scenarios.index
Generator_Time_Series.to_excel(writer, sheet_name='Generator Time Series')
Cost_Time_Series = pd.DataFrame()
for s in range(1, Number_Scenarios + 1):
if instance.Lost_Load_Probability > 0:
name_1 = 'Lost Load ' + str(s) + ' (Wh)'
name_1_1 = 'Lost Load ' + str(s) + ' (USD)'
name_2 = 'Battery Flow Out ' + str(s) + ' (Wh)'
name_2_1 = 'Battery Flow Out ' + str(s) + ' (USD)'
name_3 = 'Battery Flow in ' + str(s) + ' (Wh)'
name_3_1 = 'Battery Flow In ' + str(s) + ' (USD)'
name_4_1 = 'Generator Cost ' + str(s) + ' (USD)'
for t in Scenarios.index:
if instance.Lost_Load_Probability > 0:
Cost_Time_Series.loc[t,name_1_1] = Scenarios[name_1][t]*Project_Data['Value of lost load (USD/Wh)']
Cost_Time_Series.loc[t,name_2_1] = (Scenarios[name_2][t]
*Battery_Data.loc['Unitary Battery Reposition Cost (USD/Wh)','Battery'])
Cost_Time_Series.loc[t,name_3_1] = (Scenarios[name_3][t]
*Battery_Data.loc['Unitary Battery Reposition Cost (USD/Wh)','Battery'])
Fuel_Cost = 0
for g in range(1, Number_Generator + 1):
name_5 = 'Fuel Cost ' + str(s) + ' ' + str(g) + ' (USD)'
Fuel_Cost += Generator_Time_Series.loc[t,name_5]
Cost_Time_Series.loc[t,name_4_1] = Fuel_Cost
if instance.Curtailment_Unitary_Cost > 0:
name_6 = 'Curtailment ' + str(s) + ' (Wh)'
name_6_1 = 'Curtailment Cost ' + str(s) + ' (USD)'
Cost_Time_Series.loc[t,name_6_1] = (Scenarios[name_6][t]*Project_Data['Curtailment Unitary Cost (USD/Wh)'])
Cost_Time_Series.to_excel(writer, sheet_name='Cost Time Series')
Scenario_Cost = pd.DataFrame()
for s in range(1, Number_Scenarios + 1):
if instance.Lost_Load_Probability > 0:
name_1_1 = 'Lost Load ' + str(s) + ' (USD)'
name_1 ='Lost Load (USD)'
name_2_1 = 'Battery Flow Out ' + str(s) + ' (USD)'
name_2 = 'Battery Flow Out (USD)'
name_3_1 = 'Battery Flow In ' + str(s) + ' (USD)'
name_3 = 'Battery Flow In (USD)'
name_4_1 = 'Generator Cost ' + str(s) + ' (USD)'
name_4 = 'Generator Cost (USD)'
if instance.Curtailment_Unitary_Cost > 0:
name_6 = 'Curtailment ' + str(s) + ' (Wh)'
name_6_1 = 'Curtailment Cost ' + str(s) + ' (USD)'
name_5 = 'Scenario ' + str(s)
if instance.Lost_Load_Probability > 0:
Scenario_Cost.loc[name_1,name_5] = Cost_Time_Series[name_1_1].sum()
Scenario_Cost.loc[name_2,name_5] = Cost_Time_Series[name_2_1].sum()
Scenario_Cost.loc[name_3,name_5] = Cost_Time_Series[name_3_1].sum()
Scenario_Cost.loc[name_4,name_5] = Cost_Time_Series[name_4_1].sum()
if instance.Curtailment_Unitary_Cost > 0:
Scenario_Cost.loc[name_6,name_5] = Cost_Time_Series[name_6_1].sum()
gen_oym = 0
for g in range(1, Number_Generator + 1):
Name_2 = 'Generator ' + str(g)
gen_oym += Generator_Data.loc['OyM Cost (USD)', Name_2]
Scenario_Cost.loc['Gen OyM Cost (USD)',name_5] = gen_oym
renewable_energy_oym = 0
for r in range(1, Number_Renewable_Source + 1):
Name = 'Source ' + str(r)
renewable_energy_oym += Data_Renewable.loc['OyM Cost (USD)', Name]
Scenario_Cost.loc['PV OyM Cost (USD)',name_5] = renewable_energy_oym
Scenario_Cost.loc['Battery OyM Cost (USD)',name_5] = Battery_Data['Battery']['OyM Cost (USD)']
Scenario_Cost.loc['Operation Cost (USD)',name_5] = Scenario_Cost[name_5].sum()
Discount_rate = Project_Data['Discount Rate']
Years = int(Project_Data['Proyect Life Time (years)'])
Scenario_Cost.loc['OyM (USD)',name_5] = (Scenario_Cost.loc['Gen OyM Cost (USD)',name_5]
+Scenario_Cost.loc['PV OyM Cost (USD)',name_5]
+Scenario_Cost.loc['Battery OyM Cost (USD)',name_5])
Scenario_Cost.loc['Present Gen Cost (USD)',name_5] = Scenario_Cost.loc[name_4,name_5]/Project_Data['Capital Recovery Factor']
if instance.Lost_Load_Probability > 0:
Scenario_Cost.loc['Present Lost Load Cost (USD)',name_5] = Scenario_Cost.loc[name_1,name_5]/Project_Data['Capital Recovery Factor']
Scenario_Cost.loc['Present Bat Out Cost (USD)',name_5] = Scenario_Cost.loc[name_2,name_5]/Project_Data['Capital Recovery Factor']
Scenario_Cost.loc['Present Bat In Cost (USD)',name_5] = Scenario_Cost.loc[name_3,name_5]/Project_Data['Capital Recovery Factor']
Scenario_Cost.loc['Present Bat Reposition Cost (USD)',name_5] = (Scenario_Cost.loc[name_2,name_5] + Scenario_Cost.loc[name_3,name_5])/Project_Data['Capital Recovery Factor']
Scenario_Cost.loc['Present OyM Cost (USD)',name_5] = Scenario_Cost.loc['OyM (USD)',name_5]/Project_Data['Capital Recovery Factor']
Scenario_Cost.loc['Present Operation Cost (USD)',name_5] = Scenario_Cost[name_5]['Operation Cost (USD)']/Project_Data['Capital Recovery Factor']
Scenario_Cost.loc['Present Operation Cost Weighted (USD)',name_5] = (Scenario_Cost[name_5]['Present Operation Cost (USD)']
*Scenario_Information[name_5]['Scenario Weight'])
Scenario_Cost.to_excel(writer, sheet_name='Scenario Costs')
NPC = pd.DataFrame()
NPC.loc['Battery Invesment (USD)', 'Data'] = Battery_Data['Battery']['Invesment Cost (USD)']
gen_Invesment = 0
for g in range(1, Number_Generator + 1):
Name_2 = 'Generator ' + str(g)
gen_Invesment += Generator_Data.loc['Invesment Generator (USD)', Name_2]
NPC.loc['Generator Invesment Cost (USD)', 'Data'] = gen_Invesment
renewable_energy_invesment = 0
for r in range(1, Number_Renewable_Source + 1):
Name = 'Source ' + str(r)
renewable_energy_invesment += Data_Renewable.loc['Invesment (USD)', Name]
NPC.loc['Renewable Investment Cost (USD)', 'Data'] = renewable_energy_invesment
operation_cost = 0
for s in range(1, Number_Scenarios + 1):
name_1 = 'Scenario ' + str(s)
operation_cost += Scenario_Cost[name_1]['Present Operation Cost Weighted (USD)']
NPC.loc['Present Operation Cost Weighted (USD)', 'Data'] = operation_cost
NPC.loc['NPC (USD)', 'Data'] = NPC['Data'].sum()
z = round(NPC.loc['NPC (USD)', 'Data'],5) == round(instance.ObjectiveFuntion.expr(), 5)
print(z)
NPC.loc['NPC LP (USD)', 'Data'] = Project_Data['Net Present Cost (USD)']
NPC.loc['Invesment (USD)', 'Data'] = (NPC.loc['Battery Invesment (USD)', 'Data']
+ NPC.loc['Generator Invesment Cost (USD)', 'Data']
+ NPC.loc['Renewable Investment Cost (USD)', 'Data'])
Demand = pd.DataFrame()
NP_Demand = 0
for s in range(1, Number_Scenarios + 1):
a = 'Energy Demand ' + str(s) + ' (Wh)'
b = 'Scenario ' + str(s)
Demand.loc[a,'Total Demand (Wh)'] = sum(Scenarios[a][i] for i in Scenarios.index)
Demand.loc[a,'Present Demand (Wh)'] = sum((Demand.loc[a,'Total Demand (Wh)']/(1+Discount_rate)**i)
for i in range(1, Years+1))
Demand.loc[a,'Rate'] = Scenario_Information[b]['Scenario Weight']
Demand.loc[a,'Rated Demand (Wh)'] = Demand.loc[a,'Rate']*Demand.loc[a,'Present Demand (Wh)']
NP_Demand += Demand.loc[a,'Rated Demand (Wh)']
NPC.loc['LCOE (USD/kWh)', 'Data'] = (Project_Data['Net Present Cost (USD)']/NP_Demand)
NPC.loc['Status','Data'] = z
NPC.to_excel(writer, sheet_name='Results')
Data = []
Data.append(NPC)
Data.append(Scenario_Cost)
Data.append(Project_Data)
Data.append(Scenarios)
Data.append(Generator_Data)
Data.append(Scenario_Information)
Data.append(Data_Renewable)
Data.append(Battery_Data)
writer.save()
return Data
def Integer_Time_Series(instance,Scenarios, S, Data):
if S == 0:
S = instance.PlotScenario.value
Time_Series = pd.DataFrame(index=range(0,8760))
Time_Series.index = Scenarios.index
if instance.Lost_Load_Probability > 0:
Time_Series['Lost Load (Wh)'] = Scenarios['Lost Load ' + str(S) + ' (Wh)']
Time_Series['Renewable Energy (Wh)'] = Scenarios['Renewable Energy '+str(S) + ' (Wh)']
Time_Series['Discharge energy from the Battery (Wh)'] = Scenarios['Battery Flow Out ' + str(S) + ' (Wh)']
Time_Series['Charge energy to the Battery (Wh)'] = Scenarios['Battery Flow in '+str(S) + ' (Wh)']
Time_Series['Curtailment (Wh)'] = Scenarios['Curtailment '+str(S) + ' (Wh)']
Time_Series['Energy Demand (Wh)'] = Scenarios['Energy Demand '+str(S) + ' (Wh)']
Time_Series['State Of Charge Battery (Wh)'] = Scenarios['SOC '+str(S) + ' (Wh)']
Time_Series['Generator Energy (Wh)'] = Scenarios['Gen energy '+str(S) + ' (Wh)']
Renewable_Source = instance.Renewable_Source.value
if Renewable_Source > 1:
Renewable_Energy = pd.read_excel('Results/Results.xls',index_col=0,Header=None,
sheet_name='Renewable Energy Time Series')
for r in range(1,Renewable_Source+1):
name = 'Renewable ' + str(S) + ' ' + str(r) + ' (Wh)'
name_1 = 'Renewable ' + str(r) + ' (Wh)'
Time_Series[name_1] = Renewable_Energy[name]
return Time_Series
def Load_results1_binary(instance):
'''
This function loads the results that depend of the periods in to a
dataframe and creates a excel file with it.
:param instance: The instance of the project resolution created by PYOMO.
:return: A dataframe called Time_series with the values of the variables
that depend of the periods.
'''
# Creation of an index starting in the 'model.StartDate' value with a frequency step equal to 'model.Delta_Time'
Number_Scenarios = int(instance.Scenarios.extract_values()[None])
Number_Periods = int(instance.Periods.extract_values()[None])
#Scenarios = [[] for i in range(Number_Scenarios)]
columns = []
for i in range(1, Number_Scenarios+1):
columns.append('Scenario_'+str(i))
# columns=columns
Scenarios = pd.DataFrame()
Lost_Load = instance.Lost_Load.get_values()
PV_Energy = instance.Total_Energy_PV.get_values()
Battery_Flow_Out = instance.Energy_Battery_Flow_Out.get_values()
Battery_Flow_in = instance.Energy_Battery_Flow_In.get_values()
Curtailment = instance.Energy_Curtailment.get_values()
Energy_Demand = instance.Energy_Demand.extract_values()
SOC = instance.State_Of_Charge_Battery.get_values()
Gen_Energy_Integer = instance.Generator_Energy_Integer.get_values()
Gen_Energy_I = {}
for i in range(1,Number_Scenarios+1):
for j in range(1, Number_Periods+1):
Gen_Energy_I[i,j]=(Gen_Energy_Integer[i,j]*instance.Generator_Nominal_Capacity.extract_values()[None])
Last_Generator_Energy = instance.Last_Energy_Generator.get_values()
Total_Generator_Energy = instance.Generator_Total_Period_Energy.get_values()
Gen_cost = instance.Period_Total_Cost_Generator.get_values()
Scenarios_Periods = [[] for i in range(Number_Scenarios)]
for i in range(0,Number_Scenarios):
for j in range(1, Number_Periods+1):
Scenarios_Periods[i].append((i+1,j))
foo=0
for i in columns:
Information = [[] for i in range(11)]
for j in Scenarios_Periods[foo]:
Information[0].append(Lost_Load[j])
Information[1].append(PV_Energy[j])
Information[2].append(Battery_Flow_Out[j])
Information[3].append(Battery_Flow_in[j])
Information[4].append(Curtailment[j])
Information[5].append(Energy_Demand[j])
Information[6].append(SOC[j])
Information[7].append(Gen_Energy_I[j])
Information[8].append(Last_Generator_Energy[j])
Information[9].append(Total_Generator_Energy[j])
Information[10].append(Gen_cost[j])
Scenarios=Scenarios.append(Information)
foo+=1
index=[]
for j in range(1, Number_Scenarios+1):
index.append('Lost_Load '+str(j))
index.append('PV_Energy '+str(j))
index.append('Battery_Flow_Out '+str(j))
index.append('Battery_Flow_in '+str(j))
index.append('Curtailment '+str(j))
index.append('Energy_Demand '+str(j))
index.append('SOC '+str(j))
index.append('Gen energy Integer '+str(j))
index.append('Last Generator Energy '+str(j))
index.append('Total Generator Energy '+str(j))
index.append('Total Cost Generator'+str(j))
Scenarios.index= index
# Creation of an index starting in the 'model.StartDate' value with a frequency step equal to 'model.Delta_Time'
if instance.Delta_Time() >= 1 and type(instance.Delta_Time()) == type(1.0) : # if the step is in hours and minutes
foo = str(instance.Delta_Time()) # trasform the number into a string
hour = foo[0] # Extract the first character
minutes = str(int(float(foo[1:3])*60)) # Extrac the last two character
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(hour + 'h'+ minutes + 'min')) # Creation of an index with a start date and a frequency
elif instance.Delta_Time() >= 1 and type(instance.Delta_Time()) == type(1): # if the step is in hours
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(str(instance.Delta_Time()) + 'h')) # Creation of an index with a start date and a frequency
else: # if the step is in minutes
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(str(int(instance.Delta_Time()*60)) + 'min'))# Creation of an index with a start date and a frequency
Scenarios.columns = columns
Scenarios = Scenarios.transpose()
Scenarios.to_excel('Results/Time_Series.xls') # Creating an excel file with the values of the variables that are in function of the periods
columns = [] # arreglar varios columns
for i in range(1, Number_Scenarios+1):
columns.append('Scenario_'+str(i))
Scenario_information =[[] for i in range(Number_Scenarios)]
Scenario_NPC = instance.Scenario_Net_Present_Cost.get_values()
LoL_Cost = instance.Scenario_Lost_Load_Cost.get_values()
Scenario_Weight = instance.Scenario_Weight.extract_values()
Diesel_Cost = instance.Sceneario_Generator_Total_Cost.get_values()
for i in range(1, Number_Scenarios+1):
Scenario_information[i-1].append(Scenario_NPC[i])
Scenario_information[i-1].append(LoL_Cost[i])
Scenario_information[i-1].append(Scenario_Weight[i])
Scenario_information[i-1].append(Diesel_Cost[i])
Scenario_Information = pd.DataFrame(Scenario_information,index=columns)
Scenario_Information.columns=['Scenario NPC', 'LoL Cost','Scenario Weight', 'Diesel Cost']
Scenario_Information = Scenario_Information.transpose()
Scenario_Information.to_excel('Results/Scenario_Information.xls')
S = instance.PlotScenario.value
Time_Series = pd.DataFrame(index=range(0,8760))
Time_Series.index = Scenarios.index
Time_Series['Lost Load'] = Scenarios['Lost_Load '+str(S)]
Time_Series['Energy PV'] = Scenarios['PV_Energy '+str(S)]
Time_Series['Discharge energy from the Battery'] = Scenarios['Battery_Flow_Out '+str(S)]
Time_Series['Charge energy to the Battery'] = Scenarios['Battery_Flow_in '+str(S)]
Time_Series['Curtailment'] = Scenarios['Curtailment '+str(S)]
Time_Series['Energy_Demand'] = Scenarios['Energy_Demand '+str(S)]
Time_Series['State_Of_Charge_Battery'] = Scenarios['SOC '+str(S)]
Time_Series['Gen energy Integer'] = Scenarios['Gen energy Integer '+str(S)]
Time_Series['Last Generator Energy'] = Scenarios['Last Generator Energy ' +str(j)]
Time_Series['Energy Diesel'] = Scenarios['Total Generator Energy '+str(j)]
return Time_Series
def Load_results2_binary(instance):
'''
This function extracts the unidimensional variables into a data frame
and creates a excel file with this data
:param instance: The instance of the project resolution created by PYOMO.
:return: Data frame called Size_variables with the variables values.
'''
# Load the variables that doesnot depend of the periods in python dyctionarys
Amortizacion = instance.Cost_Financial.get_values()[None]
cb = instance.PV_Units.get_values()
cb = cb.values()
Size_PV=[list(cb)[0]*instance.PV_Nominal_Capacity.value]
Size_Bat = instance.Battery_Nominal_Capacity.get_values()[None]
Gen_cap = instance.Generator_Nominal_Capacity.value
Gen_Power = Gen_cap*instance.Integer_generator.get_values()[None]
NPC = instance.ObjectiveFuntion.expr()
Mge_1 = instance.Marginal_Cost_Generator_1.value
Start_Cost = instance.Start_Cost_Generator.value
Funded= instance.Porcentage_Funded.value
DiscountRate = instance.Discount_Rate.value
InterestRate = instance.Interest_Rate_Loan.value
PricePV = instance.PV_invesment_Cost.value
PriceBatery= instance.Battery_Invesment_Cost.value
PriceGenSet= instance.Generator_Invesment_Cost.value
OM = instance.Maintenance_Operation_Cost_PV.value
Years=instance.Years.value
VOLL= instance.Value_Of_Lost_Load.value
Mge_2 = instance.Marginal_Cost_Generator.value
data3 = [Amortizacion, Size_PV[0], Size_Bat, Gen_cap, Gen_Power,NPC,Mge_1, Mge_2 ,
Start_Cost, Funded,DiscountRate,InterestRate,PricePV,PriceBatery,
PriceGenSet,OM,Years,VOLL] # Loading the values to a numpy array
Size_variables = pd.DataFrame(data3,index=['Amortization', 'Size of the solar panels',
'Size of the Battery','Nominal Capacity Generator',
'Generator Install power','Net Present Cost',
'Marginal cost Full load',
'Marginal cost Partial load', 'Start Cost',
'Funded Porcentage', 'Discount Rate',
'Interest Rate','Precio PV', 'Precio Bateria',
'Precio GenSet','OyM', 'Project years','VOLL'])
Size_variables.to_excel('Results/Size.xls') # Creating an excel file with the values of the variables that does not depend of the periods
I_Inv = instance.Initial_Inversion.get_values()[None]
O_M = instance.Operation_Maintenance_Cost.get_values()[None]
Financial_Cost = instance.Total_Finalcial_Cost.get_values()[None]
Batt_Reposition = instance.Battery_Reposition_Cost.get_values()[None]
Data = [I_Inv, O_M, Financial_Cost,Batt_Reposition]
Value_costs = pd.DataFrame(Data, index=['Initial Inversion', 'O & M',
'Financial Cost', 'Battery reposition'])
Value_costs.to_excel('Results/Partial Costs.xls')
VOLL = instance.Scenario_Lost_Load_Cost.get_values()
Scenario_Generator_Cost = instance.Sceneario_Generator_Total_Cost.get_values()
NPC_Scenario = instance.Scenario_Net_Present_Cost.get_values()
columns = ['VOLL', 'Scenario Generator Cost', 'NPC Scenario']
scenarios= range(1,instance.Scenarios.extract_values()[None]+1)
Scenario_Costs = pd.DataFrame(columns=columns, index=scenarios)
for j in scenarios:
Scenario_Costs['VOLL'][j]= VOLL[j]
Scenario_Costs['Scenario Generator Cost'][j]= Scenario_Generator_Cost[j]
Scenario_Costs['NPC Scenario'][j]= NPC_Scenario[j]
Scenario_Costs.to_excel('Results/Scenario Cost.xls')
return Size_variables
def Load_results1_Dispatch(instance):
'''
This function loads the results that depend of the periods in to a
dataframe and creates a excel file with it.
:param instance: The instance of the project resolution created by PYOMO.
:return: A dataframe called Time_series with the values of the variables
that depend of the periods.
'''
Names = ['Lost_Load', 'PV_Energy', 'Battery_Flow_Out','Battery_Flow_in',
'Curtailment', 'Energy_Demand', 'SOC', 'Gen Int', 'Gen energy',
'Total Cost Generator']
Number_Periods = int(instance.Periods.extract_values()[None])
Time_Series = pd.DataFrame(columns= Names, index=range(1,Number_Periods+1))
Lost_Load = instance.Lost_Load.get_values()
PV_Energy = instance.Total_Energy_PV.extract_values()
Battery_Flow_Out = instance.Energy_Battery_Flow_Out.get_values()
Battery_Flow_in = instance.Energy_Battery_Flow_In.get_values()
Curtailment = instance.Energy_Curtailment.get_values()
Energy_Demand = instance.Energy_Demand.extract_values()
SOC = instance.State_Of_Charge_Battery.get_values()
Gen_Energy_Integer = instance.Generator_Energy_Integer.get_values()
Total_Generator_Energy = instance.Generator_Total_Period_Energy.get_values()
Gen_cost = instance.Period_Total_Cost_Generator.get_values()
for i in range(1,Number_Periods+1):
Time_Series['Lost_Load'][i] = Lost_Load[i]
Time_Series['PV_Energy'][i] = PV_Energy[i]
Time_Series['Battery_Flow_Out'][i] = Battery_Flow_Out[i]
Time_Series['Battery_Flow_in'][i] = Battery_Flow_in[i]
Time_Series['Curtailment'][i] = Curtailment[i]
Time_Series['Energy_Demand'][i] = Energy_Demand[i]
Time_Series['SOC'][i] = SOC[i]
Time_Series['Gen Int'][i] = Gen_Energy_Integer[i]
Time_Series['Gen energy'][i] = Total_Generator_Energy[i]
Time_Series['Total Cost Generator'][i] = Gen_cost[i]
# Creation of an index starting in the 'model.StartDate' value with a frequency step equal to 'model.Delta_Time'
if instance.Delta_Time() >= 1 and type(instance.Delta_Time()) == type(1.0) : # if the step is in hours and minutes
foo = str(instance.Delta_Time()) # trasform the number into a string
hour = foo[0] # Extract the first character
minutes = str(int(float(foo[1:3])*60)) # Extrac the last two character
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(hour + 'h'+ minutes + 'min')) # Creation of an index with a start date and a frequency
elif instance.Delta_Time() >= 1 and type(instance.Delta_Time()) == type(1): # if the step is in hours
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(str(instance.Delta_Time()) + 'h')) # Creation of an index with a start date and a frequency
else: # if the step is in minutes
columns = pd.DatetimeIndex(start=instance.StartDate(),
periods=instance.Periods(),
freq=(str(int(instance.Delta_Time()*60)) + 'min'))# Creation of an index with a start date and a frequency
Time_Series.index = columns
Time_Series.to_excel('Results/Time_Series.xls') # Creating an excel file with the values of the variables that are in function of the periods
Time_Series_2 = pd.DataFrame()
Time_Series_2['Lost Load'] = Time_Series['Lost_Load']
Time_Series_2['Renewable Energy'] = Time_Series['PV_Energy']
Time_Series_2['Discharge energy from the Battery'] = Time_Series['Battery_Flow_Out']
Time_Series_2['Charge energy to the Battery'] = Time_Series['Battery_Flow_in']
Time_Series_2['Curtailment'] = Time_Series['Curtailment']
Time_Series_2['Energy_Demand'] = Time_Series['Energy_Demand']
Time_Series_2['State_Of_Charge_Battery'] = Time_Series['SOC']
Time_Series_2['Energy Diesel'] = Time_Series['Gen energy']
Time_Series_2['Total Cost Generator'] = Time_Series['Total Cost Generator']
Time_Series_2.index = columns
return Time_Series_2
def Load_results2_Dispatch(instance):
'''
This function extracts the unidimensional variables into a data frame
and creates a excel file with this data
:param instance: The instance of the project resolution created by PYOMO.
:return: Data frame called Size_variables with the variables values.
'''
Data = []
# Load the variables that doesnot depend of the periods in python dyctionarys
Generator_Efficiency = instance.Generator_Efficiency.extract_values()
Generator_Min_Out_Put = instance.Generator_Min_Out_Put.extract_values()
Low_Heating_Value = instance.Low_Heating_Value.extract_values()
Fuel_Cost = instance.Diesel_Cost.extract_values()
Marginal_Cost_Generator_1 = instance.Marginal_Cost_Generator_1.extract_values()
Cost_Increase = instance.Cost_Increase.extract_values()
Generator_Nominal_Capacity = instance.Generator_Nominal_Capacity.extract_values()
Start_Cost_Generator = instance.Start_Cost_Generator.extract_values()
Marginal_Cost_Generator = instance.Marginal_Cost_Generator.extract_values()
Generator_Data = pd.DataFrame()
g = None
Name = 'Generator ' + str(1)
Generator_Data.loc['Generator Min Out Put',Name] = Generator_Min_Out_Put[g]
Generator_Data.loc['Generator Efficiency',Name] = Generator_Efficiency[g]
Generator_Data.loc['Low Heating Value',Name] = Low_Heating_Value[g]
Generator_Data.loc['Fuel Cost',Name] = Fuel_Cost[g]
Generator_Data.loc['Marginal cost Full load',Name] = Marginal_Cost_Generator_1[g]
Generator_Data.loc['Marginal cost Partial load',Name] = Marginal_Cost_Generator[g]
Generator_Data.loc['Cost Increase',Name] = Cost_Increase[g]
Generator_Data.loc['Generator Nominal Capacity',Name] = Generator_Nominal_Capacity[g]
Generator_Data.loc['Start Cost Generator',Name] = Start_Cost_Generator[g]
Data.append(Generator_Data)
Generator_Data.to_excel('Results/Generator_Data.xls')
Size_Bat = instance.Battery_Nominal_Capacity.extract_values()[None]
O_Cost = instance.ObjectiveFuntion.expr()
VOLL= instance.Value_Of_Lost_Load.value
Bat_ef_out = instance.Discharge_Battery_Efficiency.value
Bat_ef_in = instance.Charge_Battery_Efficiency.value
DoD = instance.Deep_of_Discharge.value
Inv_Cost_Bat = instance.Battery_Invesment_Cost.value
Inv_Cost_elec = instance.Battery_Electronic_Invesmente_Cost.value
Bat_Cycles = instance.Battery_Cycles.value
Bat_U_C = Inv_Cost_Bat - Inv_Cost_elec
Battery_Reposition_Cost= Bat_U_C/(Bat_Cycles*2*(1-DoD))
Number_Periods = int(instance.Periods.extract_values()[None])
data3 = [Size_Bat, O_Cost, VOLL, Bat_ef_out, Bat_ef_in, DoD,
Inv_Cost_Bat, Inv_Cost_elec, Bat_Cycles,
Battery_Reposition_Cost, Number_Periods] # Loading the values to a numpy array
Results = pd.DataFrame(data3,index = ['Size of the Battery',
'Operation Cost', 'VOLL',
'Battery efficiency discharge',
'Battery efficiency charge',
'Deep of discharge',
'Battery unitary invesment cost',
'Battery electronic unitary cost',
'Battery max cycles',
'Battery Reposition Cost',
'Number of periods'])
Results.to_excel('Results/Size.xls') # Creating an excel file with the values of the variables that does not depend of the periods
Data.append(Results)
return Data
def Dispatch_Economic_Analysis(Results,Time_Series):
Data = []
Generator_Data = Results[0]
Result = Results[1]
Time_Series_Economic = pd.DataFrame()
for t in Time_Series.index:
name_1 = "Fuel"
name_2 = "Discharge energy from the Battery"
name_3 = "Charge energy to the Battery"
name_4 = 'Battery Reposition Cost'
name_5 = 'Battery operation Cost'
name_6 = 'VOLL'
Power_Bat = Time_Series[name_2][t] + Time_Series[name_3][t]
Time_Series_Economic.loc[t,name_5] = Power_Bat*Result[0][name_4]
LL = Time_Series['Lost Load'][t]
Time_Series_Economic.loc[t,name_6] = LL*Result[0][name_6]
if Time_Series['Energy Diesel'][t] > 0.1:
a = Generator_Data['Generator 1']['Start Cost Generator']
b = Generator_Data['Generator 1']['Marginal cost Partial load']
Time_Series_Economic.loc[t,name_1]=a + b*Time_Series['Energy Diesel'][t]
else:
Time_Series_Economic.loc[t,name_1]= 0
Operation_Cost = Time_Series_Economic.sum()
Operation_Cost['Total Cost'] = Operation_Cost.sum()
Data.append(Time_Series_Economic)
Data.append(Operation_Cost)
return Data
def Plot_Energy_Total(instance, Time_Series, plot, Plot_Date, PlotTime):
'''
This function creates a plot of the dispatch of energy of a defined number of days.
:param instance: The instance of the project resolution created by PYOMO.
:param Time_series: The results of the optimization model that depend of the periods.
'''
if plot == 'No Average':
Periods_Day = 24/instance.Delta_Time() # periods in a day
foo = pd.DatetimeIndex(start=Plot_Date,periods=1,freq='1h')# Asign the start date of the graphic to a dumb variable
for x in range(0, instance.Periods()): # Find the position form wich the plot will start in the Time_Series dataframe
if foo == Time_Series.index[x]:
Start_Plot = x # asign the value of x to the position where the plot will start
End_Plot = Start_Plot + PlotTime*Periods_Day # Create the end of the plot position inside the time_series
Time_Series.index=range(1,(len(Time_Series)+1))
Plot_Data = Time_Series[Start_Plot:int(End_Plot)] # Extract the data between the start and end position from the Time_Series
columns = pd.DatetimeIndex(start=Plot_Date,
periods=PlotTime*Periods_Day,
freq=('1H'))
Plot_Data.index=columns
Plot_Data = Plot_Data.astype('float64')
Plot_Data = Plot_Data
Plot_Data['Charge energy to the Battery (Wh)'] = -Plot_Data['Charge energy to the Battery (Wh)']
Plot_Data = round(Plot_Data,2)
Fill = pd.DataFrame()
r = 'Renewable Energy (Wh)'
g = 'Generator Energy (Wh)'
c = 'Curtailment (Wh)'
c2 ='Curtailment min (Wh)'
b = 'Discharge energy from the Battery (Wh)'
d = 'Energy Demand (Wh)'
ch = 'Charge energy to the Battery (Wh)'
SOC = 'State Of Charge Battery (Wh)'
Renewable_Source = instance. Renewable_Source.value
for t in Plot_Data.index:
if (Plot_Data[r][t] > 0 and Plot_Data[g][t]>0):
curtailment = Plot_Data[c][t]
Fill.loc[t,r] = Plot_Data[r][t]
Fill.loc[t,g] = Fill[r][t] + Plot_Data[g][t]-curtailment
Fill.loc[t,c] = Fill[r][t] + Plot_Data[g][t]
Fill.loc[t,c2] = Fill.loc[t,g]
elif Plot_Data[r][t] > 0:
Fill.loc[t,r] = Plot_Data[r][t]-Plot_Data[c][t]
Fill.loc[t,g] = Fill[r][t] + Plot_Data[g][t]
Fill.loc[t,c] = Fill[r][t] + Plot_Data[g][t]+Plot_Data[c][t]
Fill.loc[t,c2] = Plot_Data[r][t]-Plot_Data[c][t]
elif Plot_Data[g][t] > 0:
Fill.loc[t,r] = 0
Fill.loc[t,g]= (Fill[r][t] + Plot_Data[g][t] - Plot_Data[c][t] )
Fill.loc[t,c] = Fill[r][t] + Plot_Data[g][t]