Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to convert CPLEX LP format to OPL format

    Posted 04/19/12 10:24 AM

    Originally posted by: SystemAdmin


    Hi All,

    I am new to ILOG CPLEX. I have a model in CPLEX LP format. I would like to convert to OPL format and solve using the OPL optimizer. Is there any easy way to convert the LP file to OPL file.
    Kindly let me know

    Thanks in advance!!!!

    Kind Regards,
    Dinesh
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How to convert CPLEX LP format to OPL format

    Posted 04/23/12 08:09 AM

    Originally posted by: SystemAdmin


    First of all, you could solve the problem directly in OPL by putting this code into a .mod file
    
    using CPLEX;   main 
    { cplex.importModel(<model file>); cplex.solve(); 
    }
    

    But it is also quite easy to write a C++ program that reads a model and prints it as a .mod file. Here is a code that I used sometimes (no guarantees). It is an ad-hoc code, so it is not very nice but should give you an idea about what to do.
    
    
    // Convert a model that is specified in one of CPLEX's file formats 
    // into an OPL model. 
    // This code 
    // - assumes that all variables and constraints have names in the file, 
    // - only supports linear constraints, 
    // - does not support ranged constraints, 
    // - only supports binary, integer and continuous variables. #include <string> #include <sstream> #include <iostream> #include <ilcplex/ilocplex.h>   
    
    int main(
    
    int argc, 
    
    char **argv) 
    { 
    
    try 
    { 
    
    for (
    
    int i = 1; i < argc; ++i) 
    { IloEnv env; IloModel model(env); IloObjective obj(env); IloNumVarArray vars(env); IloRangeArray rngs(env); IloCplex cplex(model); cplex.setOut(env.getNullStream()); cplex.importModel(model, argv[i], obj, vars, rngs);   std::cout << 
    "using CPLEX;" << std::endl;   
    // Variables. 
    
    for (IloInt i = 0; i < vars.getSize(); ++i) 
    { IloNumVar v = vars[i]; 
    
    char 
    
    const *type = 
    "float"; std::string lb, ub; std::ostringstream s; 
    
    switch (v.getType()) 
    { 
    
    case ILOBOOL: type = 
    "boolean"; 
    
    if ( v.getLB() <= -IloIntMax ) lb = 
    "-maxint"; 
    
    else 
    { s.str(std::string(
    "")); s << v.getLB(); lb = s.str(); 
    } 
    
    if ( v.getUB() >= IloIntMax ) ub = 
    "maxint"; 
    
    else 
    { s.str(std::string(
    "")); s << v.getUB(); ub = s.str(); 
    } 
    
    break; 
    
    case ILOINT: type = 
    "int"; 
    
    if ( v.getLB() <= -IloIntMax ) lb = 
    "-maxint"; 
    
    else 
    { s.str(std::string(
    "")); s << v.getLB(); lb = s.str(); 
    } 
    
    if ( v.getUB() >= IloIntMax ) ub = 
    "maxint"; 
    
    else 
    { s.str(std::string(
    "")); s << v.getUB(); ub = s.str(); 
    } 
    
    break; 
    
    case ILOFLOAT: type = 
    "float"; 
    
    if ( v.getLB() <= -1e20 ) lb = 
    "-infinity"; 
    
    else 
    { s.str(std::string(
    "")); s << v.getLB(); lb = s.str(); 
    } 
    
    if ( v.getUB() >= 1e20 ) ub = 
    "infinity"; 
    
    else 
    { s.str(std::string(
    "")); s << v.getUB(); ub = s.str(); 
    } 
    
    break; 
    
    default: 
    
    throw -1; 
    // not supported 
    } std::cout << 
    "dvar " << type << 
    " " << v.getName() << 
    " in " << lb << 
    ".." << ub << 
    ";" << std::endl; 
    }   
    // Objective function. std::cout << (obj.getSense() == IloObjective::Minimize ? 
    "minimize" : 
    "maximize") << std::endl << 
    "  " << obj.getExpr() << 
    ";" << std::endl;   
    // Constraints. std::cout << 
    "subject to {" << std::endl; 
    
    for (IloInt i = 0; i < rngs.getSize(); ++i) 
    { IloRange rng = rngs[i]; 
    
    char 
    
    const *sense = 
    "=="; 
    
    double bnd = rng.getLB(); 
    
    if ( rng.getLB() <= -1e20 ) 
    { sense = 
    "<="; bnd = rng.getUB(); 
    } 
    
    else 
    
    if ( rng.getUB() >= 1e20 ) 
    { sense = 
    ">="; bnd = rng.getLB(); 
    } 
    
    else 
    
    if ( rng.getLB() != rng.getUB() ) 
    { 
    
    throw -1; 
    // not supported 
    } std::cout << rng.getExpr() << 
    " " << sense << 
    " " << bnd << 
    ";" << std::endl; 
    } std::cout << 
    "}" << std::endl;   env.end(); 
    } 
    } 
    
    catch (IloException& e) 
    { std::cerr << 
    "IloException: " << e << std::endl; 
    
    return -1; 
    } 
    
    return 0; 
    }
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How to convert CPLEX LP format to OPL format

    Posted 04/27/12 05:23 AM

    Originally posted by: SystemAdmin


    Hi,

    I tried compiling the C++ code you have provided. I could not see any effects on it. When I execute it just opens a command window and printing....

    Press any key to continue....

    Also I have one general question kindly clarify here....

    What is the difference in using the ilomipex2.cpp file ( which reads in LP file and solves MIP problem) and implementing our own c++ code for the model.

    As I understood, both the ways returns a same output solution readable through file. Is it correct???
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How to convert CPLEX LP format to OPL format

    Posted 05/01/12 11:48 AM

    Originally posted by: SystemAdmin


    > I tried compiling the C++ code you have provided. I could not see any effects on it. When I execute it just opens a command window and printing....
    >
    > Press any key to continue....
    >
    The code expects the model file to be converted on the command line and then prints the OPL model to stdout. So you have to provide the model file to be converted on the command line and capture the output of the program in a .mod file.
    On the other hand, it should be no problem to change the code to use a different method for input and/or output.

    > Also I have one general question kindly clarify here....
    >
    > What is the difference in using the ilomipex2.cpp file ( which reads in LP file and solves MIP problem) and implementing our own c++ code for the model.
    >
    These are just two different ways to populate and instance of the IloModel class. Once the instance is populated it does not matter any longer where the data came from. If you already have existing code that generates LP files then it might be simpler to just read in LP files. If you don't have such code yet then it is probably simpler to directly build up your model in C++.

    > As I understood, both the ways returns a same output solution readable through file. Is it correct???
    >
    Correct.
    #CPLEXOptimizers
    #DecisionOptimization