以下参考博文 https://www.jianshu.com/p/e1c45b3d8d8a
xxxvar(n) % 产生n*n的对称方阵
xxxvar(n,m) % 产生n*m的矩阵
xxxvar(n,n,'full') % 产生n*n的非对称方阵
C = [条件1;条件2;条件3;…];
例如条件 0 ≤ x 1 + x 2 ≤ 5 0\leq x_1+x_2 \leq 5 0≤x1+x2≤5,可以写成:
x = intvar(1,2) % 定义决策变量
C = [0 <= x(1) + x(2) <= 5]; % 定义约束条件
即设置求解器,有时可以不设置
options = sdpsettings('solver','cplex') % 设置求解器为cplex
默认为求解最小化问题
optimize(constraints, target, options)
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+2x3≥8
3 x 1 + 2 x 2 ≥ 6 \quad\quad\; 3x_1 + 2x_2 \geq 6 3x1+2x2≥6
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
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,} & {1minZst.=∑i=1n∑j=1ndijxij∑i=1,i=jnxij=1,∑j=1,j=inxij=1,ui−uj+nxij≤n−1,xij=0或1,ui为实数,j=1,…,ni=1,…,n1<i=j≤nx,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
```