Skip to content
Snippets Groups Projects
test_esipp_problem.py 43 KiB
Newer Older
  • Learn to ignore specific revisions
  •         # capex should be 7.0: 4+3
            assert math.isclose(pyo.value(ipp.instance.var_capex), 7.0, abs_tol=1e-3)
    
            # sdncf should be -2.5: -3.5+1.0
    
    Pedro L. Magalhães's avatar
    Pedro L. Magalhães committed
            assert math.isclose(pyo.value(ipp.instance.var_sdncf_q[q]), -2.5, abs_tol=1e-3)
    
    
            # the objective function should be -9.5: -7.5-2.5
            assert math.isclose(pyo.value(ipp.instance.obj_f), -9.5, abs_tol=1e-3)
    
        # *************************************************************************
        # *************************************************************************
    
        def test_problem_converter_sink(self):
            # scenario
            q = 0
            # time
            number_intervals = 3
            # periods
            number_periods = 1
    
            tf = TimeFrame(
                reporting_periods={q: [0]},
                reporting_period_durations={q: [365 * 24 * 3600]},
                time_intervals={q: [0]},
                time_interval_durations={q: [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={
                    (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()
    
            # converters
    
            # number of samples
            time_step_durations = [1, 1, 1]
            number_time_steps = len(time_step_durations)
    
            # get the coefficients
            import numpy as np
    
            # a_innk
            a_innk = {
                ("cvt1", 0, 0, 0): 0.95,
                ("cvt1", 0, 0, 1): 0.95,
                ("cvt1", 0, 0, 2): 0.95,
            }
    
            # b_inmk
            b_inmk = {("cvt1", 0, 0, 0): 3, ("cvt1", 0, 0, 1): 3, ("cvt1", 0, 0, 2): 3}
    
            # c_irnk
            c_irnk = {}
            # d_irmk
            d_irmk = {}
            # e_x_ink: depends on fixed signals
            e_x_ink = {}
            # e_y_irk: depends on fixed signals
            e_y_irk = {}
    
            # get the signals
            inputs, states, outputs = get_two_node_model_signals(number_time_steps)
    
            # create a dynamic system
            ds = dynsys.DynamicSystem(
                time_interval_durations=time_step_durations, A=a, B=b, C=c, D=d
            )
    
            # create a converter
            cvn1 = cvn.Converter(
                "cvn1",
                sys=ds,
                initial_states=x0,
                turn_key_cost=3,
                inputs=inputs,
                states=states,
                outputs=outputs,
            )
    
            # 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,
                time_frame=tf,
                networks={"mynet": mynet},
                converters={"mycvt": cvt},
                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 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's avatar
    Pedro L. Magalhães committed
    
    # *****************************************************************************
    
    # *****************************************************************************