or-fusion / pao

A Python Package for Adversarial Optimization
Other
18 stars 15 forks source link

Problem in solving the modified example model by PCCG+CPLEX/GUROBI #92

Open yhy0258 opened 1 year ago

yhy0258 commented 1 year ago

The example is solved by PCCG and CPLEX as follows. https://pao.readthedocs.io/en/latest/examples.html

import pyomo.environ as pe
from pao.pyomo import *

# Create a model object
M = pe.ConcreteModel()

# Define decision variables
M.x = pe.Var(bounds=(0,None))
M.y = pe.Var(bounds=(0,None))

# Define the upper-level objective
M.o = pe.Objective(expr=M.x - 4*M.y)

# Create a SubModel component to declare a lower-level problem
# The variable M.x is fixed in this lower-level problem
M.L = SubModel(fixed=M.x)

# Define the lower-level objective
M.L.o = pe.Objective(expr=M.y)

# Define lower-level constraints
M.L.c1 = pe.Constraint(expr=   -M.x -   M.y <= -3)
M.L.c2 = pe.Constraint(expr= -2*M.x +   M.y <=  0)
M.L.c3 = pe.Constraint(expr=  2*M.x +   M.y <= 12)
M.L.c4 = pe.Constraint(expr=  3*M.x - 2*M.y <=  4)

# Create a solver and apply it
with Solver('pao.pyomo.PCCG',mip_solver="cplex") as solver:
    results = solver.solve(M)

# The final solution is loaded into the model
print(M.x.value)
print(M.y.value)

The solution is x=3 and y=6, which is different from the solution x=4 and y=4 obtained by FA. Th solution, x=3 and y=6, is inorrect since y should be 2.5 to reduce its objective function if x=3. Then I check the status as follows.

print(results.solver.termination_condition)
print(results.check_optimal_termination())

The outputs are

maxIterations
False

How to set the the max iteration? The version of the CPLEX for this test is 22.1.0.0.

Thanks.

KuKi2121 commented 1 year ago

You can consider following approach as pao.pyomo-Solver do currently unfortunatley not consider all available config-parameters their mpr-counterparts do.

# build your pyomo-Model
M_pyomo = pyo.ConcreteModel()
# transform model
M_mpr, soln = pao.pyomo.convert.convert_pyomo2MultilevelProblem(M_pyomo , linear = True)
# Solve model, with given config-attributes
# for an overview of existing config-Parameter consult help(pao.Solver('pao.mpr.YourSolverChoice'))
opt = pao.Solver('pao.mpr.PCCG', mip_solver = 'gurobi', quiet = False,maxit=10, rtol= 1e-10)
result = opt.solve(M_mpr)
# Check the terminition conditions (for a describtion of existing terminition conditions, consult https://github.com/or-fusion/pao/blob/master/pao/common/solver.py#L31)

result.solver.termination_condition

# Load solution from mpr to pyomo-Model
soln.copy(From = M_mpr, To = M_pyomo )
# Access values in pyomo model
M_pyomo.x.extract_values()