2017-02-28 77 views
0

我是CPLEX Python API的新手。我希望通過以.mod和.dat文件作爲輸入,在python中解決我已經在CPLEX OPL IDE中完成的線性編程問題。我想在Python中使用它,因爲我希望不斷改變我的輸入。我的問題的mod文件在下面給出。有人可以幫助我如何使用這個python API。使用CPLEX python API進行連續輸入的線性編程

int n = ...; 
int m = ...; 

int c = ...; 
int s = ...; 

range v = 1..n; 
range p = 1..m; 

int c_req[v] = ...; 
int s_req[v] = ...; 

int trust[v][v] = ...; 


// decision variables 

dvar boolean assign[p][v]; 

// expressions 

dexpr int used[pi in p] = max(vi in v) assign[pi][v]; // used[i] = 1  iff pi is used 
dexpr int totalUsed = sum(pi in p) used[pi]; 

execute { 
    cplex.tilim = 60; // Time limit 60 seconds 
} 

// model 

minimize totalUsed; 

subject to { 
    forall(pi in p) 
    c_cap: 
    sum(vi in v) c_req[vi] * assign[pi][vi] <= c; 

    forall(pi in p) 
    s_cap: 
    sum(vi in v) s_req[vi] * assign[pi][vi] <= s; 

    forall(vi in v) 
    v_all: 
    sum(pi in p) assign[pi][vi] == 1; 

    forall(pi in p, v1 in v, v2 in v) if (v1 < v2) if (trust[v1][v2] == 0) 
    trust_constraint: 
    assign[p][v1] + assign[p][v2] <= 1; 
} 

回答

0

你可以爲了從蟒蛇叫OPL寫

subprocess.check_call(["C:/CPLEXStudio127/opl/bin/x64_win64/oplrun", "diet.mod", "diet.dat"]) 

。你會事先生成diet.dat。

https://www.ibm.com/developerworks/community/forums/html/threadTopic?id=0b6cacbe-4dda-4da9-9282-f527c3464f47

那麼你不必你的模型從OPL遷移到Python的完整的示例。

您也可以翻譯模型,以Python和那麼我建議DOCPLEX:https://developer.ibm.com/docloud/documentation/optimization-modeling/modeling-for-python/

問候

+0

如果生成diet.dat,請確保該文件句柄是同步,然後關閉。 – PerlDev