def test_composite_nlp(self): G = np.array([[6, 2, 1], [2, 5, 2], [1, 2, 4]]) A = np.array([[1, 0, 1], [0, 1, 1]]) b = np.array([3, 0]) c = np.array([-8, -3, -3]) scenarios = dict() coupling_vars = dict() n_scenarios = 2 np.random.seed(seed=985739465) bs = [b, b + 0.001] for i in range(n_scenarios): instance = create_model3(G, A, bs[i], c) nlp = PyomoNLP(instance) scenario_name = "s{}".format(i) scenarios[scenario_name] = nlp coupling_vars[scenario_name] = [nlp.variable_idx(instance.x[0])] nlp = TwoStageStochasticNLP(scenarios, coupling_vars) solver = CyIpoptSolver(nlp) x, info = solver.solve(tee=False) x_sol = np.array([ 2.00003846, -0.99996154, 0.99996154, 2.00003846, -0.99996154, 1.00096154, 2.00003846 ]) self.assertTrue(np.allclose(x, x_sol, rtol=1e-4)) self.assertAlmostEqual(nlp.objective(x), -6.99899, 3)
def test_model2(self): model = create_model2() nlp = PyomoNLP(model) solver = CyIpoptSolver(nlp) x, info = solver.solve(tee=False) x_sol = np.array([3.0, 1.99997807]) y_sol = np.array([0.00017543]) self.assertTrue(np.allclose(x, x_sol, rtol=1e-4)) self.assertAlmostEqual(nlp.objective(x), -31.000000057167462, 3) self.assertTrue(np.allclose(info['mult_g'], y_sol, rtol=1e-4))
def test_model1(self): model = create_model1() nlp = PyomoNLP(model) solver = CyIpoptSolver(nlp) x, info = solver.solve(tee=False) x_sol = np.array([3.85958688, 4.67936007, 3.10358931]) y_sol = np.array([-1.0, 53.90357665]) self.assertTrue(np.allclose(x, x_sol, rtol=1e-4)) self.assertAlmostEqual(nlp.objective(x), -428.6362455416348) self.assertTrue(np.allclose(info['mult_g'], y_sol, rtol=1e-4))
def test_model3(self): G = np.array([[6, 2, 1], [2, 5, 2], [1, 2, 4]]) A = np.array([[1, 0, 1], [0, 1, 1]]) b = np.array([3, 0]) c = np.array([-8, -3, -3]) model = create_model3(G, A, b, c) nlp = PyomoNLP(model) solver = CyIpoptSolver(nlp) x, info = solver.solve(tee=False) x_sol = np.array([2.0, -1.0, 1.0]) y_sol = np.array([-3., 2.]) self.assertTrue(np.allclose(x, x_sol, rtol=1e-4)) self.assertAlmostEqual(nlp.objective(x), -3.5, 3) self.assertTrue(np.allclose(info['mult_g'], y_sol, rtol=1e-4))