Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Question: Flow Control + SQL with parameters

    Posted 07/12/18 07:53 AM

    Originally posted by: OndrejKral


    Hi, I have OPL project and I'm getting familiar with Flow Control scripting and I can't deal with this scenario. Data in .dat file are populated from DB by DBRead. I'm using "parametrized" SQL in the following manner: In OPL model (.mod file)  I have variable defined as 'string parameter = "parameterValue"'. Then in .dat file I'm using prepare { } block to concatenate string for SQL query: Query = "SELECT * FROM" + parameterValue;. Then I'm using this query in DBRead(db,Query).

    Meaning of this was to easily change parameter value in model and then, when running whole project, get different data based on parameter defined in model. (this is also possible with parametric notation of DBRead). So far I was satisfied, but then I wanted to add Flow Control and iterate over set of parameters and run several different optimizations based on current parameter.

    Problem is that I don't know how to do that. According OPL documentation, I can only change scalar values in Flow Control in this manner: In .mod I have to define string parameter = ... ; and then in Flow Control I need to create new instance of data elements (data = new IloOplDataElements();) and add scalar value to it (data.parameter = "parameterValue";). Then this data with only one variable add to model with data from .dat file (model.addDataSource(data);)

    But when is parameter defined as above, I get OPL error because parameter is not defined in prepare {} phase of my .dat file.

    I'll be glad for any help or reaction. Basically I can rework some stuff, but I would like to find solution based on this pattern: Iterate over set of strings (defined or loaded from DB), run multiple CPLEX optimizations based on string value (data for current optimization are fetched from DB based on  SQL parametrized by current string value - becauese I have all data in one table and different instance data are separated by value in one column ) and put results somehow togehter (not important).

    Thank you, Ondrej.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Question: Flow Control + SQL with parameters



  • 3.  Re: Question: Flow Control + SQL with parameters

    Posted 07/15/18 09:32 AM

    Originally posted by: OndrejKral


    Thanks for the response. So I moved SQL string preparation to main{} block so its more like the example you gave in the link. Seems it worked  but now I'm stuck on error 'Oplrun process is not responding, you must relaunch the Run Configuration.' Followed in log by 'Connection refused to host: 127.0.0.1; nested exception is: java.net.ConnectException: Connection refused: connect'.  I'm not able to debug where in main block the error has been thrown since it doesn't stop on any of my break points. If there is some way how to pin point the problem, please let me know. Meanwhile I'll try to alter the code. Thanks, Ondrej.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Question: Flow Control + SQL with parameters



  • 5.  Re: Question: Flow Control + SQL with parameters

    Posted 07/15/18 06:47 PM

    Originally posted by: OndrejKral


    Thank you, it worked I guess. Now IDE tells me that several string scalars in my model are not defined. So problem is in how I understand data modification in flow control. I'm following documentation for editing scalar variable in flow control and this simple example should be right by documentation:

    // .mod file

     int maxValue = ...;
     int minValue = ... ;
     dvar int x;
     
     maximize x;
     subject to{
             x <= maxValue;
     }
     
     main{
            var src = new IloOplModelSource("TEST.mod");
            var def = new IloOplModelDefinition(src);
            var cplex = new IloCplex();
            var data = new IloOplDataSource("TEST.dat");
            var Model = new IloOplModel(def, cplex);
    
            var data2 = new IloOplDataElements(); 
            data2.maxValue = 30;
            
            Model.addDataSource(data);
            Model.addDataSource(data2);
            Model.generate();
     }
    

    // .dat file

    minValue = 10;
    

    This simple example is not working. In documentation it is specified that in order to get scalar variable to change, one should not defined it neither in .mod file or .dat file, only external declaration via ellipsis syntax. Then an example is given, similar to one shown above.

     

    There must be something I'm missing. I would expect data-linkage in .generate() phase, but it seems that this is happening earlier, in .mod file syntax parsing phase?

     

    I'll be glad for any advice. Ondrej.

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Question: Flow Control + SQL with parameters

    Posted 07/17/18 04:31 AM

    Originally posted by: OndrejKral


    To be specific, Tutorial: Flow Control part of the documentation, assuming string is 'scalar'

    So what is the difference, what I'm doing wrong?

    Thanks, Ondrej.


    #DecisionOptimization


  • 7.  Re: Question: Flow Control + SQL with parameters

    Posted 07/17/18 06:56 AM

    Originally posted by: OndrejKral


    Well, moving main {} block to separate file and using it as default model works. Seems like something not mentioned in docs.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer