[matlab]cplex和yalmip安装后联合测试

测试环境:

windows10 x64 

matlab2018b

cplex==12.10.0

yalmip最新版本

代码一:

cplex = which('cplex');
if isempty(cplex)
    error('Cplex is not installed.');
else
    disp('Cplex is installed.');
end

% Define decision variables
x = sdpvar(2,1);

% Define objective function
obj = [1 2]*x;

% Define constraints
Constraints = [2*x(1) + x(2) <= 3, x(1) + 3*x(2) <= 4, x >= 0];

% Define options for the solver
ops = sdpsettings('solver','cplex');

% Solve the problem
sol = optimize(Constraints,obj,ops);

% Display the solution
disp(value(x))

输出结果:

Cplex is installed.
Matlab Toolbox parameter "Display" found.
警告: Using Matlab Toolbox parameters in the CPLEX Matlab API is deprecated. They will be treated as unknown
parameters in the future. 
> In handleparams
  In cplexqcp
  In cplexlp
  In call_cplexibm_miqp>localSolverCall (line 140)
  In call_cplexibm_miqp (line 32)
  In call_cplexibm_qcmiqp (line 9)
  In solvesdp (line 423)
  In optimize (line 31)
  In Untitled (line 21) 
Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae
CPXPARAM_MIP_Display                             1
Tried aggregator 1 time.
LP Presolve eliminated 4 rows and 2 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.00 ticks)
     0
     0

代码二:



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

测试结果:

开始求解
Matlab Toolbox parameter "Display" found.
警告: Using Matlab Toolbox parameters in the CPLEX Matlab API is deprecated. They will be treated as unknown
parameters in the future. 
> In handleparams
  In cplexqcp
  In cplexlp
  In call_cplexibm_miqp>localSolverCall (line 140)
  In call_cplexibm_miqp (line 32)
  In call_cplexibm_qcmiqp (line 9)
  In solvesdp (line 423)
  In optimize (line 31)
  In Untitled2 (line 19) 
Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae
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.00 sec. (0.00 ticks)

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

温馨提示:

由于新版本yalmip不支持Strict inequalities are not supported (learn why),即如果上面代码

constraint=[constraint,x(1)+x(2)>350];

就会报错,需要用>=才行哦。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农张三疯

你的打赏是我写文章最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值