Topic
9 replies Latest Post - ‏2013-09-24T13:20:24Z by AlexFleischer
SystemAdmin
SystemAdmin
2130 Posts
ACCEPTED ANSWER

Pinned topic How to alter a integer dataElement with an Interface (API C# or Java)

‏2012-09-03T00:51:36Z |
I'am didn't find documentation or example on how to alter a integer dataElement with an Interface (API C# or Java).

I use the mulprod_main exemple to show my problem.

OPL Parameters:
int NbPeriods = ...;
float CapacityResources = ...;

OPL Script data element
var data = produce.dataElements;

In OPL script, I can alter a parameter value this way:

For array or int:
data.Capacity = capFlour;
For int:
data.NbPeriods=3;

With the API.

For array of float I can do it this way:
dataElements.GetElement("Capacity").AsNumMap().Set("flour", capFlour);

For integer, I can't figure how to do the same operation. My problem is that this didn't work:
dataElements.GetElement("NbPeriods").AsInt().Set(2);

The solution is probably obvious but I didn't find anything about that in the documentation or in the examples.

Thank you in advance,

MB
Updated on 2012-09-06T17:40:39Z at 2012-09-06T17:40:39Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    2130 Posts
    ACCEPTED ANSWER

    Re: How to alter a integer dataElement with an Interface (API C# or Java)

    ‏2012-09-04T12:00:59Z  in response to SystemAdmin
    Hi

    I think that your issue is that

    AsInt()

    gives a value and not a reference.
    You should change your int value into an array and then you will able to use

    AsIntMap()

    regards
    • SystemAdmin
      SystemAdmin
      2130 Posts
      ACCEPTED ANSWER

      Re: How to alter a integer dataElement with an Interface (API C# or Java)

      ‏2012-09-04T15:28:04Z  in response to SystemAdmin
      If I treat the integer as an integer array of one by doing that:

      dataElements.GetElement("NbPeriods").AsIntMap().Set(0,2);

      I receive the following error:

      Type attendu pour l'élément "int". (type for element « int »).

      à ILOG.OPL.opl_lang_wrap.SWIGIloExceptionHelper.ThrowIloException(String message)
      à ILOG.OPL.opl_lang_wrapPINVOKE.OplElement_cpp_asIntMap(IntPtr jarg1)
      à ILOG.OPL.OplElement.cpp_asIntMap()
      à ILOG.OPL.OplElement.AsIntMap()
      à Mulprod_main.Mulprod_main.Main(String[] args) dans C:\Program Files\ibm\ILOG\CPLEX_Studio124\opl\examples\opl_interfaces\dotnet\x86_windows_vs2010\CSharp\Mulprod_main\Mulprod_main.cs:ligne 63
      • SystemAdmin
        SystemAdmin
        2130 Posts
        ACCEPTED ANSWER

        Re: How to alter a integer dataElement with an Interface (API C# or Java)

        ‏2012-09-05T08:22:54Z  in response to SystemAdmin
        Hi,

        you should also change NBPeriods from an int to an array in your OPL model

        regards
        • SystemAdmin
          SystemAdmin
          2130 Posts
          ACCEPTED ANSWER

          Re: How to alter a integer dataElement with an Interface (API C# or Java)

          ‏2012-09-05T13:53:41Z  in response to SystemAdmin
          This mean that we can't change a parameter define by an int :
          int NbPeriods = ...;
          with the API in C# or Java?

          MB
  • SystemAdmin
    SystemAdmin
    2130 Posts
    ACCEPTED ANSWER

    Re: How to alter a integer dataElement with an Interface (API C# or Java)

    ‏2012-09-06T17:40:39Z  in response to SystemAdmin
    This is covered in one of IBM technotes:
    http://www-01.ibm.com/support/docview.wss?uid=swg21401478
    • CplexUser1453
      CplexUser1453
      24 Posts
      ACCEPTED ANSWER

      Re: How to alter a integer dataElement with an Interface (API C# or Java)

      ‏2013-09-24T07:16:39Z  in response to SystemAdmin

      So if I understand this thread, the tech note and the mulprod example correctly, you can update non integer data directly, while with integer data it is impossible, is that correct?

      To give an example :

      ...

      model.generate()

      var def = thisOplModel.modelDefinition;
      var dataLocal =  thisOplModel.dataElements;

      dataLocal.myArray[i] = x // possible

      dataLocal.myIntValue = y // impossible

      newModel = new IloOplModel(def,cplex);

      newModel.addDataSource(dataLocal);

      I understand that there is a work around with .dat files as explained in the tech note but I find it a little bit painful...  Please correct me if what I wrote above is wrong.

      Thanks!

      • AlexFleischer
        AlexFleischer
        455 Posts
        ACCEPTED ANSWER

        Re: How to alter a integer dataElement with an Interface (API C# or Java)

        ‏2013-09-24T09:23:46Z  in response to CplexUser1453

        Hi,

         

        do you intend to use javascript API ?

        You have an example of both scalar and not scalar update at

        https://www.ibm.com/developerworks/community/forums/html/topic?id=84f171d3-48fe-4d6a-bfed-5d0837185b5e&ps=25

        is there something there that cannot fit your need ?

        regards

        • CplexUser1453
          CplexUser1453
          24 Posts
          ACCEPTED ANSWER

          Re: How to alter a integer dataElement with an Interface (API C# or Java)

          ‏2013-09-24T12:27:15Z  in response to AlexFleischer

          Hello Alex

          Yes your response was very helpful, I forgot to thank you once saw it a couple of weeks ago.

          If I understood your example and the link provided above correctly, in order to modify a scalar element it is necessary to create a new data object, (var data2= new IloOplDataElements()  )and copy the data that is unchanged (data2.y=thisOplModel.s). The alternative is to have a separate data file with elements that change to create separate data objects that can be modified before calling the generate() method. Is that right?

          Copying works fine if the amount of data that is unchanged is small. Unfortunately in my case I have only one integer data element that changes and dozens of various data elements that do not change.

          I just wanted to make sure I understood this point correctly - OPL is an amazing tool that allows the modeler great flexibility and productivity in model development. I was a little bit surprised that this kind of operation is this difficult compared to other nice features in OPL...

          • AlexFleischer
            AlexFleischer
            455 Posts
            ACCEPTED ANSWER

            Re: How to alter a integer dataElement with an Interface (API C# or Java)

            ‏2013-09-24T13:20:24Z  in response to CplexUser1453

            Hi,

            for the same model you may have some data handled by

            data2= new IloOplDataElements() and the rest in the .dat

            You are not limited to having everything in the .dat or everything in the "new IloOplDataElements()"

             

            regards