YALMIP + CPLEX安装及测试

1 前情提要

首先从YLMIP获取YALMIP,然后从ShareApps获取CPLEX

注意CPLEXmatlab存在兼容性问题,因此需要在IBM产品兼容性检测平台确定matlab版本对应的CPLEX版本。

在这里插入图片描述

2 安装过程

  • YALMIP

压缩包下载之后,解压至可用路径,然后在matlab中把解压文件所在路径进行路径添加。YALMIP安装完毕,可以通过yalmiptest命令进行检测。

  • CPLEX

CPLEX为软件,进行正常的软件安装操作即可。把安装目录下的\CPLEX_Studio\cplex\matlab文件夹路径进行路径添加。CPLEX安装完毕,同样可以通过yalmiptest命令进行检测。

  • 小结

上述操作完成后,键入yalmiptest,我们可以发现CPLEX对应版本处于found状态。

3 样例

这里主要参考知乎专栏文章Matlab+yalmip+cplex安装教程及算例

  • 问题

m i n f ( x ) = 2 x 1 + 3 x 2 s . t . x 1 + x 2 ≥ 350 x 1 ≥ 100 2 x 1 + x 2 ≤ 600 x 1 , x 2 ≥ 0 min \quad f(x)=2x_1+3x_2 \\ s.t. \quad x_1+x_2 \geq 350\\ x_1 \geq 100 \\ 2x_1 + x_2 \leq 600 \\ x_1,x_2 \geq 0 minf(x)=2x1+3x2s.t.x1+x2350x11002x1+x2600x1,x20

  • 代码
%定义变量
x=sdpvar(2,1);
%目标函数
obj=2*x(1)+3*x(2);
%约束条件
constraint=[];
constraint=[constraint,x(1)+x(2)>350];
constraint=[constraint,x(1)>100];
constraint=[constraint,2*x(1)+x(2)<600];
constraint=[constraint,x(2)>0];
%求解
ops = sdpsettings('solver','cplex','verbose',1);
ops.cplex.display='on';
ops.cplex.timelimit=600;
ops.cplex.mip.tolerances.mipgap=0.001;
% 诊断求解可行性
disp('开始求解')
diagnostics=optimize(constraint,obj,ops);
if diagnostics.problem==0
    disp('Solver thinks it is feasible')
elseif diagnostics.problem == 1
    disp('Solver thinks it is infeasible')
    pause();
else
    disp('Timeout, Display the current optimal solution')
end
  • 说明
sdpvar:实数变量
intvar:整数变量
binvar:0-1变量
value:变量或表达式的值
ops = sdpsettings('solver','cplex','verbose',1):设置求解方法为调用 Cplex
  • 结果
CPXPARAM_TimeLimit                               600
CPXPARAM_MIP_Tolerances_MIPGap                   0.001
Tried aggregator 1 time.
LP Presolve eliminated 2 rows and 0 columns.
Reduced LP has 2 rows, 2 columns, and 4 nonzeros.
Presolve time = 0.17 sec. (0.00 ticks)

Iteration log . . .
Iteration:     1   Dual objective     =           700.000000
Solver thinks it is feasible

4 总结

在这里插入图片描述

参考文献

  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值