Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Assignment

    Posted Tue May 06, 2008 12:19 PM

    Originally posted by: SystemAdmin


    [shadi said:]

    Im using the following code in order to execute my model btpair,





            Dim oplF As OplFactory = New OplFactory

            Dim errHandler As OplErrorHandler = oplF.CreateOplErrorHandler(Console.Out)

            OplFactory.DebugMode = True

            Dim masterCp As CP = oplF.CreateCP

            Dim Modelsource As OplModelSource = oplF.CreateOplModelSource(applicationPath & "\OPLFiles\MyModel.mod")

            Dim datasource As OplDataSource = oplF.CreateOplDataSource(applicationPath & "\OPLFiles\MyData.dat")

            Dim setting As OplSettings = oplF.CreateOplSettings(errHandler)

            Dim def As OplModelDefinition = oplF.CreateOplModelDefinition(Modelsource, setting)

            Dim opl As OplModel = oplF.CreateOplModel(def, masterCp)

            opl.AddDataSource(datasource)

          opl.generate()







    I want use this code many times with some changes in the datasource file, so I want my code looks like the following:         





                                For counter=0 to 80

                              datasource.coverTrip=counter

                              opl.AddDataSource(datasource)

                              opl.generate()

                                Next







    I tried the following code:

                Dim CpDataElements As OplDataElements = opl.MakeDataElements

                CpDataElements.setElement(CpDataElements2.MakeElement("coverTrip", 22))

    But it doesn’t work






    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: Assignment

    Posted Wed May 07, 2008 12:49 PM

    Originally posted by: SystemAdmin


    [alain.chabrier said:]

    Hi,

    why the mix of dataelements objects ?

    Shouldn't it be something like :
    CpDataElements.setElement(CpDataElements.MakeElement("coverTrip", 22))
    or
    CpDataElements2.setElement(CpDataElements2.MakeElement("coverTrip", 22))

    Alain
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: Assignment

    Posted Wed May 07, 2008 01:31 PM

    Originally posted by: SystemAdmin


    [shadi said:]

    Alain, thank you...

    yes i have it as you said, i mean like this
    CpDataElements2.setElement(CpDataElements2.MakeElement("coverTrip", 22))

    i can see that this code change the value of the variable  just in datasource i mean when i write the following line code:
    MsgBox(CpDataElements2.GetElement("coverTrip").AsInt)
    i get 22 (correct value). but when i execute the model, it use the old value (11 and not 22)

    so i think i need somthing like commit()...

    shadi
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 4.  Re: Assignment

    Posted Wed May 07, 2008 01:34 PM

    Originally posted by: SystemAdmin


    [alain.chabrier said:]

    Hi,

    I don't know your complete code so it is hard to say.
    but from your comment, I wonder if you are really using this data source (the data elements is a particualr kind of data source) in your opl model.
    You should use the method :
    public void addDataSource(IloOplDataSource source)

    at some point with the newly create data elements.
    You can add various datasource for one opl model, but the element should not duplicate.

    Alain
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 5.  Re: Assignment

    Posted Wed May 07, 2008 02:55 PM

    Originally posted by: SystemAdmin


    [shadi said:]

    my code is like this:

    Dim oplF As OplFactory = New OplFactory

            Dim errHandler As OplErrorHandler = oplF.CreateOplErrorHandler(Console.Out)

            OplFactory.DebugMode = True

            Dim masterCp As CP = oplF.CreateCP

            Dim Modelsource As OplModelSource = oplF.CreateOplModelSource(applicationPath & "\OPLFiles\MyModel.mod")

            Dim datasource As OplDataSource = oplF.CreateOplDataSource(applicationPath & "\OPLFiles\MyData.dat")

            Dim setting As OplSettings = oplF.CreateOplSettings(errHandler)

            Dim def As OplModelDefinition = oplF.CreateOplModelDefinition(Modelsource, setting)

            Dim opl As OplModel = oplF.CreateOplModel(def, masterCp)

            opl.AddDataSource(datasource)

          opl.generate()



    i want to change the coverTrip which was diclared in the model file and assigned in the datasource

                  For counter=0 to 80

                        Dim CpDataElements As OplDataElements = opl.MakeDataElements
                        CpDataElements.setElement(CpDataElements2.MakeElement("coverTrip", counter))
                        opl.AddDataSource(datasource)
                        opl.generate()

                  Next

    #DecisionOptimization
    #OPLusingCPOptimizer


  • 6.  Re: Assignment

    Posted Wed May 07, 2008 03:15 PM

    Originally posted by: SystemAdmin


    [alain.chabrier said:]

    So indeed you miss something in the loop to do :
    opl.AddDataSource(CpDataElements)

    Also, you cannot call generate() several time on the same OplModel. You need to create a new one.

    Alain

    #DecisionOptimization
    #OPLusingCPOptimizer


  • 7.  Re: Assignment

    Posted Wed May 07, 2008 05:48 PM

    Originally posted by: SystemAdmin


    [shadi said:]

    Dear alain.chabrier





            Dim counter As Integer
            For counter = 1 To 50
                Dim oplF As OplFactory = New OplFactory
                Dim errHandler As OplErrorHandler = oplF.CreateOplErrorHandler(Console.Out)
                OplFactory.DebugMode = True
                Dim masterCp As CP = oplF.CreateCP

                Dim Modelsource As OplModelSource = oplF.CreateOplModelSource(applicationPath & "\OPLFiles\MyModel.mod")
                Dim datasource As OplDataSource = oplF.CreateOplDataSource(applicationPath & "\OPLFiles\MyData.dat")
                Dim setting1 As OplSettings = oplF.CreateOplSettings(errHandler)
                Dim def As OplModelDefinition = oplF.CreateOplModelDefinition(Modelsource, setting1)
                Dim opl As OplModel = oplF.CreateOplModel(def, masterCp)
                opl.AddDataSource(datasource)



                Dim CpDataElements As OplDataElements = opl.MakeDataElements
                CpDataElements.setElement(CpDataElements.MakeElement("coverTrip", counter))
                opl.AddDataSource(CpDataElements)
                opl.Generate()

                MsgBox(CpDataElements.GetElement("coverTrip").AsInt)
                MsgBox(opl.GetElement("coverTrip").AsInt)

                If masterCp.Solve Then
                    opl.printSolution(Console.Out)
                End If


                oplF.End()

            Next


    the first messagebox ([b]MsgBox(CpDataElements.GetElement("coverTrip").AsInt)[/b]) shows me that the value changed (correct one) but the second one [b](MsgBox(opl.GetElement("coverTrip").AsInt)) [/b] shows me the value didnt change



    Thank you
    Kshieboun Shadi

    #DecisionOptimization
    #OPLusingCPOptimizer


  • 8.  Re: Assignment

    Posted Wed May 07, 2008 06:30 PM

    Originally posted by: SystemAdmin


    [alain.chabrier said:]

    Hi,

    here is an updated program that works for me (I join my dummet mod and dat too) :

      Dim counter As Integer
                For counter = 1 To 50
                    Dim oplF As OplFactory = New OplFactory
                    Dim errHandler As OplErrorHandler = oplF.CreateOplErrorHandler(Console.Out)
                    OplFactory.DebugMode = True
                    Dim masterCp As CP = oplF.CreateCP

                    Dim Modelsource As OplModelSource = oplF.CreateOplModelSource("C:\temp\MyModel.mod")
                    Dim datasource As OplDataSource = oplF.CreateOplDataSource("C:\temp\MyData.dat")
                    Dim setting1 As OplSettings = oplF.CreateOplSettings(errHandler)
                    Dim def As OplModelDefinition = oplF.CreateOplModelDefinition(Modelsource, setting1)
                    Dim opl As OplModel = oplF.CreateOplModel(def, masterCp)
                    REM opl.AddDataSource(datasource)



                    REM Dim CpDataElements As OplDataElements = opl.MakeDataElements
                    Dim CpDataElements As OplDataElements = oplF.CreateOplDataElements()

                    REM CpDataElements.SetElement(CpDataElements.MakeElement("coverTrip", counter))
                    CpDataElements.AddElement(CpDataElements.MakeElement("coverTrip", counter))

                    opl.AddDataSource(CpDataElements)
                    opl.Generate()

                    MsgBox(CpDataElements.GetElement("coverTrip").AsInt)
                    MsgBox(opl.GetElement("coverTrip").AsInt)

                    If masterCp.Solve Then
                        opl.PrintSolution(Console.Out)
                    End If


                    oplF.End()


    My .mod :
    using CP;

    int  coverTrip = ...;

    My .dat
    coverTrip = 10;

    (this .dat is not used at all, as in my case, all the data is given by DataElements, in your case, you have to add 2 datasource, one form file, and one from calculated data.) If I keep the adddataSource call I hget a correct errir aboiut having coverTrip defined twice.

    If I use
    opl.MakeDataElements
    then I force opl to generate, and hence alre4ady evaluate coverTrip from the data source.
    here, I just want an empty DataElements, I create it from the factory.

    hope this helps,

    if you have more problem, you can send your complete .modt and .dat

    Alain

    #DecisionOptimization
    #OPLusingCPOptimizer


  • 9.  Re: Assignment

    Posted Fri May 09, 2008 02:24 PM

    Originally posted by: SystemAdmin


    [shadi said:]

    Dear alain.chabrier
    thank you
    i tried the soloution that you told me and its work,im using the following code:


    String applicationPath  = Application.StartupPath;
    OplFactory oplFCP = new OplFactory();
    OplErrorHandler errHandler = oplFCP.CreateOplErrorHandler(Console.Out);
    OplFactory.DebugMode=true;
    CP cp = oplFCP.CreateCP();
    OplModelSource modelSource=oplFCP.CreateOplModelSource(applicationPath + "\\OPLFiles\\MyModel.mod");
    OplDataSource dataSource=oplFCP.CreateOplDataSource(applicationPath + "\\OPLFiles\\MyData.dat");
    OplSettings settings = oplFCP.CreateOplSettings(errHandler);
    OplModelDefinition def=oplFCP.CreateOplModelDefinition(modelSource,settings);
    OplModel oplCP=oplFCP.CreateOplModel(def,cp);
    OplDataElements CpDataElements= oplFCP.CreateOplDataElements();
    CpDataElements.AddElement(CpDataElements.MakeElement("coverTrip", 4));
    oplCP.AddDataSource(CpDataElements);
    oplCP.AddDataSource(dataSource);
    oplCP.Generate();

    MessageBox.Show(CpDataElements.GetElement("coverTrip").AsInt().ToString());
    MessageBox.Show(oplCP.GetElement("coverTrip").AsInt().ToString());

    the two messagebox show me the same result 4 (correct result)
    note: i deleted the varieble coverTrip from my datasource

    my problem now is:
    if i have varieble which was declared in my model source as follow:
    int Origin[trips] = ...;
    how i can assign this variable using the same strategy??

    i mean can i make and add element (type array) using the same strategy??

    thank you
    Kshieboun Shadi






    #DecisionOptimization
    #OPLusingCPOptimizer


  • 10.  Re: Assignment

    Posted Fri May 09, 2008 02:50 PM

    Originally posted by: SystemAdmin


    [alain.chabrier said:]

    Hello,

    I am happy to see you has been able to use the OplDataElements  mechanism.

    For more complex data structure filling from interfaces (arrays, sets, tuples), the recommended way it to use CustomOplDataSource. This is the 3rd way to input data, the 3rd type of OplDataSource available, as commented before.
    Note that this way would also cover your previous case of simple structure.

    In order to use it, you might have a look at the C# example in

    C:\ILOG\OPL55\examples\dotnet\x86_.net2005_8.0\Warehouse

    This examples includes a custom class MyParams dereving from CustomOplDataSource.
    in this case, it only feeds simple data (ints), but using other methods of the helper class OplDataHandler (like startArray), you can feed any kind of data.

    When you have implemented your new sub class of CustomOplDataSource, you need to add it in the same way to the OplModel.

    Hope this helps,

    Alain




    #DecisionOptimization
    #OPLusingCPOptimizer


  • 11.  Re: Assignment

    Posted Fri May 09, 2008 03:21 PM

    Originally posted by: SystemAdmin


    [shadi said:]

    Dear alain.chabrier
    i use the 3rd type of OplDataSource (CustomOplDataSource) i think it is the best mechanism for my case...


    i added the myParams class which seems as follow:

    internal class MyParams : CustomOplDataSource
    {
    int _coverTrip;
    string _busCategory;
    ILOG.Concert.IIntMap _Origin;
       
    internal MyParams(OplFactory oplF,int coverTrip,string busCategory,ILOG.Concert.IIntMap Origin)
    : base(oplF)
    {
    _coverTrip = coverTrip;
    _busCategory=busCategory;
    _Origin=Origin;
    }
    public override void CustomRead()
    {
    OplDataHandler handler = DataHandler;

    handler.StartElement("coverTrip");
    handler.AddIntItem(_coverTrip);
    handler.EndElement();

    handler.StartElement("busCategoryId");
    handler.AddStringItem(_busCategory);
    handler.EndElement();

    handler.StartArray("Origin");
    // how to assign the Origin array
    handler.EndArray();

    }
    }


    thank you
    Kshieboun Shadi
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 12.  Re: Assignment

    Posted Fri May 09, 2008 03:32 PM

    Originally posted by: SystemAdmin


    [alain.chabrier said:]

    Hi,


    Let me give you a small 2d array example:

    With the model :

    range r=1..2;
    int ar2[r][r]=...;

    and then you write

    handler.startElement("ar2");
    handler.startArray();
    handler.startArray();
          handler.addIntItem(5);
          handler.addIntItem(6);
          handler.endArray();;
          handler.startArray();
          handler.addIntItem(7);
          handler.addIntItem(8);
          handler.endArray();;
          handler.endArray();;
          handler.endElement();

    In your case, it seems you need to take the values fom rht the IntMap. using the Intmap API (getValue, etc)

    note that in the coming OPL60 we have added a new example to show all the API to do Custom Data Source, called customdatasource, as it was a recurrent request of something not documented enough.


    I will send you the example.

    Alain




    #DecisionOptimization
    #OPLusingCPOptimizer