|
在实际的工作和生活过程中,优化问题无处不在,比如资源如何分配效益最高,拟合问题,最小最大值问题等等。优化问题一般分为局部最优和全局最优,局部最优,就是在函数值空间的一个有限区域内寻找最小值;而全局最优,是在函数值空间整个区域寻找最小值问题。
(GlobalSearch) 全局搜索和(MultiStart)多起点方法产生若干起始点,然后它们用局部求解器去找到起始点吸引盆处的最优点。
ga 遗传算法用一组起始点(称为种群),通过迭代从种群中产生更好的点,只要初始种群覆盖几个盆,GA就能检查几个盆。
(simulannealbnd)模拟退火完成一个随机搜索,通常,模拟退火算法接受一个点,只要这个点比前面那个好,它也偶而接受一个比较糟的点,目的是转向不同的盆。
(patternsearch )模式搜索算法在接受一个点之前要看看其附近的一组点。假如附近的某些点属于不同的盆,模式搜索算法本质上时同时搜索若干个盆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | %%目标函数 f = @(x) x.*sin(x) + x.*cos(2.*x); %% 的取值范围 lb = 0; ub = 10; %% 寻找最小值和绘图 x0 = [0 1 3 6 8 10]; hf = figure; for i=1:6 x(i) = fmincon(f,x0(i),[],[],[],[],lb,ub,[],... optimset('Algorithm','SQP','Disp','none')); subplot(2,3,i) ezplot(f,[lb ub]); hold on plot(x0(i),f(x0(i)),'k+') plot(x(i),f(x(i)),'ro') hold off title(['Starting at ',num2str(x0(i))]) if i == 1 || i == 4 ylabel('x sin(x) + x cos(2 x)') end end |
1 2 3 4 5 6 7 8 | x2 = fminbnd(f,lb,ub); figure ezplot(f,[lb ub]); hold on plot(x2,f(x2),'ro') hold off ylabel('x sin(x) + x cos(2 x)') title({'Solution using fminbnd.','Required no starting point!'}) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | % Leason Learned: Use the appropriate solver for your problem type! %% But what if |fmincon| was the only choice? % Use globalSearch or MultiStart problem = createOptimProblem('fmincon','objective',f,'x0',x0(1),'lb',lb,... 'ub',ub,'options',optimset('Algorithm','SQP','Disp','none')); gs = GlobalSearch; xgs = run(gs,problem); figure ezplot(f,[lb ub]); hold on plot(xgs,f(xgs),'ro') hold off ylabel('x sin(x) + x cos(2 x)') title('Solution using globalSearch.') |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | close all, clear all, clc %% Pharmacokinetic Data t = [ 3.92, 7.93, 11.89, 23.90, 47.87, 71.91, 93.85, 117.84 ] %#ok<*NOPTS> c = [0.163, 0.679, 0.679, 0.388, 0.183, 0.125, 0.086, 0.0624 ] plot(t,c,'o'), xlabel('t'), ylabel('c') %% 3 Compartment Model model = @(b,t) b(1)*exp(-b(4)*t) + b(2)*exp(-b(5)*t) + b(3)*exp(-b(6)*t) %% Define Optimization Problem problem = createOptimProblem('lsqcurvefit', ... 'objective', model, ... 'xdata', t, 'ydata', c, ... 'x0',ones(1,6),... 'lb', [-10 -10 -10 0 0 0 ],... 'ub', [ 10 10 10 0.5 0.5 0.5], ... 'options',optimset('OutputFcn',... @curvefittingPlotIterates)) %% solve b = lsqcurvefit(problem) |
1 2 3 4 5 6 7 8 9 10 | %% Multistart ms = MultiStart [b,fval,exitflag,output,solutions] = run(ms, problem, 50) %#ok<*NASGU,*ASGLU> %% curvefittingPlotIterates(solutions) %% problem.options.OutputFcn = {}; tic, [b,fval,exitflag,output,solutions] = run(ms, problem, 100), toc %计算算法的时间 |
1 2 3 4 5 | %% Parallel Version matlabpool open 2 %开启两个matlab并行计算 ms.UseParallel = 'always' %开启并行计算 tic, [bp,fvalp,exitflagp,outputp,solutionsp] = run(ms, problem, 100); toc matlabpool close |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | %% Objective Function % We wish find the minimum of the |peaks| function clear all, close all, clc peaks %% Nonlinear Constraint Function % Subject to a nonlinear constraint defined by a circular region of radius % three around the origin type circularConstraint %% Define Optimization Problem problem = createOptimProblem('fmincon',... 'objective',@(x) peaks(x(1),x(2)), ... 'nonlcon',@circularConstraint,... 'x0',[-1 -1],... 'lb',[-3 -3],... 'ub',[3 3],... 'options',optimset('OutputFcn',... @peaksPlotIterates)) %% Run the solver |fmincon| from the inital point % We can see the solution is not the global minimum [x,f] = fmincon(problem) |
1 2 3 4 5 6 7 | %% Use |MultiStart| to Find the Global Minimum % Define the multistart solver close all ms = MultiStart %这里可以换成GlobalSearch %% Run |Multistart| % Well use 5 starting points [x,f,exitflag,output,solutions] = run(ms, problem, 5) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | %% Objective Function % We wish find the minimum of the |peaks| function clear all, close all, clc peaks %% Nonlinear Constraint Function % Subject to a nonlinear constraint defined by a circular region of radius % three around the origin type circularConstraint %% Define Optimization Problem problem = createOptimProblem('fmincon',... 'objective',@(x) peaks(x(1),x(2)), ... 'nonlcon',@circularConstraint,... 'x0',[-1 -1],... 'lb',[-3 -3],... 'ub',[3 3],... 'options',optimset('OutputFcn',... @peaksPlotIterates)) %% Run the solver |fmincon| from the inital point % We can see the solution is not the global minimum [x,f] = fmincon(problem) %% Use Simmulated Annealing to Find the Global Minimum % Solve the problem using simmulated annealing. Note that simmulated % annealing does not support nonlinear so we need to account for this in % the objective function. problem.solver = 'simulannealbnd'; problem.objective = @(x) peaks(x(1),x(2)) + (x(1)^2 + x(2)^2 - 9); problem.options = saoptimset('OutputFcn',@peaksPlotIterates,... 'Display','iter',... 'InitialTemperature',10,... 'MaxIter',300) [x,f] = simulannealbnd(problem) f = peaks(x(1),x(2)) |
1 2 3 4 5 6 7 8 | %% Use Pattern Search to Find the Global Minimum % Solve the problem using pattern search. problem.solver = 'patternsearch'; problem.options = psoptimset('OutputFcn',@peaksPlotIterates,... 'Display','iter',... 'SearchMethod',{@searchlhs}) [x,f] = patternsearch(problem) |
转载请注明:笑凌子 » matlab全局优化与局部优化
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-10 20:40
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社