Example #1
0
 def test_get_dfds_dcds2(self):
     '''
     It tests the function get_sensitivity with rooney & biegler's model.
     '''
     variable_name = ['asymptote', 'rate_constant']
     theta = {
         'asymptote': 19.142575284617866,
         'rate_constant': 0.53109137696521
     }
     cov = np.array([[6.30579403, -0.4395341], [-0.4395341, 0.04193591]])
     model_uncertain = ConcreteModel()
     model_uncertain.asymptote = Var(initialize=15)
     model_uncertain.rate_constant = Var(initialize=0.5)
     model_uncertain.obj = Objective(
         expr=model_uncertain.asymptote *
         (1 - exp(-model_uncertain.rate_constant * 10)),
         sense=minimize)
     theta = {
         'asymptote': 19.142575284617866,
         'rate_constant': 0.53109137696521
     }
     for v in variable_name:
         getattr(model_uncertain, v).setlb(theta[v])
         getattr(model_uncertain, v).setub(theta[v])
     gradient_f, gradient_c, col, row, line_dic = get_dfds_dcds(
         model_uncertain, variable_name)
     np.testing.assert_almost_equal(gradient_f, [0.99506259, 0.945148])
     np.testing.assert_almost_equal(gradient_c, np.array([]))
     assert col == ['asymptote', 'rate_constant']
     assert row == ['obj']
Example #2
0
    def test_get_dfds_dcds(self):
        '''
        It tests the function get_sensitivity with a simple nonlinear programming example.
        
        min f: p1*x1+ p2*(x2^2) + p1*p2 
         s.t c1: x1 = p1
             c2: x2 = p2
             c3: 10 <= p1 <= 10
             c4: 5 <= p2 <= 5  
        '''
        variable_name = ['p1', 'p2']

        m = ConcreteModel()
        m.x1 = Var(initialize=0)
        m.x2 = Var(initialize=0)
        m.p1 = Var(initialize=0)
        m.p2 = Var(initialize=0)
        m.obj = Objective(expr=m.x1 * m.p1 + m.x2 * m.x2 * m.p2 + m.p1 * m.p2,
                          sense=minimize)
        m.c1 = Constraint(expr=m.x1 == m.p1)
        m.c2 = Constraint(expr=m.x2 == m.p2)
        theta = {'p1': 10.0, 'p2': 5.0}
        for v in variable_name:
            getattr(m, v).setlb(theta[v])
            getattr(m, v).setub(theta[v])
        gradient_f, gradient_c, col, row, line_dic = get_dfds_dcds(
            m, variable_name)
        np.testing.assert_almost_equal(gradient_f, [10., 50., 15., 35.])
        np.testing.assert_almost_equal(gradient_c.toarray(),
                                       [[1., 0., -1., 0.], [0., 1., 0., -1.]])
        assert col == ['x1', 'x2', 'p1', 'p2']
        assert row == ['c1', 'c2', 'obj']