Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Different engines in one flow control in OPL script language

    Posted 07/16/12 07:04 AM

    Originally posted by: SystemAdmin


    Hi , can anyone help how could i use two model with different engines (CPLEX and CP )in a main block and use the result of one as another input to another one ?
    thank you
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Different engines in one flow control in OPL script language

    Posted 07/16/12 07:20 AM

    Originally posted by: SystemAdmin


    I am not an OPL expert but I once used this snippet to solve the same model with two different IloCplex instances (calls to end() functions omitted):
    var model1 = thisOplModel.modelDefinition;
    var data1 = thisOplModel.dataElements;
    var cplex1 = new IloCplex();
    var opl1 = new IloOplModel(model1, cplex1);
    opl1.addDataSource(data1);
    opl1.generate();
    ...
    cplex1.solve();
    ...
    ...
    var model2 = thisOplModel.modelDefinition;
    var data2 = thisOplModel.dataElements;
    var cplex2 = new IloCplex();
    var opl2 = new IloOplModel(model2, cplex2);
    opl2.addDataSource(data2);
    opl2.generate();
    ...
    cplex2.solve();
    ...
    

    If you replace cplex2 with an instance of IloCP this may work.
    In any case, the OPL Forum is probably a better place to ask your question.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Different engines in one flow control in OPL script language

    Posted 07/16/12 07:49 AM

    Originally posted by: SystemAdmin


    Hi Daniel
    i have tried replacing it with IloCP but it doesnt work .
    in generate() method i have this error "Expecting CP, found CPLEX(default)."
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Different engines in one flow control in OPL script language

    Posted 07/18/12 05:05 AM

    Originally posted by: SystemAdmin


    The problem is that a .mod file (implicitly) defines which engines you can use to solve it: If the .mod file contains the directive 'using CPLEX;' then you can solve it only with IloCplex(). If the .mode file contains the directive 'using CP;' then you can solve it only with IloCP(). If the .mod file does not contain a 'using' directive then it is treated as if it had 'using CPLEX;'.
    You can work around this limitation by creating a .mod file without 'using' directive, copy that file to a new file with explicit 'using' directive and then read that new file. Here is a small example that does that:
    
    range idx = 1 .. 2; dvar int+ x[idx];   minimize 1*x[1] + 3*x[2]; subject to 
    { 4*x[1] + 5*x[2] >= 1; 
    }   main 
    { var input; var output; 
    // Create a copy of the .mod file that has an explicit 'using CPLEX;' directive. input = 
    
    new IloOplInputFile(
    "TwoEngines.mod"); output = 
    
    new IloOplOutputFile(
    "TwoEnginesCPLEX.mod"); output.writeln(
    "using CPLEX;"); 
    
    while (!input.eof) 
    { output.writeln(input.readline()); 
    } output.close(); input.close(); var source1 = 
    
    new IloOplModelSource(
    "TwoEnginesCPLEX.mod"); var model1 = 
    
    new IloOplModelDefinition(source1); var engine1 = 
    
    new IloCplex(); var opl1 = 
    
    new IloOplModel(model1, engine1); 
    // Uncomment the next two lines if your model uses a data file. 
    //var data1 = new IloOplDataSource("TwoEngines.dat"); 
    //opl1.addDataSource(data1); opl1.generate(); 
    
    if (engine1.solve()) 
    { writeln(
    "Optimal objective (CPLEX): " + engine1.getObjValue()); 
    } 
    
    else 
    { writeln(
    "infeasible"); 
    } 
    // Create a copy of the .mod file that has an explicit 'using CP;' directive. input = 
    
    new IloOplInputFile(
    "TwoEngines.mod"); output = 
    
    new IloOplOutputFile(
    "TwoEnginesCP.mod"); output.writeln(
    "using CP;"); 
    
    while (!input.eof) 
    { output.writeln(input.readline()); 
    } output.close(); input.close(); var source2 = 
    
    new IloOplModelSource(
    "TwoEnginesCP.mod"); var model2 = 
    
    new IloOplModelDefinition(source2); var engine2 = 
    
    new IloCP(); var opl2 = 
    
    new IloOplModel(model2, engine2); 
    // Uncomment the next two lines if your model uses a data file. 
    //var data2 = new IloOplDataSource("TwoEngines.dat"); 
    //opl2.addDataSource(data2); opl2.generate(); 
    
    if (engine2.solve()) 
    { writeln(
    "Optimal objective (CP): " + engine2.getObjValue()); 
    } 
    
    else 
    { writeln(
    "infeasible"); 
    } 
    }
    

    Since this is pure OPL question please refer to the OPL Forum for further guidance.
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Different engines in one flow control in OPL script language

    Posted 07/21/12 04:13 PM

    Originally posted by: SystemAdmin


    thanx Daniel , i think it works
    thank you very much for your effort.
    regards
    #CPLEXOptimizers
    #DecisionOptimization