-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_competitive.py
More file actions
1716 lines (1380 loc) · 54.7 KB
/
model_competitive.py
File metadata and controls
1716 lines (1380 loc) · 54.7 KB
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 -*-
"""
Created on Wed Jan 22 08:49:32 2020
@author: Luke
"""
from __future__ import division
import os
import glob
from os.path import join
import pandas as pd
import numpy as np
import math
import time
from pyomo.environ import (
AbstractModel,
Set,
Param,
Var,
Expression,
Constraint,
Objective,
minimize,
maximize,
Boolean,
Reals,
Binary,
NonNegativeIntegers,
PositiveIntegers,
NonNegativeReals,
PercentFraction,
)
from pyomo.mpec import complements, Complementarity
"""
This is the formulation of the Pyomo optimization model.
It's in this script that we'll add new constraints and functionality to the model itself
"""
start_time = time.time()
cwd = os.getcwd()
dispatch_model = AbstractModel()
###########################
# ######## SETS ######### #
###########################
# sets are in ALLCAPS without spaces
# time
dispatch_model.TIMEPOINTS = Set(domain=PositiveIntegers, ordered=True)
# generators
dispatch_model.GENERATORS = Set(ordered=True)
# zones
dispatch_model.ZONES = Set(doc="study zones", ordered=True)
# lines
dispatch_model.TRANSMISSION_LINE = Set(doc="tx lines", ordered=True)
# generator bid segments (creates piecewise heat rate curve)
dispatch_model.GENERATORSEGMENTS = Set(ordered=True)
# storage resources
dispatch_model.STORAGE = Set(ordered=True)
# case indexing, needed for changing ownership index in EPEC
dispatch_model.CASE = Set(ordered=True)
###########################
# ####### PARAMS ######## #
###########################
# Params will be in lower case with underscores between words
# Params are separated below by their indexing
# time and zone-indexed params
dispatch_model.gross_load = Param(
dispatch_model.TIMEPOINTS, dispatch_model.ZONES, within=NonNegativeReals
)
dispatch_model.wind_cf = Param(
dispatch_model.TIMEPOINTS, dispatch_model.ZONES, within=NonNegativeReals
)
dispatch_model.solar_cf = Param(
dispatch_model.TIMEPOINTS, dispatch_model.ZONES, within=NonNegativeReals
)
# timepoint-indexed params
dispatch_model.reference_bus = Param(
dispatch_model.TIMEPOINTS, within=dispatch_model.ZONES
)
dispatch_model.reg_up_mw = Param(dispatch_model.TIMEPOINTS, within=NonNegativeReals)
dispatch_model.reg_down_mw = Param(dispatch_model.TIMEPOINTS, within=NonNegativeReals)
dispatch_model.flex_up_mw = Param(dispatch_model.TIMEPOINTS, within=NonNegativeReals)
dispatch_model.flex_down_mw = Param(dispatch_model.TIMEPOINTS, within=NonNegativeReals)
# zone-indexed params
dispatch_model.wind_cap = Param(dispatch_model.ZONES, within=NonNegativeReals)
dispatch_model.solar_cap = Param(dispatch_model.ZONES, within=NonNegativeReals)
dispatch_model.voltage_angle_max = Param(dispatch_model.ZONES, within=NonNegativeReals)
dispatch_model.voltage_angle_min = Param(dispatch_model.ZONES, within=Reals)
# generator-indexed params
dispatch_model.capacity = Param(dispatch_model.GENERATORS, within=NonNegativeReals)
dispatch_model.fuelcost = Param(dispatch_model.GENERATORS, within=NonNegativeReals)
dispatch_model.pmin = Param(dispatch_model.GENERATORS, within=NonNegativeReals)
dispatch_model.startcost = Param(dispatch_model.GENERATORS, within=NonNegativeReals)
dispatch_model.canspin = Param(dispatch_model.GENERATORS, within=Binary)
dispatch_model.cannonspin = Param(dispatch_model.GENERATORS, within=Binary)
dispatch_model.minup = Param(dispatch_model.GENERATORS, within=NonNegativeIntegers)
dispatch_model.mindown = Param(dispatch_model.GENERATORS, within=NonNegativeIntegers)
dispatch_model.noloadcost = Param(dispatch_model.GENERATORS, within=NonNegativeReals)
dispatch_model.ramp = Param(dispatch_model.GENERATORS, within=NonNegativeReals)
dispatch_model.tonneCO2perMWh = Param(
dispatch_model.GENERATORS, within=NonNegativeReals
)
dispatch_model.CO2price = Param(dispatch_model.GENERATORS, within=NonNegativeReals)
dispatch_model.CO2dollarsperMWh = Param(
dispatch_model.GENERATORS, within=NonNegativeReals
)
dispatch_model.zonelabel = Param(dispatch_model.GENERATORS, within=dispatch_model.ZONES)
dispatch_model.genco_index = Param(
dispatch_model.GENERATORS, within=NonNegativeIntegers, mutable=True
)
# storage-indexed params (will be subset from other generators)
dispatch_model.discharge_max = Param(dispatch_model.STORAGE, within=NonNegativeReals)
dispatch_model.charge_max = Param(dispatch_model.STORAGE, within=NonNegativeReals)
dispatch_model.soc_max = Param(dispatch_model.STORAGE, within=NonNegativeReals)
dispatch_model.discharge_eff = Param(dispatch_model.STORAGE, within=NonNegativeReals)
dispatch_model.charge_eff = Param(dispatch_model.STORAGE, within=NonNegativeReals)
dispatch_model.storage_zone_label = Param(
dispatch_model.STORAGE, within=dispatch_model.ZONES
)
dispatch_model.storage_index = Param(dispatch_model.STORAGE, within=NonNegativeIntegers)
# generator-indexed initialization params
# shouldn't be needed for current cases, but I used them in an old model that had commitment to pass
# previous day commitment decisions into case initialization
dispatch_model.commitinit = Param(dispatch_model.GENERATORS, within=Binary)
dispatch_model.upinit = Param(dispatch_model.GENERATORS, within=NonNegativeIntegers)
dispatch_model.downinit = Param(dispatch_model.GENERATORS, within=NonNegativeIntegers)
# time and zone-indexed params
dispatch_model.scheduled_available = Param(
dispatch_model.TIMEPOINTS, dispatch_model.GENERATORS, within=PercentFraction
)
dispatch_model.capacity_time = Param(
dispatch_model.TIMEPOINTS, dispatch_model.GENERATORS, within=NonNegativeReals
)
dispatch_model.fuel_cost_time = Param(
dispatch_model.TIMEPOINTS, dispatch_model.GENERATORS, within=NonNegativeReals
)
# transmission line indexed params
dispatch_model.susceptance = Param(
dispatch_model.TRANSMISSION_LINE, within=NonNegativeReals
)
# time and transmission line-indexed params
dispatch_model.transmission_from = Param(
dispatch_model.TIMEPOINTS,
dispatch_model.TRANSMISSION_LINE,
within=dispatch_model.ZONES,
)
dispatch_model.transmission_to = Param(
dispatch_model.TIMEPOINTS,
dispatch_model.TRANSMISSION_LINE,
within=dispatch_model.ZONES,
)
dispatch_model.transmission_from_capacity = Param(
dispatch_model.TIMEPOINTS, dispatch_model.TRANSMISSION_LINE, within=Reals
)
dispatch_model.transmission_to_capacity = Param(
dispatch_model.TIMEPOINTS, dispatch_model.TRANSMISSION_LINE, within=Reals
)
dispatch_model.hurdle_rate = Param(
dispatch_model.TIMEPOINTS, dispatch_model.TRANSMISSION_LINE, within=NonNegativeReals
)
# generator segment indexed params
dispatch_model.base_generator_segment_length = Param(
dispatch_model.GENERATORSEGMENTS, within=PercentFraction
)
# generator and generator segment-indexed params
dispatch_model.generator_segment_length = Param(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=PercentFraction,
)
dispatch_model.generator_marginal_cost = Param(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=NonNegativeReals,
)
dispatch_model.previous_offer = Param(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=NonNegativeReals,
)
dispatch_model.marginal_CO2 = Param(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=NonNegativeReals,
)
dispatch_model.CO2_damage = Param(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=NonNegativeReals,
)
# genco integer for the case. Defines which generators are owned by agent and bid competitively.
dispatch_model.genco = Param(dispatch_model.CASE, within=NonNegativeIntegers)
###########################
# ###### SUBSETS ####### #
###########################
def strategic_gens_init(model):
"""Subsets generators owned by strategic agent
Arguments:
model {Pyomo model} -- the Pyomo model instance
Returns:
the subset of generators owned by the strategic agent in the case
"""
strategic_gens = list()
for c in model.CASE:
for g in model.GENERATORS:
if model.genco_index[g] == model.genco[c]:
strategic_gens.append(g)
return strategic_gens
dispatch_model.STRATEGIC_GENERATORS = Set(
within=dispatch_model.GENERATORS, initialize=strategic_gens_init
) # implements strategic_gens_init()
def non_strategic_gens_init(model):
"""Subsets generators NOT owned by strategic agent
Arguments:
model {Pyomo model} -- the Pyomo model instance
Returns:
the subset of generators NOT owned by the strategic agent in the case
"""
non_strategic_gens = list()
for c in model.CASE:
for g in model.GENERATORS:
if model.genco_index[g] != model.genco[c]:
non_strategic_gens.append(g)
return non_strategic_gens
dispatch_model.NON_STRATEGIC_GENERATORS = Set(
within=dispatch_model.GENERATORS, initialize=non_strategic_gens_init
) # implements non_strategic_gens_init()
def strategic_storage_init(model):
strategic_storage = list()
for c in model.CASE:
for s in model.STORAGE:
if model.storage_index[s] == model.genco[c]:
strategic_storage.append(s)
return strategic_storage
dispatch_model.STRATEGIC_STORAGE = Set(
within=dispatch_model.STORAGE, initialize=strategic_storage_init
) # implements strategic_storage_init()
def non_strategic_storage_init(model):
non_strategic_storage = list()
for c in model.CASE:
for s in model.STORAGE:
if model.storage_index[s] != model.genco[c]:
non_strategic_storage.append(s)
return non_strategic_storage
dispatch_model.NON_STRATEGIC_STORAGE = Set(
within=dispatch_model.STORAGE, initialize=non_strategic_storage_init
) # implements non_strategic_storage_init()
###########################
# ######## VARS ######### #
###########################
# Vars will be CamelCase without underscore
dispatch_model.segmentdispatch = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=NonNegativeReals,
initialize=0,
bounds=(0, 1000),
)
dispatch_model.windgen = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
within=NonNegativeReals,
initialize=0,
)
dispatch_model.solargen = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
within=NonNegativeReals,
initialize=0,
)
dispatch_model.curtailment = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
within=NonNegativeReals,
initialize=0,
)
dispatch_model.transmit_power_MW = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.TRANSMISSION_LINE,
within=Reals,
initialize=0,
bounds=(-1000, 1000),
)
dispatch_model.voltage_angle = Var(
dispatch_model.TIMEPOINTS, dispatch_model.ZONES, within=Reals, initialize=0, bounds=(-1000, 1000),
)
# resource specific vars
dispatch_model.discharge = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.STORAGE,
within=NonNegativeReals,
bounds=(0, 1000),
initialize=0,
)
dispatch_model.charge = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.STORAGE,
within=NonNegativeReals,
bounds=(0, 1000),
initialize=0,
)
dispatch_model.soc = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.STORAGE,
within=NonNegativeReals,
initialize=0,
)
# should now be inactive because storage is linearized
# dispatch_model.storagebool = Var(
# dispatch_model.TIMEPOINTS, dispatch_model.STORAGE, within=Boolean, initialize=0
# )
dispatch_model.storagedispatch = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.STORAGE,
within=Reals,
bounds=(-1000, 1000),
initialize=0,
)
# the following vars can make problem integer when implemented
# for now relevant constraints are unimplemented, so there is no commitment
dispatch_model.commitment = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
within=NonNegativeReals,
bounds=(0, 1),
initialize=0,
)
dispatch_model.startup = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
within=NonNegativeReals,
bounds=(0, 1),
initialize=0,
)
dispatch_model.shutdown = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
within=NonNegativeReals,
bounds=(0, 1),
initialize=0,
)
# new vars for competitive version of model#
# duals of MO problem
# bounds help reduce feasible space for BigM method, but they should be high enough to not bind
dispatch_model.zonalprice = Var(
dispatch_model.TIMEPOINTS, dispatch_model.ZONES, within=Reals, initialize=0
) # this is zonal load balance dual
dispatch_model.gensegmentmaxdual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=NonNegativeReals,
initialize=0,
bounds=(0, 1000),
)
dispatch_model.gensegmentmindual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=NonNegativeReals,
initialize=0,
bounds=(0, 1000),
)
dispatch_model.transmissionmaxdual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.TRANSMISSION_LINE,
within=NonNegativeReals,
initialize=0,
bounds=(0, 1000),
)
dispatch_model.transmissionmindual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.TRANSMISSION_LINE,
within=NonNegativeReals,
initialize=0,
bounds=(0, 1000),
)
dispatch_model.storagemaxdual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.STORAGE,
within=NonNegativeReals,
initialize=0,
bounds=(0, 1000),
)
dispatch_model.storagemindual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.STORAGE,
within=NonNegativeReals,
initialize=0,
bounds=(0, 1000),
)
dispatch_model.rampmaxdual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
within=NonNegativeReals,
initialize=0,
)
dispatch_model.rampmindual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
within=NonNegativeReals,
initialize=0,
)
dispatch_model.curtailmentdual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
within=NonNegativeReals,
initialize=0,
)
dispatch_model.winddual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
within=NonNegativeReals,
initialize=0,
)
dispatch_model.voltageanglemaxdual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
bounds=(0, 1000),
initialize=0,
)
dispatch_model.voltageanglemindual = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
bounds=(0, 1000),
initialize=0,
)
# offer-related variables (since generators no longer just offer at marginal cost)
dispatch_model.gensegmentoffer = Var(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
within=Reals,
)
dispatch_model.storageoffer = Var(
dispatch_model.TIMEPOINTS, dispatch_model.STORAGE, within=Reals
)
###########################
# ##### EXPRESSIONS ##### #
###########################
# build additional params or variables we'd like to record based on other param or variable values
def GeneratorDispatchRule(model, t, g):
return sum(model.segmentdispatch[t, g, gs] for gs in model.GENERATORSEGMENTS)
dispatch_model.dispatch = Expression(
dispatch_model.TIMEPOINTS, dispatch_model.GENERATORS, rule=GeneratorDispatchRule
) # implement GeneratorDispatchRule
def GeneratorPminRule(model, t, g):
return model.dispatch[t, g] * model.commitment[t, g] * model.pmin[g]
# dispatch_model.gpmin = Expression(
# dispatch.TIMEPOINTS, dispatch_model.GENERATORS, rule=GeneratorPminRule
# )
def AvailableSegmentCapacityExpr(model, t, g, gs):
return (
model.generator_segment_length[t, g, gs]
* model.capacity_time[t, g]
* model.scheduled_available[t, g]
)
dispatch_model.availablesegmentcapacity = Expression(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
rule=AvailableSegmentCapacityExpr,
) # implement AvailableSegmentCapacityExpr()
def CO2EmittedExpr(model, t, g, gs):
return model.segmentdispatch[t, g, gs] * model.marginal_CO2[t, g, gs]
dispatch_model.CO2_emissions = Expression(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
rule=CO2EmittedExpr,
) # implement CO2EmittedExpr
###########################
# ##### CONSTRAINTS ##### #
###########################
## RENEWABLES CONSTRAINTS ##
# No special renewables constraints anymore. They're implemented as generators.
# This does mean unlike in the old model, curtailment isn't recorded
## STORAGE CONSTRAINTS ##
# additional constraints applied only to storage resources
def StorageDischargeRule(model, t, s):
"""If storage is discharging, amount must be less than max discharge capacity
Arguments:
model -- Pyomo model
t {int} -- timepoint index
s {str} -- storage resource index
"""
return model.discharge_max[s] >= model.discharge[t, s]
# return model.discharge_max[s]*model.storagebool[t,s] >= model.storagedispatch[t,s]
dispatch_model.StorageDischargeConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.STORAGE, rule=StorageDischargeRule
) # implements StorageDischargeConstraint
def StorageChargeRule(model, t, s):
"""If storage is charging, amount must be less than max charge capacity
Arguments:
model -- Pyomo model
t {int} -- timepoint index
s {str} -- storage resource index
"""
return model.charge_max[s] >= model.charge[t, s]
# return model.storagedispatch[t,s] >= -model.charge_max[s]*(1-model.storagebool[t,s])
dispatch_model.StorageChargeConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.STORAGE, rule=StorageChargeRule
) # implements StorageChargeConstraint
def StorageTightRule(model, t, s):
return (
model.charge_max[s] * model.discharge_max[s]
>= model.charge_max[s] * model.discharge[t, s]
+ model.discharge_max[s] * model.charge[t, s]
)
dispatch_model.StorageTightConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.STORAGE, rule=StorageTightRule
)
def StorageDispatchRule(model, t, s):
"""Storage must be either charging or discharging in dispatch, not both
Arguments:
model -- Pyomo model
t {int} -- timepoint index
s {str} -- storage resource index
"""
return model.storagedispatch[t, s] == model.discharge[t, s] - model.charge[t, s]
dispatch_model.StorageDispatchConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.STORAGE, rule=StorageDispatchRule
) # implements StorageDispatchConstraint
def SOCChangeRule(model, t, s):
"""State of charge of storage changes based on dispatch
this is where we should add roundtrip efficiency param when implemented
Arguments:
model -- Pyomo model
t {int} -- timepoint index
s {str} -- storage resource index
"""
if t == 1:
return (
model.soc[t, s]
== model.charge[t, s] * model.charge_eff[s]
- model.discharge[t, s] * model.discharge_eff[s]
) # start half charged?
# return model.soc[t,s] == -model.storagedispatch[t,s]
else:
return (
model.soc[t, s]
== model.soc[t - 1, s]
+ model.charge[t, s] * model.charge_eff[s]
- model.discharge[t, s] * model.discharge_eff[s]
)
# return model.soc[t,s] == model.soc[t-1,s] - model.storagedispatch[t,s]
dispatch_model.SOCChangeConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.STORAGE, rule=SOCChangeRule
) # implements SOCChangeConstraint
def SOCMaxRule(model, t, s):
"""Storage state of charge cannot exceed its max state of charge
Arguments:
model -- Pyomo model
t {int} -- timepoint index
s {str} -- storage resource index
"""
return model.soc_max[s] >= model.soc[t, s]
dispatch_model.SOCMaxConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.STORAGE, rule=SOCMaxRule
) # implements SOCMaxConstraint
def BindFinalSOCRule(model, s):
"""Storage state of charge in final timestep must be equal to user-defined final SOC value
I've input this to be 0 for now. This will at least make each day symmetric (0 initial and final SOC)
Arguments:
model -- Pyomo model
s {str} -- storage resource index
"""
return model.soc_max[s] * 0 == model.soc[model.TIMEPOINTS[-1], s]
dispatch_model.BindFinalSOCConstraint = Constraint(
dispatch_model.STORAGE, rule=BindFinalSOCRule
) # implements BindFinalSOCConstraint
def OneCycleRule(model, s):
return model.soc_max[s] >= sum(model.discharge[t, s] for t in model.TIMEPOINTS)
dispatch_model.OneCycleConstraint = Constraint(
dispatch_model.STORAGE, rule=OneCycleRule
) # implements BindFinalSOCConstraint
## TRANSMISSION LINES ##
# flow rules, implemented as DCOPF
# first are the from/to capacity rules from the old hub/spoke version of the model
def TxFromRule(model, t, line):
"""Real power flow on line must be greater than from capacity
(note from capacity is negative by convention in the model
Arguments:
model -- Pyomo model
t {int} -- timepoint index
line {str} -- transmission line index
"""
return model.transmit_power_MW[t, line] >= model.transmission_from_capacity[t, line]
dispatch_model.TxFromConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.TRANSMISSION_LINE, rule=TxFromRule
) # implements TxFromConstraint
def TxToRule(model, t, line):
"""Real power flow on line must be less than to capacity
(note to capacity is positive by convention in the model
Arguments:
model -- Pyomo model
t {int} -- timepoint index
line {str} -- transmission line index
"""
return model.transmission_to_capacity[t, line] >= model.transmit_power_MW[t, line]
dispatch_model.TxToConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.TRANSMISSION_LINE, rule=TxToRule
) # implements TxToConstraint
# then the dcopf rules
# first, bound voltage angle above and below
def VoltageAngleMaxRule(model, t, z):
"""Bus voltage angle must be less than max bus voltage angle
Arguments:
model -- Pyomo model
t {int} -- timepoint index
z {int} -- bus index (z by convention holdover from old zonal model)
"""
return model.voltage_angle_max[z] >= model.voltage_angle[t, z]
dispatch_model.VoltageAngleMaxConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.ZONES, rule=VoltageAngleMaxRule
) # implements VoltageAngleMaxConstraint
def VoltageAngleMinRule(model, t, z):
"""Bus voltage angle must be greater than min bus voltage angle
Arguments:
model -- Pyomo model
t {int} -- timepoint index
z {int} -- bus index (z by convention holdover from old zonal model)
"""
return model.voltage_angle[t, z] >= model.voltage_angle_min[z]
dispatch_model.VoltageAngleMinConstraint = Constraint(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
rule=VoltageAngleMinRule,
name="VoltageAngleMin",
) # implements VoltageAngleMaxConstraint
# then set the reference bus
def SetReferenceBusRule(model, t, z):
"""Binds voltage angle of the system reference bus to be 0
Arguments:
model -- Pyomo model
t {int} -- timepoint index
z {int} -- bus index (z by convention holdover from old zonal model)
"""
if z == model.reference_bus[t]:
return model.voltage_angle[t, z] == 0
else:
return Constraint.Skip
dispatch_model.SetReferenceBusConstraint = Constraint(
dispatch_model.TIMEPOINTS,
dispatch_model.ZONES,
rule=SetReferenceBusRule,
name="RefBus",
) # implements SetReferenceBusConstraint
# then, bind transmission flows between lines based on voltage angle
def DCOPFRule(model, t, line):
"""Power flow defined by angle between buses and line susceptance
Arguments:
model -- Pyomo model
t {int} -- timepoint index
z {int} -- bus index (z by convention holdover from old zonal model)
"""
zone_to = model.transmission_to[t, line]
zone_from = model.transmission_from[t, line]
return model.transmit_power_MW[t, line] == model.susceptance[line] * (
model.voltage_angle[t, zone_to] - model.voltage_angle[t, zone_from]
)
dispatch_model.DCOPFConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.TRANSMISSION_LINE, rule=DCOPFRule
) # implements DCOPFConstraint
## LOAD BALANCE ##
# load/gen balance at all buses
def LoadRule(model, t, z):
"""It's long but does what it sounds like: load must be served at all buses in the system
so the sum of generation at, storage at, and transmission flows into the bus
again, some things get "zonal" or "z" labels/indices based on old zonal model
Arguments:
model -- Pyomo model
t {int} -- timepoint index
z {int} -- bus index (z by convention holdover from old zonal model)
"""
# implement total tx flow
imports_exports = 0
zonal_generation = 0
zonal_storage = 0
for line in model.TRANSMISSION_LINE:
if model.transmission_to[t, line] == z or model.transmission_from[t, line] == z:
if model.transmission_to[t, line] == z:
imports_exports += model.transmit_power_MW[t, line]
elif model.transmission_from[t, line] == z:
imports_exports -= model.transmit_power_MW[t, line]
# add additional note to dec import/exports by line losses
# no, this will just be done as a hurdle rate
for g in model.GENERATORS:
if model.zonelabel[g] == z:
zonal_generation += sum(
model.segmentdispatch[t, g, gs] for gs in model.GENERATORSEGMENTS
)
for s in model.STORAGE:
if model.storage_zone_label[s] == z:
# zonal_storage += model.discharge[t,s]
# zonal_storage -= model.charge[t,s]
zonal_storage += model.storagedispatch[t, s]
# full constraint, with tx flow now
# (sum(sum(model.segmentdispatch[t,g,z,gs] for gs in model.GENERATORSEGMENTS) for g in model.GENERATORS)+\
return zonal_generation + imports_exports + zonal_storage == model.gross_load[t, z]
dispatch_model.LoadConstraint = Constraint(
dispatch_model.TIMEPOINTS, dispatch_model.ZONES, rule=LoadRule
) # implements load constraint
## CONVENTIONAL GENERATORS CONSTRAINTS ##
# gen capacity with scheduled outage factored in: INACTIVE
def CapacityMaxRule(model, t, g, z):
""" Generator dispatch cannot exceed (available) capacity
this is actually disabled by defaul right now since it's implement by bid segment
update docstring if enabled
Arguments:
model -- Pyomo model
t {int} -- timepoint index
g {str} -- generator index
z {int} -- bus index (z by convention holdover from old zonal model)
"""
return (
model.capacity[g] * model.commitment[t, g] * model.scheduled_available[t, g]
>= model.dispatch[t, g, z]
)
# dispatch_model.CapacityMaxConstraint = Constraint(dispatch_model.TIMEPOINTS, dispatch_model.GENERATORS, dispatch_model.ZONES, rule=CapacityMaxRule)
# pmin: INACTIVE
def PminRule(model, t, g, z):
""" Generator dispatch must be above minimum stable level if generator is dispatched
this is actually disabled by default right now since I linearized this version of the model
update docstring if enabled
Arguments:
model -- Pyomo model
t {int} -- timepoint index
g {str} -- generator index
z {int} -- bus index (z by convention holdover from old zonal model)
"""
return (
model.dispatch[t, g, z]
>= model.capacity[g]
* model.commitment[t, g]
* model.scheduled_available[t, g]
* model.pmin[g]
)
# dispatch_model.PminConstraint = Constraint(dispatch_model.TIMEPOINTS, dispatch_model.GENERATORS, dispatch_model.ZONES, rule=PminRule)
### GENERATOR SEGMENT DISPATCH ###
# basically I reimplemented most generator constraints segment-wise, below
# this allows generators to bid multiple segments and have a heat rate curve
# max on segment
def GeneratorSegmentDispatchMax(model, t, g, gs):
""" Generator segment dispatch cannot exceed (available) capacity on segment
Arguments:
model -- Pyomo model
t {int} -- timepoint index
g {str} -- generator index
gs {int} -- generator segment index
"""
return model.availablesegmentcapacity[t, g, gs] >= model.segmentdispatch[t, g, gs]
dispatch_model.GeneratorSegmentMaxConstraint = Constraint(
dispatch_model.TIMEPOINTS,
dispatch_model.GENERATORS,
dispatch_model.GENERATORSEGMENTS,
rule=GeneratorSegmentDispatchMax,
) # implements GeneratorSegmentMaxConstraint
### GENERATOR RAMP ###
# these are currently inactive but may be a helfpul template for implementing TRUC
def GeneratorRampUpRule(model, t, g):
""" Increase in generator dispatch between timepoints cannot exceed upward ramp rate
Note this isn't implemented in the first timepoint, so any initialization is allowed
There are a couple ways around this, one of the more common/simple is looping the day back on itself
Arguments:
model -- Pyomo model
t {int} -- timepoint index
g {str} -- generator index
"""
if t == 1:
return Constraint.Skip
else:
return (
model.dispatch[t - 1, g] + model.ramp[g] * model.commitment[t - 1, g]
>= model.dispatch[t, g]
)
# dispatch_model.GeneratorRampUpConstraint = Constraint(dispatch_model.TIMEPOINTS, dispatch_model.GENERATORS, rule=GeneratorRampUpRule)
def GeneratorRampDownRule(model, t, g):
""" Decrease in generator dispatch between timepoints cannot exceed downward ramp rate
Note this isn't implemented in the first timepoint, so any initialization is allowed
There are a couple ways around this, one of the more common/simple is looping the day back on itself
Arguments:
model -- Pyomo model
t {int} -- timepoint index
g {str} -- generator index
"""
if t == 1:
return Constraint.Skip
else:
return model.dispatch[t, g] >= model.dispatch[t - 1, g] - model.ramp[g]
# dispatch_model.GeneratorRampDownConstraint = Constraint(dispatch_model.TIMEPOINTS, dispatch_model.GENERATORS, rule=GeneratorRampDownRule)