scipopt / PySCIPOpt

Python interface for the SCIP Optimization Suite
https://pyscipopt.readthedocs.io/en/latest/
MIT License
826 stars 255 forks source link

SCIPinitBendersDefault doesn't work properly #907

Open DouBiBaNi opened 2 months ago

DouBiBaNi commented 2 months ago

Dear support team,

I want to know how to set the initial solution when using benders decomposition.

I want to solve the problem with benders decomposition

min 3x + 2y + z 
s.t. 2x + y>=5, 
x + z>=4
0 <= x <= 10,
0 <= y <= 10,
0 <= z

This is my demo code.

from pyscipopt import Model, Benders

def create_master_problem():
    master = Model("Master Problem")

    x = master.addVar("x", vtype="C", lb=0, ub=10)
    y = master.addVar("y", vtype="C", lb=0, ub=10)

    master.setObjective(3 * x + 2 * y, "minimize")

    master.addCons(2 * x + y >= 5)

    return master, x, y

def create_sub_problem():
    sub = Model("Sub Problem")

    z = sub.addVar("z", vtype="C", lb=0)
    x = sub.addVar("x", vtype="C", lb=0, ub=10)

    sub.setObjective(z , "minimize")

    sub.addCons(z >= 4 - x)

    return sub, x, z

def benders_decomposition():
    master, x, y = create_master_problem()

    sub, sub_x, sub_z = create_sub_problem()

    master.initBendersDefault(sub)

    master_initial_sol = master.createSol()
    master.setSolVal(master_initial_sol, x, 3)  
    master.setSolVal(master_initial_sol, y, 1)  
    master.addSol(master_initial_sol)

    master.optimize()

    print("Optimal value for x:", master.getVal(x))
    print("Optimal value for y:", master.getVal(y))
    print("Optimal objective value:", master.getObjVal())

if __name__ == "__main__":
    benders_decomposition()

When I don't set master.initBendersDefault(sub) , scip hints

1/1 feasible solution given by solution candidate storage, new primal bound 1.100000e+01

I can get answer:

Optimal value for x: 2.5
Optimal value for y: 0.0
Optimal objective value: 7.5

But I set master.initBendersDefault(sub), scip hints

all 1 solutions given by solution candidate storage are infeasible

Benders' decomposition: Objective coefficients of copied of master problem variables has been changed to zero.

I get answer

Optimal value for x: 2.5
Optimal value for y: 0.0
Optimal objective value: 9.0

Why is there infeasible when I set initBendersDefault? And I'm really confused about how to set the initial solution for the Benders decomposition.

Joao-Dionisio commented 2 months ago

Can reproduce. Here is a smaller MRE

def create_master_problem():
    master = Model()
    x = master.addVar()
    return master, x

def create_sub_problem():
    sub = Model()
    return sub

def benders_decomposition():
    master, x = create_master_problem()
    sub = create_sub_problem()
    master.initBendersDefault(sub)

    master_initial_sol = master.createSol()
    master.setSolVal(master_initial_sol, x, 1)  
    master.addSol(master_initial_sol)

    master.optimize()

benders_decomposition()

Of course the code crashes when optimizing, but before doing so it says that the solution is not feasible. I disabled presolving, but it remains infeasible then.

Another separate problem is that optimizing just crashes without error message when using trySol instead.

DouBiBaNi commented 2 months ago

Thank you for your reply and look forward to a quick resolution. T_T