求解器 | Matlab + Yalmip + Gurobi 联合求解

前言

最近跟老师在做数字孪生的问题,里面涉及到了要去求解规划问题,主要涉及了鲁棒性优化 [Robust Optimization] 和 整数规划。在这个过程中,了解到了很多利用matlab与python的求解器平台,比如CplexXprogYalmipGurobi

Gurobi整数规划代码实例

function [x,y]=Gurobi_LP
    
    clc;
    clear;
    yalmip('clear');

    % 定义变量
    cons=[];

    % 决策变量
    x=sdpvar(1,3);

    % 目标函数
    y=2*x(1)+3*x(2)+x(3);

    % 约束条件
    cons=[cons,x(1)+4*x(2)+2*x(3)>=8];
    cons=[cons,3*x(1)+2*x(2)>=6];
    cons=[cons,x(1)>=0];
    cons=[cons,x(2)>=0];
    cons=[cons,x(3)>=0];

    % 求解
    ops = sdpsettings('solver','gurobi','showprogress',1);

    optimize(cons,y,ops);

    % 结果
    x=double(x);
    y=double(y);
    
end
ops = sdpsettings('solver','gurobi','showprogress',1);

这条语句可以设置求解的细节,比如是否展示求解过程 showprogress

结果

+ Solver chosen : GUROBI-GUROBI
+ Processing objective function
+ Processing constraints
+ Calling GUROBI
Academic license - for non-commercial use only - expires 2021-12-21
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (win64)
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 5 rows, 3 columns and 8 nonzeros
Model fingerprint: 0x2525fbe3
Coefficient statistics:
  Matrix range     [1e+00, 4e+00]
  Objective range  [1e+00, 3e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [6e+00, 8e+00]
Presolve removed 3 rows and 0 columns
Presolve time: 0.07s
Presolved: 2 rows, 3 columns, 5 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    0.0000000e+00   3.500000e+00   0.000000e+00      0s
       2    7.0000000e+00   0.000000e+00   0.000000e+00      0s

Solved in 2 iterations and 0.10 seconds
Optimal objective  7.000000000e+00

ans =

     2     0     3

你可能感兴趣的:(matlab,matlab)