Exemple #1
0
def test_le_cnt_with_opt():
    prob = OptProb()
    model = prob.get_model()

    param = TestParam("test", 3, 1)
    param.resample()
    var = Variable(model, param)
    prob.add_var(var)

    twoe1 = AffExpr(constant=np.array([[2], [0], [-1]], dtype=np.float))

    # var = Variable(model, param)
    lhs = AffExpr({param: 1})
    fluent = LinLEFluent('test_eq', 0, lhs, twoe1)

    variables = [var]
    param_to_var = {param: var}

    constraints = Constraints(model)
    if isinstance(fluent, LinLEFluent):
        lhs = to_gurobi_expr(fluent.lhs, param_to_var)
        rhs = to_gurobi_expr(fluent.rhs, param_to_var)
        model.update()
        constraints.add_leq_cntr(lhs, rhs)
    assert not fluent.satisfied()

    prob.add_constraints(constraints)

    obj = grb.QuadExpr()
    for var in variables:
        for i in range(var.rows):
            for j in range(var.cols):
                v = var.grb_vars[i, j]
                obj += v*v - 2*v + 1

    prob.obj_sqp = obj
    prob.optimize()

    var.update_hl_param()
    assert fluent.satisfied()
    assert np.all(variables[0].get_val() == np.array([[1], [0], [-1]]))
Exemple #2
0
def test_eq_cnt_with_gurobi():
    model = grb.Model()
    obj = grb.QuadExpr()

    param = TestParam("test", 3, 1)
    param.resample()
    var = Variable(model, param)

    zero = AffExpr(constant=np.array([[0], [0], [0]], dtype=np.float))

    # var = Variable(model, param)
    lhs = AffExpr({param: 1})
    fluent = LinEqFluent('test_eq', 0, lhs, zero)

    variables = [Variable(model, param)]
    param_to_var = {param: variables[0]}

    constraints = Constraints(model)
    if isinstance(fluent, LinEqFluent):
        lhs = to_gurobi_expr(fluent.lhs, param_to_var)
        rhs = to_gurobi_expr(fluent.rhs, param_to_var)
        model.update()
        constraints.add_eq_cntr(lhs, rhs)
    assert not fluent.satisfied()

    for var in variables:
        for i in range(var.rows):
            for j in range(var.cols):
                v = var.grb_vars[i, j]
                obj += v*v - 2*v + 1

    model.setObjective(obj)
    model.update()
    model.optimize()

    var.update()
    var.update_hl_param()
    assert fluent.satisfied()