示例#1
0
    def test_optimize_multidim_quad_obj(self):
        Q = np.array([[2, 0], [0, 0]])
        A = np.array([[-4, 0]])
        quad = QuadExpr(Q, A, np.zeros((1, 1)))
        model = grb.Model()
        prob = Prob(model)
        grb_var1 = model.addVar(lb=-1 * GRB.INFINITY,
                                ub=GRB.INFINITY,
                                name='x1')
        grb_var2 = model.addVar(lb=-1 * GRB.INFINITY,
                                ub=GRB.INFINITY,
                                name='x2')
        grb_vars = np.array([[grb_var1], [grb_var2]])
        var = Variable(grb_vars)

        bexpr_quad = BoundExpr(quad, var)
        prob.add_obj_expr(bexpr_quad)

        self.assertTrue(bexpr_quad in prob._quad_obj_exprs)
        self.assertTrue(var in prob._vars)

        prob.update_obj(penalty_coeff=0)
        prob.optimize()
        var_value = var.get_value()
        value = np.zeros((2, 1))
        value[0, 0] = 2
        self.assertTrue(np.allclose(var_value, value))
示例#2
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]])))
示例#3
0
文件: test_prob.py 项目: 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]])))
示例#4
0
文件: test_prob.py 项目: c-l/sco
    def test_callback(self):
        x = {}
        def test():
            x[1] = 2
        callback = test
        model = grb.Model()
        prob = Prob(model, callback)

        prob.find_closest_feasible_point()
        self.assertTrue(1 in x)
        x[1] = 3
        prob.optimize()
        self.assertTrue(1 in x)
示例#5
0
    def test_callback(self):
        x = {}

        def test():
            x[1] = 2

        callback = test
        model = grb.Model()
        prob = Prob(model, callback)

        prob.find_closest_feasible_point()
        self.assertTrue(1 in x)
        x[1] = 3
        prob.optimize()
        self.assertTrue(1 in x)
示例#6
0
    def test_add_cnt_expr_eq_aff(self):
        aff = AffExpr(np.ones((1, 1)), np.zeros((1, 1)))
        comp = EqExpr(aff, np.array([[2]]))
        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()

        bexpr = BoundExpr(comp, var)
        prob.add_cnt_expr(bexpr)

        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[2]])))
示例#7
0
文件: test_prob.py 项目: c-l/sco
    def test_add_cnt_expr_eq_aff(self):
        aff = AffExpr(np.ones((1,1)), np.zeros((1,1)))
        comp = EqExpr(aff, np.array([[2]]))
        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()

        bexpr = BoundExpr(comp, var)
        prob.add_cnt_expr(bexpr)

        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[2]])))
示例#8
0
    def test_optimize_just_quad_obj(self):
        quad = QuadExpr(2 * np.eye(1), -2 * np.ones((1, 1)), np.zeros((1, 1)))
        aff = AffExpr(-2 * np.ones((1, 1)), np.zeros((1, 1)))
        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)

        bexpr_quad = BoundExpr(quad, var)
        bexpr_aff = BoundExpr(aff, var)
        prob.add_obj_expr(bexpr_quad)
        prob.add_obj_expr(bexpr_aff)

        self.assertTrue(bexpr_aff in prob._quad_obj_exprs)
        self.assertTrue(bexpr_quad in prob._quad_obj_exprs)
        self.assertTrue(var in prob._vars)

        prob.update_obj(penalty_coeff=0)
        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[2.0]])))
示例#9
0
文件: test_prob.py 项目: c-l/sco
    def test_optimize_just_quad_obj(self):
        quad = QuadExpr(2*np.eye(1), -2*np.ones((1,1)), np.zeros((1,1)))
        aff = AffExpr(-2*np.ones((1,1)), np.zeros((1,1)))
        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)

        bexpr_quad = BoundExpr(quad, var)
        bexpr_aff = BoundExpr(aff, var)
        prob.add_obj_expr(bexpr_quad)
        prob.add_obj_expr(bexpr_aff)

        self.assertTrue(bexpr_aff in prob._quad_obj_exprs)
        self.assertTrue(bexpr_quad in prob._quad_obj_exprs)
        self.assertTrue(var in prob._vars)

        prob.update_obj(penalty_coeff=0)
        prob.optimize()
        self.assertTrue(np.allclose(var.get_value(), np.array([[2.0]])))
示例#10
0
文件: test_prob.py 项目: c-l/sco
    def test_optimize_multidim_quad_obj(self):
        Q = np.array([[2,0], [0,0]])
        A = np.array([[-4, 0]])
        quad = QuadExpr(Q, A, np.zeros((1,1)))
        model = grb.Model()
        prob = Prob(model)
        grb_var1 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x1')
        grb_var2 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x2')
        grb_vars = np.array([[grb_var1], [grb_var2]])
        var = Variable(grb_vars)

        bexpr_quad = BoundExpr(quad, var)
        prob.add_obj_expr(bexpr_quad)

        self.assertTrue(bexpr_quad in prob._quad_obj_exprs)
        self.assertTrue(var in prob._vars)

        prob.update_obj(penalty_coeff=0)
        prob.optimize()
        var_value = var.get_value()
        value = np.zeros((2,1))
        value[0,0] = 2
        self.assertTrue(np.allclose(var_value, value))
示例#11
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]])))
示例#12
0
文件: test_prob.py 项目: 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]])))