def test_include_inequality(self):
     aop = AbstractOptimizationProblem()
     x = aop.create_variable('x', 2)
     g = x[0] - x[1]
     aop.include_inequality(g)
     self.assertTrue(is_equal(aop.g, g))
     self.assertTrue(is_equal(aop.g_lb, -inf))
     self.assertTrue(is_equal(aop.g_ub, inf))
 def test_include_inequality_scalar_bound(self):
     lb = 1
     ub = 4
     aop = AbstractOptimizationProblem()
     x = aop.create_variable('x', 2)
     g = 2 * x
     aop.include_inequality(g, lb=lb, ub=ub)
     self.assertTrue(is_equal(aop.g_lb, repmat(lb, 2)))
     self.assertTrue(is_equal(aop.g_ub, repmat(ub, 2)))
    def test_include_inequality_w_external_variable_in_expr(self):
        theta = MX.sym('theta')

        aop = AbstractOptimizationProblem()
        x = aop.create_variable('x', 2)
        g = theta * x[0] - x[1]
        aop.include_inequality(g)
        self.assertTrue(is_equal(aop.g, g))
        self.assertTrue(is_equal(aop.g_lb, -inf))
        self.assertTrue(is_equal(aop.g_ub, inf))
 def test_include_inequality_with_bounds(self):
     lb = 2
     ub = 3
     aop = AbstractOptimizationProblem()
     x = aop.create_variable('x', 2)
     g = x[0] - x[1]
     aop.include_inequality(g, lb=lb, ub=ub)
     self.assertTrue(is_equal(aop.g, g))
     self.assertTrue(is_equal(aop.g_lb, lb))
     self.assertTrue(is_equal(aop.g_ub, ub))
 def test_include_inequality_w_external_variable_in_bound(self):
     theta = MX.sym('theta')
     lb = -theta
     ub = theta
     aop = AbstractOptimizationProblem()
     x = aop.create_variable('x', 2)
     g = x[0] - x[1]
     aop.include_inequality(g, lb=lb, ub=ub)
     self.assertTrue(is_equal(aop.g, g))
     self.assertTrue(is_equal(aop.g_lb, lb))
     self.assertTrue(is_equal(aop.g_ub, ub))
    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]))