Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Reading a Problem from *.mod and *.dat files in Python

  • 1.  Reading a Problem from *.mod and *.dat files in Python

    Posted Tue November 12, 2013 03:33 PM

    Originally posted by: Alireza.M


    Hi,

     

    Is there a way to define a problem in python using a model defined as a *.mod file and its external data in a *.dat file?

    I know a workaround for this is to add an export statement at the end of mod file

    main { 
    thisOplModel.generate(); 
    cplex.exportModel("lpmodel.lp")
    }

    and then create the lp model either in command line or using "subprocess" or a similar module in python.

    Then the problem can be built using 

    problem = cplex.Cplex("lpmodel.lp")

    The problem with this approach is that I want to have the external data of dat file read into the problem and be able to change them dynamically. For example if there is a variable "n = 5;" in the dat file which is fed as an external data to the mod file, I want to be able to solve my problem with n=5 and then change n to 6 solve the problem again and so on (Obviously lp model changes if I change the value of n in this example, but the mod file does not need to change).

     

    Is there a way to read the problem using the mod and dat file into python API of Cplex directly and then change values of the dat file, inside python, as data variables of the problem?


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Reading a Problem from *.mod and *.dat files in Python

    Posted Wed November 13, 2013 02:46 AM

    There is no direct way to do that in Python. You could of course re-run the command line program to rebuild the problem file after you changed the .dat file and then read the created problem file again (you should use the .sav file format instead of the .lp file format). That should work.

    I think what you are trying to do could be done directly in OPL using custom data sources (see also here) or by using IBM ILOG Script in data files.

    Would it be an option to replace 'n' in the model formulation by something like

    dvar float varN;
    varN == n;

    ? This way you could look in the Python model for variable 'varN' and change its bounds. That would have the same effect as changing the data and regenerating the model file. This will of course not work if 'n' is used to define ranges etc.


    #CPLEXOptimizers
    #DecisionOptimization