Nattakorn Promwongsa
Nattakorn Promwongsa

Reputation: 71

How to call cplex .mod and .data from Java

I have an optimization problem modelled and written in IBM ILOG CPLEX Optimization Studio. I want to call .mod and .dat from Java. I found some example to do it. However, I got some error.

My code is shown below. I also added all cplex and opl library

package cplexJava;
import ilog.concert.*;
import ilog.cplex.*;
import ilog.opl.*;


public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
              model();
    }
     public static void model() {
          int status = 127;                                                       

              IloOplFactory.setDebugMode(true);                                     
              IloOplFactory oplF = new IloOplFactory();                             
              IloOplErrorHandler errHandler = oplF.createOplErrorHandler();         
              IloOplModelSource modelSource = oplF.createOplModelSource("D:/Cplex project/Example_2/Example_2.mod");                                            
              IloOplSettings settings = oplF.createOplSettings(errHandler);         
              IloOplModelDefinition def = oplF.createOplModelDefinition(modelSource,settings);
              IloCplex cplex = oplF.createCplex();                                  
              cplex.setOut(null);                                                   
              IloOplModel opl = oplF.createOplModel(def, cplex);                    
              IloOplDataSource dataSource = oplF.createOplDataSource("D:/Cplex project/Example_2/Example_2.dat");                                            
              opl.addDataSource(dataSource);                                        
              opl.generate();                                                       
              if (cplex.solve())                                                    
              {                                                                     
                  System.out.println("OBJECTIVE: " + opl.getCplex().getObjValue()); 
                  opl.postProcess();                                                
                  opl.printSolution(System.out);                                    
              }                                                                     
              else                                                                  
              {                                                                     
                  System.out.println("No solution!");                               
              }                                                                     
              oplF.end();                                                           
                      status = 0;                                                   

            System.exit(status);
     }
}

In my code, the errors came from from oplF.createCplex() and cplex.solve(). When I tried to run it, this is the error I got. enter image description here I could not figure out why I got the errors from oplF.createCplex() and cplex.solve() although I already added the cplex and opl library

Upvotes: 0

Views: 1410

Answers (3)

Oliver Gebert
Oliver Gebert

Reputation: 1176

Actually your IDE tells you what the problem is: There are possible IloExceptions thrown and you do not handle them. You need to either surround your code with a try catch block, or your main-method should have a "throws IloException" in the signature:

package cplexJava;
import ilog.concert.*;
import ilog.cplex.*;
import ilog.opl.*;


public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        model();
    }
    public static void model() {
        int status = 127;
        try {

            IloOplFactory.setDebugMode(true);
            IloOplFactory oplF = new IloOplFactory();
            IloOplErrorHandler errHandler = oplF.createOplErrorHandler();
            IloOplModelSource modelSource = oplF.createOplModelSource("D:/Cplex project/Example_2/Example_2.mod");
            IloOplSettings settings = oplF.createOplSettings(errHandler);
            IloOplModelDefinition def = oplF.createOplModelDefinition(modelSource,settings);
            IloCplex cplex = oplF.createCplex();
            cplex.setOut(null);
            IloOplModel opl = oplF.createOplModel(def, cplex);
            IloOplDataSource dataSource = oplF.createOplDataSource("D:/Cplex project/Example_2/Example_2.dat");
            opl.addDataSource(dataSource);
            opl.generate();
            if (cplex.solve())
            {
                System.out.println("OBJECTIVE: " + opl.getCplex().getObjValue());
                opl.postProcess();
                opl.printSolution(System.out);
            }
            else
            {
                System.out.println("No solution!");
            }
            oplF.end();
        } catch (IloException ilx) {
            // log error message or something like that
        }
        status = 0;

        System.exit(status);
    }
}

And please use class names with upper case first letter and package names with all lower case.

Upvotes: 1

Ky Phuc
Ky Phuc

Reputation: 1

I also faced the the same problem.

After some trial and error I realized that the correct name is DYLD_LIBRARY_PATH for macos.

Referral link

Upvotes: 0

rkersh
rkersh

Reputation: 4465

For the OPL Java API, you should only need oplall.jar.

SETUP

On my x86-64 Linux machine with Eclipse 3.6, this is done, like so (hopefully it's similar for you):

  1. Right click on your Java Project and select Properties
  2. Select "Java Build Path" on the left and the Libraries tab on the right
  3. Click on the "Add External JARs..." button and select COS_INSTALL_DIR/opl/lib/oplall.jar (where COS_INSTALL_DIR is the location where you installed CPLEX Optimization Studio)
  4. Click OK

One more thing to do is make sure that your LD_LIBRARY_PATH environment variable is set to COS_INSTALL_DIR/opl/bin/x86-64_linux. (NOTE: On Windows, I think you should set the PATH environment variable instead.) You can set this in Eclipse, like so:

  1. Select "Run > Run Configurations..." in the menu
  2. On the left, select your java application
  3. On the right, select the Environment tab and click on the "New..." button
  4. Enter LD_LIBRARY_PATH in the Name field (try PATH on Windows)
  5. Enter COS_INSTALL_DIR/opl/bin/x86-64_linux in the Value field (again, where COS_INSTALL_DIR is the location where you installed CPLEX Optimization Studio)
  6. Click OK

FIX COMPILER ERRORS

Once you have that set up, you'll probably notice that you are still getting compiler errors (the little red squiggle lines indicate this). Hover your mouse over the those and you'll be presented with a list of quick fixes: 1) add throws declaration; 2) Surround with try/catch. Pick one of those to fix the issue. After all of the red squiggly lines are gone you should be able to run your program.

If you're not familiar with fixing compiler errors in Eclipse, maybe this Eclipse tutorial with help. Sometimes you have to select "Project > Clean" to force a recompile.

Upvotes: 0

Related Questions