如何將 CPLEX 中的所有解決方案獲取到 MIP

莎拉

我正在製定單人循環賽的時間表。它在 CPLEX 中建模為 MIP,並且在我的解決方案池中,目前有四個解決方案具有相同的最佳目標值。我想獲得這四個解決方案中的每一個,以便可以單獨打印和檢查它們。那可能嗎?

// Create Parameters:
 {string} G1 = ...; // Set of teams in first group
 {string} G2 = ...; // Set of teams in second group
 
 {string} Teams = G1 union G2;
 
 tuple Match {string team1; string team2;}
 
 {Match} Matches_G1 = {<t1,t2>| ordered t1,t2 in G1};
 {Match} Matches_G2 = {<t1,t2>| ordered t1,t2 in G2};
 {Match} MD1 = ...;
 {Match} MD2 = ...;
 {Match} MD3 = ...;
 {Match} M = Matches_G1 union Matches_G2; //All matches for the two groups
 
 {Match} matchForTeam[t in Teams] = {m| m in M : m.team1 == t || m.team2 == t}; //List of all teams
 
 {string} S =...; //Set of stadiums
 
 {string} T = ...; //Set of kick off times
 
 {string} D = ...; //Set of kick off days
 
 int K[D][S][T] = ...; //Predetermined schedule between stadium and kickoff time
 
 float VT[M][T] = ...; //Value of match if played on Matchday M at Time T according to TV distribution

 // Decision Variables:
 
 dvar int X[M][S][T] in 0..1; // if match M is played at time T
 
 dvar int Dist; //Object function for distribution
 
 

 //////////// OBJECTIVE FUNCTION ///////////////
 
 maximize 
    Dist;
 
 //////////// CONSTRAINTS ///////////////
 
subject to{ 

Dist == sum(m in M, s in S, t in T) (VT[m][t])*X[m][s][t];


 //A match can only be played one time
 forall(m in M)
    sum(s in S, t in T) X[m][s][t] == 1;
    
//Simultaneous Kickoff on matchday 3
 sum(s in S)X[<"A1", "A4">][s]["22.00"] == sum(s in S)X[<"A2", "A3">][s]["22.00"];
     
//only one match on possible kick off times at matchday 1
 forall(t in T : t != "18.00")
   sum(s in S, m in MD1) X[m][s][t]==1;
//only one match on possible kick off times at matchday 2
 forall(t in T : t != "18.00")
   sum(s in S, m in MD2) X[m][s][t]==1;
//two matches per possible kick off times at matchday 3
 forall(t in T : t in {"18.00", "22.00"})
   sum(s in S, m in MD3) X[m][s][t]==2;
//One match per stadium on matchday 1
 forall(s in S)
    sum(m in MD1, t in T: t != "18.00") X[m][s][t] == 1;
 //One match per stadium on matchday 2
 forall(s in S)
   sum(m in MD2, t in T: t != "18.00") X[m][s][t] == 1;  
 //one match per stadium on matchday 3
forall(s in S)
 sum(m in MD3, t in T: t in {"18.00", "22.00"}) X[m][s][t] == 1;    
 
    
 //Each team can play at most two matches per stadium
forall(i in Teams, s in S)
 sum(t in T, m in matchForTeam[i]) X[m][s][t] <= 2;
  
 //Each team can play at most two matches per kickoff time
 forall(i in Teams, t in T)
  sum(s in S, m in matchForTeam[i]) X[m][s][t] <= 2;
  

 forall(s in S, t in T, m in MD1)
  X[m][s][t] <= K["1"][s][t];
 forall(s in S, t in T, m in MD2)
  X[m][s][t] <= K["2"][s][t];
 forall(s in S, t in T, m in MD3)
  X[m][s][t] <= K["3"][s][t];
  
 }
 
 
main{
  thisOplModel.generate();
  cplex.populate();
  writeln("number of solutions: " + cplex.getSolnPoolNsolns());
  writeln("Average objective value is: " + cplex.getSolnPoolMeanObjValue());
  }
  
亞歷克斯·弗萊舍

是的,在腳本編寫中,您可以循環到解決方案池中的所有解決方案。

https://github.com/AlexFleischerParis/zooopl/blob/master/zooseveral.mod

    int nbKids=300;
    float costBus40=500;
    float costBus30=400;
     
    dvar int+ nbBus40;
    dvar int+ nbBus30;
     
    //minimize
     //costBus40*nbBus40  +nbBus30*costBus30;
     
    subject to
    {
     40*nbBus40+nbBus30*30>=nbKids;
    }

    execute
    {
    writeln("nbBus40 = ",nbBus40," and nbBus30 = ",nbBus30," and the cost is ",costBus40*nbBus40  +nbBus30*costBus30);
    }

     main {
    cplex.solnpoolintensity=4;

        thisOplModel.generate();
        cplex.solve();
        if (cplex.populate()) {
          var nsolns = cplex.solnPoolNsolns;
          
          
          writeln("Number of solutions found = ",nsolns);
          writeln();
          for (var s=0; s<nsolns; s++) {
            thisOplModel.setPoolSolution(s);
            thisOplModel.postProcess();
          }
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章