目錄
一、基礎知識
1.下載安裝過程
請看:matlab中調用cplex 以及使用 Yalmip 工具箱https://blog.csdn.net/robert_chen1988/article/details/44464369。
2.比較好的命令
教程在yalmip官網上有:https://yalmip.github.io/tutorials/
sdpvar:實數變量,intvar:整數變量,binvar:0-1變量
check:可以檢查約束條件是否被滿足(檢查約束條件的餘值)
value:可以查看變量或表達式的值
assign: 可以給變量賦值,這個命令調試時很重要
options=sdpsettings('solver','cplex'); 設置求解方法爲調用 CPLEX
optimize(constraints,f,options); Yalmip求解的命令。
3.yalmip + lpsolve + matlab
求解混合整數線性規劃問題(MIP/MILP) https://blog.csdn.net/a_learning/article/details/53337335。
4.#Matlab# Yalmip & CPLEX使用
https://blog.csdn.net/qq_22476313/article/details/100937540。
5.YALMIP學習總結
https://blog.csdn.net/qq_15651815/article/details/79615664。
6.YALMIP學習筆記-基礎知識
https://blog.csdn.net/qq_16309049/article/details/91549610。
7.yalmip簡單的例子
https://blog.csdn.net/cj461733387/article/details/101694103。
二、親測案例11個
1.案例一
max 4x1 + 2x2 + x3
s. t. 2x1 + x2 <= 1,x1 + 2x3 <= 2,x1 + x2 + x3 = 1,
x1 >= 0,x1 <= 1,x2 >= 0,x2 <= 1,x3 >= 0,x3 <= 2
clear;clear all;clc;close all;
tic;
%案例一
x=sdpvar(3,1);
f=[4 2 1]*x;
F=(2*x(1)+x(2)<=1);
F=F+(x(1)+2*x(3)<=2);
F=F+(x(1)+x(2)+x(3)==1);
F=F+(0<=x(1)<=1)+(0<=x(2)<=1)+(0<=x(3)<=2);
ops=sdpsettings('solver','cplex','verbose',2);
result=solvesdp(F,-f,ops);
if result.problem==0
xresult=value(x)
zresult=value(f)
else
disp('求解錯誤')
end
toc;
Iteration: 1 Dual objective = -2.500000
xresult =
0.5000
0
0.5000
zresult =
2.5000
時間已過 1.452390 秒。
2.案例二
clear;clear all;clc;close all;
tic;
x=sdpvar(3,1);
f=[2 3 1]*x;
F=[x(1)+4*x(2)+2*x(3)>=8;3*x(1)+2*x(2)>=6;x(1)>=0;x(2)>=0;x(3)>=0];
opt=sdpsettings('solver','cplex');
result=solvesdp(F,f,opt);
if result.problem==0 %求解成功
resultx=double(x)
resultz=double(f)
else
disp('求解出錯')
end
toc;
Iteration: 1 Dual objective = 4.000000
resultx =
2
0
3
resultz =
7
時間已過 1.462861 秒。
3.案例三
求解題目與2相同,但使用默認求解器,可以不用人爲選擇cplex。
clear;clear all;clc;close all;
tic;
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
toc;
Iteration: 1 Dual objective = 4.000000
xresult =
2 0 3
zresult =
7
時間已過 1.499445 秒。
4.案例四
求解混合整數線性規劃
clear;clear all;clc;close all;
tic;
%案例四
% 數據
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));
% ops=sdpsettings('solver', 'cplex');%選擇求解器
% result=solvesdp(C,z,ops);%求解
result=optimize(C,z);
if result.problem == 0
xresult = value(x)
zresult = value(z)
else
disp('求解出錯')
end
toc;
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
zresult =
77
時間已過 1.922839 秒。
5.案例五
YALMIP默認使用QUADPROG來求解問題
這裏的公式約束條件沒表示完,還有部分
clear;clear all;clc;close all;
tic;
%案例五
x=sdpvar(10,1);
%添加約束
C=[];
for i=1:10;
C=[C;sum(x(i))<=10;x(1)==0;0.5<=x(2)<=1.5];
end
for i=1:7
C=[C;x(i)+x(i+1)<=x(i+2)+x(i+3)];
end
%目標函數
z=x'*x+sum(abs(x));%norm(x,1);
ops=sdpsettings('solver','cplex,quadprag');
result=optimize(C,z,ops);
if result.problem==0
xrusult=value(x)
zresult=value(z)
else
disp('求解錯誤')
end
toc;
xrusult =
0
0.5000
0.0833
0.4167
0.1667
0.3333
0.2500
0.2500
0.3333
0.1667
zresult =
3.3333
時間已過 1.870840 秒。
6.案例六
在這個例子中,我們給出兩組數據,分別稱之爲藍色和綠色.我們的目標就是利用線性分類器,儘可能地將這兩組數據分開
blues=randn(2,25);%0附近正態隨機生成
greens=randn(2,25)+2;
plot(greens(1,:),greens(2,:),'g*')
hold on
plot(blues(1,:),blues(2,:),'b*')
clear;clear all;clc;close all;
tic;
% %案例六分類器
blues=randn(2,25);%0附近正態隨機生成
greens=randn(2,25)+2;
plot(greens(1,:),greens(2,:),'g*')
hold on
plot(blues(1,:),blues(2,:),'b*')
a=sdpvar(2,1);
b=sdpvar(1);
u=sdpvar(1,25);
v=sdpvar(1,25);
%z=sum(u)+sum(v);
%ops=sdpsettings('solver','cplex');
%result=solvesdp(C,z,ops);
C=[a'*greens+b>=1-u;a'*blues+b<=-(1-v);u>=0;v>=0;-1<=a<=1];%約束
Objective=sum(u)+sum(v);
result=optimize(C,Objective)
if result.problem==0
aresult=value(a)
bresult=value(b)
else
disp('求解錯誤')
end
x = sdpvar(2,1);
P1 = [-5<=x<=5,value(a)'*x+value(b)>=0];%這裏該怎麼改
P2 = [-5<=x<= 5,value(a)'*x+value(b)<=0];
clf
plot(P1);hold on
plot(P2);
plot(greens(1,:),greens(2,:), 'g*')
plot(blues(1,:),blues(2,:), 'b*')
toc;
Iteration: 1 Dual objective = 0.000000
result =
yalmiptime: 0.8460
solvertime: 0.0620
info: 'Successfully solved (CPLEX-IBM)'
problem: 0
aresult =
1
1
bresult =
-1.8502
時間已過 2.717646 秒。
7.案例七
clear;clear all;clc;close all;
tic;
%案例七一般線性規劃
c=[12 5 8];
A=[2 3 1; 4 1 5];
b=[30;15];
%決策變量
x=sdpvar(3,1);
Z=c*x;%目標函數
C=[A*x>=b;x>=0];%約束
ops=sdpsettings('solver','cplex');
result=solvesdp(C,Z,ops);
if result.problem==0
xresult=value(x)
zresult=value(Z)
else
disp('求解錯誤')
end
toc;
Iteration: 1 Dual objective = 50.000000
xresult =
0
9.6429
1.0714
zresult =
56.7857
時間已過 1.314671 秒。
8.案例八
clear;clear all;clc;close all;
tic;
%案例八運輸問題
%基礎數據
c=[1 3 5 7 13 ;6 4 3 14 8 ;13 3 1 7 4 ;1 10 12 7 11];
a=[40 50 30 80];
b=[10 20 15 18 25];
%決策變量
x=sdpvar(4,5);
%目標函數
Z=sum(sum(c.*x));
%約束條件
C=[x>=0];
for i=1:4
s=sum(x(i,:));
C=[C;s<=a(i)];
end
for j=1:5
s=sum(x(:,j));
C=[C;s>=b(j)];
end
%求解
ops=sdpsettings('solver','cplex');
result=solvesdp(C,Z,ops);
if result.problem==0
xresult=value(x)
zresult=value(Z)
else
disp('求解錯誤')
end
toc;
xresult =
2 20 0 18 0
0 0 10 0 0
0 0 5 0 25
8 0 0 0 0
zresult =
331
時間已過 1.383890 秒。
9.案例九
clear;clear all;clc;close all;
tic;
%案例九揹包問題
%基本數據
w=[17 19 3 19 13 2 6 11 20 20];%重量
v=[2 10 10 5 9 2 5 10 8 10];%體積
n=[5 2 4 3 5 4 3 1 5 3];%數量
c=[8 1 11 12 9 10 9 5 8 3];%效用
%決策變量
x=intvar(10,1);
%目標函數
z=-(c*x);
%約束條件
C=[w*x<=80;v*x<=60;(0<=x)];
for i=1:10
C=[C;x(i)<=n(i)];
end
%求解
ops=sdpsettings('solver','cplex');
result=solvesdp(C,z,ops);
if result.problem==0
xresult=value(x)
zresult=-value(z)
else
disp('求解錯誤')
end
toc;
xresult =
1
0
3
1
0
4
3
0
0
0
zresult =
120
時間已過 1.372422 秒。
10.案例十
求解最短路程問題
clear;clear all;clc;close all;
tic;
%案例十最短路程問題
% 利用yamlip求解最短路問題
D=load('1.txt')%距離數據
n=size(D,1);
%決策變量
x=binvar(n,n,'full');
%目標
z=sum(sum(D.*x));
%約束
C=[];
C=[C;(sum(x(1,:))-sum(x(:,1))==1)];
C=[C;(sum(x(n,:))-sum(x(:,n))==-1)];
for i=2:(n-1)
C=[C;(sum(x(i,:))-sum(x(:,i))==0)];
end
%求解
ops=sdpsettings('solver','cplex');
result=solvesdp(C,z,ops);
%結果顯示
if result.problem==0
xresult=value(x)
zresult=value(z)
else
disp('求解錯誤')
end
toc;
xresult =
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
zresult =
9
時間已過 1.787330 秒。
11.案例十一
clear;clear all;clc;close all;
tic;
%案例十一指派任務
%數據輸入
c=load('2.txt')
%決策變量
x=binvar(5,5,'full');
%目標函數
z=sum(sum(c.*x));
%約束條件
C=[];
% C = [C;sum(x,1)==1]; % 1 橫向相加
% C = [C;sum(x,2)==1]; % 2 縱向相加
for j=1:5
C=[C;sum(x(:,j))==1];
end
for i=1:5
C=[C;sum(x(i,:))==1];
end
%求解
ops=sdpsettings('solver','cplex');
result=solvesdp(C,z,ops);
%結果表示
if result.problem==0
xresult=value(x)
zresult=value(z)
else
disp('求解錯誤')
end
toc;
xresult =
0 1 0 0 0
0 0 1 0 0
0 0 0 0 1
0 0 0 1 0
1 0 0 0 0
zresult =
32
時間已過 1.429932 秒。
下次繼續學習魯棒算法