求C代码:遗传算法求函数最大值f(x)=xsin(10Pix)+1.0

来源:百度知道 编辑:UC知道 时间:2024/05/28 10:15:07
小数点精确6位,函数区间为[-1,2]
定有高分相送!
谢谢各位了!

提示:用22位二进制数,省去编码!x=-1+t*(3/[pow(2,22)-1])其中t为0到[pow(2,22)-1]之间的随机数。

%输出参数:x:求的最优解
% endpop:最终的种群
% bpop:最优种群的一个搜索轨迹
% 输出参数:
% bounds:代表变量的上下界的矩阵
% eevalFN:适应度函数
% startPop:初始群体
% termFN:终止函数的名字
% termOps: 终止函数的参数
% selectFN:选择函数的名称
% selectOpts:选择参数。
function [x,endPop,bPop,traceInfo] = ga(bounds,eevalFN,eevalOps,startPop,opts,...
termFN,termOps,selectFN,selectOps,xOverFNs,xOverOps,mutFNs,mutOps)

n=nargin;
if n<2 | n==6 | n==10 | n==12
disp('Insufficient arguements')
end
if n<3 %Default eevalation opts.
eevalOps=[];
end
if n<5
opts = [1e-6 1 0];
end
if isempty(opts)
opts = [1e-6 1 0];
end

if any(eevalFN<48) %Not using a .m file
if opts(2)==1 %Float ga
e1str=['x=c1; c1(xZomeLength)=', eevalFN ';'];
e2str=['x=c2; c2(xZomeLength)=', eevalFN ';'];