根据条件使用脚本在OPL中创建元组列表

时间:2019-03-31 07:23:50

标签: cplex opl

我正在尝试在OPL中编写一个脚本,该脚本将采用1和0的nxm矩阵,并为每个n返回对应的m,其中值是1。

比方说,我已经这样定义了我的数据:

col = {"r1","r2","r3"};
row = {"s1","s2"};
value = [   [1 0 1]
            [1 0 0]]; 

我的代码现在看起来像这样:

{string} row = ...;
{string} col = ...;
float value[row][col] = ...;
{string} t = {};

tuple P {
  string id;
  {string} list;

{P} myPs;

execute {
  for (var i in row) {
    t.clear();
    var id = i;
    for (var j in col) {
        if (value [i][j] == 1)
            {t.add(j);}
    }
    myPs.add(id, t);;
  }    
  writeln(myPs);
}

我试图得到的结果是:

{   <"s1" {"r1" "r3"}>
    <"s2" {"r1"}>}

现在,myPS的结果是:

{   <"s1" {"r1"}>
    <"s2" {"r1"}>}

1 个答案:

答案 0 :(得分:1)

对于我宁愿编写的模型

{string} row = ...;
{string} col = ...;
float value[row][col] = ...;


tuple P {
  string id;
  {string} list;
}

{string} listPerId[id in row]={j | j  in col: value[id][j] == 1};

{P} myPs={<id,listPerId[id]> | id in row};

execute {
writeln(myPs);
}  

给出

{<"s1" {"r1" "r3"}> <"s2" {"r1"}>}

致谢