def test_set_objective_w_mock(self): obj = Mock() obj.numel = lambda: 1 aop = AbstractOptimizationProblem() aop.set_objective(obj) self.assertEqual(obj, aop.f)
def test_set_objective(self): aop = AbstractOptimizationProblem() x = aop.create_variable('x', 2) f = x[0]**2 + x[1]**2 aop.set_objective(f) self.assertTrue(is_equal(aop.f, f))
def test_get_problem_dict(self): aop = AbstractOptimizationProblem() x = aop.create_variable('x', 3) p = aop.create_parameter('p', 3) f = sum([x[i]**2 for i in range(x.numel())]) g = x[0] - x[1] + 2 * x[2] aop.set_objective(f) aop.include_inequality(g, lb=-10, ub=20) d_res = {'x': x, 'p': p, 'f': f, 'g': g} d = aop.get_problem_dict() for key in set(d_res.keys()).union(set(d.keys())): self.assertTrue(is_equal(d_res[key], d[key]))
def test_get_default_call_dict(self): aop = AbstractOptimizationProblem() lbx = -DM([2, 3, 10]) ubx = DM([2, 3, 10]) x = aop.create_variable('x', 3, lb=lbx, ub=ubx) p = aop.create_parameter('p', 3) f = sum([x[i]**2 for i in range(x.numel())]) g = x[0] - x[1] + 2 * x[2] aop.set_objective(f) aop.include_inequality(g, lb=-10, ub=20) expected = {'lbx': lbx, 'ubx': ubx, 'lbg': DM(-10), 'ubg': DM(20)} d = aop.get_default_call_dict() for key in set(expected.keys()).union(set(d.keys())): self.assertTrue(is_equal(expected[key], d[key]))