2011年12月12日星期一

设置 Eclipse 以在 Java 程序中调用 CPLEX

前提当然是要成功安装CPLEX,我的系统为 Ubuntu 9.10 Karmic + CPLEXStudioAcademic123(安装在 /opt/ibm/ILOG/CPLEXStudioAcademic123/ 下)

我的做法是:


  1. /opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/lib

    下找到 cplex.jar


  2. /opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/bin/x86-64_sles10_4.1

    下找到 libcplex123.so

  3. 拷贝 cplex.jar 与 libcplex123.so 到 Eclipse 根目录 {EclipseHomeDir} 下的 needed/cplex 子目录中

首先在编译路径中加入CPLEX Jar 包,以便代码能够正确编译

(1). Project > Properties > Java Build Path > Libraries
(2). Add External JARs.
(3). Browse to the location of your CPLEX installation and select the file named cplex.jar.

For me,即是加入外部 Jar 包 {EclipseHomeDir}/needed/cplex/cplex.jar

如此程序即可正确编译,但还不能正常运行。

IBM ILOG CPLEX 官网上的 Tutorial 提及这一问题,并给出解决方案如下:

http://publib.boulder.ibm.com/infocenter/cosinfoc/v12r3/index.jsp

CPLEX > Getting Started with CPLEX > Setting up CPLEX > Setting up Eclipse for the Java API of CPLEX

Before you can run your compiled code, you must create an appropriate run configuration in Eclipse or modify an existing run configuration appropriately. To do so, follow these steps.
1. ...
2. ...
3. Go to the Arguments tab, select VM arguments, and add the path to the CPLEX library. The CPLEX library is conventionally a DLL file on Windows or a .so file on UNIX or GNU/Linux, with an indication of the version number in its name, like cplexXX.dll or cplexXX.so where XX represents a version number of the product.

Tip:
Add the path to the directory or folder containing the CPLEX library (not the name of the file itself).

For example, on UNIX or GNU/Linux , if the file libcplexXX.so is located in the directory COSinstallation/cplex/bin/myPlatform/myLibFormat then add a path similar to this example:
-Djava.library.path= COSinstallation/cplex/myPlatform/myLibFormat

Similarly, on Windows, if the file cplexXX.dll is located in the folder COSinstallation\cplex\myPlatform\myLibFormat then add a path similar to this example:
-Djava.library.path=COSinstallation/cplex/myPlatform/myLibFormat

4. ...

After these steps, you can run your Java application using CPLEX as you run other applications from Eclipse.

按照这一说明,对于我的case,

Run > Run Configurations ... > Java Application > Arguments > VM arguments 中的参数应为

-Djava.library.path= /opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/bin/x86-64_sles10_4.1/

-Djava.library.path= {Eclipse_Home_Dir}/needed/cplex/

遗憾的是,这样的设置并不能成功运行,而是会导致类似如下的异常:

Exception in thread "main" java.lang.NoClassDefFoundError: /opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/bin/x86-64_sles10_4/1/
Caused by: java.lang.ClassNotFoundException: .opt.ibm.ILOG.CPLEX_Studio_Academic123.cplex.bin.x86-64_sles10_4.1.

我的解决方法是:

{Eclipse 菜单} Window > Preferences > Run/Debug > String Substitution > New...

建立一个新的变量
variable: cplex_library
value: {EclipseHomeDir}/needed/cplex/

(注-1:{EclipseHomeDir} 应该替换为你自己实际的Eclipse 根目录。)
(注-2:这也就是为什么在开头我们把 cplex.jar 与 libcplex123.so 拷贝到这一目录中的原因)

回到 Run > Run Configurations ... > Java Application > Arguments > VM arguments

-Djava.library.path=${cplex_library}

Apply > run, 可以看到

IBM ILOG License Manager: "IBM ILOG Optimization Suite for Academic Initiative" is accessing CPLEX 12 with option(s): "e m b q ".
Parallel mode: deterministic, using up to 2 threads for concurrent optimization.

说明成功!

Ubuntu 下安装 CPLEX 12.3 (学院版)

我的系统:Ubuntu 9.10 Karmic

首先下载如下文件
1. CPLEX for Linux 的安装文件: cplex_studio123.acad.linux-x86-64.bin
2. License 文件: access.ilm
3. 安装帮助文件: ILOGQuickStart.pdf

安装CPLEX

$ sudo chmod 775 cplexstudio123.acad.linux-x86-64.bin
$ cplex
studio123.acad.linux-x86-64.bin

跟随提示即可安装。安装后的CPLEX 在 /opt/ibm/ILOG/CPLEXStudioAcademic123 下

安装 CPLEX License 文件

按照 ILOGQuickStart.pdf 中的说明,建立 /usr/ilog/ilm (需root权限)
复制 access.ilm 至 /usr/ilog/ilm

测试:


/opt/ibm/ILOG/CPLEXStudioAcademic123/cplex/bin/x86-64sles104.1$ cplex

应出现


IBM ILOG License Manager: "IBM ILOG Optimization Suite for Academic Initiative" is accessing CPLEX 12 with option(s): "e m b q ".

IBM ILOG CPLEX Optimization Studio Academic Research Edition
Welcome to IBM(R) ILOG(R) CPLEX(R) Interactive Optimizer 12.3.0.0
with Simplex, Mixed Integer & Barrier Optimizers
5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55
Copyright IBM Corp. 1988, 2011. All Rights Reserved.

Type 'help' for a list of available commands.
Type 'help' followed by a command name for more
information on commands.

CPLEX> exit

说明安装成功。