从Python PYOMO使用GAMS/CPLEX [英] Using GAMS/CPLEX from Python PYOMO

查看:841
本文介绍了从Python PYOMO使用GAMS/CPLEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I noticed that Pyomo 5.3 offers a GAMS solver plugin. https://github.com/Pyomo/pyomo/blob/master/pyomo/solvers/plugins/solvers/GAMS.py

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

This is very exciting, as we have a GAMS/CPLEX license where we can use CPLEX as solver, but only via GAMS. With the new Pyomo-Gams interface, it should from my understanding be possible to formulate a problem in Pyomo, and have it translated to GAMS and solved by CPLEX.

但是,当我使用shell集成进行测试时,它的速度非常慢(对于一个小MIP,30个解算需要40s,而glpk/ipopt/cbc则需要6s).另外,该插件的文档实际上不存在.

However, when I test this with the shell integration, it is very slow (40s for 30 solves of a small MIP versus 6s with glpk/ipopt/cbc). Also, the documentation of the plugin is effectively non-existent.

但是也许你们当中有人在使用该界面方面有一定经验,可以为我提供帮助

But maybe someone of you has some experience using that interface and can help me with it

    pyomo是否真的将pyomo模型转换为gams代码?如果是,我在哪里可以找到gams文件?
  • 翻译效率如何?如果我想反复求解一个小模型,应该如何进行?
  • 使用Shell或GAMS Python API有什么区别?
  • 是否可以找到有关此文档的地方?

  • does pyomo actually translate the pyomo model into gams code? If yes, where can I find the gams-file?
  • how efficient is the translation, and how should I proceed if I want to solve a small model repeatedly?
  • what is the difference between using the shell or the GAMS Python API?
  • is there any place to find documentation about this?

此外,似乎conda仅为Linux/Python 3.6或Windows/Python 2.7提供Pyomo 5.3. https://anaconda.org/conda-forge/pyomo/files?version= 5.3 ,因此我必须使用pip在计算机上安装Pyomo 5.3.

Also, it seems that conda provides Pyomo 5.3 only for Linux/Python 3.6 OR for Windows/Python 2.7 https://anaconda.org/conda-forge/pyomo/files?version=5.3, so I had to use pip to install Pyomo 5.3 on my machine.

提前感谢,西奥

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(" ")

推荐答案

这是比较延迟的,但是我可以回答您的几个问题.

This is rather delayed, but I can answer a few of your questions.

首先,刚刚在readthedocs上为GAMS界面创建了一个基本文档页面,您可以在以下页面找到该页面:

First, a basic documentation page was just created for the GAMS interface on readthedocs which you can find at: http://pyomo.readthedocs.io/en/latest/library_reference/solvers/gams.html. Note that this location may change as I believe we are restructuring the documentation tree some time soon, but you should be able to search for "gams" to find it again in the future. If there's more documentation that you believe you or others would like to see, please let me know as I'd be happy to provide anything that would be helpful.

关于shell接口和Python API接口之间的区别,实际上没有任何区别.我以为使用API​​会提高性能,但过去似乎并非如此(实际上,我尝试过的那个模型无论如何都会看到Shell接口更快).如果您尝试两者并尝试其他方式,那么我也很高兴知道这一点.

As for the difference between the shell interface and the Python API interface, there really isn't any. I thought there would have been a performance increase by using the API but that didn't seem to be the case in the past (and in fact the one model I tried it on saw the shell interface be faster anyway). If you try both and experience otherwise, again I'd be happy to know that too.

这篇关于从Python PYOMO使用GAMS/CPLEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆