这里主要对于matlab优化工具箱进行整理
官方文档有例子,用到哪个根据目录检索,再官方文档查阅
linprog求解线性规划:
求解一个线性不等式、线性等式和边界的简单线性规划。
调用格式为:
[x,fval] =linprog(f,A,b,Aeq,beq,lb,ub)
这里fval 返回目标函数的值,lb 和ub分别是变量x 的下界和上界. 在 Matlab 指令窗运行 help linprog 可以看到所有的函数调用形式.官方文档
intlinprog求解求解整数或混合整数规划。
例题1:
matlab代码
clear
clc
f=-1*[1,1.65,2.4,1,1.65,2.4,0,0,0,0,0,0,0,0,0];
intcon=[1:15];
A=[5,10,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,7,9,12,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,6,8,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,4,0,11,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,7,0,0];
b=[6000;10000;4000;7000;4000];
Aeq=[1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0;
0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0;
0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1];
beq=[0,0,0];
lb=zeros(15,1);
ub=[Inf;Inf;0;Inf;Inf;Inf;Inf;Inf;0;Inf;0;Inf;Inf;0;0];
[x,fval,exitflag,output]= intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
-1*fval-300-321-250-783-200
结果:
LP: Optimal objective value is -3032.696552.
Cut Generation: Applied 1 strong CG cut.
Lower bound is -3032.600000.
Relative gap is 0.00%.
Optimal solution found.
Intlinprog stopped at the root node because the
objective value is within a gap tolerance of the optimal value,
options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are
integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
x =
1.0e+03 *
1.2000
0
0
0.2300
0.5000
0.3240
0
0.5000
0
0.8590
0
0.3240
0.5710
0
0
ans =
1.1786e+03
所以结果为
利润:1.1786e+03即1178.6元
例题2
matlab代码:
clear
clc
f=[log(0.9),log(0.92),log(0.8),log(0.84),log(0.85),log(0.88),log(0.75),log(0.8)];
intcon=(1:8);
A=[450/2+200+450/4,450/3+200+450/4,480/2+200+480/4,480/3+200+480/4,540/2+200+540/4,540/3+200+540/4,600/2+200+600/4,600/3+200+600/4;
1,0,1,0,1,0,1,0;
0,1,0,1,0,1,0,1];
b=[48000;48;32];
lb=zeros(8,1);
ub=[Inf,Inf,Inf,Inf,Inf,Inf,Inf,Inf];
Aeq=[];
beq=[];
[x,fval,exitflag,output] = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
1-exp(fval)
结果
LP: Optimal objective value is -20.624761.
Cut Generation: Applied 1 strong CG cut, 3 Gomory cuts,
and 1 mir cut.
Lower bound is -20.560232.
Heuristics: Found 1 solution using rounding.
Upper bound is -20.309430.
Relative gap is 1.18%.
Branch and Bound:
nodes total num int integer relative
explored time (s) solution fval gap (%)
12 0.02 2 -2.053257e+01 1.272330e-01
28 0.02 3 -2.054832e+01 4.449189e-02
40 0.02 3 -2.054832e+01 0.000000e+00
Optimal solution found.
Intlinprog stopped because the objective value is within a gap tolerance of the
optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon
variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the
default value).
x =
0
0
1
1
0
0
46
31
ans =
1.0000
所以结果为
摧毁敌方军事目标可能性基本趋近于1
例题3
matlab代码:
clear
clc
f=-1*[40,90];
intcon=(1:2);
A=[9,7;-7,-20];
b=[56;-70];
Aeq=[];
beq=[];
lb=zeros(2,1);
ub=[Inf;Inf];
[x,fval,exitflag,output]= intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
-1*fval
结果:
LP: Optimal objective value is -720.000000.
Optimal solution found.
Intlinprog stopped at the root node because the
objective value is within a gap tolerance of the optimal value,
options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are
integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
x =
0
8
ans =
720
例题4
matlab代码:
clear
clc
f=-1*[3,-2,5];
intcon=(1:3);
A=[1,2,-1;
1,4,1;
1,1,0;
0,4,1];
b=[2;4;3;6];
Aeq=[];
beq=[];
lb=zeros(3,1);
ub=[Inf;Inf;Inf];
[x,fval,exitflag,output]= intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
-1*fval
结果
LP: Optimal objective value is -20.000000.
Optimal solution found.
Intlinprog stopped at the root node because the
objective value is within a gap tolerance of the optimal value,
options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are
integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
x =
0
0
4
ans =
20
例题5
matlab代码:
clear
clc
c=input('输入各井的钻探费用:');
f=c;
intcon=(1:10);
A=[0,0,0,0,1,1,1,1,0,0];
b=[2];
Aeq=[1,1,1,1,1,1,1,1,1,1;
1/2,0,0,0,0,0,1/2,0,1,0;
0,0,1,0,1,0,0,0,0,0;
0,0,0,1,1,0,0,0,0,0];
beq=[5;1;1;1];
lb=zeros(10,1);
ub=ones(10,1);
[x,fval,exitflag,output]= intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
fval
结果
输入各井的钻探费用:ones(1,10)
LP: Optimal objective value is 5.000000.
Optimal solution found.
Intlinprog stopped at the root node because the
objective value is within a gap tolerance of the optimal value,
options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are
integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
x =
0
1.0000
1.0000
1.0000
0
0
0
0
1.0000
1.0000
fval =
5.0000
fminbnd求一元函数在定区间上最小值(有导数),传统优化算法
fminbnd是一个函数文件。该算法基于黄金分割搜索和抛物线插值。可以求解一元的可导函数的最小值
限制
- 要最小化的函数必须是连续的。
- fminbnd可能只提供本地解决方案。
- 当解决方案位于间隔的边界上时, fminbnd可以呈现缓慢的收敛性。
fminsearch使用无导数法(Nelder-Mead直接搜索法)计算无约束的多元函数的最小值(无导数)传统优化算法
fminsearch 使用 Lagarias 等的单纯形搜索方法。[1]这是一种直接搜索方法,不像在 fminunc 中那样使用数值或解析梯度。
提示
fminsearch 仅对实数求最小值,即向量或数组 x 只能由实数组成,并且 f(x) 必须只返回实数。当 x 具有复数值时,将 x 拆分为实部和虚部。
使用 fminsearch 解决不可微分的问题或者具有不连续性的问题,尤其是在解附近没有出现不连续性的情况下。
fminunc无约束的多元函数的最小值传统优化算法
ga使用遗传算法(GA)求解无约束优化问题的最小值
例题1
clc
clear
fun = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
A = [];
b = [];
Aeq = [];
beq = [];
lb = -30*ones(2,1);
ub = 30*ones(2,1);
nonlcon = [];
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf,'MaxGenerations',800);
[x,fval,exitflag,output] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
结果
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
0.9893 0.9788
fval =
1.1468e-04
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'boundconstraints'
rngstate: [1×1 struct]
generations: 473
funccount: 23700
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance.'
maxconstraint: 0
options的修改可以参考
例题2
function f=Fun(x)
f1=0;
f2=1;
for i=1:30
f1=f1 +1/4000*x(i)^2;
f2=f2*cos(x(i)/sqrt(i));
end
f=f1-f2+1;
end
%主函数
clc
clear
A = [];
b = [];
Aeq = [];
beq = [];
lb = -600*ones(30,1);
ub = 600*ones(30,1);
nonlcon = [];
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf);
[x,fval,exitflag,output] = ga(@Fun,30,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
结果
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
1 至 7 列
-0.0000 0.0016 0.0002 0.0029 -0.0020 0.0071 -0.0002
8 至 14 列
0.0030 0.0003 0.0003 -0.0001 0.0047 -0.0002 0.0064
15 至 21 列
-0.0001 -0.0016 0.0015 0.0010 0.0094 0.0009 0.0045
22 至 28 列
0.0014 0.0060 0.0060 -0.0001 0.0012 0.0007 -0.0002
29 至 30 列
0.0017 0.0028
fval =
1.4155e-05
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'boundconstraints'
rngstate: [1×1 struct]
generations: 399
funccount: 80000
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance.'
maxconstraint: 0
例题3
function f = Fun(x)
global m;
f = 0;
for i = 1:m
f = f -x(i)*sin(sqrt(abs(x(i))));
end
%主函数
clc
clear
global m;
m=input('函数x为几元:');
A = [];
b = [];
Aeq = [];
beq = [];
lb = -500*ones(m,1);
ub = 500*ones(m,1);
nonlcon = [];
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf,'MaxGenerations',800);
[x,fval,exitflag,output] = ga(@Fun,m,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
- global是定义全局变量的函数,在其他函数中用时,也要用global再次指明调用的是全局变量
结果
函数x为几元:10
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
1 至 7 列
420.9627 420.9846 420.9301 -302.4783 420.9803 420.9537 420.9884
8 至 10 列
-302.5260 420.9911 420.9851
fval =
-3.9530e+03
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'boundconstraints'
rngstate: [1×1 struct]
generations: 199
funccount: 40000
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance.'
maxconstraint: 0
particleswarm使用粒子群优化算法(PSO)求解无约束优化问题的最小值
patternsearch使用模式搜索法(PS)求解无约束优化问题的最小值
未完待续················