代码1

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

代码2

 
 
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

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐