Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Load Array from database to CPLEX

    Posted 06/16/20 11:31 AM


    //mod

    range Weeks = 0..3;

    range Movies = 1..5;

    float expDemand [Movies][Weeks];

    float WeekDemand[Movies][Weeks] = ...;

    execute Demand{

                    for(var w in Weeks)

                          for(var m in Movies)

                                   expDemand[m][w] = WeekDemand[m][w]*2.25;

    }

     

    //dat

    SheetConnection sheet("D:\\Box office 2015.xlsx");

    WeekDemand from SheetRead(sheet,"'Week demand'!V2:Y6");

     

    Note: It can be seen that the indexes (week, movie) are defined in advance and the WeekDemand route is made based on these indexes (correlative numbers).

     

    This script works correctly connecting to Excel, now can someone help me migrate this script using database tables. Please be explicit as I am new to this world of CPLEX programming. Thank you



    ------------------------------
    MIGUEL FLORES QUISPE
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Load Array from database to CPLEX



  • 3.  RE: Load Array from database to CPLEX

    Posted 06/17/20 10:18 AM
    Hi Alex, thanks for answering, your help helped me a lot

    Now, I have successfully connected from the database and I am executing the sql command like this in * .dat;

    db.read ("WeekDemand", "SELECT demand_week0, demand_week1, demand_week2, demand_week3 FROM week_demand);

    I kept the * .mod file code as it was originally in my first post.

    But it does not carry out the load.

    Can you give me a hint to adapt the code of my MOD file, I understand that it is different to load an array from excel and from table.

    My idea is to continue going through the array through the indexes (without using tuples) because it is flexible since they are correlative numbers. I want to keep the array reading form as it was originally with minimal changes. This is possible? or should I change the structure of my table? Thanks again.

    ------------------------------
    MIGUEL FLORES QUISPE
    ------------------------------



  • 4.  RE: Load Array from database to CPLEX

    Posted 06/18/20 01:43 PM
    Can you give me a hint to adapt the code of my MOD file, stream I understand that it is different to load an array from excel and from table. because it is flexible since they are correlative numbers. I want to keep the array reading form as it was originally with minimal changes. 

    https://medium.com/@AlainChabrier/connect-opl-models-with-databases-217baed0935c


    ------------------------------
    ali jaber
    ------------------------------



  • 5.  RE: Load Array from database to CPLEX

    Posted 06/19/20 12:47 PM
    Solved the problem. Thank you all for your time in responding.
    I leave the script of the model for those who need it.
    Now works with a mssql database connection

    Note: The table structure was kept the same as it was in excel and an unpivot statement had to be used when extracting the data from the table because cplex works it in rows. Another thing that must be highlighted is that unlike the connection to Excel, when the connection is with a database, you cannot define the number of rows and columns in advance, but this is done at the same time that the data is extracted from the table.

    //Table mssql (version cplex 12.6.3 or lower since higher versions have another database access syntax)


    // mod

    {int} Weeks= ...;
    {int} Movies= ...;

    //Load data from table mssql
    float WeekDemand[Movies][Weeks] = ...;

    float expDemand [Movies][Weeks];
    dvar boolean k[Movies][Weeks];

    execute Demand{
                                for(var w in Weeks)
                                              for(var m in Movies)
                                                                 expDemand[m][w] = WeekDemand[m][w]*2.25;
    }

    dexpr float Objective =
    sum(m in Movies, w in Weeks)
    (expDemand[m][w]*k[m][w]);
    maximize Objective;

    subject to{
    }

    tuple Cinema{
    int mm;
    int ww;
    int value;
    }

    {Cinema} FilmSet = {<a,b,k[a][b]> | a in Movies, b in Weeks};


    // dat

    DBConnection db("oledb","user/password/name_database/HOST\\INSTANCE_NAME");
    Movies,Weeks,WeekDemand from DBRead(db,"SELECT id_movie, cast(weeks As int), demandas FROM demand UNPIVOT ( demandas FOR [weeks] IN ([1], [2], [3], [4])) AS P");

    FilmSet to DBUpdate(db,"INSERT INTO result(id_movie,week,demand) VALUES(?,?,?)");


    Good luck to everyone !!!



    ------------------------------
    MIGUEL FLORES QUISPE
    ------------------------------