Esempio n. 1
0
def test_ipopt_solver(model_name):
    current_dir = pathlib.Path(__file__).parent
    osil_file = current_dir / 'models' / (model_name + '.osil')
    pyomo_model = read_pyomo_model(osil_file)
    problem = problem_from_pyomo_model(pyomo_model)

    atol = rtol = 1e-4

    galini = Galini()
    galini.update_configuration({
        'galini': {
            'constraint_violation_tol': 1e-2,
        },
        'logging': {
            'stdout': True,
        },
        'branch_and_cut': {
            'tolerance': atol,
            'relative_tolerance': rtol,
            'root_node_feasible_solution_search_timelimit': 0,
            'cuts': {
                'maxiter': 100,
            }
        },
        'cuts_generator': {
            'generators': ['outer_approximation'],
        },
        'ipopt': {
            'ipopt': {
                'acceptable_constr_viol_tol': 1e-3
            },
        },
    })
    set_timelimit(30)
    start_timelimit()
    solver = BranchAndBoundSolver(galini)
    solver.before_solve(pyomo_model, problem)
    solution = solver.solve(problem)

    assert solution.status.is_success()

    sol_file = current_dir / 'solutions' / (model_name + '.sol')
    expected_solution = read_solution(sol_file)

    expected_objective = expected_solution['objective']
    assert solution.objective is not None
    assert is_close(expected_objective,
                    solution.objective.value,
                    atol=atol,
                    rtol=rtol)

    expected_variables = expected_solution['variables']

    for var_sol in solution.variables:
        assert is_close(
            expected_variables[var_sol.name],
            var_sol.value,
            atol=atol,
            rtol=rtol,
        )
Esempio n. 2
0
def problem():
    Q = [[28.0, 23.0, 0.0, 0.0, 0.0, 2.0, 0.0, 24.0],
         [23.0, 0.0, -23.0, -44.0, 10.0, 0.0, 7.0, -7.0],
         [0.0, -23.0, 18.0, 41.0, 0.0, -3.0, -5.0, 2.0],
         [0.0, -44.0, 41.0, -5.0, 5.0, -1.0, 16.0, -50.0],
         [0.0, 10.0, 0.0, 5.0, 0.0, -2.0, -4.0, 21.0],
         [2.0, 0.0, -3.0, -1.0, -2.0, 34.0, -9.0, 20.0],
         [0.0, 7.0, -5.0, 16.0, -4.0, -9.0, 0.0, 0.0],
         [24.0, -7.0, 2.0, -50.0, 21.0, 20.0, 0.0, -45.0]]

    C = [-44, -48, 10, 45, 0, 2, 3, 4, 5]

    Qc = [
        [-28, 13, 5],
        [13, 0, 0],
        [0, 0, 0],
    ]

    m = aml.ConcreteModel("model_1")
    m.I = range(8)
    m.x = aml.Var(m.I, bounds=(0, 1))
    m.f = aml.Objective(expr=sum(-Q[i][j] * m.x[i] * m.x[j] for i in m.I
                                 for j in m.I) + sum(-C[i] * m.x[i]
                                                     for i in m.I))
    m.c = aml.Constraint(expr=sum(Qc[i][j] * m.x[i] * m.x[j] for i in m.I[0:3]
                                  for j in m.I[0:3]) >= -10)

    return problem_from_pyomo_model(m)
Esempio n. 3
0
def test_relaxed_problem():
    m = pe.ConcreteModel()
    m.I = range(10)
    m.x = pe.Var(m.I, bounds=(0, 1))
    m.obj = pe.Objective(expr=sum(m.x[i] * m.x[i] for i in m.I))
    m.c0 = pe.Constraint(expr=sum(m.x[i] * m.x[i] for i in m.I) >= 0)

    dag = problem_from_pyomo_model(m)

    relaxed = RelaxedProblem(_linear_relaxation(dag), dag)

    assert len(relaxed.relaxed.constraints) == 1 + 1 + 4 * 10

    linear_constraint = LinearExpression([dag.variable(i) for i in m.I],
                                         [i for i in m.I], 0.0)
    relaxed.add_constraint('test_linear', linear_constraint, None, 0.0)
    assert len(relaxed.relaxed.constraints) == 43

    quadratic_constraint = QuadraticExpression(
        [dag.variable(0)],
        [dag.variable(1)],
        [-2.0],
    )
    relaxed.add_constraint('test_quadratic', quadratic_constraint, 0.0, 0.0)
    assert len(relaxed.relaxed.constraints) == 43 + 1 + 4

    relaxed.add_constraint(
        'test_mixed', SumExpression([linear_constraint, quadratic_constraint]),
        0.0, None)
    assert len(relaxed.relaxed.constraints) == 49
Esempio n. 4
0
def test_linear_relaxation_with_quadratic_and_linear():
    m = pe.ConcreteModel()
    m.I = range(10)
    m.x = pe.Var(m.I, bounds=(0, 1))
    m.obj = pe.Objective(
        expr=pe.quicksum(m.x[i]*m.x[i] for i in m.I) + pe.quicksum(m.x[i] for i in m.I)
    )
    m.c0 = pe.Constraint(
        expr=pe.quicksum(2 * m.x[i] * m.x[i] for i in m.I) + pe.quicksum(m.x[i] for i in m.I) >= 0
    )
    dag = problem_from_pyomo_model(m)

    relaxation = _linear_relaxation(dag)
    relaxed = relaxation.relax(dag)
    print_problem(relaxed)
    assert relaxed.objective
    # 1 objective, 1 c0, 4 * 10 x^2 (3 mccormick, 1 midpoint)
    assert len(relaxed.constraints) == 1 + 1 + 4*10

    objective = relaxed.objective
    constraint = relaxed.constraint('c0')

    assert isinstance(objective.root_expr, LinearExpression)
    assert isinstance(constraint.root_expr, SumExpression)

    # Root is only objvar
    assert len(objective.root_expr.children) == 1

    c0, c1 = constraint.root_expr.children

    assert isinstance(c0, LinearExpression)
    assert isinstance(c1, LinearExpression)
    assert len(c0.children) == 10
    assert len(c1.children) == 10
Esempio n. 5
0
def test_convex_relaxation_with_quadratic_only():
    m = pe.ConcreteModel()
    m.I = range(10)
    m.x = pe.Var(m.I)
    m.obj = pe.Objective(expr=pe.quicksum(m.x[i]*m.x[i] for i in m.I))
    m.c0 = pe.Constraint(expr=pe.quicksum(2 * m.x[i] * m.x[i] for i in m.I) >= 0)
    dag = problem_from_pyomo_model(m)

    relaxation = _convex_relaxation(dag)
    relaxed = relaxation.relax(dag)

    assert relaxed.objective
    # original constraint + 10 for disaggregated squares
    assert len(relaxed.constraints) == 1 + 10

    objective = relaxed.objective
    constraint = relaxed.constraints[0]

    assert isinstance(objective.root_expr, LinearExpression)
    assert len(objective.root_expr.children) == 10

    for constraint in relaxed.constraints[:-1]:
        assert isinstance(constraint.root_expr, SumExpression)
        children = constraint.root_expr.children
        assert len(children) == 2
        assert isinstance(children[0], LinearExpression)
        assert isinstance(children[1], QuadraticExpression)

    constraint = relaxed.constraints[-1]
    assert len(constraint.root_expr.children) == 10
    assert isinstance(constraint.root_expr, LinearExpression)
Esempio n. 6
0
def test_ipopt_solver(model_name):
    current_dir = pathlib.Path(__file__).parent
    osil_file = current_dir / 'models' / (model_name + '.osil')
    pyomo_model = read_pyomo_model(osil_file)
    problem = problem_from_pyomo_model(pyomo_model)

    galini = Galini()
    galini.update_configuration({
        'galini': {
            'constraint_violation_tol': 1e-2,
        },
        'ipopt': {
            'ipopt': {
                'acceptable_constr_viol_tol': 1e-3
            },
        },
    })
    solver = IpoptNLPSolver(galini)
    solution = solver.solve(problem)

    assert solution.status.is_success()

    sol_file = current_dir / 'solutions' / (model_name + '.sol')
    expected_solution = read_solution(sol_file)

    expected_objective = expected_solution['objective']
    assert solution.objective is not None
    assert np.isclose(expected_objective, solution.objective.value)

    if False:
        expected_variables = expected_solution['variables']
        assert len(expected_variables) == len(solution.variables)
        for variable, expected in zip(solution.variables, expected_variables):
            assert np.isclose(expected, variable.value)
Esempio n. 7
0
def create_problem():
    m = aml.ConcreteModel()
    m.I = range(10)
    m.x = aml.Var(m.I, bounds=(-1, 2))
    m.obj = aml.Objective(expr=sum(m.x[i] for i in m.I))
    m.cons = aml.Constraint(
        m.I[1:], rule=lambda m, i: aml.cos(m.x[0]) * aml.sin(m.x[i]) >= 0)
    return problem_from_pyomo_model(m)
Esempio n. 8
0
 def execute(self, args):
     assert args.problem
     pyomo_model = read_pyomo_model(
         args.problem,
         objective_prefix=args.objective_prefix,
     )
     problem = problem_from_pyomo_model(pyomo_model)
     return self.execute_with_problem(pyomo_model, problem, args)
Esempio n. 9
0
def test_osil_model(order, model_name):
    if model_name in ['hatfldf.py']:
        pytest.skip('Known derivative fail.')
    current_dir = pathlib.Path(__file__).parent
    osil_file = current_dir / 'models' / model_name

    pyomo_model = read_pyomo_model(osil_file)
    dag = problem_from_pyomo_model(pyomo_model)
    derivative_check(model_name, dag, order)
Esempio n. 10
0
def problem():
    m = aml.ConcreteModel()

    m.x = aml.Var()
    m.y = aml.Var()
    m.z = aml.Var()

    m.obj = aml.Objective(expr=aml.exp(m.x) + aml.exp(m.y))
    m.c0 = aml.Constraint(expr=2.0 * m.x*m.x + 3.0 * m.x*m.y + 4.0 * m.y*m.y >= 0)
    return problem_from_pyomo_model(m)
Esempio n. 11
0
def problem():
    m = aml.ConcreteModel()

    m.I = range(10)
    m.x = aml.Var(m.I, bounds=(-2, 2))
    m.y = aml.Var(bounds=(-3, 3))

    m.obj = aml.Objective(expr=m.y)

    m.linear = aml.Constraint(expr=sum(m.x[i] for i in m.I) >= 0)
    m.not_linear = aml.Constraint(expr=m.y * sum(m.x[i] for i in m.I) >= 0)

    return problem_from_pyomo_model(m)
Esempio n. 12
0
def problem():
    m = aml.ConcreteModel()

    m.x = aml.Var(bounds=(-2, 2))
    m.y = aml.Var(bounds=(-3, 3))
    m.z = aml.Var(bounds=(-4, 2))

    m.obj = aml.Objective(expr=m.x)

    m.bilinear = aml.Constraint(expr=m.x * m.y >= 0)
    m.bilinear_coef = aml.Constraint(expr=5.0 * m.x * m.y >= 0)
    m.bilinear_coef_2 = aml.Constraint(expr=m.x * m.y * 6.0 >= 0)
    m.bilinear_sum = aml.Constraint(
        expr=2 * m.x * m.y + 3 * m.x * m.z + 4 * m.y * m.z >= 0)
    m.trilinear = aml.Constraint(expr=m.x * m.y * m.z >= 0)
    m.power = aml.Constraint(expr=m.x**2 >= 0)

    return problem_from_pyomo_model(m)
Esempio n. 13
0
def problem():
    m = pe.ConcreteModel()

    m.I = range(2)
    m.x = pe.Var(m.I, domain=pe.Integers, bounds=(0, 200))
    m.x[0].setub(4.0)
    m.x[1].setub(7.0)

    @m.Objective()
    def f(m):
        return m.x[0]**2 - 16 * m.x[0] + m.x[1]**2 - 4 * m.x[1] + 68

    @m.Constraint()
    def g0(m):
        return m.x[0]**2 + m.x[1] >= 0

    @m.Constraint()
    def g1(m):
        return -0.33 * m.x[0] - m.x[1] + 4.5 >= 0

    return problem_from_pyomo_model(m)
Esempio n. 14
0
def test_convex_relaxation_with_quadratic_and_linear():
    m = pe.ConcreteModel()
    m.I = range(10)
    m.x = pe.Var(m.I)
    m.obj = pe.Objective(
        expr=pe.quicksum(m.x[i]*m.x[i] for i in m.I) + pe.quicksum(m.x[i] for i in m.I)
    )
    m.c0 = pe.Constraint(
        expr=pe.quicksum(2 * m.x[i] * m.x[i] for i in m.I) + pe.quicksum(m.x[i] for i in m.I) >= 0
    )
    dag = problem_from_pyomo_model(m)

    relaxation = _convex_relaxation(dag)
    relaxed = relaxation.relax(dag)

    print_problem(relaxed)

    assert relaxed.objective
    assert len(relaxed.constraints) == 1 + 10

    objective = relaxed.objective
    assert isinstance(objective.root_expr, SumExpression)

    assert all(
        isinstance(c, LinearExpression)
        for c in objective.root_expr.children
    )

    for constraint in relaxed.constraints[:-1]:
        assert isinstance(constraint.root_expr, SumExpression)
        children = constraint.root_expr.children
        assert len(children) == 2
        assert isinstance(children[0], LinearExpression)
        assert isinstance(children[1], QuadraticExpression)

    constraint = relaxed.constraints[-1]
    assert all(
        isinstance(c, LinearExpression)
        for c in constraint.root_expr.children
    )
Esempio n. 15
0
def problem():
    m = aml.ConcreteModel()

    m.x = aml.Var(bounds=(2, 20))
    m.y = aml.Var(bounds=(3, 30))
    m.z = aml.Var(bounds=(4, 20))

    m.obj = aml.Objective(expr=m.x)

    m.univariate_concave = aml.Constraint(expr=aml.sqrt(m.x) >= 0)
    m.univariate_concave_2 = aml.Constraint(
        expr=aml.sqrt(3.0 * m.x + 2.0) >= 0)
    m.univariate_concave_3 = aml.Constraint(
        expr=aml.sqrt(aml.log(3 * m.x + 2)) >= 0)
    m.univariate_nonconcave = aml.Constraint(expr=aml.sin(m.x) >= 0)
    m.univariate_nonconcave_2 = aml.Constraint(expr=m.x**3 >= 0)
    m.univariate_nonconcave_3 = aml.Constraint(expr=3**m.x >= 0)

    m.nonunivariate = aml.Constraint(expr=m.x**m.y >= 0)
    m.nonunivariate_2 = aml.Constraint(expr=m.x + m.y + m.z >= 0)
    m.nonunivariate_3 = aml.Constraint(expr=aml.sin(m.x + aml.log(m.y)) >= 0)

    return problem_from_pyomo_model(m)
Esempio n. 16
0
def test_convex_relaxation_of_linear_problem():
    m = pe.ConcreteModel()
    m.I = range(10)
    m.x = pe.Var(m.I)
    m.obj = pe.Objective(expr=pe.quicksum(m.x[i] for i in m.I) + 2.0)
    m.c0 = pe.Constraint(expr=pe.quicksum(2 * m.x[i] for i in m.I) - 4.0 >= 0)
    dag = problem_from_pyomo_model(m)

    relaxation = _convex_relaxation(dag)
    relaxed = relaxation.relax(dag)

    assert relaxed.objective
    assert len(relaxed.constraints) == 1

    objective = relaxed.objective
    constraint = relaxed.constraints[0]

    assert isinstance(objective.root_expr, LinearExpression)
    assert len(objective.root_expr.children) == 10
    assert np.isclose(objective.root_expr.constant_term, 2.0)

    assert isinstance(constraint.root_expr, LinearExpression)
    assert len(constraint.root_expr.children) == 10
    assert np.isclose(constraint.root_expr.constant_term, -4.0)