Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

python - 使用PulP时,如何从Gurobi求解器中检索当前的最佳解决方案?(How can I retrieve the current best solution from the Gurobi solver when using PulP?)

If I use the interactive solver in the gurobi solver, I can do the following:

(如果在gurobi求解器中使用交互式求解器,则可以执行以下操作:)

gurobi> m = read('model.mp')
gurobi> m.optimize()
[...]
Found heuristic solution: objective 821425.00000

Then abort and get the current solution via

(然后中止并通过获取当前解决方案)

gurobi> m.printAttr('X')

I want to have the same behavior in pulp.

(我希望在纸浆中具有相同的行为。)

In particular, after having called:

(特别是在致电之后:)

prob =  pulp.LpProblem(name="MIPProblem", sense=pulp.LpMaximize)
[...]
status = prob.solve(pulp.GUROBI_CMD(msg=True, keepFiles=1))

I want to wait until the first heuristic solution is found/abort after a certain timespan and then obtain the current best solution found by Gurobi.

(我想等到某个时间间隔后找到/中止第一个启发式解决方案,然后获得Gurobi找到的当前最佳解决方案。)

How would I do that?

(我该怎么做?)

  ask by securitymensch translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can either use pulp.GUROBI or pulp.GUROBI_CDM .

(您可以使用pulp.GUROBIpulp.GUROBI_CDM 。)
The main difference is that pulp.GUROBI is a wrapper to gurobipy (the Gurobi Python Interface) while pulp.GUROBI_CDM uses the command line (ie, it writes the LP/ILP in a file and then calls the solver).

(主要区别是pulp.GUROBIgurobipy (Gurobi Python接口)的包装,而pulp.GUROBI_CDM使用命令行(即,它在文件中写入LP / ILP,然后调用求解器)。)

Let's distinguish the two cases:

(让我们区分两种情况:)

  • Case 1 : pulp.GUROBI :

    (案例1pulp.GUROBI :)

    In this case you can access the solverModel object.

    (在这种情况下,您可以访问SolverModel对象。)

    For the fields you can refer directly to the documentation.

    (对于这些字段,您可以直接参考文档。)

    However, for your specific use case what you're looking for is ObjBound .

    (但是,对于您的特定用例,您需要的是ObjBound 。)
    A Small Example:

    (一个小例子:)

     import pulp ... status = prob.solve(pulp.GUROBI(timeLimit=1)) print(pulp.LpStatus[status]) # status print(prob.solverModel.ObjBound) # best objective found 
  • Case 2 : pulp.GUROBI_CDM :

    (案例2pulp.GUROBI_CDM :)

    In this case, the problem is solved by command line and the solution is read from the result file (ie, here )

    (在这种情况下,可以通过命令行解决问题,并从结果文件中读取解决方案(即此处 ))

     import pulp ... status = prob.solve(pulp.GUROBI_CMD(options=[('TimeLimit','1')])) print(pulp.LpStatus[status]) # status print(pulp.value(prob.objective)) # best objective found 

    Note that, GUROBI_CMD does provide good solution status [see here] , so you may read Optimal even though the solution is not since no information about the status are provided by the Gurobi solution file.

    (请注意, GUROBI_CMD确实提供了良好的解决方案状态 (请参见此处) ,因此即使解决方案不是,您也可以阅读Optimal,因为Gurobi解决方案文件没有提供有关状态的信息。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...