CPLEX OPL海量代码

时间:2017-09-22 09:11:14

标签: cplex opl

我希望通过cplex opl找到多商品流的解决方案,但是大量代码出错。我的代码是正确的,因为它可以在命令行环境中运行

enter image description here

当我使用命令行来操作cplex时,它显示了这个解决方案。

enter image description here

这是我的代码:

.mod文件:

int NumNodes = ...;   // Number of nodes
range Nodes = 1..NumNodes;

int NumDemands = ...; // Number of demands
range Dem = 1..NumDemands;

// Create a record to hold information about each arc
tuple arc {
   key int fromnode;
   key int tonode;
   float cost;
   float capacity;
}

// Get the set of arcs

{arc} Arcs = ...;//集合

tuple demand {
    int id;
    int srcnode;
    int destnode;
    float supply;
}

{demand} Demands = ...;


// The network flow model has decision variables indexed on 
// the com_id and the arcs. 

dvar float+ Flow[Dem][Arcs];

dexpr float TotalFlow = sum (k in Dem, a in Arcs) a.cost * Flow[k][a];
minimize TotalFlow;

subject to {
     // flow balance constraints
   forall (k in Dem, i in Nodes)
     ctNodeFlow:
      sum (<i,j,c,ca> in Arcs) Flow[k][<i,j,c,ca>]
    - sum (<j,i,c,ca> in Arcs) Flow[k][<j,i,c,ca>] 
    == sum (dem in Demands : dem.id == k && dem.srcnode == i) dem.supply
    - sum (dem in Demands : dem.id == k && dem.destnode == i) dem.supply;

    // bandwidth constraints

    forall (a in Arcs)
      ctBandwidth:
      sum(k in Dem) Flow[k][a] <= a.capacity;

}


execute DISPLAY {
   writeln("\n<id,fromnode,tonode,Flow[k][a]>\n");
   for(var k in Dem)
    for(var a in Arcs)
         writeln("<",k,",",a.fromnode,",",a.tonode,",",Flow[k][a],">");

} 

.dat文件:

NumNodes = 6;

NumCom = 2;

Arcs = {
   // from, to, cost, capacity
   <  1,    2,  1,     5  >,
   <  1,    3,  5,     30 >,
   <  5,    3,  1,     30 >, 
   <  5,    6,  5,     30 >,
   <  3,    4,  1,     10 >,
   <  4,    2,  5,     30 >,
   <  4,    6,  1,     30 >,
};
/*
Commodities = {
   // com_id, node, supply
   <  1,      1,    10  >,
   <  1,      2,   -10  >,
   <  2,      5,    20  >,
   <  2,      6,   -20  >,
};
*/

Supply = [
   // com_id, nodes
   [10,-10,0,0,0,0]
   [0,0,0,0,20,-20]
];

0 个答案:

没有答案