IC5Notice: We have upgraded developerWorks Community to the latest version of IBM Connections. For more information, read our upgrade FAQ.
Topic
  • 4 replies
  • Latest Post - ‏2013-07-30T14:32:32Z by EBKN_Eduardo_Aime
EBKN_Eduardo_Aime
EBKN_Eduardo_Aime
7 Posts

Pinned topic Starting solution in OPL Script

‏2013-07-28T17:00:21Z |

 Hello,

Actually (v12.5.1),  can I set a starting solution for a CP model in OPL Script?

In other words, can be made a CP version of the WarmStart.mod example?

Thank you very much for your help

  • Philippe_Refalo
    Philippe_Refalo
    48 Posts
    ACCEPTED ANSWER

    Re: Starting solution in OPL Script

    ‏2013-07-30T10:05:27Z  

    Thanks a lot Philippe.

     

    I tried the following:

    The model is syntactically correct.

    Runs well the first solution, but in the second run indefinitely.

    I'm not sure how to do the step: "fill solution".

     

    /*********************************************

     * OPL 12.5.1.0 Model

     *********************************************/

    using CP;

     

    ranger = 1..10;

    dvar int+x[r];

    dvar int+y[r];

    // The following array of values (defined as data) will be used as

    // a starting solution to warm-start the CP search.

    int RestartValues[iin r] = (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 ThisModelDef = thisOplModel.modelDefinition;

      /* Default behaviour */

      writeln("Default Behaviour");

      var cp1 = new IloCP();

      var opl1 = new IloOplModel(ThisModelDef, cp1);

      opl1.generate();

      cp1.solve();    

      writeln(opl1.printSolution());  /* Solution OK */

     

     /* Setting initial solution  */

      writeln("Setting Initial Solution ");

      var cp2 = new IloCP();

      var opl2 = new IloOplModel(ThisModelDef, cp2);

      opl2.generate();

     

      var RestartValues = thisOplModel.RestartValues ;

      var x = thisOplModel.x;

      var cpSolution = new IloOplCPSolution();

      for(var i = 1; i=10; i++)  

          cpSolution.setValue(x[i], RestartValues[i]);  /* Is this the right form to assign values ?? */

      cp2.SetStartingPoint(cpSolution);

     

      cp2.solve();  /* non-stop running !! */

      writeln(opl2.printSolution());

     

      opl1.end();

      cp1.end();

      opl2.end();

      cp2.end();

    }

     

    //OUTPUT:

    //Default Behaviour

    //// solution with objective 65

    //x = [0 0 0 10 0 0 0 0 0 0];

    //y = [1 2 3 4 5 6 7 8 9 10];

    //

    //Setting Initial Solution

    There is no "non stop run" in CP Optimizer : the loop that populates the solution must contain i == 10 and not i = 0 and you need to use cp2.setStartingPoint(cpSolution); with a lower case s. 

    In this simple case, CP Optimizer finds a solution quickly by itself and does not make use of the starting point. This happens sometimes when the model is simple. 

    Philippe

  • Philippe_Refalo
    Philippe_Refalo
    48 Posts

    Re: Starting solution in OPL Script

    ‏2013-07-29T08:19:58Z  

    With CP Optimizer providing a starting point will look like this:

      var sp = new IloOplCPSolution(); 
      // fill solution 
      cp.setStartingPoint(sp);
      cp.solve();  

    You do not need to provide a value for every variables, in this case the assignments will be used as an indication for finding a first feasible solution. 

    Regards

    Philippe

  • EBKN_Eduardo_Aime
    EBKN_Eduardo_Aime
    7 Posts

    Re: Starting solution in OPL Script

    ‏2013-07-29T15:23:21Z  

    With CP Optimizer providing a starting point will look like this:

      var sp = new IloOplCPSolution(); 
      // fill solution 
      cp.setStartingPoint(sp);
      cp.solve();  

    You do not need to provide a value for every variables, in this case the assignments will be used as an indication for finding a first feasible solution. 

    Regards

    Philippe

    Thanks a lot Philippe.

     

    I tried the following:

    The model is syntactically correct.

    Runs well the first solution, but in the second run indefinitely.

    I'm not sure how to do the step: "fill solution".

     

    /*********************************************

     * OPL 12.5.1.0 Model

     *********************************************/

    using CP;

     

    ranger = 1..10;

    dvar int+x[r];

    dvar int+y[r];

    // The following array of values (defined as data) will be used as

    // a starting solution to warm-start the CP search.

    int RestartValues[iin r] = (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 ThisModelDef = thisOplModel.modelDefinition;

      /* Default behaviour */

      writeln("Default Behaviour");

      var cp1 = new IloCP();

      var opl1 = new IloOplModel(ThisModelDef, cp1);

      opl1.generate();

      cp1.solve();    

      writeln(opl1.printSolution());  /* Solution OK */

     

     /* Setting initial solution  */

      writeln("Setting Initial Solution ");

      var cp2 = new IloCP();

      var opl2 = new IloOplModel(ThisModelDef, cp2);

      opl2.generate();

     

      var RestartValues = thisOplModel.RestartValues ;

      var x = thisOplModel.x;

      var cpSolution = new IloOplCPSolution();

      for(var i = 1; i=10; i++)  

          cpSolution.setValue(x[i], RestartValues[i]);  /* Is this the right form to assign values ?? */

      cp2.SetStartingPoint(cpSolution);

     

      cp2.solve();  /* non-stop running !! */

      writeln(opl2.printSolution());

     

      opl1.end();

      cp1.end();

      opl2.end();

      cp2.end();

    }

     

    //OUTPUT:

    //Default Behaviour

    //// solution with objective 65

    //x = [0 0 0 10 0 0 0 0 0 0];

    //y = [1 2 3 4 5 6 7 8 9 10];

    //

    //Setting Initial Solution

  • Philippe_Refalo
    Philippe_Refalo
    48 Posts

    Re: Starting solution in OPL Script

    ‏2013-07-30T10:05:27Z  

    Thanks a lot Philippe.

     

    I tried the following:

    The model is syntactically correct.

    Runs well the first solution, but in the second run indefinitely.

    I'm not sure how to do the step: "fill solution".

     

    /*********************************************

     * OPL 12.5.1.0 Model

     *********************************************/

    using CP;

     

    ranger = 1..10;

    dvar int+x[r];

    dvar int+y[r];

    // The following array of values (defined as data) will be used as

    // a starting solution to warm-start the CP search.

    int RestartValues[iin r] = (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 ThisModelDef = thisOplModel.modelDefinition;

      /* Default behaviour */

      writeln("Default Behaviour");

      var cp1 = new IloCP();

      var opl1 = new IloOplModel(ThisModelDef, cp1);

      opl1.generate();

      cp1.solve();    

      writeln(opl1.printSolution());  /* Solution OK */

     

     /* Setting initial solution  */

      writeln("Setting Initial Solution ");

      var cp2 = new IloCP();

      var opl2 = new IloOplModel(ThisModelDef, cp2);

      opl2.generate();

     

      var RestartValues = thisOplModel.RestartValues ;

      var x = thisOplModel.x;

      var cpSolution = new IloOplCPSolution();

      for(var i = 1; i=10; i++)  

          cpSolution.setValue(x[i], RestartValues[i]);  /* Is this the right form to assign values ?? */

      cp2.SetStartingPoint(cpSolution);

     

      cp2.solve();  /* non-stop running !! */

      writeln(opl2.printSolution());

     

      opl1.end();

      cp1.end();

      opl2.end();

      cp2.end();

    }

     

    //OUTPUT:

    //Default Behaviour

    //// solution with objective 65

    //x = [0 0 0 10 0 0 0 0 0 0];

    //y = [1 2 3 4 5 6 7 8 9 10];

    //

    //Setting Initial Solution

    There is no "non stop run" in CP Optimizer : the loop that populates the solution must contain i == 10 and not i = 0 and you need to use cp2.setStartingPoint(cpSolution); with a lower case s. 

    In this simple case, CP Optimizer finds a solution quickly by itself and does not make use of the starting point. This happens sometimes when the model is simple. 

    Philippe

  • EBKN_Eduardo_Aime
    EBKN_Eduardo_Aime
    7 Posts

    Re: Starting solution in OPL Script

    ‏2013-07-30T14:32:32Z  

    There is no "non stop run" in CP Optimizer : the loop that populates the solution must contain i == 10 and not i = 0 and you need to use cp2.setStartingPoint(cpSolution); with a lower case s. 

    In this simple case, CP Optimizer finds a solution quickly by itself and does not make use of the starting point. This happens sometimes when the model is simple. 

    Philippe

    You are right Philippe
    thank you very much

    Eduardo.