Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

How can I convert a Boolean decision variable to Float decision variable using OPL scripting

ALEX FLEISCHER

ALEX FLEISCHERMon December 16, 2019 12:12 PM

ALEX FLEISCHER

ALEX FLEISCHERTue December 17, 2019 07:41 AM

ALEX FLEISCHER

ALEX FLEISCHERTue December 17, 2019 10:34 AM

  • 1.  How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Tue April 28, 2015 03:00 PM

    Originally posted by: bindasmariner


    I a trying to implement a Rolling Horizon Heuristic (RHH) for a large scale MIP problem. In this method we iteratively treat a small subset of the planning horizon as consisting of binary variables, the earlier binary solutions are freezed and later are relaxed. The ssubproblem is then extended to the next planning period and the algorithm continues till the end of problem planning horizon. So I would like to know how can I convert some binary variables into floats and vice-versa iteratively for my MIP using OPL scripting language.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Thu April 30, 2015 12:42 PM

    Hi,

    let me quote the documentation:

    OPL allows relaxation of integer constraints on decision variables. With OPL, there is a simple way to relax all integer decision variables at once and to convert a MIP problem to an LP problem: just call the method IloOplModel.convertAllIntVars as shown in the example convert_example.mod

    that is in examples/opl

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Tue May 05, 2015 02:51 PM

    Originally posted by: bindasmariner


    Thanks Alex! I am trying to convert a subset of decision variables at a time rather than all of them together. I came across a method of IloCplex, as mentioned in the OPL Classes. The description is as follows:

    conversion(dvar, type, name)
    Creates an IloConversion object, with the specified name, for converting the type of a variable in a model. See the reference manual for IloCplex. Type is one of "ILOINT", "ILOFLOAT", "ILOBOOL".

    So far I tried my best but could not correctly use this method in my code. Can you please help me out with this? My understanding is that this method works only in interfaces. But then, why it is mentioned as a OPL class method?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Wed May 06, 2015 04:35 AM

    Hi

    then what you can do is relax the integrity constraints one by one.

    Let me give you an example:

    dvar float x in 0..9.2;
    dvar float y in 0..9.2;

    dvar int intx;
    dvar int inty;


    maximize x+y;
    subject to
    {
    ctx:intx-x==0;
    cty:inty==y;

    }

    execute
    {
    writeln("x and y : ",x," ",y);
    }

    main
    {
    thisOplModel.generate();
    cplex.solve();
    thisOplModel.postProcess();

    cplex.setLb(thisOplModel.ctx,-Infinity);
    cplex.setUb(thisOplModel.ctx,Infinity);
    cplex.solve();
    thisOplModel.postProcess();
    }
     

    which gives

    x and y : 9 9
    x and y : 9.2 9

     

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Sat June 17, 2017 03:27 AM

    Originally posted by: Hakeem-Ur-Rehman


    Hi: In my Case i need to convert all the integer variables into continuous 


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Sun June 18, 2017 02:29 AM

    Hi,

    then you should use convertAllIntvars Which is a méthod of IloOplModel

    regards

     

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri January 05, 2018 07:13 AM

    Originally posted by: JUAN_HOME


    Hi. I am trying to implement a Rolling Horizon Heuristic (RHH) for a large scale MIP problem. I used the the relax and freeze that AlexFleischer
     shows. For one variable is OK.

    But for  for many vaiables in the second iteration CPLEX OPL shows:

    *** ERROR[GENERATE_103]: OPL cannot extract expression:0.
    How can I solve this problem ?? 
    Thanks.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri January 05, 2018 07:19 AM

    Hi

    if you attach all your files other users could try to help you

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri January 05, 2018 07:40 AM

    Originally posted by: JUAN_HOME


    Hi again. Thanks


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri January 05, 2018 10:00 AM

    Hi,

    can you try with PSDE2.mod ?

    Once you start to modify some UB and LB the solution gets invalid.

    regards

    PS: Many interesting posts at https://www.ibm.com/developerworks/community/forums/html/topic?id=0d0b2396-3b48-4638-b032-3b9ea74f1a11&ps=25


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 11.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri January 05, 2018 01:02 PM

    Originally posted by: JUAN_HOME


    Thank you very much for responding so quickly and accurately. The file PSDE2.mod worked exacly as I needed it.

    
    
    
    

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 12.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting



  • 13.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri April 21, 2017 12:46 AM

    Originally posted by: Hakeem-Ur-Rehman


    Hi, I a trying to implement a Rolling Horizon Heuristic (RHH) for a MIP problem. In this method we iteratively treat a small subset of the planning horizon as consisting of binary variables, the earlier binary solutions are frozen and later are relaxed. The sub-problem is then extended to the next planning period and the algorithm continues until the end of problem planning horizon. So I would like to know how can I implement this heuristic OPL scripting language. Please explain with the small example. Thanks in Advance.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 14.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri April 21, 2017 02:48 AM

    Hi,

    let me give you a small example on how to freeze and relax:

    dvar float+ Gas;
        dvar float+ Chloride;  
         
        maximize 40 * Gas+ 50 * Chloride;

        subject to {

          ctMaxTotal:    
              Gas+ Chloride<= 50;

            ctMaxTotal2:   
                3 * Gas+ 4 * Chloride<= 180;

          ctMaxChloride:
            Chloride<= 40;

           ctEmpty:
             Gas<=infinity;
        }

        execute
        {
        writeln("Gas = ",Gas);
        writeln("Chloride = ",Chloride);
        writeln("Objective = ",cplex.getObjValue());
        }
         
         main
        {
           thisOplModel.generate();
           cplex.solve();
           thisOplModel.postProcess();
           
           writeln();
           writeln("And then we freeze Gas 10");
           writeln();
           
           thisOplModel.Gas.UB=10;
           thisOplModel.Gas.LB=10;
           
           cplex.solve();
           thisOplModel.postProcess();
           
            writeln();
           writeln("And then we relax");
           writeln();
           
           thisOplModel.Gas.UB=1000;
           thisOplModel.Gas.LB=0;
           
           cplex.solve();
           thisOplModel.postProcess();
           
           
         }

     

    gives

    Gas = 20
    Chloride = 30
    Objective = 2300

    And then we freeze Gas 10

    Gas = 10
    Chloride = 37.5
    Objective = 2275

    And then we relax

    Gas = 20
    Chloride = 30
    Objective = 2300

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 15.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri April 21, 2017 02:58 AM

    Originally posted by: Hakeem-Ur-Rehman


    Thanks Alex Fleischer


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 16.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri April 21, 2017 01:02 PM

    Originally posted by: Hakeem-Ur-Rehman


    Hi, Alex, I needed more help from you. Can you please guide me, how can I implement  Relax and fix procedure on rolling horizon basis. Actually, I want to Apply this procedure to the problem by decomposing the time into four periods, Each period has it own demand which must be fulfilled at the end of that period. 


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 17.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Thu April 27, 2017 09:25 PM

    Originally posted by: Hakeem-Ur-Rehman


    Hi, Alex, I needed more help from you. Can you please guide me, how can I implement  Relax and fix procedure on rolling horizon basis. Actually, I want to Apply this procedure to the problem by decomposing the time into four periods, Each period has it own demand which must be fulfilled at the end of that period. 


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 18.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri June 16, 2017 04:53 PM

    Originally posted by: felycite28


    Hello Hakeem ,

    I am workıng on the  same relax and fıx algorıthm and stucked ın the same poınt wıth you .

    Could you find a solution for your model Do ou have any example for Relax and Fıx ?

    Thank you so much ın advance

     

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 19.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri June 02, 2017 12:46 PM

    Originally posted by: felycite28


    Hello ,

    I am working on the same method, relax and fix . I have coded my model on cplex and got the results . For this heuristic, I divided into whole time period of the problem instance  into two part. For the first part which is from t=1..5, I am taking y[k][z] (binary variable) 0 or 1, and the rest of the planning horizon which is t=6..T, y[k][z] will be relaxed . I am trying to do that with flow control but I stuck in several points .

    I found something like

    IloConversion conversion = cplex.conversion(y[k][z], IloNumVarType.Float);

    to change the type of the variable

    but I do not know how to use it exactly and how to develop for loops to solve the problem partially ? Can you help me please ?

      for ( i in t=1..5)

          y[k][z]=0 or y[k][z]=1

      for ( i in t=6..T)

     

          0<=y[k][z]<=1

    Solve the master problem

     

    Thank you so much in advance

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 20.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri June 02, 2017 04:19 PM

    Hi,

    is your model in OPL or C++ ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 21.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Fri June 02, 2017 04:46 PM

    Originally posted by: felycite28


    Hi ,

    I have developed and tested main model on cplex and wıth  .mod extension and want to solve it  with relax and fıx with opl flow control

    You are rıght ıt ıs similar to previous questions but how can I integrate time periods ınto your model ? To clarıfy ıt more , fore example the main model wıll be solved :when one selected binary varıble ıs binary for period t=1 and for t=2 and the rest of the planning horizon for t=3 , t=4... t=T binary varıble will be  [continous [0,1] How can I do ıt ? I had thought to use for loop but I could not apply it.

     

    Thank you so much ın advance

     

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 22.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Sat June 03, 2017 05:08 AM

    Hi,

    again

    dvar float x in 0..9.2;
    dvar float y in 0..9.2;

    dvar int intx;
    dvar int inty;


    maximize x+y;
    subject to
    {
    ctx:intx-x==0;
    cty:inty==y;

    }

    execute
    {
    writeln("x and y : ",x," ",y);
    }

    main
    {
    writeln("solve model");

    thisOplModel.generate();
    cplex.solve();
    thisOplModel.postProcess();

    writeln("relax integrity constraint on x and solve model");
    cplex.setLb(thisOplModel.ctx,-Infinity);
    cplex.setUb(thisOplModel.ctx,Infinity);
    cplex.solve();
    thisOplModel.postProcess();

    writeln("put back integrity constraint on x and solve model");
    cplex.setLb(thisOplModel.ctx,0);
    cplex.setUb(thisOplModel.ctx,0);
    cplex.solve();
    thisOplModel.postProcess();
    }

    that gives

    solve model
    x and y : 9 9
    relax integrity constraint on x and solve model
    x and y : 9.2 9
    put back integrity constraint on x and solve model
    x and y : 9 9

    shows you how to relax integrity constraint on one decision variable and then remove that relaxation

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 23.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Sun June 04, 2017 08:17 AM

    Originally posted by: felycite28


    Hi Alex , thank you so much for your reply. I have a couple of questions: I trıed your model but I am gettıng en error message whıch says "ctx (ct y[z][k] for my case)" does not exist. When I changed ctx wıth the dırectly the name of the boolean variable which ıs  y[z][k], then cplex gives me error "not of type IloConstraint ":What can I do ? (In original model the varıable which I try to relax is binary integer.  So for fixed part I have to take this variable as it is in original and for relaxed part it can take any value between 0 and 1).

    Thank you so much


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 24.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Mon June 05, 2017 12:39 PM

    Hi,

    can you attach your model and data so that other users could try to help you ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 25.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Mon December 16, 2019 12:00 PM

    Originally posted by: rebecca.fries


    Hello everyone,

    for my master thesis I also need to implement a relax and fix heuristic. I already completed the fix and optimize, maybe this could be a starting point.

    I would like to optimize the binary variables for a machine setup Gam[m][k][t] for product k in period t  for period 1+2,( then 3+4, then 5+6) together.

    Whilst periods 3-6 (5-6) are relaxed and are able to be continuous.

    In the second iteration, the binary variables for period 1+2 would be fixed, the bin variables for per 3+4 would be optimized and, 5+6 would be relaxed.

    So all in all 3 iterations.

    I used opl script and my method is a bit different to the one above. I am new to opl and would be really thankful for your help!

     

    Kind regards,

    Rebecca


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 26.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Mon December 16, 2019 12:12 PM

    Hi,

    2 years ago in that thread

    Hi,

    again

    dvar float x in 0..9.2;
    dvar float y in 0..9.2;

    dvar int intx;
    dvar int inty;


    maximize x+y;
    subject to
    {
    ctx:intx-x==0;
    cty:inty==y;

    }

    execute
    {
    writeln("x and y : ",x," ",y);
    }

    main
    {
    writeln("solve model");

    thisOplModel.generate();
    cplex.solve();
    thisOplModel.postProcess();

    writeln("relax integrity constraint on x and solve model");
    cplex.setLb(thisOplModel.ctx,-Infinity);
    cplex.setUb(thisOplModel.ctx,Infinity);
    cplex.solve();
    thisOplModel.postProcess();

    writeln("put back integrity constraint on x and solve model");
    cplex.setLb(thisOplModel.ctx,0);
    cplex.setUb(thisOplModel.ctx,0);
    cplex.solve();
    thisOplModel.postProcess();
    }

    that gives

    solve model
    x and y : 9 9
    relax integrity constraint on x and solve model
    x and y : 9.2 9
    put back integrity constraint on x and solve model
    x and y : 9 9

    shows you how to relax integrity constraint on one decision variable and then remove that relaxation

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 27.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Tue December 17, 2019 06:48 AM

    Originally posted by: rebecca.fries


    Thanks for the quick response,

    I tried this approach already, but it is not working. I get the error message - Scriptinglaufzeitfehler: not of type 'IloConstraint', "[a IloNumVar]". 

    And I dont need it relaxed to infinity, the variable should just be continuous btw 0 and 1; so 0.25 etc

    So I tried to use mod.convertAllIntVars(); as you recommended. I think I applied it wrong since, it is not working either..

    main
    {
    thisOplModel.generate();
    var mod = thisOplModel;
    mod.convertAllIntVars();

    //Relaxation    
        writeln("Relax all=");    
         for(var m=1;m<=mod.M;m++){
         for(var k=1;k<=mod.K;k++){    
         for(var t=1;t<=mod.T;t++){                                
         if(t>=2){                                // here the constraint needs to be relaxed btw 0 and 1 (0.1 etc)
            mod.convertAllIntVars();
                cplex.setLb(mod.Gam[m][k][t], 0);
                cplex.setUb(mod.Gam[m][k][t], 1);                    
                    }}}}    
                    
    //Iterative Process 6 periods
     for (var iteration in mod.T){  
     writeln("Iteration no=",iteration);    
            
    for(var m=1;m<=mod.M;m++){
    for(var k=1;k<=mod.K;k++){    
    for(var t=1;t<=mod.T;t++){    
                                            
                if(t>iteration && iteration==1){    // relaxes all values btw 0 and 1
                    cplex.setLb(mod.Gam[m][k][t],-Infinity);
                    cplex.setUb(mod.Gam[m][k][t], Infinity);}
                            
                if( t == iteration){                   
                    cplex.setLb(mod.Gam[m][k][t],0);
                    cplex.setUb(mod.Gam[m][k][t],0);}            
                        
                if(t==iteration-1){        
                    writeln("fixed period",t," Gam = ",mod.Gam[m][k][t]);                
                    mod.Gam[m][k][t].UB = mod.Gam[m][k][t].LB = mod.Gam[m][k][t].solutionValue;    
                    writeln("======")    
                        }}}}
                    
            if(cplex.solve()){    
                writeln("======");
                mod.postProcess();    
                iteration++;
            }        
            else{
                break;
                writeln("No solution in ",iteration);        
            }
        }
        //mod.postProcess();            
    }


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 28.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Tue December 17, 2019 07:41 AM

    Hi,

    cplex.setUb works on constraints non on decision variables

    See

    dvar int x in 0..10;
    dvar int y in 0..10;

    maximize x+y;
    subject to
    {
      ct:x<=4;
      y<=3;
    }

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

    main

    {
     
      thisOplModel.generate();
      cplex.solve();
      thisOplModel.postProcess();
      thisOplModel.ct.UB=3;
      cplex.solve();
      thisOplModel.postProcess();
      cplex.setUb(thisOplModel.ct,2);
      cplex.solve();
      thisOplModel.postProcess();
      thisOplModel.y.UB=2;
     
      cplex.solve();
      thisOplModel.postProcess();
    }

    which gives

     

    x=4 and y=3
    x=3 and y=3
    x=2 and y=3
    x=2 and y=2

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 29.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Tue December 17, 2019 09:45 AM

    Originally posted by: rebecca.fries


    Thanks that was very helpful !

    I am still struggling with the relax and fix heuristic. I still get an error message  And I think I didnt model the iterations correctly..

    Is it correct that I relax the binary constraint from 0 to 1 so they can be continuous 0.2, 0.4 etc. with the following?
                    mod.Gam[m][k][t].LB= 0;
                    mod.Gam[m][k][t].UB= 1;

     

    I would be really grateful, if you could take a look at my model..

    Kind regards,

    Rebecca

     

    int K = ...;         // products
    int M = ...;         // parallel machines
    int T =... ;         // periods

    dvar boolean Gam[1..M,1..K,0..T];   // setup variable

    main
    {
    thisOplModel.generate();
    var mod = thisOplModel;
    var m, k, t;
    // mod.convertAllIntVars();
    //Relaxation    
        writeln("Relax all=");    
         for(var m=1;m<=mod.M;m++){
         for(var k=1;k<=mod.K;k++){    
         for(var t=1;t<=mod.T;t++){                                
         if(t>1){                // here the constraint needs to be relaxed btw 0 and 1 (0.1 etc)
                mod.Gam[m][k][t].LB= 0;
                mod.Gam[m][k][t].UB= 1;                    
                    } // if(t>=2){    
                    } // T
                    } // K
                    } // M    
    //Iterative Process 6 Perioden
     var iteration =1; // start with iteration number 1 out of 6
     while (iteration <=6){
     writeln("Iteration=",iteration);    
     for(var m=1;m<=mod.M;m++){
         for(var k=1;k<=mod.K;k++){     
             for(var t=1;t<=mod.T;t++){    
                                            
                if(t>iteration && iteration==1){
                    mod.Gam[m][k][t].LB= 0; // relaxes all values btw 0 and 1
                    mod.Gam[m][k][t].UB= 1;
                    }        
                if(t == iteration){        
                    mod.Gam[m][k][t].LB= 0; //determine optimal value
                    mod.Gam[m][k][t].UB= 0;
                    }                
                if(t==iteration-1){        
                writeln("fixed period ",t," Gam = ",mod.Gam[m][k][t]);                
                
                mod.Gam[m][k][t].LB = mod.Gam[m][k][t].UB = mod.Gam[m][k][t].solutionValue;    
                    writeln("======")        
                      }
            }
        }
    }                  
            if(cplex.solve()){    
                writeln("======");
                mod.postProcess();
                iteration++        
            }        
            else{
            break;    
            writeln("No solution in ",iteration);
            }
    }             
        mod.postProcess();            
        }


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 30.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Tue December 17, 2019 10:34 AM

    Hi,

    end of your model you should write

    else{
              writeln("No solution in ",iteration);
            break;    
            
            }

    instead of

    else{

             break;
              writeln("No solution in ",iteration);
            
            
            }

    if you want to see some display telling you that the model is not feasible

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 31.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Wed December 18, 2019 10:09 AM

    Originally posted by: rebecca.fries


    Hi Alex,

    thank you for the tip, but my model is not working yet... it is not relaxing the binary constraint for values bte [0,1] I have read a few posts in this forum now about this issue. I have read sth about using IloConversion .. But I don't know how to use it..

    Another issue I have is that I get this message in the script, so it doesnt show me the value Gam of my decision variable (that should be relaxed btw 0 and 1)

    Sorry for bothering you, but I just recently started using OPL for my masterthesis. I am really grateful for your help!

    This is my current script
    main
    {
    thisOplModel.generate();
    var mod = thisOplModel;  

    //Relaxation    
        writeln("==Relax all==");    
         for(var m=1;m<=mod.M;m++){
         for(var k=1;k<=mod.K;k++){    
         for(var t=1;t<=mod.T;t++){                                
         if(t>1){                // here the constraint needs to be relaxed btw 0 and 1 (0.1 etc)
        
        /* IloConversion conversion = cplex.conversion(mod.Gam[m][k][t], IloNumVarType.Float);
        cplex.add(conversion); */
        
                mod.Gam[m][k][t].LB= 0;
                mod.Gam[m][k][t].UB= 1;        
                writeln("machine ",m," of product ",k," in period ",t,": Gam ",mod.Gam[m][k][t]);            
                    } // if(t>=2){    
                    } // T
                    } // K
                    } // M    
                    
    // Fix and Relax
     var iteration =1;
     while (iteration <= 6){
     writeln("Iteration=",iteration);    
     for(var m=1;m<=mod.M;m++){
         for(var k=1;k<=mod.K;k++){     
             for(var t=1;t<=mod.T;t++){    
                                            
                if(t>iteration && iteration==1){
                    mod.Gam[m][k][t].LB= 0; //should relax all values btw 0 and 1
                    mod.Gam[m][k][t].UB= 1;
                    }        
                if(t == iteration){        
                    mod.Gam[m][k][t].LB= 0; //determine optimal value setup btw. 0 and 1
                    mod.Gam[m][k][t].UB= 1;
                    }                
                if(t==iteration-1){             // set to opt value        
                writeln("fixed periods are: ",t," Gam at machine ",m," of product ",k,mod.Gam[m][k][t]);    
                mod.Gam[m][k][t].LB = mod.Gam[m][k][t].UB = mod.Gam[m][k][t].solutionValue;        
                    writeln("======")        
                      }
            }
        }
    }          
            if(cplex.solve()){    
                writeln("=== ",mod.printSolution());
                writeln(mod.printRelaxation());
                writeln(mod.printConflict())
                mod.postProcess();
                iteration++    
            }        
            else{
            writeln("No solution in ",iteration);
            break;    
            }
    }             
        mod.postProcess();            
        }

     

    Regards,

    Rebecca


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 32.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Wed December 18, 2019 11:48 AM

    Hi

    I think you could have a look at https://www.ibm.com/developerworks/community/forums/html/topic?id=ea982e79-8ea8-473b-ae84-3e729ebb7575&ps=25

    You change some UB and LB and then the model gets infeasible so you can t read sny more the results

     

    if I change your main into

    main
    {
    thisOplModel.generate();
    var mod = thisOplModel;  
    cplex.solve();

    //Relaxation  

    var cop=new Array(10);
                for(var it=0;it<=10;it++) cop[it]=new Array(10);
                for(var it1=0;it1<=10;it1++) for(var it2=0;it2<=10;it2++) cop[it1][it2]=new Array(10);
                for(var m2=1;m2<=mod.M;m2++)  for(var k2=1;k2<=mod.K;k2++) for(var t2=1;t2<=mod.T;t2++)
                    cop[m2][k2][t2]=mod.Gam[m2][k2][t2].solutionValue;  
        writeln("==Relax all==");    
         for(var m=1;m<=mod.M;m++){
         for(var k=1;k<=mod.K;k++){    
         for(var t=1;t<=mod.T;t++){                                
         if(t>1){                // here the constraint needs to be relaxed btw 0 and 1 (0.1 etc)
        
        /* IloConversion conversion = cplex.conversion(mod.Gam[m][k][t], IloNumVarType.Float);
        cplex.add(conversion); */
        
                
        
                mod.Gam[m][k][t].LB= 0;
                mod.Gam[m][k][t].UB= 1;        
                writeln("machine ",m," of product ",k," in period ",t,": Gam ",cop[m][k][t]);            
                    } // if(t>=2){    
                    } // T
                    } // K
                    } // M    
                    
    // Fix and Relax
     var iteration =1;
     while (iteration <= 6){
     writeln("Iteration=",iteration);    
     for(var m=1;m<=mod.M;m++){
         for(var k=1;k<=mod.K;k++){     
             for(var t=1;t<=mod.T;t++){    
                                            
                if(t>iteration && iteration==1){
                    mod.Gam[m][k][t].LB= 0; //should relax all values btw 0 and 1
                    mod.Gam[m][k][t].UB= 1;
                    }        
                if(t == iteration){        
                    mod.Gam[m][k][t].LB= 0; //determine optimal value setup btw. 0 and 1
                    mod.Gam[m][k][t].UB= 1;
                    }                
                if(t==iteration-1){             // set to opt value        
                writeln("fixed periods are: ",t," Gam at machine ",m," of product ",k,cop[m][k][t]);    
                mod.Gam[m][k][t].LB = cop[m][k][t];
                mod.Gam[m][k][t].UB = cop[m][k][t];        
                    writeln("======")        
                      }
            }
        }
    }          
            if(cplex.solve()){    
                writeln("=== ",mod.printSolution());
                //writeln(mod.printRelaxation());
                //writeln(mod.printConflict())
                mod.postProcess();
                iteration++    
            }        
            else{
            writeln("No solution in ",iteration);
            break;    
            }
    }             
        mod.postProcess();            
        }

    then I do not get any error

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 33.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Wed December 18, 2019 01:51 PM

    Originally posted by: rebecca.fries


    Thank you so much Alex!

    I am looking through your material. Do you have an example on how to relax a boolean decisiion variable, in my case Gam[m][k][t] to take continuous values between 0 and 1 ?

    I tried it with setting the bounds to infinity, but that doesnt work

     

    Regards,
    Rebecca


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 34.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Thu December 19, 2019 03:21 AM

    Hi,

    so let me adapt the previous model I shared to binary decision variables:

     dvar float x in 0..1;
        dvar float y in 0..1;

        dvar boolean intx;
        dvar boolean inty;


        minimize abs(x-0.5);
        subject to
        {
        y==1;
          
        ctx:intx-x==0;
        cty:inty==y;

        }

        execute
        {
        writeln("x and y : ",x," ",y);
        }

        main
        {
        writeln("solve model");

        thisOplModel.generate();
        cplex.solve();
        thisOplModel.postProcess();

        writeln("relax integrity constraint on x and solve model");
        cplex.setLb(thisOplModel.ctx,-Infinity);
        cplex.setUb(thisOplModel.ctx,Infinity);
        cplex.solve();
        thisOplModel.postProcess();

        writeln("put back integrity constraint on x and solve model");
        cplex.setLb(thisOplModel.ctx,0);
        cplex.setUb(thisOplModel.ctx,0);
        cplex.solve();
        thisOplModel.postProcess();
        }

     

    which gives

     

    solve model
    x and y : 1 1
    relax integrity constraint on x and solve model
    x and y : 0.5 1
    put back integrity constraint on x and solve model
    x and y : 1 1

    regards

     

    https://medium.com/@alexfleischer_84755/optimization-simply-do-more-with-less-zoo-buses-and-kids-66940178db6


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 35.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Thu December 19, 2019 01:23 PM

    Originally posted by: rebecca.fries


    Thank you very much Alex!

    It wasn't clear to me that I have to add another decision variable and the constraint as a work around :)


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 36.  Re: How can I convert a Boolean decision variable to Float decision variable using OPL scripting

    Posted Mon December 23, 2019 05:57 AM