使用来自 Python PYOMO 的 GAMS/CPLEX

Using GAMS/CPLEX from Python PYOMO

我注意到 Pyomo 5.3 提供了一个 GAMS 求解器插件。 https://github.com/Pyomo/pyomo/blob/master/pyomo/solvers/plugins/solvers/GAMS.py

这非常令人兴奋,因为我们拥有 GAMS/CPLEX 许可证,我们可以在其中使用 CPLEX 作为求解器,但只能通过 GAMS。使用新的 Pyomo-Gams 界面,根据我的理解,应该可以在 Pyomo 中提出问题,并将其转换为 GAMS 并由 CPLEX 解决。

但是,当我使用 shell 集成对其进行测试时,它非常慢(40 秒解决了 30 个小型 MIP,而使用 glpk/ipopt/cbc 则需要 6 秒)。此外,该插件的文档实际上不存在。

但也许你们中有人有使用该界面的经验,可以帮助我

提前致谢,西奥

import pyomo.environ as pe

# set up the model
model = pe.ConcreteModel()

model.MaxWeight = pe.Param(initialize=0,mutable=True)
model.Item = ['hammer','wrench','screwdriver','towel']

Weight = {'hammer':5,'wrench':7,'screwdriver':4,'towel':3}
Value = {'hammer':8,'wrench':3,'screwdriver':6,'towel':11}

model.x = pe.Var(model.Item,within=pe.Binary)
model.z = pe.Objective(expr=sum(Value[i] * model.x[i] for i in model.Item),sense=pe.maximize)
model.constraint = pe.Constraint(expr=sum(Weight[i]*model.x[i] for i in model.Item) <= model.MaxWeight)

# time execution
solver_list = ['cbc', 'ipopt', 'gams', 'glpk']

for i, solver_name in enumerate(solver_list):
    solver = pe.SolverFactory(solver_name)
    print(solver_name)
    tic = time.time()
    for MaxWeight_i in range(0,30):
        model.MaxWeight = MaxWeight_i
        result = solver.solve(model)

        soln_items = list()
        for i in model.x:
            if pe.value(model.x[i]) > 0.5:
                soln_items.append(i)
        # print("Maximum Weight =", MaxWeight_i, soln_items)

    print("{:7.2f} s".format(time.time()-tic))
    print(" ")

这有点延迟,但我可以回答你的几个问题。

首先,刚刚在 readthedocs 上为 GAMS 界面创建了一个基本文档页面,您可以在以下位置找到它:http://pyomo.readthedocs.io/en/latest/library_reference/solvers/gams.html。请注意,此位置可能会更改,因为我相信我们很快就会重组文档树,但您应该能够搜索 "gams" 以在将来再次找到它。如果您认为自己或其他人希望看到更多文档,请告诉我,我很乐意提供任何有用的信息。

至于shell接口和PythonAPI接口的区别,还真没有。我认为使用 API 会提高性能,但过去似乎并非如此(事实上我试过的一个模型看到 shell 接口是无论如何更快)。如果您同时尝试这两种方法并体验其他方式,我也很乐意知道这一点。