Search code examples
javailogopl

Failed in interfacing Ilog OPL elements in Java


I am fairly new to Java OPL interface and I am being faced with a problem about how to obtain the values of the decision variables after CPLEX solves it. More particularly, I have this code which integrates an OPL model file and a data file:

String inDataFile = "F:\\UPS\\OPL\\data_mod_6.dat";    
String modfileloc = "F:\\UPS\\OPL\\model2.mod";     
IloOplFactory.setDebugMode(false);    
IloOplFactory oplF = new IloOplFactory();    
IloOplErrorHandler errHandler = oplF.createOplErrorHandler(System.out);    
IloOplModelSource modelSource = oplF.createOplModelSource(modfileloc);    
IloCplex cplex = null;

try {    
    cplex = oplF.createCplex();    
} catch (IloException e) {    
// TODO Auto-generated catch block
    e.printStackTrace();
}

IloOplSettings settings = oplF.createOplSettings(errHandler);    
IloOplModelDefinition def=oplF.createOplModelDefinition(modelSource,settings);    
IloOplModel opl=oplF.createOplModel(def,cplex);    
//IloOplDataElements DataElements = opl.makeDataElements();     

IloOplDataSource dataSource=oplF.createOplDataSource(inDataFile);    
opl.addDataSource(dataSource);    

opl.generate();     
if (cplex.solve()){    
    IloNumMap  sw1 = opl.getElement("sw").asNumMap();
    double [] sw = null;
    int sw_length = sw1.getSize();
    for (int i=0; i< sw_length; i++)
    {
       sw[i] = sw1.get(i+1);
    }
}

Now sw here is an "double" vector decision variable which is obtained from the cplex.solve() step. However it gives me an error saying:

Exception in thread "main" ilog.concert.IloException: Type dvar float+ expected for element "sw"

at ilog.opl.lang.wrapJNI.IloOplElement_cpp_asNumMap(Native Method)

Please help me as to whether it's a declaration error or am I missing some of the libraries for OPL (I have only the oplall.jar files from the CPLEx installation kit)

Regards,

Praveen


Solution

  • If sw is a dvar float+, then you rather use asNumVarMap rather than asNumMap.

    You should have a look at examples in

    opl\examples\opl_interfaces\java