Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Writing to SQL db from OPL Script

    Posted 08/16/11 11:34 AM

    Originally posted by: pradeepram80


    Hi,
    I'm using ILOG's OPL script for flow control in my MILP problem. I'd like to write the output of conflict iterator to a SQL database. I'm unable to do so. All I have available is a "writeln()" function that writes it back to the console or a text file. Are there other ways to do this? I'm generating tuple sets in my postprocessing that I then use to writeback solution to a database. So far unable to generate such tuples to writeback the conflict info.

    Thanks in advance,
    Pradeep
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Writing to SQL db from OPL Script

    Posted 08/16/11 12:54 PM

    Originally posted by: SystemAdmin


    In order to write your set of tuples to a database, you need a .dat file in which you have the database update instructions, something like:
    conflicts to DBUpdate(db,"INSERT INTO CONFLICTS values (?,?)");
    


    If you have a main script, then you need to trigger these database update instructions by calling
    oplModel.postProcess();
    

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Writing to SQL db from OPL Script

    Posted 08/16/11 01:29 PM

    Originally posted by: pradeepram80


    Thanks for your reply.

    I do understand how to write tuples back to the db.But I don't know how to populate the tuple - conflicts, with the information returned by OPL. I'm unable to use printConflict() function to populate these tuples. All i can do is to use that function within writeln() function to either write it to the console or to a text file but not populate the tuple- conflicts. Hope I'm clear here.

    Thanks again
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Writing to SQL db from OPL Script

    Posted 08/19/11 06:20 AM

    Originally posted by: SatishKumarA


    Hi,
    We define tuples and tuple objects to derive data from database . Similarly, you need to define tuples in order to write back the variable values to database. Here, let us suppose "var_name" is a variable which is a function of Y and takes some value after the solve. So define a tuple like
    tuple X
    {
    int Y;
    float var_name;
    }
    

    For this tuple, we define an object like
    {X} Conflicts =...;
    

    And the main thing is you need to write the update query(given by vblanchard above) in a separate .dat file and add this dat file to the model in your script and then trigger postprocess command.

    I hope this will solve your problem.

    Thanks,
    Satish Kumar A
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Writing to SQL db from OPL Script

    Posted 08/19/11 11:51 AM

    Originally posted by: pradeepram80


    I do have the tuples defined as you say. In my case, since I'm trying to writeback conflict info, I've the following:

    tuple ts {string str1};
    string conflicts = ...;

    and in post processing, I've the following code:
    {ts} oConflicts = {<conflicts>};

    and under main, I've the following:

    main{
    thisOplModel.generate();
    if(cplex.solve()){
    writeln("Objective ",cplex.getObjValue());
    thisOplModel.postProcess();
    }
    else{
    var conflictstr, relaxstr;
    conflictstr = thisOplModel.printConflict(); //printconflict() is the only function available from OPL and
    //I cant use it in my original model. I can only use it here inside main

    //Can you tell me how I can write conflictstr to the string conflicts defined earlier in my model? since
    // this does not work
    //thisOplModel.conflicts = conflictstr;

    //so I've to create another model-newOplModel where I change its data with conflictstr using dataElements;
    ....
    ....
    newOplModel.generate();
    cplex.solve(); //Now i've to do the solve again, in order to run postprocessing. In my original model //settings, I had feasopt set to run both phases but newOplModel doesnt seem to have that setting. There is no way I //can change that setting through code. so the model returns infeasible and doesnt run postprocessing. SCREWED!!:-(

    }
    }

    I cant believe OPL script will be this complicated to accomplish something this straightforward. Surely, I must be missing something...

    Pradeep
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Writing to SQL db from OPL Script

    Posted 08/22/11 08:18 PM

    Originally posted by: SystemAdmin


    Here is an example that shows passing relaxation/conflict from main script back to model.

    
    
    //.mod 
    {string
    } relaxations = ...; 
    {string
    } conflicts = ...; dvar float+ x in 0..10; constraints 
    { ct: x >= 11; 
    } execute 
    { writeln(relaxations); writeln(conflicts); 
    } main 
    { thisOplModel.generate(); 
    
    if(cplex.solve()) writeln(
    "Solution Found"); 
    
    else 
    { writeln(
    "No Solution Found"); var relaxStr = thisOplModel.printRelaxation(); thisOplModel.relaxations.add(relaxStr); var conflictStr = thisOplModel.printConflict(); thisOplModel.conflicts.add(conflictStr); 
    } thisOplModel.postProcess(); 
    }   
    //.dat relaxations = 
    {
    "Relaxations: "
    }; conflicts = 
    {
    "Conflicts: "
    };
    


    Note that you will need a solution in order to invoke postProcess() so, in above example using printRelaxation() is necessary; printConflict() by itself doesn't get a solution.

    Regards,
    Faisal
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Writing to SQL db from OPL Script

    Posted 08/23/11 02:35 PM

    Originally posted by: pradeepram80


    Thanks Faisal. You answered my question. That was exactly what I was looking for. Somehow reading the documentation lead me to believe that I had to use dataElements to do this.

    One final issue that I'm seeing (not sure if you can help me) is: I see the following error when I try to write back a tuple containing the conflicts information.

    Database error (state=22005 code=-22): Bad size for variable being bound, near "Conflicts"

    I initially thought the column in my SQL table did not have sufficient character length/byte size. But I've set it to maximum possible length and still see this error. I dont see this error when there is just one line to be written back. When there is more than one line, this error shows up. For example:

    ct at 35:8-16 C:\Documents and Settings\Desktop\test\test.mod
    relax 11,infinity to 0,infinity value is 0
    ct2 at 36:9-15 C:\Documents and Settings\ramachandran\Desktop\MiPPS\MPP2.mod
    relax -infinity,-1 to -infinity,0 value is 0

    The above output is from the console. When I add another violating constraint ct2 that needs to be relaxed, the above error shows up. I'm thinking there is some newline character or some formatting issue with the string returned from OPL..Let me know if you know the fix

    Thanks
    Pradeep
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: Writing to SQL db from OPL Script

    Posted 08/23/11 04:06 PM

    Originally posted by: SystemAdmin


    what is the datatype you are using? If it is SQL server, I would suggest trying varchar(max) or, text type.

    Regards,
    Faisal
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: Writing to SQL db from OPL Script

    Posted 08/23/11 06:11 PM

    Originally posted by: pradeepram80


    I'm using varchar(max). I even tried text datatype. Throws up the same error.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: Writing to SQL db from OPL Script

    Posted 08/23/11 06:44 PM

    Originally posted by: pradeepram80


    I'm also using OPL 5.5.1.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer