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

问题描述 投票:0回答:1

我被一个流量控制问题卡住了,从给定的CPLEX例子中未能得到出路。我想求解16次的优化问题,唯一不同的是需求的输入。(因为我们服务器上的 "SheetConnection "代码运行有问题,所以我想建立从dat文件读取原始数据的模型)。

我有16个不同时期的16个需求。在第1次运行时,我希望模型的需求是需求数组的第1个数字。第n次运行时,模型的需求是需求数组的第n个数字。

我已经从CPLEX的例子 "mulprod "中修改了模型,但我发现这个例子会越来越多地改变面粉的容量(每次增加一个单位)。所以,它没有提到如何按顺序读取数据。我想问题在于,如果我一开始就想把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]; 
loops cplex read-data
1个回答
0
投票

这是流量控制与增量输入 https:/www.linkedin.compulsemaking-decision-optimization-simple-alex-fleischer

.model

{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]; 

效果更好

© www.soinside.com 2019 - 2024. All rights reserved.