Topic
  • 4 replies
  • Latest Post - ‏2013-10-03T15:56:32Z by CplexUser1453
CplexUser1453
CplexUser1453
25 Posts

Pinned topic Assignement by value in OPL script for non scalars

‏2013-10-01T12:14:20Z |

Hello

I have a question that may be really stupid (but looking at the documentation and the forum I did not find anything useful) :

I would like to use assignment by value rather that the standard assignement by reference in OPL Script for arrays. Can it be done in OPL script?

To be even more specific : let's say I want to modify the LagrangianRelaxation example in such a way that I am reusing the solution of the m2 and m3 models in the next iteration as the initial solution. How can I do that correctly?

I can't store the solution in an array because with the default behaviour a reference is passed and not a value. By calling m2.end() and m3.end() I loose the solution. Not calling m2.end() and m3.end() has obvious consequences...

Thanks a lot!

  • AlexFleischer
    AlexFleischer
    1657 Posts

    Re: Assignement by value in OPL script for non scalars

    ‏2013-10-01T19:02:52Z  

    Hi,

     

    why not using a scrip array to store the solution ?

    Do you know the script syntax var a=new Array(); ?

     

    Regards

  • CplexUser1453
    CplexUser1453
    25 Posts

    Re: Assignement by value in OPL script for non scalars

    ‏2013-10-02T16:40:05Z  

    Hi,

     

    why not using a scrip array to store the solution ?

    Do you know the script syntax var a=new Array(); ?

     

    Regards

    Yes I tried to store the solution in an array.

    However, when I wanted to access elements of the array after calling the .end() method for the model that generated the solution, I got a segmentation error.

    That is logical as the OPL documentation says that non scalars are passed by reference by the assignment operator. Therefore, if I understand things correctly, calling the end() method means that the memory space allocated for the model object is freed and my my array variable points to nothing, hence the segmentation error.

    To be more specific, let's take the LagrangianRelaxation example. I create a new array object data at the end of the loop to store the Ship variables :

    data =  new Array();
    for (i in thisOplModel.cities) {
        data[i] =  new Array();
        for (j in thisOplModel.cities) {
              data[i][j] =  0;
        }
     }

    for (i in thisOplModel.cities) {
        for (j in thisOplModel.cities) {
             data[i][j] = m2.Ship[i][j];
       }
    }

    Now if I call write :

       writeln(data[1][1]);
      m2.end();

    all is fine

     

    However

      m2.end();
      writeln(data[1][1]);

    gives me a segmentation error.

    The only work around I currently know (aside from not calling the end() method) is to write the variables to a .txt file and reading it at the beginning of the next iteration. I would like to know if there is anything more inteligent...

    Thanks a lot!

     

  • AlexFleischer
    AlexFleischer
    1657 Posts

    Re: Assignement by value in OPL script for non scalars

    ‏2013-10-03T15:20:45Z  

    Yes I tried to store the solution in an array.

    However, when I wanted to access elements of the array after calling the .end() method for the model that generated the solution, I got a segmentation error.

    That is logical as the OPL documentation says that non scalars are passed by reference by the assignment operator. Therefore, if I understand things correctly, calling the end() method means that the memory space allocated for the model object is freed and my my array variable points to nothing, hence the segmentation error.

    To be more specific, let's take the LagrangianRelaxation example. I create a new array object data at the end of the loop to store the Ship variables :

    data =  new Array();
    for (i in thisOplModel.cities) {
        data[i] =  new Array();
        for (j in thisOplModel.cities) {
              data[i][j] =  0;
        }
     }

    for (i in thisOplModel.cities) {
        for (j in thisOplModel.cities) {
             data[i][j] = m2.Ship[i][j];
       }
    }

    Now if I call write :

       writeln(data[1][1]);
      m2.end();

    all is fine

     

    However

      m2.end();
      writeln(data[1][1]);

    gives me a segmentation error.

    The only work around I currently know (aside from not calling the end() method) is to write the variables to a .txt file and reading it at the beginning of the next iteration. I would like to know if there is anything more inteligent...

    Thanks a lot!

     

    Hi,

     

    instead of

    data[i][j] = m2.Ship[i][j];

    can you try

    data[i][j] = 1.0*m2.Ship[i][j];

    ?

    So that the ref copy turns into a value copy

     

    regards

  • CplexUser1453
    CplexUser1453
    25 Posts

    Re: Assignement by value in OPL script for non scalars

    ‏2013-10-03T15:56:32Z  

    Hi,

     

    instead of

    data[i][j] = m2.Ship[i][j];

    can you try

    data[i][j] = 1.0*m2.Ship[i][j];

    ?

    So that the ref copy turns into a value copy

     

    regards

    Thank A LOT!!!

    That is exactly what I was looking for - now I can copy the solution values of interest and reuse them later while dealing with memory in a correct fashion.