Newer
Older
Pedro L. Magalhães
committed
import pytest
import pyomo.environ as pyo
# import src.topupopt.problems.esipp.utils as utils
from src.topupopt.data.misc.utils import generate_pseudo_unique_key
from src.topupopt.problems.esipp.problem import InfrastructurePlanningProblem
from src.topupopt.problems.esipp.network import Arcs, Network
from src.topupopt.problems.esipp.network import ArcsWithoutStaticLosses
from src.topupopt.problems.esipp.resource import ResourcePrice
# from src.topupopt.problems.esipp.utils import compute_cost_volume_metrics
from src.topupopt.problems.esipp.utils import statistics
from src.topupopt.problems.esipp.time import EconomicTimeFrame
# from src.topupopt.problems.esipp.converter import Converter
Pedro L. Magalhães
committed
from test_esipp import build_solve_ipp, check_problem_size
# *****************************************************************************
# *****************************************************************************
class TestESIPPProblem:
# *************************************************************************
# *************************************************************************
Pedro L. Magalhães
committed
@pytest.mark.parametrize(
"solver, use_sos_arcs, arc_sos_weight_key",
[('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_CAP),
('scip', False, None)]
Pedro L. Magalhães
committed
)
def test_single_network_single_arc_problem(self, solver, use_sos_arcs, arc_sos_weight_key):
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
# assessment
q = 0
tf = EconomicTimeFrame(
discount_rate=3.5/100,
reporting_periods={q: (0, 1)},
reporting_period_durations={q: (365 * 24 * 3600, 365 * 24 * 3600)},
time_intervals={q: (0, 1, 2)},
time_interval_durations={q: (1, 1, 1)},
)
# 2 nodes: one import, one regular
mynet = Network()
# import node
node_IMP = generate_pseudo_unique_key(mynet.nodes())
mynet.add_import_node(
node_key=node_IMP,
prices={
qpk: ResourcePrice(prices=1.0, volumes=None)
for qpk in tf.qpk()
},
)
# other nodes
node_A = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_A,
base_flow={(q, 0): 0.50, (q, 1): 0.00, (q, 2): 1.00},
)
# arc IA
arc_tech_IA = Arcs(
name="any",
efficiency={qk: 0.5 for qk in tf.qk()},
efficiency_reverse=None,
static_loss=None,
capacity=[3],
minimum_cost=[2],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
validate=False,
)
mynet.add_directed_arc(node_key_a=node_IMP, node_key_b=node_A, arcs=arc_tech_IA)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
Pedro L. Magalhães
committed
use_sos_arcs=use_sos_arcs,
arc_sos_weight_key=arc_sos_weight_key,
perform_analysis=False,
plot_results=False, # True,
print_solver_output=False,
time_frame=tf,
networks={"mynet": mynet},
static_losses_mode=True, # just to reach a line,
mandatory_arcs=[],
max_number_parallel_arcs={},
simplify_problem=False,
)
# *********************************************************************
# *********************************************************************
# validation
assert len(ipp.instance.constr_arc_sos1) == 0
assert ipp.has_peak_total_assessments()
# assert ipp.results["Problem"][0]["Number of constraints"] == 24
# assert ipp.results["Problem"][0]["Number of variables"] == 22
# assert ipp.results["Problem"][0]["Number of nonzeros"] == 49
# check_problem_size(ipp, 24, 22, 49)
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
# the arc should be installed since it is required for feasibility
assert (
True
in ipp.networks["mynet"]
.edges[(node_IMP, node_A, 0)][Network.KEY_ARC_TECH]
.options_selected
)
# the flows should be 1.0, 0.0 and 2.0
assert math.isclose(
pyo.value(ipp.instance.var_v_glljqk[("mynet", node_IMP, node_A, 0, q, 0)]),
1.0,
abs_tol=1e-6,
)
assert math.isclose(
pyo.value(ipp.instance.var_v_glljqk[("mynet", node_IMP, node_A, 0, q, 1)]),
0.0,
abs_tol=1e-6,
)
assert math.isclose(
pyo.value(ipp.instance.var_v_glljqk[("mynet", node_IMP, node_A, 0, q, 2)]),
2.0,
abs_tol=1e-6,
)
# arc amplitude should be two
assert math.isclose(
pyo.value(ipp.instance.var_v_amp_gllj[("mynet", node_IMP, node_A, 0)]),
2.0,
abs_tol=0.01,
)
# capex should be four
assert math.isclose(pyo.value(ipp.instance.var_capex), 4.0, abs_tol=1e-3)
# sdncf should be -5.7
assert math.isclose(pyo.value(ipp.instance.var_sdncf_q[q]), -5.7, abs_tol=1e-3)
# the objective function should be -9.7
assert math.isclose(pyo.value(ipp.instance.obj_f), -9.7, abs_tol=1e-3)
# *************************************************************************
# *************************************************************************
Pedro L. Magalhães
committed
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
def test_single_network_two_arcs_problem(self):
# TODO: test simplifying this problem
# assessment
q = 0
tf = EconomicTimeFrame(
discount_rate=3.5/100,
reporting_periods={q: (0, 1)},
reporting_period_durations={q: (365 * 24 * 3600, 365 * 24 * 3600)},
time_intervals={q: (0, 1, 2)},
time_interval_durations={q: (1, 1, 1)},
)
# 2 nodes: one import, one regular
mynet = Network()
# import node
node_IMP = generate_pseudo_unique_key(mynet.nodes())
mynet.add_import_node(
node_key=node_IMP,
prices={
qpk: ResourcePrice(prices=1.0, volumes=None)
for qpk in tf.qpk()
}
)
# export node
node_EXP = generate_pseudo_unique_key(mynet.nodes())
mynet.add_export_node(
node_key=node_EXP,
prices={
qpk: ResourcePrice(prices=0.5, volumes=None)
for qpk in tf.qpk()
}
)
# other nodes
node_A = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_A,
base_flow={(q, 0): 0.50, (q, 1): -1.50, (q, 2): 1.00},
)
# arc IA
arc_tech_IA = Arcs(
name="any",
efficiency={qk: 0.5 for qk in tf.qk()},
efficiency_reverse=None,
static_loss=None,
capacity=[3],
minimum_cost=[2],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
validate=False,
)
mynet.add_directed_arc(node_key_a=node_IMP, node_key_b=node_A, arcs=arc_tech_IA)
# arc AE
arc_tech_AE = Arcs(
name="any",
efficiency={qk: 0.8 for qk in tf.qk()},
efficiency_reverse=None,
static_loss=None,
capacity=[3],
minimum_cost=[2],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
validate=False,
)
mynet.add_directed_arc(node_key_a=node_A, node_key_b=node_EXP, arcs=arc_tech_AE)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
Pedro L. Magalhães
committed
# use_sos_arcs=use_sos_arcs,
# arc_sos_weight_key=arc_sos_weight_key,
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
perform_analysis=False,
plot_results=False, # True,
print_solver_output=False,
time_frame=tf,
networks={"mynet": mynet},
static_losses_mode=True, # just to reach a line,
mandatory_arcs=[],
max_number_parallel_arcs={},
simplify_problem=False,
)
assert ipp.has_peak_total_assessments()
assert ipp.results["Problem"][0]["Number of constraints"] == 42
assert ipp.results["Problem"][0]["Number of variables"] == 40
assert ipp.results["Problem"][0]["Number of nonzeros"] == 95
# *********************************************************************
# *********************************************************************
# validation
# the arc should be installed since it is required for feasibility
assert (
True
in ipp.networks["mynet"]
.edges[(node_IMP, node_A, 0)][Network.KEY_ARC_TECH]
.options_selected
)
# flows
true_v_glljqk = {
("mynet", node_IMP, node_A, 0, q, 0): 1,
("mynet", node_IMP, node_A, 0, q, 1): 0,
("mynet", node_IMP, node_A, 0, q, 2): 2,
("mynet", node_A, node_EXP, 0, q, 0): 0,
("mynet", node_A, node_EXP, 0, q, 1): 1.5,
("mynet", node_A, node_EXP, 0, q, 2): 0
}
for key, v in true_v_glljqk.items():
assert math.isclose(pyo.value(ipp.instance.var_v_glljqk[key]), v, abs_tol=1e-6)
# arc amplitude should be two
assert math.isclose(
pyo.value(ipp.instance.var_v_amp_gllj[("mynet", node_IMP, node_A, 0)]),
2.0,
abs_tol=0.01,
)
assert math.isclose(
pyo.value(ipp.instance.var_v_amp_gllj[("mynet", node_A, node_EXP, 0)]),
1.5,
abs_tol=0.01,
)
# capex should be four
assert math.isclose(pyo.value(ipp.instance.var_capex), 7.5, abs_tol=1e-3)
# sdncf should be -5.7+0.6*(0.966+0.934)
assert math.isclose(pyo.value(ipp.instance.var_sdncf_q[q]), -5.7+0.6*(0.966+0.934), abs_tol=1e-3)
# the objective function should be -9.7+0.6*(0.966+0.934)
assert math.isclose(pyo.value(ipp.instance.obj_f), -9.7+0.6*(0.966+0.934)-3.5, abs_tol=1e-3)
# *************************************************************************
# *************************************************************************
Pedro L. Magalhães
committed
@pytest.mark.parametrize(
"solver, use_sos_arcs, arc_sos_weight_key",
[('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_COST),
('scip', False, None)]
Pedro L. Magalhães
committed
)
def test_single_network_single_arc_problem_simpler(self, solver, use_sos_arcs, arc_sos_weight_key):
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
# assessment
q = 0
tf = EconomicTimeFrame(
discount_rate=3.5/100,
reporting_periods={q: (0, 1)},
reporting_period_durations={q: (365 * 24 * 3600, 365 * 24 * 3600)},
time_intervals={q: (0, 1, 2)},
time_interval_durations={q: (1, 1, 1)},
)
# 2 nodes: one import, one regular
mynet = Network()
# import node
node_IMP = "thatimpnode"
mynet.add_import_node(
node_key=node_IMP,
prices={
qpk: ResourcePrice(prices=1.0, volumes=None)
for qpk in tf.qpk()
},
)
# other nodes
node_A = "thatnodea"
mynet.add_source_sink_node(
node_key=node_A,
# base_flow=[0.5, 0.0, 1.0],
base_flow={(q, 0): 0.50, (q, 1): 0.00, (q, 2): 1.00},
)
# arc IA
arc_tech_IA = Arcs(
name="any",
efficiency={qk: 0.5 for qk in tf.qk()},
efficiency_reverse=None,
static_loss=None,
capacity=[3],
minimum_cost=[2],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
validate=False,
)
mynet.add_directed_arc(node_key_a=node_IMP, node_key_b=node_A, arcs=arc_tech_IA)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
solver_options={},
perform_analysis=False,
plot_results=False, # True,
print_solver_output=False,
time_frame=tf,
networks={"mynet": mynet},
static_losses_mode=True, # just to reach a line,
mandatory_arcs=[],
max_number_parallel_arcs={},
simplify_problem=True,
)
Pedro L. Magalhães
committed
# validation
assert len(ipp.instance.constr_arc_sos1) == 0
assert ipp.has_peak_total_assessments()
# assert ipp.results["Problem"][0]["Number of constraints"] == 16 # 20
# assert ipp.results["Problem"][0]["Number of variables"] == 15 # 19
# assert ipp.results["Problem"][0]["Number of nonzeros"] == 28 # 36
# check_problem_size(ipp, 16, 15, 28)
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
# *********************************************************************
# *********************************************************************
# validation
# the arc should be installed since it is required for feasibility
assert (
True
in ipp.networks["mynet"]
.edges[(node_IMP, node_A, 0)][Network.KEY_ARC_TECH]
.options_selected
)
# capex should be four
assert math.isclose(pyo.value(ipp.instance.var_capex), 4.0, abs_tol=1e-3)
# the objective function should be -9.7
assert math.isclose(pyo.value(ipp.instance.obj_f), -9.7, abs_tol=1e-3)
# *************************************************************************
# *************************************************************************
def test_problem_two_scenarios(self):
# number_intraperiod_time_intervals = 4
nominal_discount_rate = 0.035
assessment_weights = {0: 0.7, 1: 0.3}
tf = EconomicTimeFrame(
discount_rate=nominal_discount_rate,
reporting_periods={0: (0, 1), 1: (0, 1, 2)},
reporting_period_durations={0: (1, 1), 1: (1, 1, 1)}, # does not matter
time_intervals={0: (0, 1, 2), 1: (0, 1)},
time_interval_durations={0: (1, 1, 1), 1: (1, 1)},
)
# 2 nodes: one import, one regular
mynet = Network()
node_IMP = generate_pseudo_unique_key(mynet.nodes())
mynet.add_import_node(
node_key=node_IMP,
prices={
qpk: ResourcePrice(prices=1.0, volumes=None)
for qpk in tf.qpk()
},
)
# other nodes
node_A = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_A,
base_flow={
(0, 0): 0.50,
(0, 1): 0.00,
(0, 2): 1.00,
(1, 0): 1.25,
(1, 1): 0.30,
},
)
# arc IA
arc_tech_IA = Arcs(
name="any",
# efficiency=[0.5, 0.5, 0.5],
efficiency={(0, 0): 0.5, (0, 1): 0.5, (0, 2): 0.5, (1, 0): 0.5, (1, 1): 0.5},
efficiency_reverse=None,
static_loss=None,
capacity=[3],
minimum_cost=[2],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
validate=False,
)
mynet.add_directed_arc(node_key_a=node_IMP, node_key_b=node_A, arcs=arc_tech_IA)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
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
solver_options={},
perform_analysis=False,
plot_results=False, # True,
print_solver_output=False,
networks={"mynet": mynet},
converters={},
time_frame=tf,
static_losses_mode=True, # just to reach a line,
mandatory_arcs=[],
max_number_parallel_arcs={},
assessment_weights=assessment_weights,
)
assert ipp.has_peak_total_assessments()
assert ipp.results["Problem"][0]["Number of constraints"] == 42
assert ipp.results["Problem"][0]["Number of variables"] == 38
assert ipp.results["Problem"][0]["Number of nonzeros"] == 87
# *********************************************************************
# validation
# the arc should be installed since it is the only feasible solution
assert (
True
in ipp.networks["mynet"]
.edges[(node_IMP, node_A, 0)][Network.KEY_ARC_TECH]
.options_selected
)
# the flows should be 1.0, 0.0 and 2.0
true_v_glljqk = {
("mynet", node_IMP, node_A, 0, 0, 0): 1,
("mynet", node_IMP, node_A, 0, 0, 1): 0,
("mynet", node_IMP, node_A, 0, 0, 2): 2,
("mynet", node_IMP, node_A, 0, 1, 0): 2.5,
("mynet", node_IMP, node_A, 0, 1, 1): 0.6,
}
for key, v in true_v_glljqk.items():
assert math.isclose(pyo.value(ipp.instance.var_v_glljqk[key]), v, abs_tol=1e-6)
# arc amplitude should be two
assert math.isclose(
pyo.value(ipp.instance.var_v_amp_gllj[("mynet", node_IMP, node_A, 0)]),
2.5,
abs_tol=0.01,
)
# capex should be four
assert math.isclose(pyo.value(ipp.instance.var_capex), 4.5, abs_tol=1e-3)
# sdncf_q[0] should be -5.7
assert math.isclose(pyo.value(ipp.instance.var_sdncf_q[0]), -5.7, abs_tol=1e-3)
# the objective function should be -9.7
assert math.isclose(pyo.value(ipp.instance.obj_f), -11.096, abs_tol=3e-3)
# *************************************************************************
# *************************************************************************
def test_problem_two_scenarios_simpler(self):
# number_intraperiod_time_intervals = 4
nominal_discount_rate = 0.035
assessment_weights = {0: 0.7, 1: 0.3}
tf = EconomicTimeFrame(
discount_rate=nominal_discount_rate,
reporting_periods={0: (0, 1), 1: (0, 1, 2)},
reporting_period_durations={0: (1, 1), 1: (1, 1, 1)}, # does not matter
time_intervals={0: (0, 1, 2), 1: (0, 1)},
time_interval_durations={0: (1, 1, 1), 1: (1, 1)},
)
# 2 nodes: one import, one regular
mynet = Network()
node_IMP = generate_pseudo_unique_key(mynet.nodes())
mynet.add_import_node(
node_key=node_IMP,
prices={
qpk: ResourcePrice(prices=1.0, volumes=None)
for qpk in tf.qpk()
},
)
# other nodes
node_A = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_A,
base_flow={
(0, 0): 0.50,
(0, 1): 0.00,
(0, 2): 1.00,
(1, 0): 1.25,
(1, 1): 0.30,
},
)
# arc IA
arc_tech_IA = Arcs(
name="any",
# efficiency=[0.5, 0.5, 0.5],
efficiency={(0, 0): 0.5, (0, 1): 0.5, (0, 2): 0.5, (1, 0): 0.5, (1, 1): 0.5},
efficiency_reverse=None,
static_loss=None,
capacity=[3],
minimum_cost=[2],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
validate=False,
)
mynet.add_directed_arc(node_key_a=node_IMP, node_key_b=node_A, arcs=arc_tech_IA)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
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
solver_options={},
perform_analysis=False,
plot_results=False, # True,
print_solver_output=False,
networks={"mynet": mynet},
converters={},
time_frame=tf,
static_losses_mode=True, # just to reach a line,
mandatory_arcs=[],
max_number_parallel_arcs={},
assessment_weights=assessment_weights,
simplify_problem=True
)
assert ipp.has_peak_total_assessments()
assert ipp.results["Problem"][0]["Number of constraints"] == 28 # 42
assert ipp.results["Problem"][0]["Number of variables"] == 25 # 38
assert ipp.results["Problem"][0]["Number of nonzeros"] == 51 # 87
# *********************************************************************
# validation
# capex should be 4.5
assert math.isclose(pyo.value(ipp.instance.var_capex), 4.5, abs_tol=1e-3)
# the objective function should be -11.096
assert math.isclose(pyo.value(ipp.instance.obj_f), -11.096, abs_tol=3e-3)
# *************************************************************************
# *************************************************************************
def test_problem_two_scenarios_two_discount_rates(self):
# two discount rates
assessment_weights = {0: 0.7, 1: 0.3}
tf = EconomicTimeFrame(
discount_rates_q={0: (0.035, 0.035), 1: (0.1, 0.1, 0.1)},
reporting_periods={0: (0, 1), 1: (0, 1, 2)},
reporting_period_durations={0: (1, 1), 1: (1, 1, 1)}, # does not matter
time_intervals={0: (0, 1, 2), 1: (0, 1)},
time_interval_durations={0: (1, 1, 1), 1: (1, 1)},
)
# 2 nodes: one import, one regular
mynet = Network()
node_IMP = generate_pseudo_unique_key(mynet.nodes())
mynet.add_import_node(
node_key=node_IMP,
prices={
qpk: ResourcePrice(prices=1.0, volumes=None)
for qpk in tf.qpk()
},
)
# other nodes
node_A = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_A,
base_flow={
(0, 0): 0.50,
(0, 1): 0.00,
(0, 2): 1.00,
(1, 0): 1.25,
(1, 1): 0.30,
},
)
# arc IA
arc_tech_IA = Arcs(
name="any",
# efficiency=[0.5, 0.5, 0.5],
efficiency={(0, 0): 0.5, (0, 1): 0.5, (0, 2): 0.5, (1, 0): 0.5, (1, 1): 0.5},
efficiency_reverse=None,
static_loss=None,
capacity=[3],
minimum_cost=[2],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
validate=False,
)
mynet.add_directed_arc(node_key_a=node_IMP, node_key_b=node_A, arcs=arc_tech_IA)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
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
solver_options={},
perform_analysis=False,
plot_results=False, # True,
print_solver_output=False,
networks={"mynet": mynet},
converters={},
time_frame=tf,
static_losses_mode=True, # just to reach a line,
mandatory_arcs=[],
max_number_parallel_arcs={},
assessment_weights=assessment_weights,
)
assert ipp.has_peak_total_assessments()
assert ipp.results["Problem"][0]["Number of constraints"] == 42
assert ipp.results["Problem"][0]["Number of variables"] == 38
assert ipp.results["Problem"][0]["Number of nonzeros"] == 87
# *********************************************************************
# validation
# the arc should be installed since it is the only feasible solution
assert (
True
in ipp.networks["mynet"]
.edges[(node_IMP, node_A, 0)][Network.KEY_ARC_TECH]
.options_selected
)
# the flows should be 1.0, 0.0 and 2.0
assert math.isclose(
pyo.value(ipp.instance.var_v_glljqk[("mynet", node_IMP, node_A, 0, 0, 0)]),
1.0,
abs_tol=1e-6,
)
assert math.isclose(
pyo.value(ipp.instance.var_v_glljqk[("mynet", node_IMP, node_A, 0, 0, 1)]),
0.0,
abs_tol=1e-6,
)
assert math.isclose(
pyo.value(ipp.instance.var_v_glljqk[("mynet", node_IMP, node_A, 0, 0, 2)]),
2.0,
abs_tol=1e-6,
)
assert math.isclose(
pyo.value(ipp.instance.var_v_glljqk[("mynet", node_IMP, node_A, 0, 1, 0)]),
2.5,
abs_tol=1e-6,
)
assert math.isclose(
pyo.value(ipp.instance.var_v_glljqk[("mynet", node_IMP, node_A, 0, 1, 1)]),
0.6,
abs_tol=1e-6,
)
# arc amplitude should be two
assert math.isclose(
pyo.value(ipp.instance.var_v_amp_gllj[("mynet", node_IMP, node_A, 0)]),
2.5,
abs_tol=0.01,
)
# capex should be 4.5
assert math.isclose(pyo.value(ipp.instance.var_capex), 4.5, abs_tol=1e-3)
# sdncf_q[0] should be -5.7
assert math.isclose(pyo.value(ipp.instance.var_sdncf_q[0]), -5.7, abs_tol=1e-3)
# the objective function should be -10.80213032963115
assert math.isclose(pyo.value(ipp.instance.obj_f), -10.80213032963115, abs_tol=3e-3)
# *************************************************************************
# *************************************************************************
def test_problem_two_scenarios_two_discount_rates_simpler(self):
# two discount rates
assessment_weights = {0: 0.7, 1: 0.3}
tf = EconomicTimeFrame(
discount_rates_q={0: (0.035, 0.035), 1: (0.1, 0.1, 0.1)},
reporting_periods={0: (0, 1), 1: (0, 1, 2)},
reporting_period_durations={0: (1, 1), 1: (1, 1, 1)}, # does not matter
time_intervals={0: (0, 1, 2), 1: (0, 1)},
time_interval_durations={0: (1, 1, 1), 1: (1, 1)},
)
# 2 nodes: one import, one regular
mynet = Network()
node_IMP = generate_pseudo_unique_key(mynet.nodes())
mynet.add_import_node(
node_key=node_IMP,
prices={
qpk: ResourcePrice(prices=1.0, volumes=None)
for qpk in tf.qpk()
},
)
# other nodes
node_A = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_A,
base_flow={
(0, 0): 0.50,
(0, 1): 0.00,
(0, 2): 1.00,
(1, 0): 1.25,
(1, 1): 0.30,
},
)
# arc IA
arc_tech_IA = Arcs(
name="any",
# efficiency=[0.5, 0.5, 0.5],
efficiency={(0, 0): 0.5, (0, 1): 0.5, (0, 2): 0.5, (1, 0): 0.5, (1, 1): 0.5},
efficiency_reverse=None,
static_loss=None,
capacity=[3],
minimum_cost=[2],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
validate=False,
)
mynet.add_directed_arc(node_key_a=node_IMP, node_key_b=node_A, arcs=arc_tech_IA)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
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
solver_options={},
perform_analysis=False,
plot_results=False, # True,
print_solver_output=False,
networks={"mynet": mynet},
converters={},
time_frame=tf,
static_losses_mode=True, # just to reach a line,
mandatory_arcs=[],
max_number_parallel_arcs={},
assessment_weights=assessment_weights,
simplify_problem=True
)
assert ipp.has_peak_total_assessments()
assert ipp.results["Problem"][0]["Number of constraints"] == 28 # 42
assert ipp.results["Problem"][0]["Number of variables"] == 25 # 38
assert ipp.results["Problem"][0]["Number of nonzeros"] == 51 # 87
# *********************************************************************
# validation
# arc amplitude should be two
assert math.isclose(
pyo.value(ipp.instance.var_v_amp_gllj[("mynet", node_IMP, node_A, 0)]),
2.5,
abs_tol=0.01,
)
# capex should be four
assert math.isclose(pyo.value(ipp.instance.var_capex), 4.5, abs_tol=1e-3)
# sdncf_q[0] should be -5.7
# assert math.isclose(pyo.value(ipp.instance.var_sdncf_q[0]), -5.7, abs_tol=1e-3)
# the objective function should be -10.80213032963115 (or -10.8027723516153)
assert math.isclose(pyo.value(ipp.instance.obj_f), -10.80213032963115, abs_tol=3e-3)
# *************************************************************************
# *************************************************************************
# problem with two symmetrical nodes and one undirected arc
# problem with symmetrical nodes and one undirected arc with diff. tech.
# problem with symmetrical nodes and one undirected arc, irregular steps
# same problem as the previous one, except with interface variables
# problem with two symmetrical nodes and one undirected arc, w/ simple sos1
Pedro L. Magalhães
committed
@pytest.mark.parametrize(
"solver, use_sos_arcs, arc_sos_weight_key",
[('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_NONE),
# ('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_CAP),
# ('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_COST),
# ('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_SPEC_CAP),
# ('scip', True, InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_SPEC_COST),
('scip', False, None)]
)
def test_isolated_undirected_network(self, solver, use_sos_arcs, arc_sos_weight_key):
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
q = 0
tf = EconomicTimeFrame(
discount_rate=3.5/100,
reporting_periods={q: (0,)},
reporting_period_durations={q: (365 * 24 * 3600,)},
time_intervals={q: (0,1,2,3)},
time_interval_durations={q: (1,1,1,1)},
)
# 4 nodes: one import, one export, two supply/demand nodes
mynet = Network()
# other nodes
node_A = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_A,
# base_flow=[1, -1, 0.5, -0.5],
base_flow={(0, 0): 1, (0, 1): -1, (0, 2): 0.5, (0, 3): -0.5},
)
node_B = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_B,
# base_flow=[-1, 1, -0.5, 0.5],
base_flow={(0, 0): -1, (0, 1): 1, (0, 2): -0.5, (0, 3): 0.5},
)
# add arcs
# undirected arc
arc_tech_AB = ArcsWithoutStaticLosses(
name="any",
# efficiency=[1, 1, 1, 1],
efficiency={(0, 0): 1, (0, 1): 1, (0, 2): 1, (0, 3): 1},
efficiency_reverse=None,
capacity=[0.5, 0.75, 1.0, 1.25, 1.5, 2.0],
minimum_cost=[10, 10.1, 10.2, 10.3, 10.4, 10.5],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
)
arc_key_AB_und = mynet.add_undirected_arc(
node_key_a=node_A, node_key_b=node_B, arcs=arc_tech_AB
)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
Pedro L. Magalhães
committed
use_sos_arcs=use_sos_arcs,
arc_sos_weight_key=arc_sos_weight_key,
solver_options={},
perform_analysis=False,
plot_results=False, # True,
print_solver_output=False,
time_frame=tf,
networks={"mynet": mynet},
static_losses_mode=True, # just to reach a line,
mandatory_arcs=[],
max_number_parallel_arcs={}
)
Pedro L. Magalhães
committed
if use_sos_arcs:
assert len(ipp.instance.constr_arc_sos1) != 0
else:
assert len(ipp.instance.constr_arc_sos1) == 0
assert ipp.has_peak_total_assessments() # TODO: make sure this is true
# assert ipp.results["Problem"][0]["Number of constraints"] == 34
# assert ipp.results["Problem"][0]["Number of variables"] == 28
# assert ipp.results["Problem"][0]["Number of nonzeros"] == 105
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
# *********************************************************************
# *********************************************************************
# validation
# the arc should be installed since it is the only feasible solution
assert (
True
in ipp.networks["mynet"]
.edges[(node_A, node_B, arc_key_AB_und)][Network.KEY_ARC_TECH]
.options_selected
)
# there should be no opex (imports or exports), only capex from arcs
assert pyo.value(ipp.instance.var_sdncf_q[q]) == 0
assert pyo.value(ipp.instance.var_capex) > 0
assert (
pyo.value(
ipp.instance.var_capex_arc_gllj[("mynet", node_A, node_B, arc_key_AB_und)]
)
> 0
)
# *************************************************************************
# *************************************************************************
def test_isolated_undirected_network_diff_tech(self):
# time frame
q = 0
tf = EconomicTimeFrame(
discount_rate=3.5/100,
reporting_periods={q: (0,)},
reporting_period_durations={q: (365 * 24 * 3600,)},
time_intervals={q: (0,1,2,3)},
time_interval_durations={q: (1,1,1,1)},
)
# 4 nodes: one import, one export, two supply/demand nodes
mynet = Network()
# other nodes
node_A = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_A,
# base_flow=[1, -1, 0.5, -0.5]
base_flow={(0, 0): 1, (0, 1): -1, (0, 2): 0.5, (0, 3): -0.5},
)
node_B = generate_pseudo_unique_key(mynet.nodes())
mynet.add_source_sink_node(
node_key=node_B,
# base_flow=[-1.25, 1, -0.625, 0.5]
base_flow={(0, 0): -1.25, (0, 1): 1.0, (0, 2): -0.625, (0, 3): 0.5},
)
# add arcs
# undirected arc
arc_tech_AB = ArcsWithoutStaticLosses(
name="any",
# efficiency=[0.8, 1.0, 0.8, 1.0],
efficiency={(0, 0): 0.8, (0, 1): 1.0, (0, 2): 0.8, (0, 3): 1.0},
efficiency_reverse=None,
capacity=[1.25, 2.5],
minimum_cost=[10, 15],
specific_capacity_cost=1,
capacity_is_instantaneous=False,
)
arc_key_AB_und = mynet.add_undirected_arc(
node_key_a=node_A, node_key_b=node_B, arcs=arc_tech_AB
)
# no sos, regular time intervals
Pedro L. Magalhães
committed
ipp = build_solve_ipp(
solver_options={},
perform_analysis=False,
plot_results=False,
print_solver_output=False,
time_frame=tf,
networks={"mynet": mynet},
static_losses_mode=InfrastructurePlanningProblem.STATIC_LOSS_MODE_DEP,
mandatory_arcs=[],
max_number_parallel_arcs={}
)
Pedro L. Magalhães
committed
assert len(ipp.instance.constr_arc_sos1) == 0
assert ipp.has_peak_total_assessments() # TODO: make sure this is true
assert ipp.results["Problem"][0]["Number of constraints"] == 0 # 34
assert ipp.results["Problem"][0]["Number of variables"] == 23 # 24
# assert ipp.results["Problem"][0]["Number of nonzeros"] == 77