使用CPLEX python API进行连续输入的线性编程

时间:2017-02-28 09:21:07

标签: linear-programming cplex

我是CPLEX Python API的新手。我希望解决python中的线性编程问题,我已经在CPLEX OPL IDE中通过将.mod和.dat文件作为输入来完成。我想在python中使用它,因为我希望不断改变我的输入。我的问题的mod文件如下。有人可以帮我解决如何将它用于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;
}

1 个答案:

答案 0 :(得分:0)

你可以写

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

从python中调用OPL。你会事先生成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/

问候