Ejemplo n.º 1
0
    def test_get_value_and_get_approx_value_nonlin_constr(self):
        """
        min x^2 -2x + 1 st. x^2 == 4
        when convexified at x = 1,
        min x^2 -2x + 1 + penalty_coeff*|2x-5|
        when penalty_coeff == 0.5, solution is x = 1.5 and the value is 1.25
        (according to Wolfram Alpha)

        approx value should be 1.25
        value should be 1.125
        """
        quad = QuadExpr(2 * np.eye(1), -2 * np.ones((1, 1)), np.ones((1, 1)))
        quad_cnt = QuadExpr(2 * np.eye(1), np.zeros((1, 1)), np.zeros((1, 1)))
        eq = EqExpr(quad_cnt, np.array([[4]]))

        model = grb.Model()
        prob = Prob(model)

        grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x')
        grb_vars = np.array([[grb_var]])
        var = Variable(grb_vars, np.array([[1.0]]))
        model.update()

        obj = BoundExpr(quad, var)
        prob.add_obj_expr(obj)
        bexpr = BoundExpr(eq, var)
        prob.add_cnt_expr(bexpr)

        prob.convexify()
        prob.update_obj(penalty_coeff=0.5)
        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[1.5]])))
        self.assertTrue(
            np.allclose(prob.get_approx_value(0.5), np.array([[1.25]])))
        self.assertTrue(np.allclose(prob.get_value(0.5), np.array([[1.125]])))
Ejemplo n.º 2
0
Archivo: test_prob.py Proyecto: c-l/sco
    def test_get_value_and_get_approx_value_nonlin_constr(self):
        """
        min x^2 -2x + 1 st. x^2 == 4
        when convexified at x = 1,
        min x^2 -2x + 1 + penalty_coeff*|2x-5|
        when penalty_coeff == 0.5, solution is x = 1.5 and the value is 1.25
        (according to Wolfram Alpha)

        approx value should be 1.25
        value should be 1.125
        """
        quad = QuadExpr(2*np.eye(1), -2*np.ones((1,1)), np.ones((1,1)))
        quad_cnt = QuadExpr(2*np.eye(1), np.zeros((1,1)), np.zeros((1,1)))
        eq = EqExpr(quad_cnt, np.array([[4]]))

        model = grb.Model()
        prob = Prob(model)

        grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x')
        grb_vars = np.array([[grb_var]])
        var = Variable(grb_vars, np.array([[1.0]]))
        model.update()

        obj = BoundExpr(quad, var)
        prob.add_obj_expr(obj)
        bexpr = BoundExpr(eq, var)
        prob.add_cnt_expr(bexpr)

        prob.convexify()
        prob.update_obj(penalty_coeff=0.5)
        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[1.5]])))
        self.assertTrue(np.allclose(prob.get_approx_value(0.5), np.array([[1.25]])))
        self.assertTrue(np.allclose(prob.get_value(0.5), np.array([[1.125]])))
Ejemplo n.º 3
0
    def test_convexify_leq(self):
        model = grb.Model()
        prob = Prob(model)
        grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x')
        grb_vars = np.array([[grb_var]])
        var = Variable(grb_vars)

        model.update()
        grb_cnt = model.addConstr(grb_var, GRB.EQUAL, 0)
        model.optimize()
        var.update()

        e = Expr(f)
        eq = LEqExpr(e, np.array([[4]]))
        bexpr = BoundExpr(eq, var)
        prob.add_cnt_expr(bexpr)

        prob.convexify()
        self.assertTrue(len(prob._penalty_exprs) == 1)
        self.assertTrue(isinstance(prob._penalty_exprs[0].expr, HingeExpr))
Ejemplo n.º 4
0
Archivo: test_prob.py Proyecto: c-l/sco
    def test_convexify_leq(self):
        model = grb.Model()
        prob = Prob(model)
        grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x')
        grb_vars = np.array([[grb_var]])
        var = Variable(grb_vars)

        model.update()
        grb_cnt = model.addConstr(grb_var, GRB.EQUAL, 0)
        model.optimize()
        var.update()

        e = Expr(f)
        eq = LEqExpr(e, np.array([[4]]))
        bexpr = BoundExpr(eq, var)
        prob.add_cnt_expr(bexpr)

        prob.convexify()
        self.assertTrue(len(prob._penalty_exprs) == 1)
        self.assertTrue(isinstance(prob._penalty_exprs[0].expr, HingeExpr))
Ejemplo n.º 5
0
    def test_get_approx_value_lin_constr(self):
        """
        min x^2 st. x == 4
        when convexified,
        min x^2 + penalty_coeff*|x-4|
        when penalty_coeff == 1, solution is x = 0.5 and the value is 3.75
        (according to Wolfram Alpha)

        when penalty_coeff == 2, solution is x = 1.0 and the value is 7.0
        (according to Wolfram Alpha)
        """
        quad = QuadExpr(2 * np.eye(1), np.zeros((1, 1)), np.zeros((1, 1)))
        e = Expr(f)
        eq = EqExpr(e, np.array([[4]]))

        model = grb.Model()
        prob = Prob(model)

        grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x')
        grb_vars = np.array([[grb_var]])
        var = Variable(grb_vars)
        model.update()

        obj = BoundExpr(quad, var)
        prob.add_obj_expr(obj)
        bexpr = BoundExpr(eq, var)
        prob.add_cnt_expr(bexpr)

        prob.optimize()  # needed to set an initial value
        prob.convexify()
        prob.update_obj(penalty_coeff=1.0)
        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[0.5]])))
        self.assertTrue(
            np.allclose(prob.get_approx_value(1.0), np.array([[3.75]])))

        prob.update_obj(penalty_coeff=2.0)
        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[1.0]])))
        self.assertTrue(
            np.allclose(prob.get_approx_value(2.0), np.array([[7]])))
Ejemplo n.º 6
0
Archivo: test_prob.py Proyecto: c-l/sco
    def test_get_approx_value_lin_constr(self):
        """
        min x^2 st. x == 4
        when convexified,
        min x^2 + penalty_coeff*|x-4|
        when penalty_coeff == 1, solution is x = 0.5 and the value is 3.75
        (according to Wolfram Alpha)

        when penalty_coeff == 2, solution is x = 1.0 and the value is 7.0
        (according to Wolfram Alpha)
        """
        quad = QuadExpr(2*np.eye(1), np.zeros((1,1)), np.zeros((1,1)))
        e = Expr(f)
        eq = EqExpr(e, np.array([[4]]))

        model = grb.Model()
        prob = Prob(model)

        grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x')
        grb_vars = np.array([[grb_var]])
        var = Variable(grb_vars)
        model.update()

        obj = BoundExpr(quad, var)
        prob.add_obj_expr(obj)
        bexpr = BoundExpr(eq, var)
        prob.add_cnt_expr(bexpr)

        prob.optimize() # needed to set an initial value
        prob.convexify()
        prob.update_obj(penalty_coeff=1.0)
        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[0.5]])))
        self.assertTrue(np.allclose(prob.get_approx_value(1.0), np.array([[3.75]])))

        prob.update_obj(penalty_coeff=2.0)
        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[1.0]])))
        self.assertTrue(np.allclose(prob.get_approx_value(2.0), np.array([[7]])))