一、基础知识
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;
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)
disp('求解错误')
Iteration: 1 Dual objective = -2.500000
xresult =
0.5000
0.5000
zresult =
2.5000
时间已过 1.452390 秒。
2.案例二
clear;clear all;clc;close all;
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)
disp('求解出错')
Iteration: 1 Dual objective = 4.000000
resultx =
2
0
3
resultz =
时间已过 1.462861 秒。
3.案例三
求解题目与2相同,但使用默认求解器,可以不用人为选择cplex。
clear;clear all;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)
disp('求解出错')
Iteration: 1 Dual objective = 4.000000
xresult =
2 0 3
zresult =
时间已过 1.499445 秒。
4.案例四
求解混合整数线性规划
clear;clear all;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];
for j=1:n
s=sum(x(:,j))-x(j,j);
C=[C;s==1];
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];
%目标函数
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)
disp('求解出错')
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 =
时间已过 1.922839 秒。
5.案例五
YALMIP默认使用QUADPROG来求解问题
这里的公式约束条件没表示完,还有部分
clear;clear all;clc;close all;
x=sdpvar(10,1);
%添加约束
C=[];
for i=1:10;
C=[C;sum(x(i))<=10;x(1)==0;0.5<=x(2)<=1.5];
for i=1:7
C=[C;x(i)+x(i+1)<=x(i+2)+x(i+3)];
%目标函数
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)
disp('求解错误')
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;
% %案例六分类器
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)
disp('求解错误')
x = sdpvar(2,1);
P1 = [-5<=x<=5,value(a)'*x+value(b)>=0];%这里该怎么改
P2 = [-5<=x<= 5,value(a)'*x+value(b)<=0];
plot(P1);hold on
plot(P2);
plot(greens(1,:),greens(2,:), 'g*')
plot(blues(1,:),blues(2,:), 'b*')
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;
%案例七一般线性规划
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)
disp('求解错误')
Iteration: 1 Dual objective = 50.000000
xresult =
0
9.6429
1.0714
zresult =
56.7857
时间已过 1.314671 秒。
8.案例八
clear;clear all;clc;close all;
%案例八运输问题
%基础数据
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)];
for j=1:5
s=sum(x(:,j));
C=[C;s>=b(j)];
ops=sdpsettings('solver','cplex');
result=solvesdp(C,Z,ops);
if result.problem==0
xresult=value(x)
zresult=value(Z)
disp('求解错误')
xresult =
2 20 0 18 0
0 0 10 0 0
0 0 5 0 25
8 0 0 0 0
zresult =
时间已过 1.383890 秒。
9.案例九
clear;clear all;clc;close all;
%案例九背包问题
%基本数据
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)];
ops=sdpsettings('solver','cplex');
result=solvesdp(C,z,ops);
if result.problem==0
xresult=value(x)
zresult=-value(z)
disp('求解错误')
xresult =
1
0
3
1
0
4
3
0
0
0
zresult =
时间已过 1.372422 秒。
10.案例十
求解最短路程问题
clear;clear all;clc;close all;
%案例十最短路程问题
% 利用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)];
ops=sdpsettings('solver','cplex');
result=solvesdp(C,z,ops);
%结果显示
if result.problem==0
xresult=value(x)
zresult=value(z)
disp('求解错误')
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 =
时间已过 1.787330 秒。
11.案例十一
clear;clear all;clc;close all;
%案例十一指派任务
%数据输入
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];
for i=1:5
C=[C;sum(x(i,:))==1];
ops=sdpsettings('solver','cplex');
result=solvesdp(C,z,ops);
%结果表示
if result.problem==0
xresult=value(x)
zresult=value(z)
disp('求解错误')
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 =
时间已过 1.429932 秒。
下次继续学习鲁棒算法
一基础知识 yalmip决策变量数据类型binvar(0-1变量),intvar(整数变量),sdpvar(实数、连续变量)。https://blog.csdn.net/qq_16309049/article/details/91549610定义变量矩阵想x(m行、n列)。例如:x=intvar(10,1);x=sdpvar(10,1);x=binvar(5,5,'full');...
由于工程项目需要,在求解新能源消纳问题时,需要进行电力系统优化调度,不可避免的需要优化求解电力系统机组组合问题。其本质是一个优化问题。查阅相关资料,目前做电力系统优化最主流的方法就是:Matlab + Yalmip + Cplex
Yalmip
Yalmip是一个Matlab工具包,在Yalmip官网下载,解压至Matlab Toolbox 工具箱中,然后设置好Pat...
4.eye(m,n) 功能:产生m×n的单位矩阵。
5.yalmip定义矩阵-sdpvar定义决策变量
P = sdpvar(3,3) + diag(sdpvar(3,1));
X = [P P;P eye(length(P))] + 2*trace(P);
Y = X + sum(sum(P*rand(length(P)))) + P(end,end)+hankel(X(:,1))
这里将P矩
最近建立了一个网络流模型,是一个混合整数线性规划问题(模型中既有连续变量,又有整型变量)。当要求解此模型的时候,发现matlab优化工具箱竟没有自带的可以求解这类问题的算法(只有bintprog求解器,但是只能求解不含连续变量的二值线性规划问题)。于是在网上找了一些解决问题的途径,下面说说我试过的几种可能的解决方案,包括cplex、GLPK、lpsolve 和 yalmip。
cplex
>> x = sdpvar(m, n [, option]):创建m*n的连续型决策变量矩阵,option是对矩阵的一些参数指定。
相应的,如果要创建整型或二值型决策变量,matlab语句分别为:
>> x = intvar(m, n, [option])
>> x = binvar(m, n, [option])
2. 添加约束:
>> F = set(constraint [,
目录YALMIP简介怎么在Matlab中加YALMIP?1. 创建决策变量:2. 添加约束:3. 参数配置4. 求解怎么在MatlabR2019a中加YALMIP?1.下载工具包2.放置工具包3.添加路径4.测试安装结果怎么在MatlabR2019a中加SDPT3?
YALMIP简介
怎么在Matlab中加YALMIP?
yalmip是一位“集大成者”,它不仅自己包含基本的线性规划求解算法,比如linprog(线性规划)、bintprog(二值线性规划)、bnb(分支界定算法)等,他还提供了对cpl
最近有文章复现需要用到Cplex,所以捣鼓了一下,看其他博客感觉不解渴,所以索性在这里记录一些key point
1)下载与安装:Cplex的下载可以在官方网站下载,学生是免费下载使用的,非学生可以在网上找一找,有可用的网盘下载方案;
安装过程比较简单,个人是把应用程序安装好之后再在matlab中添加的路径,参考链接:https://blog.csdn.net/robert_chen1988/...
1.yalmip简介
yalmip是由Lofberg开发的一种免费的优化求解工具,其最大特色在于集成许多外部的最优化求解器(包括cplex),形成一种统一的建模求解语言,提供了Matlab的调用API,减少学习者学习成本。简而言之,它可以让你像书写数学模型那样输入你的模型。
2.环境搭建
2.1 yalmip安装
yalmip下载页面,点击下载即可。
解压后,将其复制到toolbox文件...