Skip to content
Snippets Groups Projects
test_esipp_problem.py 24 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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
    # imports
    
    # standard
    
    import math
    
    from statistics import mean
    
    # local
    
    # import numpy as np
    
    # import networkx as nx
    
    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.resource import ResourcePrice
    
    from src.topupopt.problems.esipp.problem import simplify_peak_total_problem
    
    from src.topupopt.problems.esipp.problem import is_peak_total_problem
    
    # *****************************************************************************
    # *****************************************************************************
    
    class TestESIPPProblem:
    
        def build_solve_ipp(
                self,
                solver: str = 'glpk',
                solver_options: dict = None,
                use_sos_arcs: bool = False,
                arc_sos_weight_key: str = (
                    InfrastructurePlanningProblem.SOS1_ARC_WEIGHTS_NONE),
                arc_use_real_variables_if_possible: bool = False,
                use_sos_sense: bool = False,
                sense_sos_weight_key: int = (
                    InfrastructurePlanningProblem.SOS1_SENSE_WEIGHT_NOMINAL_HIGHER
                    ),
                sense_use_real_variables_if_possible: bool = False,
                sense_use_arc_interfaces: bool = False,
                perform_analysis: bool = False,
                plot_results: bool = False,
                print_solver_output: bool = False,
                irregular_time_intervals: bool = False,
                networks: dict = None,
                number_intraperiod_time_intervals: int = 4,
                static_losses_mode = None,
                mandatory_arcs: list = None,
                max_number_parallel_arcs: dict = None,
                arc_groups_dict: dict = None,
                init_aux_sets: bool = False,
                discount_rates: dict = None,
                reporting_periods: dict = None,
                time_intervals: dict = None,
                assessment_weights: dict = None,
                simplify_problem: bool = False):
            
            reporting_period_duration = 365*24*3600
            
            if type(discount_rates) != dict:
            
                discount_rates = {
                    0: tuple([0.035, 0.035])
                    }
                
            if type(assessment_weights) != dict:
                
                assessment_weights = {} # default
            
            if type(reporting_periods) != dict:
                
                reporting_periods = {0: (0,1)}
                
            # time intervals
            
            if type(time_intervals) != dict:
                    
                if irregular_time_intervals:
                    
                    time_step_max_relative_variation = 0.25
                    
                    intraperiod_time_interval_duration = [
                        (reporting_period_duration/number_intraperiod_time_intervals)*
                        (1+(k/(number_intraperiod_time_intervals-1)-0.5)*
                         time_step_max_relative_variation)
                        for k in range(number_intraperiod_time_intervals)]
                    
                else:
                
                    intraperiod_time_interval_duration = [
                        reporting_period_duration/number_intraperiod_time_intervals
                        for k in range(number_intraperiod_time_intervals)]
                
                # average time interval duration
                
                average_time_interval_duration = round(
                    mean(
                        intraperiod_time_interval_duration
                        )
                    )
                
                time_intervals = {
                    0: tuple(dt for dt in intraperiod_time_interval_duration)
                    }
                    
            # time weights
            
            # relative weight of time period
            
            # one interval twice as long as the average is worth twice
            # one interval half as long as the average is worth half
            
            # time_weights = [
            #     [time_period_duration/average_time_interval_duration 
            #       for time_period_duration in intraperiod_time_interval_duration] 
            #     for p in range(number_periods)]
            
            time_weights = None # nothing yet
        
            normalised_time_interval_duration = None # nothing yet
            
            # create problem object
            
            ipp = InfrastructurePlanningProblem(
                name='problem', 
                discount_rates=discount_rates, 
                reporting_periods=reporting_periods,
                time_intervals=time_intervals,
                time_weights=time_weights,
                normalised_time_interval_duration=normalised_time_interval_duration,
                assessment_weights=assessment_weights
                )
            
            # add networks and systems
            
            for netkey, net in networks.items():
            
                ipp.add_network(network_key=netkey, network=net)
                
            # define arcs as mandatory
            
            if type(mandatory_arcs) == list:
            
                for full_arc_key in mandatory_arcs:
                    
                    ipp.make_arc_mandatory(full_arc_key[0], full_arc_key[1:])
            
            # if make_all_arcs_mandatory:
                
            #     for network_key in ipp.networks:
                    
            #         for arc_key in ipp.networks[network_key].edges(keys=True):
                        
            #             # preexisting arcs are no good
                        
            #             if ipp.networks[network_key].edges[arc_key][
            #                     Network.KEY_ARC_TECH].has_been_selected():
                            
            #                 continue 
            
            #             ipp.make_arc_mandatory(network_key, arc_key)
            
            # set up the use of sos for arc selection
            
            if use_sos_arcs:
                
                for network_key in ipp.networks:
                    
                    for arc_key in ipp.networks[network_key].edges(keys=True):
                        
                        if ipp.networks[network_key].edges[arc_key][
                                Network.KEY_ARC_TECH].has_been_selected():
                            
                            continue
                
                        ipp.use_sos1_for_arc_selection(
                            network_key, 
                            arc_key,
                            use_real_variables_if_possible=(
                                arc_use_real_variables_if_possible),
                            sos1_weight_method=arc_sos_weight_key)
            
            
            # set up the use of sos for flow sense determination
            
            if use_sos_sense:
                
                for network_key in ipp.networks:
                    
                    for arc_key in ipp.networks[network_key].edges(keys=True):
                        
                        if not ipp.networks[network_key].edges[arc_key][
                                Network.KEY_ARC_UND]:
                            
                            continue
                        
                        ipp.use_sos1_for_flow_senses(
                            network_key, 
                            arc_key,
                            use_real_variables_if_possible=(
                                sense_use_real_variables_if_possible
                                ),
                            use_interface_variables=sense_use_arc_interfaces,
                            sos1_weight_method=sense_sos_weight_key)
            
            elif sense_use_arc_interfaces: # set up the use of arc interfaces w/o sos1
                
                for network_key in ipp.networks:
                    
                    for arc_key in ipp.networks[network_key].edges(keys=True):
                        
                        if ipp.networks[network_key].edges[arc_key][
                                Network.KEY_ARC_TECH].has_been_selected():
                            
                            continue
                
                        ipp.use_interface_variables_for_arc_selection(
                            network_key, 
                            arc_key
                            )
                        
            # static losses
                
            if static_losses_mode == ipp.STATIC_LOSS_MODE_ARR:
                
                ipp.place_static_losses_arrival_node()
                
            elif static_losses_mode == ipp.STATIC_LOSS_MODE_DEP:
                
                ipp.place_static_losses_departure_node()
                
            elif static_losses_mode == ipp.STATIC_LOSS_MODE_US:
                
                ipp.place_static_losses_upstream()
            
            elif static_losses_mode == ipp.STATIC_LOSS_MODE_DS:
                
                ipp.place_static_losses_downstream()
                
            else:
                
                raise ValueError('Unknown static loss modelling mode.')
                                
            # *********************************************************************
            
            # groups
            
            if type(arc_groups_dict) != type(None):
                
                for key in arc_groups_dict:
                    
                    ipp.create_arc_group(arc_groups_dict[key])
                            
            # *********************************************************************
                
            # maximum number of parallel arcs
            
            for key in max_number_parallel_arcs:
            
                ipp.set_maximum_number_parallel_arcs(
                    network_key=key[0], 
                    node_a=key[1], 
                    node_b=key[2], 
                    limit=max_number_parallel_arcs[key])
                
            # *********************************************************************
                
            if simplify_problem:
                
                ipp = simplify_peak_total_problem(ipp)
            
            # *********************************************************************
                
            # instantiate (disable the default case v-a-v fixed losses)
            
            # ipp.instantiate(place_fixed_losses_upstream_if_possible=False)
            
            ipp.instantiate(initialise_ancillary_sets=init_aux_sets)
            
            # optimise
            
            ipp.optimise(solver_name=solver, 
                         solver_options=solver_options, 
                         output_options={},
                         print_solver_output=print_solver_output)
                
            # return the problem object
                
            return ipp
        
            # *********************************************************************
            # *********************************************************************
            
        # *************************************************************************
        # *************************************************************************
        
        def test_single_network_single_arc_problem(self):
            
            # scenario
            q = 0
            # time
            number_intervals = 3
            # periods
            number_periods = 2
        
            # 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={
                    (q,p,k): ResourcePrice(
                        prices=1.0,
                        volumes=None
                        )
                    for p in range(number_periods)
                    for k in range(number_intervals)
                    }
                )
            
            # other nodes
            
            node_A = generate_pseudo_unique_key(mynet.nodes())
            
            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=[0.5, 0.5, 0.5], 
                efficiency={
                    (q,0): 0.5,
                    (q,1): 0.5,
                    (q,2): 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)
            
            # identify node types
            
            mynet.identify_node_types()
            
            # no sos, regular time intervals
            
            ipp = self.build_solve_ipp(
                # solver=solver,
                solver_options={},
                # use_sos_arcs=use_sos_arcs,
                # arc_sos_weight_key=sos_weight_key,
                # arc_use_real_variables_if_possible=use_real_variables_if_possible,
                # use_sos_sense=use_sos_sense,
                # sense_sos_weight_key=sense_sos_weight_key,
                # sense_use_real_variables_if_possible=sense_use_real_variables_if_possible,
                # sense_use_arc_interfaces=use_arc_interfaces,
                perform_analysis=False,
                plot_results=False, # True,
                print_solver_output=False,
                # irregular_time_intervals=irregular_time_intervals,
                networks={'mynet': mynet},
                number_intraperiod_time_intervals=number_intervals,
                static_losses_mode=True, # just to reach a line,
                mandatory_arcs=[],
                max_number_parallel_arcs={},
                # init_aux_sets=init_aux_sets,
                simplify_problem=False
                )
            
            assert is_peak_total_problem(ipp)
            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
            
            # *********************************************************************
            # *********************************************************************
            
            # 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, 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)
            
        # *************************************************************************
        # *************************************************************************
        
        def test_single_network_single_arc_problem_simpler(self):
            
            # scenario
            q = 0
            # time
            number_intervals = 3
            # periods
            number_periods = 2
        
            # 2 nodes: one import, one regular
            mynet = Network()
            
            # import node        
            # node_IMP = generate_pseudo_unique_key(mynet.nodes())
            node_IMP = 'thatimpnode'
            mynet.add_import_node(
                node_key=node_IMP, 
                prices={
                    (q,p,k): ResourcePrice(
                        prices=1.0,
                        volumes=None
                        )
                    for p in range(number_periods)
                    for k in range(number_intervals)
                    }
                )
            
            # other nodes
            
            # node_A = generate_pseudo_unique_key(mynet.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=[0.5, 0.5, 0.5], 
                efficiency={
                    (q,0): 0.5,
                    (q,1): 0.5,
                    (q,2): 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)
            
            # identify node types
            
            mynet.identify_node_types()
            
            # no sos, regular time intervals
            
            ipp = self.build_solve_ipp(
                # solver=solver,
                solver_options={},
                # use_sos_arcs=use_sos_arcs,
                # arc_sos_weight_key=sos_weight_key,
                # arc_use_real_variables_if_possible=use_real_variables_if_possible,
                # use_sos_sense=use_sos_sense,
                # sense_sos_weight_key=sense_sos_weight_key,
                # sense_use_real_variables_if_possible=sense_use_real_variables_if_possible,
                # sense_use_arc_interfaces=use_arc_interfaces,
                perform_analysis=False,
                plot_results=False, # True,
                print_solver_output=False,
                # irregular_time_intervals=irregular_time_intervals,
                networks={'mynet': mynet},
                number_intraperiod_time_intervals=number_intervals,
                static_losses_mode=True, # just to reach a line,
                mandatory_arcs=[],
                max_number_parallel_arcs={},
                # init_aux_sets=init_aux_sets,
                simplify_problem=True
                )
            
            assert is_peak_total_problem(ipp)
            assert ipp.results['Problem'][0]['Number of constraints'] == 20
            assert ipp.results['Problem'][0]['Number of variables'] == 19
            assert ipp.results['Problem'][0]['Number of nonzeros'] == 36
            
            # *********************************************************************
            # *********************************************************************
                
            # 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
            
            # 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)
            
    
            # TODO: create method to automate getting data from the command line
            import io
            import sys
            from contextlib import redirect_stdout
            # print('wow wow wow')
            # ipp.instance.constr_imp_flow_cost.pprint()
            expected_string = """constr_imp_flow_cost : Size=4, Index=constr_imp_flow_cost_index, Active=True\n    Key                                     : Lower : Body                                                                                      : Upper : Active\n     ('mynet', 'thatimpnode', 'peak', 0, 0) :   0.0 : 0*var_if_glqpks[mynet,thatimpnode,peak,0,0,0] - var_ifc_glqpk[mynet,thatimpnode,peak,0,0] :   0.0 :   True\n     ('mynet', 'thatimpnode', 'peak', 1, 0) :   0.0 : 0*var_if_glqpks[mynet,thatimpnode,peak,1,0,0] - var_ifc_glqpk[mynet,thatimpnode,peak,1,0] :   0.0 :   True\n    ('mynet', 'thatimpnode', 'total', 0, 0) :   0.0 : var_if_glqpks[mynet,thatimpnode,total,0,0,0] - var_ifc_glqpk[mynet,thatimpnode,total,0,0] :   0.0 :   True\n    ('mynet', 'thatimpnode', 'total', 1, 0) :   0.0 : var_if_glqpks[mynet,thatimpnode,total,1,0,0] - var_ifc_glqpk[mynet,thatimpnode,total,1,0] :   0.0 :   True\n"""
            
            cmd_output = io.StringIO()
            sys.stdout = cmd_output
            ipp.instance.constr_imp_flow_cost.pprint()
            sys.stdout = sys.__stdout__
            assert cmd_output.getvalue() == expected_string
            
            expected_string = """constr_exp_flow_revenue : Size=0, Index=constr_exp_flow_revenue_index, Active=True\n    Key : Lower : Body : Upper : Active\n"""
            f = io.StringIO()
            with redirect_stdout(f):
                ipp.instance.constr_exp_flow_revenue.pprint()
            assert f.getvalue() == expected_string
            
            # try the whole model
            # print('wow wow wow')
            # ipp.instance.pprint()
            # expected_string = """constr_imp_flow_cost : Size=4, Index=constr_imp_flow_cost_index, Active=True\n    Key                                     : Lower : Body                                                                                      : Upper : Active\n     ('mynet', 'thatimpnode', 'peak', 0, 0) :   0.0 : 0*var_if_glqpks[mynet,thatimpnode,peak,0,0,0] - var_ifc_glqpk[mynet,thatimpnode,peak,0,0] :   0.0 :   True\n     ('mynet', 'thatimpnode', 'peak', 1, 0) :   0.0 : 0*var_if_glqpks[mynet,thatimpnode,peak,1,0,0] - var_ifc_glqpk[mynet,thatimpnode,peak,1,0] :   0.0 :   True\n    ('mynet', 'thatimpnode', 'total', 0, 0) :   0.0 : var_if_glqpks[mynet,thatimpnode,total,0,0,0] - var_ifc_glqpk[mynet,thatimpnode,total,0,0] :   0.0 :   True\n    ('mynet', 'thatimpnode', 'total', 1, 0) :   0.0 : var_if_glqpks[mynet,thatimpnode,total,1,0,0] - var_ifc_glqpk[mynet,thatimpnode,total,1,0] :   0.0 :   True\n"""
            # cmd_output = io.StringIO()
            # sys.stdout = cmd_output
            # ipp.instance.pprint()
            # sys.stdout = sys.__stdout__
            # assert cmd_output.getvalue() == expected_string
    
            
            # from contextlib import redirect_stdout
            # import io
    
            # ipp.instance.constr_imp_flow_cost.pprint() # only one constraint
            # f = io.StringIO()
            # with redirect_stdout(f):
            #     # ipp.instance.pprint() # full model
            #     ipp.instance.constr_imp_flow_cost.pprint() # only one constraint
                    
            # expected_string = r"""constr_imp_flow_cost : Size=4, Index=constr_imp_flow_cost_index, Active=True
            #     Key                                     : Lower : Body                                                                                      : Upper : Active
            #      ('mynet', 'thatimpnode', 'peak', 0, 0) :   0.0 : 0*var_if_glqpks[mynet,thatimpnode,peak,0,0,0] - var_ifc_glqpk[mynet,thatimpnode,peak,0,0] :   0.0 :   True
            #      ('mynet', 'thatimpnode', 'peak', 1, 0) :   0.0 : 0*var_if_glqpks[mynet,thatimpnode,peak,1,0,0] - var_ifc_glqpk[mynet,thatimpnode,peak,1,0] :   0.0 :   True
            #     ('mynet', 'thatimpnode', 'total', 0, 0) :   0.0 : var_if_glqpks[mynet,thatimpnode,total,0,0,0] - var_ifc_glqpk[mynet,thatimpnode,total,0,0] :   0.0 :   True
            #     ('mynet', 'thatimpnode', 'total', 1, 0) :   0.0 : var_if_glqpks[mynet,thatimpnode,total,1,0,0] - var_ifc_glqpk[mynet,thatimpnode,total,1,0] :   0.0 :   True
            # """
            # assert expected_string == f.getvalue()
            
            
            # from contextlib import redirect_stdout
            # import io
            # f = io.StringIO()
            # with redirect_stdout(f):
            #     print('foobar')
            #     print(12)
            #     12+3
            # print('Got stdout: "{0}"'.format(f.getvalue()))
            
            
            # *********************************************************************
            # *********************************************************************
        
    # *****************************************************************************
    # *****************************************************************************