Exemplo n.º 1
0
def test_solve_control_problem(nmpc):
    controller = nmpc.controller

    init_obj_value = value(controller._NMPC_NAMESPACE.tracking_objective.expr)
    nmpc.solve_control_problem()
    final_obj_value = value(controller._NMPC_NAMESPACE.tracking_objective.expr)

    # Not always true because initial model might not be feasible
    assert final_obj_value < init_obj_value

    for con in activated_equalities_generator(controller):
        assert abs(value(con.body) - value(con.upper)) < 1e-5

    for var in unfixed_variables_generator(controller):
        if var.lb is not None:
            assert var.lb - var.value < 1e-6
        if var.ub is not None:
            assert var.value - var.ub < 1e-6
Exemplo n.º 2
0
 def test_solve_control_problem(self):
     # make nmpc
     # add setpoint
     # initialize, from ICs probably
     # solve control problem
     # assert values are as expected.
     # Could probably even do this without initialization
     nmpc = self.make_nmpc()
     time = nmpc.controller_time
     t0 = time.first()
     tl = time.last()
     controller = nmpc.controller
     namespace = getattr(controller, nmpc.namespace_name)
     t1 = namespace.sample_points[1]
     setpoint = [
         (nmpc.controller.flow_in[t0], 3.0),
     ]
     # Note: without this initialization, the setpoint arrives at a
     # local optimum with inlet flow ~= 0.
     # One way to deal with this pitfall is to add bounds to inlet flow.
     nmpc.controller.flow_in[:].set_value(3.0)
     initialize_t0(nmpc.controller)
     copy_values_forward(nmpc.controller)
     nmpc.calculate_full_state_setpoint(setpoint, outlvl=idaeslog.DEBUG)
     override = [
         (nmpc.controller.conc[t0, 'A'], 1),
         (nmpc.controller.conc[t0, 'B'], 1),
         (nmpc.controller.rate[t0, 'A'], 1),
         (nmpc.controller.rate[t0, 'B'], 1),
         (nmpc.controller.flow_out[t0], 1),
         (nmpc.controller.flow_in[t0], 1),
     ]
     nmpc.add_setpoint_to_controller(objective_weight_override=override)
     nmpc.constrain_control_inputs_piecewise_constant()
     nmpc.solve_control_problem()
     input_vars = namespace.input_vars
     diff_vars = namespace.diff_vars
     assert input_vars[0][t1].value == pytest.approx(3.192261151432352)
     assert input_vars[0][tl].value == pytest.approx(2.9818775607191648)
     assert diff_vars[0][tl].value == pytest.approx(3.7101450012850137)
     assert diff_vars[1][tl].value == pytest.approx(1.0898406680173942)
     assert nmpc.controller_solved
Exemplo n.º 3
0
    def test_initialize_from_previous_sample(self):
        nmpc = self.make_nmpc()
        time = nmpc.controller_time
        t0 = time.first()
        tl = time.last()
        controller = nmpc.controller
        namespace = getattr(controller, nmpc.namespace_name)
        sample_points = namespace.sample_points
        t1 = sample_points[1]
        setpoint = [
            (nmpc.controller.flow_in[t0], 3.0),
        ]
        # Note: without this initialization, the setpoint arrives at a
        # local optimum with inlet flow ~= 0.
        # One way to deal with this pitfall is to add bounds to inlet flow.
        nmpc.controller.flow_in[:].set_value(3.0)
        initialize_t0(nmpc.controller)
        copy_values_forward(nmpc.controller)
        nmpc.calculate_full_state_setpoint(setpoint, outlvl=idaeslog.DEBUG)
        override = [
            (nmpc.controller.conc[t0, 'A'], 1),
            (nmpc.controller.conc[t0, 'B'], 1),
            (nmpc.controller.rate[t0, 'A'], 1),
            (nmpc.controller.rate[t0, 'B'], 1),
            (nmpc.controller.flow_out[t0], 1),
            (nmpc.controller.flow_in[t0], 1),
        ]
        nmpc.add_setpoint_to_controller(objective_weight_override=override)
        nmpc.constrain_control_inputs_piecewise_constant()
        nmpc.solve_control_problem()

        diff_vars = namespace.diff_vars
        input_vars = namespace.input_vars
        sample_time = nmpc.sample_time
        n_samples = namespace.samples_per_horizon
        cat_dict = namespace.category_dict
        categories = [
            VariableCategory.DIFFERENTIAL,
            VariableCategory.INPUT,
        ]
        expected = {
            categ: {
                s: [{
                    t - sample_time: cat_dict[categ][i][t].value
                    for t in time
                    if sample_points[s] < t and t <= sample_points[s + 1]
                } for i in range(len(cat_dict[categ]))]
                for s in range(1, n_samples)
            }
            for categ in categories
        }
        for categ in categories:
            expected[categ][n_samples] = [{
                t: cat_dict[categ].setpoint[i]
                for t in time
                if sample_points[n_samples -
                                 1] < t and t <= sample_points[n_samples]
            } for i in range(len(cat_dict[categ]))]

        nmpc.initialize_from_previous_sample(nmpc.controller)

        for s in range(1, n_samples):
            interval = [
                t for t in time
                if sample_points[s - 1] < t and t <= sample_points[s]
            ]
            for categ in categories:
                for i, var in enumerate(cat_dict[categ]):
                    for t in interval:
                        assert var[t].value == expected[categ][s][i][t]