使用 doopl 的 opl.set_input() 作为参数(而不是元组)的正确方法

我正在尝试在 Python 中运行 CPLEX .mod 文件。有关如何执行此操作的讲师位于以下链接中:


如何使用 python 运行 .mod 文件 (CPLEX)?

但似乎(也许)只有元组从 Python 发送到 CPLEX。就我而言,CPLEX .mod 文件中有一个循环,如下所示:


for (var i = lowerBound; i <= upperBound; i++) {

...

我想将参数 lowerBound 和 upperBound 从 Python 发送到 CPLEX .mod 文件。为此,我在 CPLEX .mod 文件内、for 循环之前定义了一个变量,如下所示:


var lowerBound = ...;

var upperBound = ...;

然后,我在 Python 中使用以下命令:


from doopl.factory import *

with create_opl_model(model="model.mod") as opl:

    opl.set_input("upperBound", 50)

    opl.set_input("lowerBound", 1)

    opl.run()

但出现以下错误:


ERROR at 17:18 model.mod: Scripting parser error: missing expression.

我想说的是,在 CPLEX .mod 中,第 17 行和第 18 行是:


var lowerBound = ...; 

var upperBound = ...;

问题:我想知道是否只发送元组opl.set_input ()?为了理解这一点,我做了如下的事情:


CPLEX .mod 内部:


tuple bounds {

        int lowerBound;

        int upperBound;

        

    }


    

for (var i = lowerBound; i <= upperBound; i++) {

    ...

}

Python 内部:


from doopl.factory import *


Bounds = [

    (1, 50),

    ]


with create_opl_model(model=" model.mod") as opl:

    opl.set_input("bounds", Bounds)

    opl.run()

但这一次,出现了如下错误:


ERROR at 20:7 model.mod: Scripting parser error: missing ';' or newline between statements.

我想说的是,在 CPLEX .mod 文件中,第 20 行与元组边界的定义相关,即:


tuple bounds {

        int lowerBound;

        int upperBound;

        

    }

有什么办法可以解决这个问题?


HUWWW
浏览 39回答 1
1回答

慕神8447489

你需要使用元组集,但在tuple bounds {&nbsp; &nbsp; &nbsp; &nbsp; int lowerBound;&nbsp; &nbsp; &nbsp; &nbsp; int upperBound;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }那不是你所做的。你应该写tuple typebounds {&nbsp; &nbsp; &nbsp; &nbsp; int lowerBound;&nbsp; &nbsp; &nbsp; &nbsp; int upperBound;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }{typebounds} bounds=...;在你的 .mod 文件中让我分享一个完整的例子:from doopl.factory import *# DataBuses=[&nbsp; &nbsp; &nbsp; &nbsp; (40,500),&nbsp; &nbsp; &nbsp; &nbsp; (30,400)&nbsp; &nbsp; &nbsp; &nbsp; ]MinAndMax=[(1,5)]# Create an OPL model from a .mod filewith create_opl_model(model="zootuplesetwithminandmax.mod") as opl:&nbsp; &nbsp; # tuple can be a list of tuples, a pandas dataframe...&nbsp; &nbsp; opl.set_input("buses", Buses)&nbsp; &nbsp; opl.set_input("singletonMinAndMax", MinAndMax)&nbsp; &nbsp; # Generate the problem and solve it.&nbsp; &nbsp; opl.run()&nbsp; &nbsp; # Get the names of post processing tables&nbsp; &nbsp; print("Table names are: "+ str(opl.output_table_names))&nbsp; &nbsp; # Get all the post processing tables as dataframes.&nbsp; &nbsp; for name, table in iteritems(opl.report):&nbsp; &nbsp; &nbsp; &nbsp; print("Table : " + name)&nbsp; &nbsp; for t in table.itertuples(index=False):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(t)&nbsp; &nbsp; # nicer display&nbsp; &nbsp; for t in table.itertuples(index=False):&nbsp; &nbsp; &nbsp; &nbsp; print(t[0]," buses ",t[1], "seats")与zootuplesetwithminandmax.modint nbKids=300;// a tuple is like a struct in C, a class in C++ or a record in Pascaltuple bus{key int nbSeats;float cost;}// This is a tuple set{bus} buses=...;tuple minandmax{int m;int M;}{minandmax} singletonMinAndMax=...;int minBuses=first(singletonMinAndMax).m;int maxBuses=first(singletonMinAndMax).M;// asserts help make sure data is fineassert forall(b in buses) b.nbSeats>0;assert forall(b in buses) b.cost>0;// decision variable arraydvar int+ nbBus[buses] in minBuses..maxBuses;// objectiveminimize&nbsp;sum(b in buses) b.cost*nbBus[b];&nbsp;// constraintssubject to{&nbsp;sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;}tuple solution{&nbsp; int nbBus;&nbsp; int sizeBus;}{solution} solutions={<nbBus[b],b.nbSeats> | b in buses};&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python