Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Question on tuple sets and arrays

    Posted 11/29/10 12:37 PM

    Originally posted by: DanielFelixFerber


    I would appreciate your advice about pros/cons of storing attributes on tuple sets or on arrays.

    The model that reads an enumeration of "objects" that are identified by two key integers "i" and "j".
    The object has some attributes. "attr1" and "attr2".

    (1) I have declared the enumeration of the objects as:

    tuple tplObject {
    key int i;
    key int k;
    int attr1;
    string attr2;
    }
    {tplObject} objects = ...;

    (2) An alternative model could declare the same as:

    tuple tplObject {
    key int i;
    key int k;
    }
    {tplObject} objects = ...;
    tuple tplObjectData {
    int attr1;
    string attr2;
    }
    {tplObjectData } objectDatatplObject = ...;

    I wonder about the pros/cons of (1) and (2). Are there memory/performance benefits? O other advices or best practices about choosing among (1) and (2)?

    I think that (1) leads to a data file that might be easier to read as all data of an individual object stays together.
    But (2) produces a model that is easier to read, since I can get an attribute as simply as "objectDataobj.attr1".

    Best regards,
    Daniel Felix Ferber
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Question on tuple sets and arrays

    Posted 11/30/10 03:54 AM

    Originally posted by: SystemAdmin


    Daniel,

    what you will typically have is a mixture of both.
    Ti initialize extrenal data you will use (1), as this
    e.g. the only way to read data from a database. And as
    you told, data elements are kept together.

    To optimize code performance you will copy the elements
    of the tuple-set to an array as you described.
    Memory is doubled at that moment but array access
    is much faster than the "find" in the tuple set.
    So far, in my applications memory requirements were
    a minor topic than performance.
    (Please note that it should be declared as
    {tplObjectData } objectData[objects] = ...;
    

    )

    I hope these remarks are ofany help to you.

    Regards
    Norbert

    Hope this will help
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Question on tuple sets and arrays

    Posted 11/30/10 05:51 AM

    Originally posted by: DanielFelixFerber


    Thanks for the clarification.

    BTW, it was not trivial for me to discover the expression that gets attributes from a tuple that has to be "found" withing a set. Therefore I would like to share it with the community:

    item(objects, <i, k>).attr1
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Question on tuple sets and arrays

    Posted 11/30/10 06:03 AM

    Originally posted by: SystemAdmin


    Just for sake of completeness

    var tpl = objects.find(i,k);
     if(tpl!= null)
     {
        writeln(" Found : ", tpl.attr1);
     }
     else
       writeln("Not found ....");
    


    should work as well.

    To the best of my knowledge, your solution might result in a
    null-pointer-access when the element is not found ...

    Regards
    Norbert
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Question on tuple sets and arrays

    Posted 12/01/10 08:23 AM

    Originally posted by: SystemAdmin


    You are right, item and ord for example will raise an error if the tuple can not be found.
    By the way, the script can sometimes be slow.

    A workaround can be to protect the statement with the ?: syntax.
    Example:
    tuple T{
    int a;
    int b;
    };
     
    {T} S = {<i,j> | i in 1..10,j in 1..5}; //j not in 1..10
     
    // we will iterate on j in 1..10 to get through the holes.
    int X = sum(i,j in 1..10) ( <i,j> in S ? item(S, <i,j>).a : 0);
    execute{
    writeln(X);
    }
    

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Question on tuple sets and arrays

    Posted 12/01/10 08:41 AM
    Hi

    I am not sure that you would get a null pointer in this case, I think you would simply get en error.

    For example

    tuple t
    {
     int a;
     int b; 
    }  
     
    {t} s={<1,2>};
     
    execute
    {
     s; 
    }  
     
    int r1=item(s,<1,3>).b;
    execute
    {
     writeln(r1); 
    }
    


    gives

    OPL cannot extract expression: item(s,<1,3>).

    Alex
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Question on tuple sets and arrays

    Posted 12/03/10 07:11 AM

    Originally posted by: joaoamorim


    Hi

    I am still getting started in OPL, and I am having a similar problem.

    In the DB I have a table with this info:

    tuple costData {
    string Car;
    string Parking;
    int Period;
    int Cost;
    }

    {costData} CostData = ...;

    Assuming I have already got the Cars, Parkings and Periods, how do I transfer the info from CostData set to this array:

    int CostsCarsParkingsPeriods

    Also, do you know where to find some good tutorials / info about OPL?

    Thanks
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: Question on tuple sets and arrays

    Posted 12/06/10 01:20 PM

    Originally posted by: SystemAdmin


    Using generic index initialization, you could declare and initialize the array as follows:
    int CostsCarsParkingsPeriods[Cars][Parkings][Periods] = [cd.Car: [cd.Parking : [cd.Period: cd.Cost]] | cd in CostData ];
    

    Just be aware that if you don't have Cost data for each combination of Car+Parking+Period, a default value of 0 will be set.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer