def test_get_dsdp2(self): ''' It tests the function get_dsdp 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]) dsdp, col = get_dsdp(model_uncertain, variable_name, theta, {}) np.testing.assert_almost_equal(dsdp.toarray(), [[1., 0.], [0., 1.]]) assert col == ['asymptote', 'rate_constant']
def test_get_dsdp1(self): ''' It tests the function get_dsdp 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]) dsdp, col = get_dsdp(m, variable_name, theta) np.testing.assert_almost_equal(dsdp.toarray(), [[1., 0., 1., 0.], [0., 1., 0., 1.]]) assert col == ['x1', 'x2', 'p1', 'p2']