如何在java中调用cplex (Cplex in Java)

注意, 必要条件:

  1. ;J:\IBM\ILOG\CPLEX_Studio1271\cplex\bin\x64_win64;J:\IBM\ILOG\CPLEX_Studio1271\cplex\lib\cplex.jar 添加到环境变量path中;
  2. 将cplex.jar放到工程中并添加进路径;
  3. 将;J:\IBM\ILOG\CPLEX_Studio1271\cplex\bin\x64_win64添加到native library中。

下面是个小例子:

package javaTest.java;

import ilog.concert.*;
import ilog.cplex.*;
public class TestCplex {
	/**
	 * obj = x1 + 5x2 + 1X3
	 * -x1 + x2 + x3 <=56;
	 * x1 - 3x2 + x3 <= 39;
	 * 0 <= x1 <= 40;
	 * 0 <= x2, x3
	 * @param args
	 */
	
	public static void main(String[] args) {
		double[] lb = { 0.0, 0.0, 0.0};
		double[] ub = { 40.0, Double.MAX_VALUE, Double.MAX_VALUE};
		try {
			IloCplex cplex = new IloCplex();		
			IloNumVar[] x = cplex.numVarArray(3, lb, ub);
			double[] objvals = { 1.0, 5.0, 1.0 };
			cplex.addMaximize(cplex.scalProd(x, objvals));
			cplex.addLe(cplex.sum(cplex.prod(-1.0, x[0]), cplex.prod(1.0, x[1]), cplex.prod(1.0,
					x[2])), 56);
			cplex.addLe(cplex.sum(cplex.prod(1.0, x[0]), cplex.prod(-3.0, x[1]), cplex.prod(1.0,
					x[2])), 39);
			if (cplex.solve()) {
				cplex.output().println("Solution status = " + cplex.getStatus());
				cplex.output().println("Solution value = " + cplex.getObjValue());
				double[] val = cplex.getValues(x);
				int ncols = cplex.getNcols();
				for (int j = 0; j < ncols; ++j)
					cplex.output().println("Column: " + j + " Value = " + val[j]);
			}
			cplex.end();
		} catch (IloException e) {
			System.err.println("Concert exception '" + e + "' caught");
		}
	}
}

 

你可能感兴趣的:(Cplex,in,java)