建模中的一些常见模型(2)

2019-05-17 19:05

编码和种群生成 :

function [pop] = initializega(num,bounds,evalFN,evalOps,options) % pop - the initial, evaluated, random population

% num - the size of the population, i.e. the number to create % bounds - the number of permutations in an individual (e.g., number % of cities in a tsp

% evalFN - the evaluation fn, usually the name of the .m file for evaluation % evalOps- any options to be passed to the eval function defaults [ ] % options- options to the initialize function, ie. [eps, float/binary, prec] % where eps is the epsilon value and the second option is 1 for % orderOps, prec is the precision of the variables defaults [1e-6 1] 交叉

function [c1,c2] = arithXover(p1,p2,bounds,Ops)

% Arith crossover takes two parents P1,P2 and performs an interpolation % along the line formed by the two parents. %

% function [c1,c2] = arithXover(p1,p2,bounds,Ops)

% p1 - the first parent ( [solution string function value] ) % p2 - the second parent ( [solution string function value] ) % bounds - the bounds matrix for the solution space

% Ops - Options matrix for arith crossover [gen #ArithXovers] 选择

normGeomSelect:NormGeomSelect is a ranking selection function based on the normalized geometric distribution. (基于正态分布的序列选择函数) 变异

function[newPop] = normGeomSelect(oldPop,options)

% NormGeomSelect is a ranking selection function based on the normalized % geometric distribution. %

% function[newPop] = normGeomSelect(oldPop,options) % newPop - the new population selected from the oldPop % oldPop - the current population % options - options to normGeomSelect [gen probability_of_selecting_best]

一些辅助函数:

f2b :Return the binary representation of the float number fval(将浮点数转化为二进制数)

b2f:Return the float number corresponing to the binary representation of bval. (将二进制

数转化为 浮点数)

nonUnifMutation: Non uniform mutation changes one of the parameters of the parent based on a non-uniform probability distribution. This Gaussian distribution starts wide, and narrows to a point distribution as the current generation approaches the maximum generation. (基于非均一概率分布进行非均一变异)

maxGenTerm:Returns 1, i.e. terminates the GA when the maximal_generation is reached. (当迭代次数大于最大迭代次数时,终止遗传算法,返回 为1,否则返回为0。)

roulette:roulette is the traditional selection function with the probability of surviving equal to the fittness of i / sum of the fittness of all individuals

应用举例

1. 计算下列函数的最大值。

f(x)=x+10*sin(5x)+7cos(4x) , x∈[0,9] 方式1 >>gademo 方式2

step 1 编写目标函数gademo1eval1.m

function [sol, val] = gaDemo1Eval(sol,options) x=sol(1);

val = x + 10*sin(5*x)+7*cos(4*x); step 2 生成初始种群,大小为10

initPop=initializega(10,[0, 9],'gademo1eval1',[],[1e-6,1]); step 3 25次遗传迭代

[x,endPop,bpop,trace] = ga([0 9],'gademo1eval1',[],initPop,... [1e-6 1 1],'maxGenTerm',25,... 'normGeomSelect',[0.08],... ['arithXover'],[2],... 'nonUnifMutation',[2, 25 ,3]) % Output Arguments:

% x - the best solution found during the course of the run % endPop - the final population

% bPop - a trace of the best population (解的变化)

% traceInfo - a matrix of best and means of the ga for each generation (种群平均值的变化) %

% Input Arguments:

% bounds - a matrix of upper and lower bounds on the variables % evalFN - the name of the evaluation .m function

% evalOps - options to pass to the evaluation function ([NULL]) % startPop - a matrix of solutions that can be initialized % from initialize.m

% opts - [epsilon, prob_ops ,display] change required to consider two solutions different, prob_ops 0 if you want to apply the

% genetic operators probabilisticly to each solution, 1 if you are supplying a deterministic number of operator applications and display is 1 to output progress 0 for quiet. ([1e-6 1 0])

% termFN - name of the .m termination function

% termOps - options string to be passed to the termination function ([100]). % selectFN - name of the .m selection function (['normGeomSelect']) % selectOpts - options string to be passed to select after % select(pop,#,opts) ([0.08])

% xOverFNS - a string containing blank seperated names of Xover.m files (['arithXover heuristicXover simpleXover'])

% xOverOps - A matrix of options to pass to Xover.m files with the first column being the number of that xOver to perform similiarly for mutation ([2 0;2 3;2 0])

% mutFNs - a string containing blank seperated names of mutation.m files (['boundaryMutation multiNonUnifMutation ...

% nonUnifMutation unifMutation'])

% mutOps - A matrix of options to pass to Xover.m files with the first column being the number of that xOver to perform similiarly for mutation ([4 0 0;6 100 3;4 100 3;4 0 0])

2. 求sin(x) 在0到3.14之间的最大值.

function [sol, val] = sin1(sol,options) x=sol(1); val =sin(x);

initPop=initializega(10,[0, 3.14],'sin1',[],[1e-6,1]);

[x, endPop,bpop,trace] = ga([0 3.14],'sin1',[],initPop,... [1e-6 1 1],'maxGenTerm',25,... 'normGeomSelect',[0.08],... ['arithXover'],[2],... 'nonUnifMutation',[2, 25 ,3])

3 求解非线性规划问题

max f(x) s.t. gi(x)<=0,i=1,...,m hi(x)=0,i=m+1,...,n x∈Ω ?不可行的后代? 拒绝策略 修复策略 惩罚策略

e.g. min f(x)=(x1-2)2+(x2-1)2

s.t. g1(x)=x1-2x2+1>=0 g2(x)=x12/4-x22+1>=0 分析:取加法形式的适值函数: val(x)=f(x)+p(x)

惩罚函数 p(x)由两部分组成,可变乘法因子和 违反约束乘法,其表达式如下:

改进遗传算子策略

其中ri是约束i的可变惩罚系数。 步骤如下:

function [sol,eval]=f552(sol,options) x1=sol(1); x2=sol(2);

r1=0.1; r2=0.8; %约束条件 g1=x1-2*x2+1; g2=x1.^2/4-x2.^2+1; %加惩罚项的适值

if (g1>=0)&(g2>=0) eval=(x1-2).^2+(x2-1).^2; else eval=(x1-2).^2+(x2-1).^2+r1*g1+r2*g2; end eval=-eval;

%%%%%%%%%%%%%%%%%%%%%%%%%%% %维数n=2 %设置参数边界

bounds = ones(2,1)*[-1 1];%??????

%遗传算法优化 [p,endPop,bestSols,trace]=ga(bounds,'f552'); p %性能跟踪

plot(trace(:,1),trace(:,3),'b-') hold on

plot(trace(:,1),trace(:,2),'r-') xlabel('Generation'); ylabel('Fittness');

legend('解的变化','种群平均值的变化');


建模中的一些常见模型(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:第十六章二次根式全章导学案(新人教版)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: