Cplex&Eclipse&IntelliJ环境配置

安装包

采用的是老师给的学术版cplex,或者商业版安装包cplex_studio1251.win-x86-64.exe。

学术版配置过程

软件安装

1.运行cplex111.x86_win32_0.exe
2.复制access.ilm到安装目录ILOG的ILM(Mkdir ILM if not exist)
3.添加环境变量
variable “ILOG_LICENSE_FILE”
value “C:\ILOG\ILM\access.ilm”
4.安装完成后打开 \安装目录bin\cplex.exe应有以下界面
Cplex&Eclipse&IntelliJ环境配置_第1张图片

代码测试

5.新建项目,使用测试代码,需要在Java Build Path添加外部jar(安装目录的lib中的cplex.jar),并且JRE System LIbrary的Native library location写入安装目录\bin\x86_win32
6.运行测试代码 可行!

//如果是Intellij 和NetBean,需将安装目录写入VM option
Intellij在Run/Debug Configurations中配置
https://stackoverflow.com/questions/23640003/combine-cplex-and-java

import ilog.concert.IloException;
import ilog.concert.IloIntVar;
import ilog.concert.IloLinearNumExpr;
import ilog.concert.IloNumVar;
import ilog.cplex.IloCplex;
import ilog.cplex.IloCplex.DoubleParam;

import java.util.Random;


public class Partition {

    public static final int n = 50;
    public static final int k = 2;
    public static int a[] = new int [n];

    public static void generate() {
        Random random = new Random(2);
        for (int i = 0; i < n;  i++)
            a[i] = random.nextInt(1000000);
    }

    public static void solve() {
        try {
            int i, j;
            IloCplex cplex = new IloCplex();
            IloIntVar x[][] = new IloIntVar [n][k];
            for (i = 0; i < n; i++)
            for (j = 0; j < k; j++)
                x[i][j] = cplex.boolVar();
            IloIntVar Z = cplex.intVar(0, Integer.MAX_VALUE);
            cplex.addMinimize(Z);
            IloLinearNumExpr expr = cplex.linearNumExpr();
            for (i = 0; i < n; i++) {
                expr.clear();
                for (j = 0; j < k; j++)
                    expr.addTerm(1, x[i][j]);
                cplex.addEq(expr, 1);
            }
            for (j = 0; j < k; j++) {
                expr.clear();
                for (i = 0; i < n; i++)
                    expr.addTerm(a[i], x[i][j]);
                cplex.addLe(expr, Z);
            }

            cplex.setParam(DoubleParam.TiLim, 20);
            if (cplex.solve()) {
                System.out.println(cplex.getObjValue());
                for (j = 0; j < k; j++) {
                    System.out.print("Group " + j + ":");
                    int t = 0;
                    for (i = 0; i < n; i++)
                    if (cplex.getValue(x[i][j]) > 1e-5) {
                        t += a[i];
                        System.out.print(" " + a[i]);
                    }
                    System.out.println(" " + t);
                }
            }
            cplex.end();
        } catch (IloException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    public static void main(String args[]) {
        generate();
        solve();

    }
}

商业版cplex配置过程

运行安装包cplex_studio1251.win-x86-64.exe,安装完成。环境配置等都在安装过程中集成了,所以 只需要打开IntelliJ,测试代码,在File-Project Structure-Modules-Dependencies-+外部jar包,也就是C:\IBM\ILOG\CPLEX_Studio1251\cplex\lib\cplex.jar,测试代码就可以正常运行。
Cplex&Eclipse&IntelliJ环境配置_第2张图片

你可能感兴趣的:(java,Cplex,intellij-idea使用)