Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

How can we give an initial solution using OPL script?

  • 1.  How can we give an initial solution using OPL script?

    Posted Wed May 14, 2014 10:06 AM

    Originally posted by: MBEUTCHA


    Hi,

    I am using CPLEX 12.6 with IBM Optimization studio to solve a MIP problem. I want to help CPLEX to find an initial solution for the problem. For clarity porpose, let say I have an array of boolean decision variable Centrales defined on a Tuple set  Configuration

    dvar boolean Centrales[ Configuration] ;

    where Configuration is a set of tuple Config

    tuple Config{

       key int centrale;

       key int Pas;

        key string myConfig;

       int NbreGroupes;

    }

     I want to use OPL script in a main block to warmstart the model by giving some values of the decision variables Centrales; As far as I search, it seems to me that it very easy to warmstart a model using  Cplex::IloOplCplexVectors()  or Cplex:: addMipStart(variables, values). My concern is more on the method to  set the mapping between variables and values. I find an example where they use IloTupleIterator() to add variables in an IloNumVarArray and values to an IloNumArray

    https://www.ibm.com/developerworks/community/forums/html/topic?id=2b7b0b40-aab7-480d-aedf-0946abcee821

    But when I tried it, Cplex throws the error

    unknown variable 'IloTupleIterator' 

    When I look through the OPL Reference Manual, I see that objects such as ' IloTupleIterator ' , 'Ilotuple' are not available.  I would appreciate any help on that topic.

    Thanks.

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How can we give an initial solution using OPL script?

    Posted Wed May 14, 2014 10:17 AM

    Hi,

    have you had a look at the example warmstart ?

     

    (In CPLEX_Studio126\opl\examples\opl\warmstart)

     

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How can we give an initial solution using OPL script?

    Posted Wed May 14, 2014 10:48 AM

    Originally posted by: MBEUTCHA


    Hi,

    thanks for you reply.

    I have indeed looked at the warmstart example. But unfortunately It didn't help to solve my problem. In the warmstart example, the decision variable is indexed on a range 1..10 and they give a complete solution for their initialization. I want to give a partial intialization and I need to select specific variables in the Centrales decision variables array ( a subset of the Configuration tuple set ) to affect them specific values.  Is it possible or do we need to give a value for all the decision variable ?

     

    Regards.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How can we give an initial solution using OPL script?

    Posted Wed May 14, 2014 01:26 PM

    Hi,

    ok so let me change it a bit and use addMIPstart

    range r = 1..10;
    dvar int+ x[r];
    dvar int+ x2[r][1..1];
    dvar int+ y[r];
    // The following array of values (defined as data) will be used as
    // a starting solution to warm-start the CPLEX search.
    float values[i in r] = (i==5)? 10 : 0;
    float values2[i in r][j in 1..1] = (i==5)? 10 : 0;


    minimize
      sum( i in r ) x[i] + sum( j in r ) y[j];
    subject to{
      ctSum:    
        sum( i in r ) x[i] >= 10;
      forall( j in r )
        ctEqual:
          y[j] == j;
    }

    main{
      thisOplModel.generate();  
      var def = thisOplModel.modelDefinition;   
      // Default behaviour
      writeln("Default Behaviour");
      var cplex1 = new IloCplex();
      var opl1 = new IloOplModel(def, cplex1);
      opl1.generate();
      cplex1.solve();   
      writeln(opl1.printSolution());
      // Setting initial solution
      writeln("Setting initial solution");
      var cplex2 = new IloCplex();
      var opl2 = new IloOplModel(def, cplex2);
      opl2.generate();
     
     
     
      cplex2.addMIPStart(opl2.x2[5],opl2.values2[5],1,0);
     
      cplex2.solve();   
      writeln(opl2.printSolution());

      opl1.end();
      cplex1.end();
      opl2.end();
      cplex2.end();
      0;
    }

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: How can we give an initial solution using OPL script?

    Posted Wed May 14, 2014 03:03 PM

    Originally posted by: MBEUTCHA


    Hello, 

    I have two questions concerning your reply.

    First, isn't it useless to set a warmstart for the x2 decision variables while they don't appear in the model ( objective and constraints) and thus, won't be very usefull to help Cplex find a solution?

    Second, I would like to clarify my concern. It is actually very simple.  In your last post, you extracted only one variable, but if we need to extract a subset of decision variables, it would be very common to define a VarAarray in the main block, copy all the variables we want to set and give it to addMIPStart. And there is my problem, It seems impossible to create an array of decision variables outside of the model. May be we could replace them with another object, but I haven't figure out what we could use. Do you have any idea?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: How can we give an initial solution using OPL script?

    Posted Thu May 15, 2014 04:04 AM

    Hi,

    for your first question, you re right and I forgot to add

    forall(i in r) x[i]==x2[i][1];

    for your second question, I wonder whether you want really to use WARM START. What you may want to do is add constraint.

    Then you could try:

    opl2.x[5].UB=opl2.values[5];
    opl2.x[5].LB=opl2.values[5];
     

    Regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: How can we give an initial solution using OPL script?

    Posted Thu May 15, 2014 11:53 AM

    Originally posted by: MBEUTCHA


    You are absolutely right. I think the following code wil work.

    range r = 1..10;
    dvar int+ x[r];
    dvar int+ y[r];
    // The following array of values will be use to see if an initial solution could be find

    float values[i in 1..2] = (i==1)? 10 : 0;


    minimize
      sum( i in r ) x[i] + sum( j in r ) y[j];
    subject to{
      ctSum:    
        sum( i in r ) x[i] >= 10;
      forall( j in r )
        ctEqual:
          y[j] == j;
    }

    main{
      thisOplModel.generate();  
      var def = thisOplModel.modelDefinition;   
      // Searching for an initial solution
      writeln( Searching the initial solution");
      var cplex1 = new IloCplex();

      cplex1.cloktype = 1; 

      cplex1.tilim = 180;  // we set the overall time of optimization to 3 mins

      cplex1.IntSolLim = 1;  // we ask Cplex to stop if it finds one integer solution

     
      var opl1 = new IloOplModel(def, cplex1);

    opl1.x[1].UB = opl1.values[1];

    opl1.x[1].LB = opl1.values[1];

    opl1.x[2].UB = opl1.values[2];

    opl1.x[2].LB = opl1.values[2];


      opl1.generate();


      if( cplex1.solve()){

            // if a solution has been found within the time limit, we are sure that x[1] = 10 and x[2] = 0;

           // this solution will be the starting point.
           writeln(opl1.printSolution());
           // Setting initial solution
            writeln("Setting initial solution");
            var cplex2 = new IloCplex();
             var opl2 = new IloOplModel(def, cplex2);
             opl2.generate();

              cplex2.addMIPStart(opl2.x , opl1.x.solutionValue,1,0);
     
             cplex2.solve();   
              writeln(opl2.printSolution());

              opl1.end();
              cplex1.end();
              opl2.end();
              cplex2.end();

              0;
              }

               else{

                        writeln(" Starting solution not found within the available time");

                          cpelx1.setDefaults();

                           ...

                           cplex1.end();

                           opl1.end();

                            0;

             }

           


    #DecisionOptimization
    #OPLusingCPLEXOptimizer