添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
public static void main(String[] args) { // TODO Auto-generated method stub 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, 2.0, 3.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])), 20); cplex.addLe(cplex.sum(cplex.prod(1.0, x[0]), cplex.prod(-3.0, x[1]), cplex.prod(1.0, x[2])), 30); 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"); }

20190928224117741.png

3.3 注意事项

如果java64位Cplex是32位时,会报错误


java.lang.UnsatisfiedLinkError: ....\cplex\bin\x86_win32\cplex1261.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
java.library.path must point to the directory containing the CPLEX shared library
try invoking java with java -Djava.library.path=...
Exception in thread "main" java.lang.UnsatisfiedLinkError: ilog.cplex.Cplex.CPXopenCPLEX([I)J
    at ilog.cplex.Cplex.CPXopenCPLEX(Native Method)
    at ilog.cplex.CplexI.init(CplexI.java:6608)
    at ilog.cplex.CplexI.<init>(CplexI.java:629)
    at ilog.cplex.IloCplex.<init>(IloCplex.java:10194)
    at ilog.cplex.IloCplex.<init>(IloCplex.java:10209)
    at javaCplex.jCplex.main(jCplex.java:12)


4 Matlab 调用Cplex的配置与示例

4.1 配置

4.1.1 设置路径

注意保持Matlab与Cpex的位数一致性,会报类似【未定义函数或变量 'cplexlink1261']的错误

32位的配置:


20190928125851373.png

64位同上

使用help函数验证是否配置成功

20190928224450910.png


4.1.2 下载yalmip及配置

20190928225659634.png

配置完后,关闭并重启Matlab,测试:

1. >> which issymmetric
2. >> which ishemitian
3. >> which issymmetric

20190928230512454.png

4.2 算例编写

% 清除工作区
clear;clc;close all;
% 创建决策变量
x = sdpvar(1,3);
%创建约束
C = [
        -x(1) + x(2) + x(3) <= 20
        x(1) - 3 * x(2) + x(3) <= 30
        0 <= x(1) <= 40
ops = sdpsettings('verbose',0);
% 目标函数
z = -(x(1) + 2 * x(2) + 3 * x(3)); % 注意这是求解最大值,默认是求最小值,所以要加上负号
reuslt = optimize(C,z);
if reuslt.problem == 0 % problem =0 代表求解成功
    value(x)
    -value(z)   % 反转
    disp('求解出错');
end

4.3 算例结果演示

20190928232126127.png


5 python配置Cplex

5.1 配置

这个比较简单,直接找到安装目录下的cplex\python\2.7\x86_win32下找到Cplex文件夹,将其赋值到python27的安装目录下的Lib\site-packages下,打开python控制台,输入如下代码:


import cplex
help(cplex)

如果可以看到帮助文档则说明配置成功,

此外本人还做了python37的测试,发现32位的Cplex不能在64位的python37上匹配成功

5.2 编码示例

# -*- coding: utf-8 -*-
import cplex
from cplex.exceptions import CplexError
# data common to all populateby functions
my_obj = [1.0, 2.0, 3.0]
my_ub = [40.0, cplex.infinity, cplex.infinity]
my_lb = [0.0, 0.0, 0.0]
my_ctype = "CCC"
my_colnames = ["x1", "x2", "x3"]
my_rhs = [20.0, 30.0]
my_rownames = ["r1", "r2"]
my_sense = "LL"
def populatebyrow(prob):
    prob.objective.set_sense(prob.objective.sense.maximize)
    prob.variables.add(obj=my_obj, lb=my_lb, ub=my_ub, types=my_ctype,
                       names=my_colnames)
    rows = [[["x1", "x2", "x3"], [-1.0, 1.0, 1.0]],
            [["x1", "x2", "x3"], [1.0, -3.0, 1.0]]
    prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
                                rhs=my_rhs, names=my_rownames)
    my_prob = cplex.Cplex()
    handle = populatebyrow(my_prob)
    my_prob.solve()
except CplexError as exc:
    print(exc)
print()
# solution.get_status() returns an integer code
print("Solution status = ", my_prob.solution.get_status(), ":")
# the following line prints the corresponding string
print(my_prob.solution.status[my_prob.solution.get_status()])
print("Solution value  = ", my_prob.solution.get_objective_value())
numcols = my_prob.variables.get_num()
numrows = my_prob.linear_constraints.get_num()
slack = my_prob.solution.get_linear_slacks()
x = my_prob.solution.get_values()
print('x: ')
print(x)      

5.3 结果演示

20190928133120492.png

6 总结

  • 对于C++和C#,都是在VS环境下进行的开发,不存在32位和64位的兼容问题
  • java和Matlab以及Python37建议使用64位的Cpex版本
  • Matlab在效率上还是比较低的