How to manually set Initial Solution in CVXPY using CPLEX solver

痴心易碎 提交于 2021-02-11 15:44:00

问题


I am trying to solver the Unit Commitment problem (MIQP problem) by modelling the problem in CVXPY and using the CPLEX solver. I have been successful in getting everything to work with CVXPY using CPLEX. However, this was for a small system. Now I would like to do the same with a much larger system.

Side Note: I have successfully solved the MIQP problem in MATLAB using CPLEX. For larger system in MATLAB, I have used an initial solution from a MILP formulation of the problem and limited the CPLEX solver time using the "timelimit" parameter. This has successfully given me optimal solution in a short amount of time.

Now when I try to do the same with CVXPY and CPLEX, the CPLEX solver fails for the larger system. Also for the smaller system, I dont notice any difference in time to solve the problem. I have noted this based on the "solver.stats.solve_time" value. Therefore, I am unsure of whether the initial solution is getting used or neglected. This is some sample code for what I have done. These are my optimization variables:

power = cp.Variable((nUnits, nHours))
isOn = cp.Variable((nUnits, nHours), integer=True)
startup = cp.Variable((nUnits, nHours), integer=True)

I then have the section where I create my objective and constraints. Then I read in the initial solution from Excel and make the necessary adjustments to get it to look like the solution:

initial_power_soln_df = pd.read_excel(r'C:\Users\micah\Downloads\OneDrive-2020-04-17\init.xlsx', sheet_name='Sheet1', header=None)
initial_isOn_soln_df = pd.read_excel(r'C:\Users\micah\Downloads\OneDrive-2020-04-17\init.xlsx', sheet_name='Sheet2', header=None)
initial_startup_soln_df = pd.read_excel(r'C:\Users\micah\Downloads\OneDrive-2020-04-17\init.xlsx', sheet_name='Sheet3', header=None)

power.value = initial_power_soln_df.to_numpy().T
isOn.value = initial_isOn_soln_df.to_numpy().T
startup.value = initial_startup_soln_df.to_numpy().T

Finally, I create my problem and set up the solver:

problem = cp.Problem(cp.Minimize(cost), constr)
problem.solve(solver=cp.CPLEX, cplex_params={"timelimit": 300})

Not sure if this is the proper way to do this. Also NB. the initial solution comes from a MILP formulation and the optimization variables will be different from that of the MIQP formulation. So I cant just take the solution from the MILP and plug it in to the MIQP formulation. I first need to process the results.


回答1:


let me change a bit the bus and zoo example.

# Import packages.
import cvxpy as cp


# Define and solve the CVXPY problem.
nbBus40 = cp.Variable(integer=True)
nbBus30 = cp.Variable( integer=True)
cost = 500*nbBus40+400*nbBus30
prob = cp.Problem(cp.Minimize(cost),[40*nbBus40+30*nbBus30>=300,
                                     nbBus40==8,nbBus30==0
                                     ])

prob.solve(solver=cp.CPLEX,verbose=True)

prob = cp.Problem(cp.Minimize(cost),[40*nbBus40+30*nbBus30>=300,
                                     nbBus40>=0,nbBus30>=0
                                     ])

prob.solve(solver=cp.CPLEX,verbose=True,warm_start=True)

print("status = ",prob.status)

# Print result.
print("\nThe minimal cost is", prob.value)

print("number buses 40 seats = ",nbBus40.value)
print("number buses 30 seats = ",nbBus30.value)



回答2:


I have determined that the warm start functionality does not work with the CPLEX solver or that this is NOT the proper way to use the warm start functionality with CPLEX. This was done by using the example associated with the warm start section in https://www.cvxpy.org/tutorial/advanced/index.html#solve-method-options. When specifying the solver as CPLEX, there is no difference in solve time.



来源:https://stackoverflow.com/questions/61484764/how-to-manually-set-initial-solution-in-cvxpy-using-cplex-solver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!