Decision Optimization

Decision Optimization

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

 View Only
  • 1.  OPL examples in Watson Studio

    Posted Fri June 05, 2020 05:01 AM
    Hi All,

    Are there examples of OPL models in Watson Studio? It seems OPL models are not possible to directly migrate to Watson Studio. Seems reading data not always same

    Just to read data

    myData = {"Item 1", "Item2", "Item3"}

    myDataSet = [
    ,{"el1_Item1", "el2_Item1"}
    ,{"el1_Item2"}
    ,{"el1_Item3", "el2_Item3", "el3_Item3"}
    ]

    For example simple file read line
    {string} myData = ...;

    generated an error
    Element 'myData ' provided in CSV data source cannot be mapped to OPL type: 'SET' as this type is not supported for input data on the Cloud.

    I had to rewrite into to have it worked

    tuple myString{
         key string myData ;
    }
    {myString} myData = ...;

    The question is how do I read myDataSet . In OPL I would read in the following way
    {string} myDataSet [myData ] = ...;


    Thanks

    ------------------------------
    Maga
    ------------------------------

    #DecisionOptimization


  • 2.  RE: OPL examples in Watson Studio

    Posted Fri June 05, 2020 09:51 AM
    Hi,

    OPL works fine in Watson Studio.

    Let me again share the zoo example.

    Take zoo.csv
    40;500
    30;400​

    import zoo.csv in Watson Studio

    and name the columns in order to see



    then move to run a model

    and type

    int nbKids=300;
    
    // a tuple is like a struct in C, a class in C++ or a record in Pascal
    tuple bus
    {
      key int nbSeats;
      float cost;
    }
    
    // This is a tuple set
    {bus} buses=...;
    
    // asserts help make sure data is fine
    assert forall(b in buses) b.nbSeats>0;
    assert forall(b in buses) b.cost>0;
    
    // decision variable array
    dvar int+ nbBus[buses];
    
    // objective
    minimize
         sum(b in buses) b.cost*nbBus[b];
         
    // constraints
    subject to
    {
       sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;
    }​


    and then you ll be able to run and see the result



    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 3.  RE: OPL examples in Watson Studio

    Posted Fri June 05, 2020 10:27 AM
    Zoo example run fine for me. Note how in this example the parameters are read - again through tuples. 
    {bus} buses=...;

    Can you share how to read these data 
    myData = {"Item 1", "Item2", "Item3"}

    myDataSet = [
    ,{"el1_Item1", "el2_Item1"}
    ,{"el1_Item2"}
    ,{"el1_Item3", "el2_Item3", "el3_Item3"}
    ]

    The regular lines below do not work
    {string} myData = ...;
    {string} myDataSet [myData ] = ...;

    Thanks

    ------------------------------
    Margarit Khachatryan
    ------------------------------



  • 4.  RE: OPL examples in Watson Studio

    Posted Fri June 05, 2020 10:37 AM
    Hi,

    could you try to use tuple sets instead of sets ?

    Instead of

    {string} myData = ...;​
    try

    tuple t
    {
      string st;
    }
    
    
    {t} myData = ...;​
    regards

    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 5.  RE: OPL examples in Watson Studio

    Posted Fri June 05, 2020 10:51 AM
    Alex,

    Thanks, in my original message that is what I did but could't figure out how to read the 
    myDataSet = [
    ,{"el1_Item1", "el2_Item1"}
    ,{"el1_Item2"}
    ,{"el1_Item3", "el2_Item3", "el3_Item3"}
    ]

    The usual way to read is
    {string} myDataSet [myData ] = ...;

    Basically, 
    myDataSet ["Item1"] should return {"el1_Item1", "el2_Item1"}




    ------------------------------
    Margarit Khachatryan
    ------------------------------



  • 6.  RE: OPL examples in Watson Studio

    Posted Fri June 05, 2020 12:13 PM
    Hi,


    you need to use tuple set.


    For instance if you have .mod


    {string} myData = ...;
    {string} myDataSet [myData ] = ...;​


    and .dat

    myData={"A","B"};
    myDataSet=[{"C","D"},{}];


    which run fine with CPLEX on your machine.

    You need to turn that into .mod

    {string} myData = ...;
    
    tuple t
    {
      string s1;
      string s2;
    }
    
    {t} myDataSet with s1 in myData=...;
    
    {string} myDataSet2[i in myData]={j.s2 | j in myDataSet : j.s1==i};
    
    execute
    {
      writeln(myDataSet2);
    }


    and .dat

    myData={"A","B"};
    myDataSet={<"A","C">,<"A","D">};


    which run fine on your machine too but will also work in Watson Studio if you turn your .dat into a csv file

    regards



    ------------------------------
    ALEX FLEISCHER
    ------------------------------