0

I am having trouble automating the testing of several scenarios with CPLEX.

I want to test several scenarios where I play around with the weight of the weighted sum of my objective function and the ratio.

Example: W1=0.2, W2 = 0.7, and W3=0.1 with

ratio\[1\]=0
ratio\[2\]=0
ratio\[3\]=0

to start with. Then, I want my code to increment only ratio[3] from 0.1 to 0.7.

The problem I have is that my code only executes the first base value. It writes all the different W and ratio[i] values to my Excel file, but it always keeps the same value for my parameters. How can I link my constraints to the main? Compared to my first message, I have indicated how I declare my variables and parameters for clarity. In addition, I tried adding thisOplModel.W1 = currentW[1], but this gives me an error message.

Here are my variables:

  • COUTTOTAL: Total cost to complete the project.

  • Cmax: Time to complete the project.

  • QTEREMP: Quantity reused. Compared to the other two, we want to maximize this variable.

Parameters:

  • W1, W2, and W3 are the weights assigned by the user.

  • S1, S2, and S3 are correction factors to ensure that each variable has the same degree of importance.

  • esti***: Maximum and minimum values depending on whether I maximize or minimize just one variable.

FYI: I tried using Gemini to help me execute the automation portion. I can change that part without any problems.

code.mod

float estiTempsMax =...;
float estiCoutMax=...;
float estiTempsMin =...;
float estiCoutMin=...;
float estiQTEMin=...;
float estiQTEMax=...;
float W1;
float W2;
float W3;
float WMAX=...;
float S1=W1/WMAX;
float S2=W2/WMAX;
float S3=W3/WMAX;
float W_combinaisons[Mode][Mode]=...;
float ratio[Mode];
float ratio_script[Mode]= [0, 0, 0];

dvar int Cmax;
dvar float QTEREMP;
dvar float COUTTOTAL;
dexpr float TestCOU = ctCont+ ctLiv + ctMO  + ctDecMat + ctEquip +  ENF +ctRecyc - gain;
dexpr float TestQTE = sum(m in Mat) Qm[3][m];

minimize W1 *S1*((TOTALCOST-estiMinCost)/(estiMaxCost-estiMinCost)) +     W2*S2*((Cmax-estiMinTime)/(estiMaxTime-estiMinTime)) - W3*S3*((QTEREMP-estiMinQTEM)/(estiMaxQTEM-estiMinQTEM));

//Constrait....
subject to {
...
    forall(i in Mode)
        CtRatioLiaison: ratio[i] == ratio_script[i]; //I'M TRYING TO LINK MY PARAMETERS WITH THE MAIN
...
}


main {
    var ofile = new IloOplOutputFile("resultats_scenarios_ratio2_varie.csv");
    ofile.writeln("W1;W2;W3;ratio[1];ratio[2];ratio[3];Objectif;COUTTOTAL;Cmax;QTEREMP");

    var r2_start = 0.0;
    var r2_end = 0.2;
    var r2_step = 0.1;

    for (var r2_val = r2_start; r2_val <= r2_end + 0.0001; r2_val += r2_step) { 
        for (var w_index = 1; w_index <= 3; w_index++) { 
            
            var currentW = thisOplModel.W_combinaisons[w_index]; 
            
            W1 = currentW[1];
            W2 = currentW[2];
            W3 = currentW[3];

            for (var r3 = 0.0; r3 <= 0.7 + 0.0001; r3 += 0.1) {
                
             
                var tempRatio = new Array(4); // Taille 4 pour supporter les index 1, 2, 3

                tempRatio[1] = 0.0;     
                tempRatio[2] = r2_val;  
                tempRatio[3] = r3;     
                
                 thisOplModel.generate()
                writeln("--- Résolution : R2=", ratio_script[2], ", W=(", W1, ", ", W2, ", ", W3, ") et R3=", ratio_script[3], " ---");

        if (cplex.solve()) {
    
            ofile.writeln(W1, ";", W2, ";", W3, ";", 
                    //CORRECTION : Utiliser ratio_script au lieu de ratio
                                ratio_script[1], ";", ratio_script[2],    ";",ratio_script[3], ";",cplex.getObjValue(), ";", thisOplModel.TestCOU, ";", thisOplModel.Cmax, ";", thisOplModel.QTEREMP);

} 
                else {
                     ofile.writeln(W1, ";", W2, ";", W3, ";", 
                    ratio_script[1], ";", ratio_script[2], ";", ratio_script[3], ";",
                    "No Solution", ";", "N/A", ";", "N/A", ";", "N/A");
                    }
                 }
        }
    }

enter image description here

Any solution ?

1 Answer 1

0

While github is down let me share the examples about updating an array and solving again I shared at Change a 2D array and solve again

Suppose you have sub2d.mod

    int y[1..2][1..2]=...;

    execute
    {
    writeln("y=",y);
    }

    dvar float x;

    maximize x;
    subject to {
      x<=sum(i in 1..2, j  in 1..2) y[i][j];
    }

Then

if you run

   int a[1..2][1..2];

    main {
      var source = new IloOplModelSource("sub2d.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
     
     
     
      for(var k=11;k<=15;k++)
      {
      var opl = new IloOplModel(def,cplex);
     
     
        
      var data2= new IloOplDataElements();
     
      data2.y=thisOplModel.a;
      data2.y[1][1]=k;
      opl.addDataSource(data2);
     
      opl.generate();

      if (cplex.solve()) {
         writeln("OBJ = " + cplex.getObjValue());
      } else {
         writeln("No solution");
      }
      data2.end();
     opl.end();
     
     
    }  
     
    }

you get

    y= [[11 0]
             [0 0]]
    OBJ = 11
    y= [[12 0]
             [0 0]]
    OBJ = 12
    y= [[13 0]
             [0 0]]
    OBJ = 13
    y= [[14 0]
             [0 0]]
    OBJ = 14
    y= [[15 0]
             [0 0]]
    OBJ = 15

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.