Пример #1
0
    def test_clear_dir_dot_sens(self):
        m = simple_model_1()
        sens = SensitivityInterface(m, clone_model=False)
        k_aug = K_augInterface()
        opt_ipopt.solve(m, tee=True)
        m.ptb = pyo.Param(mutable=True, initialize=1.5)

        cwd = os.getcwd()
        dir_contents = os.listdir(cwd)

        sens_param = [m.p]
        sens.setup_sensitivity(sens_param)
        
        # Call k_aug
        k_aug.k_aug(m, tee=True)
        self.assertIsInstance(k_aug.data["dsdp_in_.in"], str)

        sens.perturb_parameters([m.ptb])

        # Call dot_sens. In the process, we re-write dsdp_in_.in
        k_aug.dot_sens(m, tee=True)

        # Make sure we get the values we expect. This problem is easy enough
        # to solve by hand:
        # x = [1, 1, -2] = [v1, v2, dual]
        # Sensitivity system:
        # | 2 -2  1 |
        # |-2  2  1 | dx/dp = -[dL/dxdp, dc/dp]^T = -[0, 0, -1]^T
        # | 1  1  0 |
        # => dx/dp = [0.5, 0.5, 0]^T
        # Here, dp = [0.5]
        # => dx = [0.25, 0.25, 0]^T
        # => x_new = [1.25, 1.25, -2]
        self.assertAlmostEqual(m.v1.value, 1.25, 7)
        self.assertAlmostEqual(m.v2.value, 1.25, 7)

        # We are back in our working directory
        self.assertEqual(cwd, os.getcwd())

        # The contents of this directory have not changed
        self.assertEqual(dir_contents, os.listdir(cwd))
        self.assertFalse(os.path.exists("dsdp_in_.in"))
        self.assertFalse(os.path.exists("delta_p.out"))
        self.assertFalse(os.path.exists("dot_out.out"))
        self.assertFalse(os.path.exists("timings_dot_driver_dsdp.txt"))

        # And we have saved strings of the file contents.
        self.assertIsInstance(k_aug.data["dsdp_in_.in"], str)
        self.assertIsInstance(k_aug.data["delta_p.out"], str)
        self.assertIsInstance(k_aug.data["dot_out.out"], str)
        self.assertIsInstance(k_aug.data["timings_dot_driver_dsdp.txt"], str)
Пример #2
0
def sensitivity_calculation(method,
                            instance,
                            paramList,
                            perturbList,
                            cloneModel=True,
                            tee=False,
                            keepfiles=False,
                            solver_options=None):
    """This function accepts a Pyomo ConcreteModel, a list of parameters, and
    their corresponding perturbation list. The model is then augmented with
    dummy constraints required to call sipopt or k_aug to get an approximation
    of the perturbed solution.
    
    Parameters
    ----------
    method: string
        'sipopt' or 'k_aug'
    instance: Block
        pyomo block or model object
    paramSubList: list
        list of mutable parameters or fixed variables
    perturbList: list
        list of perturbed parameter values
    cloneModel: bool, optional
        indicator to clone the model. If set to False, the original
        model will be altered
    tee: bool, optional
        indicator to stream solver log
    keepfiles: bool, optional
        preserve solver interface files
    solver_options: dict, optional
        Provides options to the solver (also the name of an attribute)
    
    Returns
    -------
    The model that was manipulated by the sensitivity interface

    """

    sens = SensitivityInterface(instance, clone_model=cloneModel)
    sens.setup_sensitivity(paramList)

    m = sens.model_instance

    if method not in {"k_aug", "sipopt"}:
        raise ValueError("Only methods 'k_aug' and 'sipopt' are supported'")

    if method == 'k_aug':
        k_aug = SolverFactory('k_aug', solver_io='nl')
        dot_sens = SolverFactory('dot_sens', solver_io='nl')
        ipopt = SolverFactory('ipopt', solver_io='nl')

        k_aug_interface = K_augInterface(k_aug=k_aug, dot_sens=dot_sens)

        ipopt.solve(m, tee=tee)
        m.ipopt_zL_in.update(m.ipopt_zL_out)  #: important!
        m.ipopt_zU_in.update(m.ipopt_zU_out)  #: important!

        k_aug.options['dsdp_mode'] = ""  #: sensitivity mode!
        k_aug_interface.k_aug(m, tee=tee)

    sens.perturb_parameters(perturbList)

    if method == 'sipopt':
        ipopt_sens = SolverFactory('ipopt_sens', solver_io='nl')
        ipopt_sens.options['run_sens'] = 'yes'

        # Send the model to ipopt_sens and collect the solution
        results = ipopt_sens.solve(m, keepfiles=keepfiles, tee=tee)

    elif method == 'k_aug':
        dot_sens.options["dsdp_mode"] = ""
        k_aug_interface.dot_sens(m, tee=tee)

    return m