#Matlab# Yalmip & CPLEX使用

2 篇文章 2 订阅
2 篇文章 0 订阅

以下参考博文 https://www.jianshu.com/p/e1c45b3d8d8a

使用yalmip求解优化问题的步骤

1. 创建决策变量

  1. sdpvar 创建实数型决策变量
  2. intvar 创建整数型决策变量
  3. binvar 创建0/1型决策变量
xxxvar(n)		      % 产生n*n的对称方阵
xxxvar(n,m)    		  % 产生n*m的矩阵
xxxvar(n,n,'full')    % 产生n*n的非对称方阵

2. 添加约束条件

C = [条件1;条件2;条件3;…];

例如条件 0 ≤ x 1 + x 2 ≤ 5 0\leq x_1+x_2 \leq 5 0x1+x25,可以写成:

x = intvar(1,2)          			% 定义决策变量
C = [0 <= x(1) + x(2) <= 5]; 		% 定义约束条件

3. 配置参数

即设置求解器,有时可以不设置

options = sdpsettings('solver','cplex')  % 设置求解器为cplex

4. 求解问题

默认为求解最小化问题

optimize(constraints, target, options)

5. 举例

5.1 求解线性规划问题

min      z = 2 x 1 + 3 x 2 + x 3 \textrm{min}\;\; z = 2x_1+ 3x_2 + x_3 minz=2x1+3x2+x3
s.t. x 1 + 4 x 2 + 2 x 3 ≥ 8 \quad x_1 + 4x_2 + 2x_3 \geq 8 x1+4x2+2x38
   3 x 1 + 2 x 2 ≥ 6 \quad\quad\; 3x_1 + 2x_2 \geq 6 3x1+2x26
   x 1 , x 2 , x 3 > = 0 \quad\quad\;x_1, x_2, x_3 >= 0 x1,x2,x3>=0

clear;clc;close all
x = sdpvar(1,3);
z = 2*x(1) + 3*x(2) + x(3);
c = [x(1) + 4*x(2) + 2*x(3) >= 8
     3*x(1) + 2*x(2) >= 6
     x(1), x(2), x(3) > 0];

result = optimize(c,z);

if result.problem == 0     % 求解成功
   xresult = value(x)
   zresult = value(z)
else
    disp("求解出错")    
end

运行结果如下

CPXPARAM_MIP_Display                             1
Tried aggregator 1 time.
LP Presolve eliminated 3 rows and 0 columns.
Reduced LP has 2 rows, 3 columns, and 5 nonzeros.
Presolve time = 0.02 sec. (0.00 ticks)

Iteration log . . .
Iteration:     1   Dual objective     =             4.000000

xresult =

     2     0     3


zresult =

     7

当然该问题也可以直接用 matlab自带的linprog进行求解,代码如下

c = [2 3 1]
a = [1 4 2;
     3 2 0];
b = [8;6];
[x,z] = linprog(c,-a,-b,[],[],zeros(3,1)) 

结果如下,与上面的一致。

Optimal solution found.


x =

    2.0000
         0
    3.0000


z =

    7.0000
5.2 求解混合整数线性规划问题

min ⁡    Z = ∑ i = 1 n ∑ j = 1 n d i j x i j s t . ∑ i = 1 , i ≠ j n x i j = 1 , j = 1 , … , n ∑ j = 1 , j ≠ i n x i j = 1 , i = 1 , … , n u i − u j + n x i j ≤ n − 1 , 1 < i ≠ j ≤ n x i j = 0 或 1 , x , j = 1 , … , n u i 为 实 数 , i = 1 , … , n \begin{array}{rl}{\min \;Z} & {=\sum_{i=1}^{n} \sum_{j=1}^{n} d_{i j} x_{i j}} \\ \\ st. & {\sum_{i=1,i\neq j}^{n} x_{i j}=1}, & {j=1, \ldots, n} \\ & {\sum_{j=1,j\neq i}^{n} x_{i j}=1}, & {i=1, \ldots, n} \\ &{u_i-u_{j}+n x_{i j} \leq n-1,} & {1<i \neq j \leq n} \\ &{x_{i j}=0或1,} & {x, j=1, \ldots, n} \\ & {u_{i}为实数,} & {i=1, \ldots, n}\end{array} minZst.=i=1nj=1ndijxiji=1,i=jnxij=1,j=1,j=inxij=1,uiuj+nxijn1,xij=01,ui,j=1,,ni=1,,n1<i=jnx,j=1,,ni=1,,n

clear; clc; close all
% 数据
d = [0 7 4 5 8 6 12 13 11 18
     7 0  3 10 9 14 5 14 17 17
     4 3 0 5 9 10 21 8 27 12
     5 10 5 0 14 9 10 9 23 16
     8 9 9 14 0 7 8 7 20 19
     6 14 10 9 7 0 13 5 25 13
     12 5 21 10 8 13 0 23 21 18
     13 14 8 9 7 5 23 0 18 12
     11 17 27 23 20 25 21 18 0 16
     18 17 12 16 19 13 18 12 16 0];
 n = size(d,1);
 x = binvar(n,n,'full');   % x为0-1
 u = sdpvar(1,n);          % u为实数
 
 % 逐一添加约束
 C = [];
 for i = 1:n
     s = sum(x(i,:))-x(i,i);
     C = [C;s == 1];
 end
 
 for j = 1:n
     s = sum(x(:,j)) - x(j,j);
     C = [C; s == 1];
 end
 
 for i = 2:n
     for j = 2:n
         if i ~= j
             s = u(i) - u(j) + n*x(i,j);
             C = [C;s <= n-1];
         end
     end
 end

 z = sum(sum(d.*x));
 result = optimize(C,z);
 if result.problem == 0
     xresult = value(x)
     zresult = value(z)
 else
     disp("求解出错")
 end

求解结果如下

CPXPARAM_MIP_Display                             1
Tried aggregator 1 time.
Reduced MIP has 92 rows, 99 columns, and 396 nonzeros.
Reduced MIP has 90 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.00 sec. (0.20 ticks)
Probing time = 0.00 sec. (0.15 ticks)
Tried aggregator 1 time.
Reduced MIP has 92 rows, 99 columns, and 396 nonzeros.
Reduced MIP has 90 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.02 sec. (0.27 ticks)
Probing time = 0.00 sec. (0.16 ticks)
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 4 threads.

Node log . . .
Best integer =   7.700000e+01  Node =       0  Best node =
  7.700000e+01
Clique cuts applied:  4

xresult =

   NaN     0     0     0     0     0     0     0     1     0
     0   NaN     1     0     0     0     0     0     0     0
     0     0   NaN     1     0     0     0     0     0     0
     1     0     0   NaN     0     0     0     0     0     0
     0     0     0     0   NaN     0     1     0     0     0
     0     0     0     0     1   NaN     0     0     0     0
     0     1     0     0     0     0   NaN     0     0     0
     0     0     0     0     0     1     0   NaN     0     0
     0     0     0     0     0     0     0     0   NaN     1
     0     0     0     0     0     0     0     1     0   NaN


yresult =

    77
```
  • 16
    点赞
  • 191
    收藏
    觉得还不错? 一键收藏
  • 25
    评论
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值