⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function_opt_demo.m

📁 使用matlab应用遗传算法进行函数优化
💻 M
字号:

bound = [-100 100];
plength = 2;
pl = ones(plength,1);
bounds = bound(pl,:); %Define bounds of variables
initSize = 100;  % The size of population;
numQbit = 20; % number of bits for coding one variable
bits=ones(1,plength).*numQbit;
xZomeLength  = sum(bits)+1;
startPop = zeros(initSize,xZomeLength);

probXOvers = 0.8;
probMuts = 0.05;

%Initializing the first generation of individuals
for i = 1:initSize
    for j = 1:xZomeLength-1
        if rand >= 0.5
            startPop(i,j) = 1;
        end;    %end of if
    end;    %end of for j = ..
end;    %end of for i = ..

% startPop(:,[1:(xZomeLength-1)]) = round(rand(initSize,(xZomeLength-1)));

% evaluating the fitness of individuals
for i = 1:initSize
    %binary representation to float representation transformation
    scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1); %The range of the variables
    cs=[0 cumsum(bits)];
    for j=1:plength
        a=startPop(i,((cs(j)+1):cs(j+1)));
        fval(j)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(j)+bounds(j,1);
    end
    %evaluating the fitness of ith individual
    startPop(i,xZomeLength) = 25 - (fval(1).^2+fval(2).^2).^0.25.*(sin(50.*(fval(1).^2+fval(2).^2).^0.1).^2 + 1);
    %     for l = 1:plength-1
    %         f(l) = 100.*(fval(l+1)-fval(l).^2).^2+(1-fval(l)).^2;  %calculation of Rosenbrock function
    %     end;
    %     startPop(i,xZomeLength) = 1./(1+sum(f));		%evaluate the fittness of the individual;
end %end of fitness evaluation

endPop = zeros(initSize,xZomeLength); %A secondary population matrix
c1 = zeros(1,xZomeLength); 	%An individual
c2 = zeros(1,xZomeLength); 	%An individual
done = 0;
gen = 1;

plot(0,0);
axis([0 500 20 25]);
hold on;
drawnow;

while(~done)
    %Elitist Model
    [bvar,bindx] = max(startPop(:,xZomeLength)); %Best of current pop
    best =  startPop(bindx,:);

    traceInfo(gen,1)=gen; 		          %current generation
    traceInfo(gen,2)=startPop(bindx,xZomeLength);       %Best fittness
    traceInfo(gen,3)=mean(startPop(:,xZomeLength));     %Avg fittness
    traceInfo(gen,4)=std(startPop(:,xZomeLength));      %Standard Deviation
    traceInfo(gen,5)=size(unique(startPop(:,[1:(xZomeLength-1)]),'rows'),1)/initSize; %diversity

    plot(gen,traceInfo(gen,2),'r*');
    drawnow;

    %Selection operation
    %Generate the relative probabilites of selection
    totalFit = sum(startPop(:,xZomeLength));
    prob=startPop(:,xZomeLength) / totalFit;
    prob=cumsum(prob);

    rNums=sort(rand(initSize,1)); 		%Generate random numbers

    %Select individuals from the startPop to the new
    fitIn=1;newIn=1;
    while newIn<=initSize
        if(rNums(newIn)<prob(fitIn))
            endPop(newIn,:) = startPop(fitIn,:);
            newIn = newIn+1;
        else
            fitIn = fitIn + 1;
        end
    end

    %Crossover operation
    for i = 1:(round(probXOvers.*initSize))
        ac = 0;
        bc = 0;
        while ac == bc
            ac = round(rand.*initSize + 0.5); 	%Pick a parent
            bc = round(rand.*initSize + 0.5); 	%Pick another parent
        end % while
        p1 = endPop(ac,:);
        p2 = endPop(bc,:);

        % Pick a cut point randomly from 1-number of vars
        cPoint = round(rand * (xZomeLength-3)) + 1;

        c1 = [p1(1:cPoint) p2(cPoint+1:xZomeLength)]; % Create the children
        c2 = [p2(1:cPoint) p1(cPoint+1:xZomeLength)];

        if c1(1:xZomeLength-1)==p1(1:xZomeLength-1) %Make sure we created a new
            c1(xZomeLength)=p1(xZomeLength); %solution before evaluating
        elseif c1(1:xZomeLength-1)==p2(1:xZomeLength-1)
            c1(xZomeLength)=p2(xZomeLength);
        else
            %binary representation to float representation transformation
            scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1); %The range of the variables
            cs=[0 cumsum(bits)];
            for j=1:plength
                a=c1((cs(j)+1):cs(j+1));
                fval(j)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(j)+bounds(j,1);
            end
            %evaluating the fitness of ith individual
            c1(xZomeLength) = 25 - (fval(1).^2+fval(2).^2).^0.25.*(sin(50.*(fval(1).^2+fval(2).^2).^0.1).^2 + 1);
            %     for l = 1:plength-1
            %         f(l) = 100.*(fval(l+1)-fval(l).^2).^2+(1-fval(l)).^2;  %calculation of Rosenbrock function
            %     end;
            %     c1(xZomeLength) = 1./(1+sum(f));		%evaluate the fittness of the individual;
        end

        if c2(1:xZomeLength-1)==p1(1:xZomeLength-1)
            c2(xZomeLength)=p1(xZomeLength);
        elseif c2(1:xZomeLength-1)==p2(1:xZomeLength-1)
            c2(xZomeLength)=p2(xZomeLength);
        else
            %binary representation to float representation transformation
            scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1); %The range of the variables
            cs=[0 cumsum(bits)];
            for j=1:plength
                a=c2((cs(j)+1):cs(j+1));
                fval(j)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(j)+bounds(j,1);
            end
            %evaluating the fitness of ith individual
            c2(xZomeLength) = 25 - (fval(1).^2+fval(2).^2).^0.25.*(sin(50.*(fval(1).^2+fval(2).^2).^0.1).^2 + 1);
            %     for l = 1:plength-1
            %         f(l) = 100.*(fval(l+1)-fval(l).^2).^2+(1-fval(l)).^2;  %calculation of Rosenbrock function
            %     end;
            %     c2(xZomeLength) = 1./(1+sum(f));		%evaluate the fittness of the individual;

        end

        endPop(ac,:)=c1;
        endPop(bc,:)=c2;

    end %end of crossover

    %Mutation operation
    for i = 1:round(probMuts.*initSize)
        am = round(rand.*initSize + 0.5); 	%Pick a parent
        cm = endPop(am,:);
        mPoint = round(rand * (xZomeLength-2)) + 1;
        cm(mPoint) = 1-cm(mPoint);
        %binary representation to float representation transformation
        scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1); %The range of the variables
        cs=[0 cumsum(bits)];
        for j=1:plength
            a=cm((cs(j)+1):cs(j+1));
            fval(j)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(j)+bounds(j,1);
        end
        %evaluating the fitness of ith individual
        cm(xZomeLength) = 25 - (fval(1).^2+fval(2).^2).^0.25.*(sin(50.*(fval(1).^2+fval(2).^2).^0.1).^2 + 1);
        %     for l = 1:plength-1
        %         f(l) = 100.*(fval(l+1)-fval(l).^2).^2+(1-fval(l)).^2;  %calculation of Rosenbrock function
        %     end;
        %     cm(xZomeLength) = 1./(1+sum(f));		%evaluate the fittness of the individual;

        endPop(am,:)=cm;

    end   %end of mutation

    %   for i = 1:initSize
    %       for j = 1:xZomeLength-1
    %           if rand < probMuts
    %               endPop(i,j) = 1-endPop(i,j);
    %           end
    %       end
    %   end

    gen = gen+1;
    if gen >= 500
        done = 1;
    end
    startPop=endPop; 			%Swap the populations

    [bval,windx] = min(startPop(:,xZomeLength)); %find the worst solution
    startPop(windx,:) = best; 		%replace it with the best

end %end while

[bvar,bindx] = max(startPop(:,xZomeLength)); %Best of current pop
traceInfo(gen,1)=gen; 		          %current generation
traceInfo(gen,2)=startPop(bindx,xZomeLength);       %Best fittness
traceInfo(gen,3)=mean(startPop(:,xZomeLength));     %Avg fittness
traceInfo(gen,4)=std(startPop(:,xZomeLength));      %Standard Deviation
traceInfo(gen,5)=size(unique(startPop(:,[1:(xZomeLength-1)]),'rows'),1)/initSize; %diversity
plot(gen,traceInfo(gen,2),'r*');
drawnow;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -