python - 如何使用 CBC 求解器在 PULP-OR 中设置最优性差距?

标签 python optimization linear-programming pulp

我想在计算最优解和实际解之间的解时设置最优性差距。

我使用 PuLP 版本 1.6.1,我想将间隙参数传递给求解器。有没有人有关于如何解决这个问题的例子或想法。

可在此处找到的文档:

https://pythonhosted.org/PuLP/solvers.html

不是很有帮助。

谢谢。

最佳答案

文档准确地向您展示了如何将选项传递给求解器。可能并非所有 cbcs 选项都受支持,但 pulp 的代码显示,您想要的任务由参数 fracGap 处理。

class COIN_CMD(LpSolver_CMD):
    """The COIN CLP/CBC LP solver
    now only uses cbc
    """

    def defaultPath(self):
        return self.executableExtension(cbc_path)

    def __init__(self, path = None, keepFiles = 0, mip = 1,
            msg = 0, cuts = None, presolve = None, dual = None,
            strong = None, options = [],
            fracGap = None, maxSeconds = None, threads = None)

让我们举一个 PuLP 的例子from here .

默认模式代码

"""
The Computer Plant Problem for the PuLP Modeller

Authors: Antony Phillips, Dr Stuart Mitchell 2007
"""

# Import PuLP modeler functions
from pulp import *

# Creates a list of all the supply nodes
Plants = ["San Francisco",
          "Los Angeles",
          "Phoenix",
          "Denver"]

# Creates a dictionary of lists for the number of units of supply at
# each plant and the fixed cost of running each plant
supplyData = {#Plant     Supply  Fixed Cost
          "San Francisco":[1700, 70000],
          "Los Angeles"  :[2000, 70000],
          "Phoenix"      :[1700, 65000],
          "Denver"       :[2000, 70000]
          }

# Creates a list of all demand nodes
Stores = ["San Diego",
          "Barstow",
          "Tucson",
          "Dallas"]

# Creates a dictionary for the number of units of demand at each store
demand = { #Store    Demand
          "San Diego":1700,
          "Barstow"  :1000,
          "Tucson"   :1500,
          "Dallas"   :1200
          }

# Creates a list of costs for each transportation path
costs = [  #Stores
         #SD BA TU DA
         [5, 3, 2, 6], #SF
         [4, 7, 8, 10],#LA    Plants
         [6, 5, 3, 8], #PH
         [9, 8, 6, 5]  #DE
         ]

# Creates a list of tuples containing all the possible routes for transport
Routes = [(p,s) for p in Plants for s in Stores]

# Splits the dictionaries to be more understandable
(supply,fixedCost) = splitDict(supplyData)

# The cost data is made into a dictionary
costs = makeDict([Plants,Stores],costs,0)

# Creates the problem variables of the Flow on the Arcs
flow = LpVariable.dicts("Route",(Plants,Stores),0,None,LpInteger)

# Creates the master problem variables of whether to build the Plants or not
build = LpVariable.dicts("BuildaPlant",Plants,0,1,LpInteger)

# Creates the 'prob' variable to contain the problem data
prob = LpProblem("Computer Plant Problem",LpMinimize)

# The objective function is added to prob - The sum of the transportation costs and the building fixed costs
prob += lpSum([flow[p][s]*costs[p][s] for (p,s) in Routes])+lpSum([fixedCost[p]*build[p] for p in Plants]),"Total Costs"

# The Supply maximum constraints are added for each supply node (plant)
for p in Plants:
    prob += lpSum([flow[p][s] for s in Stores])<=supply[p]*build[p], "Sum of Products out of Plant %s"%p

# The Demand minimum constraints are added for each demand node (store)
for s in Stores:
    prob += lpSum([flow[p][s] for p in Plants])>=demand[s], "Sum of Products into Stores %s"%s

# The problem data is written to an .lp file
prob.writeLP("ComputerPlantProblem.lp")

# The problem is solved using PuLP's choice of Solver
prob.solve(PULP_CBC_CMD())

# The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])

# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
    print(v.name, "=", v.varValue)

# The optimised objective function value is printed to the screen
print("Total Costs = ", value(prob.objective))

输出

...
('Total Costs = ', 228100.0)

设置MIPGap的代码

#prob.solve(PULP_CBC_CMD())  # old call
prob.solve(PULP_CBC_CMD(fracGap = 0.1))

输出

...
('Total Costs = ', 230300.0)

关于python - 如何使用 CBC 求解器在 PULP-OR 中设置最优性差距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39943236/

相关文章:

python - 解包元组以在 python 2 中自动运行具有未知长度参数的函数

python - 在用户发送的 Django 管理页面中显示数据

c++ - 条件不变的 if 语句会减慢我的 C++ 代码吗?

c++ - 重写 OpenCV 的 SIMD 性能膨胀

python - Gurobi Python API : model. addVars() 太慢

python - 使用 PULP 进行 CPLEX 间隙设置

python - 如何在 CSV 文件中找到最接近用户输入的数字?

Python 月份开始日期和结束日期之间的两个日期

optimization - 进程与线程

python - pickle 不能与 PuLP 一起玩