find minimum of math function with genetic algorithm in matlab

牧云@^-^@ 提交于 2019-12-08 13:17:02

问题


I want to find the minimum value of function with genetic algorithm in matlab( I know matlab have toolbox for GA but I want achive it programmically ). I have four m-file, Itterate 50 time,and in every loop step save best and mean of fitness, but when I run this code code not return me lower value in best and average , this is no normal. where is my problem?

my math function is find minmun of f(x)= -|x*sin(sqrt(|x|))|

main.m

global population;
global fitness;
global popsize;
format bank;
popsize=50;
report=zeros(popsize,2);
selected=ones(1,50);
fitness=zeros(1,50);
population = randi([0 1], 50, 10);
for j=1:popsize
calFitness();
for i=1:popsize
   selected(1,i)=(rol_wheel(fitness));
end;
population =recombin(population,selected);
report(j,:)=[min(fitness),mean(fitness)];
end

calFitness

function [] = calFitness(  )
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
global population;
global fitness;
global popsize;
%population=population.*2;
for i=1:popsize
    x=bin2dec(num2str(population(i,:)))/2;
    fitness(1,i)= abs(x*sin(sqrt(abs(x))));

 %disp(fitness);
end

%disp();

rol_wheel

% ---------------------------------------------------------
% Roulette Wheel Selection Algorithm. A set of weights
% represents the probability of selection of each
% individual in a group of choices. It returns the index
% of the chosen individual.
% Usage example:
% fortune_wheel ([1 5 3 15 8 1])
%    most probable result is 4 (weights 15)
% ---------------------------------------------------------
function choice = rol_wheel(weights)
  accumulation = cumsum(weights);
  p = rand() * accumulation(end);
  chosen_index = -1;
  for index = 1 : length(accumulation)
    if (accumulation(index) > p)
      chosen_index = index;
      break;
    end
  end
  %keyboard
  choice = chosen_index;

recombin

function pop = recombin( popu,selected )
global popsize;
pop=zeros(50,10);
for i=1:popsize/2
    rc=randi([1,10]);
    for j=1:10
            pop(i,1:rc-1)=popu(selected(i),1:rc-1);
            pop(i,rc:end)=popu(selected(i+25),rc:end);
            pop(i+25,1:rc-1)=popu(selected(i+25),1:rc-1);
            pop(i+25,rc:end)=popu(selected(i),rc:end);
          %keyboard
    end
end  
end

I would appreciate any answer and help.

来源:https://stackoverflow.com/questions/13255606/find-minimum-of-math-function-with-genetic-algorithm-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!