亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 doopl 的 opl.set_input() 作為參數(而不是元組)的正確方法

使用 doopl 的 opl.set_input() 作為參數(而不是元組)的正確方法

HUWWW 2024-01-27 16:08:43
我正在嘗試在 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;            }有什么辦法可以解決這個問題?
查看完整描述

1 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

你需要使用元組集,但在


tuple bounds {

        int lowerBound;

        int upperBound;

        

    }

那不是你所做的。


你應該寫


tuple typebounds {

        int lowerBound;

        int upperBound;

        

    }


{typebounds} bounds=...;

在你的 .mod 文件中


讓我分享一個完整的例子:


from doopl.factory import *

# Data


Buses=[

        (40,500),

        (30,400)

        ]


MinAndMax=[(1,5)]


# Create an OPL model from a .mod file

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

    # tuple can be a list of tuples, a pandas dataframe...

    opl.set_input("buses", Buses)

    opl.set_input("singletonMinAndMax", MinAndMax)


    # Generate the problem and solve it.

    opl.run()


    # Get the names of post processing tables

    print("Table names are: "+ str(opl.output_table_names))


    # Get all the post processing tables as dataframes.

    for name, table in iteritems(opl.report):

        print("Table : " + name)

    for t in table.itertuples(index=False):

            print(t)


    # nicer display

    for t in table.itertuples(index=False):

        print(t[0]," buses ",t[1], "seats")

與zootuplesetwithminandmax.mod


int nbKids=300;


// a tuple is like a struct in C, a class in C++ or a record in Pascal

tuple 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 fine

assert forall(b in buses) b.nbSeats>0;

assert forall(b in buses) b.cost>0;


// decision variable array

dvar int+ nbBus[buses] in minBuses..maxBuses;


// objective

minimize

 sum(b in buses) b.cost*nbBus[b];

 

// constraints

subject to

{

 sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;

}


tuple solution

{

  int nbBus;

  int sizeBus;

}


{solution} solutions={<nbBus[b],b.nbSeats> | b in buses}; 


查看完整回答
反對 回復 2024-01-27
  • 1 回答
  • 0 關注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號