Cplex:如何在 for 循环中读取来自 dat.file 的不同数据输入

Cplex: How to read different data input from dat.file in a for loop

我遇到了流量控制问题,无法从给定的 CPLEX 示例中找到出路。我要解决16次优化问题,唯一不同的是需求的输入。 (因为我们无法 运行 在我们的服务器中设置“SheetConnection”代码,所以我想构建从 dat 文件读取原始数据的模型)。

我有16个不同时期的16个诉求。对于第一个 运行,我希望模型的需求是需求数组的第一个数字。而对于第n个运行,模型的需求就是需求数组的第n个数。

我已经修改了 CPLEX 示例“mulprod”中的模型,但我发现该示例越来越多地改变了面粉的容量(每次增加 1 个单位)。所以,它没有提到如何按顺序读取数据。我猜问题出在如果我一开始就想把Demand设置成一个范围,那么我调用第n个号码的代码就有问题了。

/*mod.file*/
{string} Units_ = ...;
int NbUnits = ...;
range Units = 1..NbUnits;
int NbLoops = ...;

float Demand[1..16] = ...;
float MaxGen [Units] = ...; 
float MinGen [Units] = ...; 
float MarginalC [Units] = ...; 

dvar boolean Opr [u in Units];
dvar float+ Gen [u in Units] in 0..MaxGen [u];

execute {
  writeln("* This OPL model is not compliant with cloud execution");
}

minimize 
  sum(u in Units) (Gen[u] * MarginalC[u]);

subject to {
//Meet demand       
      ctPowerBalance: 
        sum(u in Units) Gen[u] == Demand;  
//Max/Min Unit Constraints           
    forall(u in Units)
      ctMaxGeneration:
        Gen[u] <= MaxGen[u]*Opr[u];
    forall(u in Units)
      ctMinGeneration:
        Gen[u] >= MinGen[u]*Opr[u];
}

tuple plan {
  float Operation;
  float Generation;
}

plan Plan[u in Units] = 
  <Opr[u], Gen[u]>;



main {
  thisOplModel.generate();

  var produce = thisOplModel;
  var Demandt = produce.Demand_[NbLoops];

  var best;
  var curr = Infinity;
  var ofile = new IloOplOutputFile("BAU_T2030.txt");
  while ( 1 ) {
    best = curr;
    writeln("Solve with Demand[", NbLoops, "] is ", Demandt);
    if ( cplex.solve() ) {
      curr = cplex.getObjValue();
      writeln();
      writeln("OBJECTIVE: ",curr);
      ofile.writeln("Objective with Demand[", NbLoops, "] is ", curr);
      ofile.writeln("plan = ",produce.Plan);      
} 
    else {
      writeln("No solution!");
      break;
    }
    if ( best==curr ) break;

NbLoops ++;
    for(var Demand in thisOplModel.Demand)          
      thisOplModel.ctPowerBalance[Demand].UB = Demandt;
  }

  ofile.close();

  0;
}

/*dat.file*/
Units_ = {G11, G12, G13, G14, G15};
NbUnits = 5;
NbLoops = 1;
Demand = [1133.16, 1826.47, 1304.79, 1330.03, 1556.02, 1952.19, 1124.82, 1622.05, 1567.69, 1499.43, 1315.01, 1135.05, 1029.67, 1949.19, 1972.64, 1812.24];
MaxGen = [519.2, 665.6, 46.5, 212, 464];
MarginalC = [55.21, 36.32, 41.62, 34.16, 36.71]; 

这是https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/

中的增量流量控制

.mod

{string} Units_ = ...;
int NbUnits = ...;
range Units = 1..NbUnits;
int NbLoops = ...;

float Demand[1..16] = ...;
float MaxGen [Units] = ...; 
float MinGen [Units] = ...; 
float MarginalC [Units] = ...; 

dvar boolean Opr [u in Units];
dvar float+ Gen [u in Units] in 0..MaxGen [u];

dvar float currentDemand;

execute {
  writeln("* This OPL model is not compliant with cloud execution");
}

minimize 
  sum(u in Units) (Gen[u] * MarginalC[u]);

subject to {
//Meet demand       
      ctPowerBalance: 
        sum(u in Units) Gen[u] == currentDemand;  
//Max/Min Unit Constraints           
    forall(u in Units)
      ctMaxGeneration:
        Gen[u] <= MaxGen[u]*Opr[u];
    forall(u in Units)
      ctMinGeneration:
        Gen[u] >= MinGen[u]*Opr[u];
}

tuple plan {
  float Operation;
  float Generation;
}

plan Plan[u in Units] = 
  <Opr[u], Gen[u]>;



main {
  thisOplModel.generate();
 var NbLoops=1;
  var produce = thisOplModel;


  var best;
  //var curr = Infinity;
  var ofile = new IloOplOutputFile("BAU_T2030.txt");
  while ( NbLoops<=16 ) {
    var Demandt = produce.Demand[NbLoops];
    //best = curr;
    writeln("Solve with Demand[", NbLoops, "] is ", Demandt);
    if ( cplex.solve() ) {
      curr = cplex.getObjValue();
      writeln();
      writeln("OBJECTIVE: ",curr);
      ofile.writeln("Objective with Demand[", NbLoops, "] is ", curr);
      ofile.writeln("plan = ",produce.Plan);      
} 
    else {
      writeln("No solution!");
      //break;
    }
    //if ( best==curr ) break;

NbLoops ++;
    //for(var Demand in thisOplModel.Demand)          
      thisOplModel.currentDemand.UB = Demandt;
      thisOplModel.currentDemand.LB = Demandt;
  }

  ofile.close();

  0;
}

.dat

Units_ = {G11, G12, G13, G14, G15};
NbUnits = 5;
NbLoops = 16;
Demand = [1133.16, 1826.47, 1304.79, 1330.03, 1556.02, 1952.19, 1124.82, 1622.05, 1567.69, 1499.43, 1315.01, 1135.05, 1029.67, 1949.19, 1972.64, 1812.24];
MaxGen = [519.2, 665.6, 46.5, 212, 464];
MinGen = [];
MarginalC = [55.21, 36.32, 41.62, 34.16, 36.71]; 

效果更好