GA code for optimizing three parameters with different interval limit

3 vues (au cours des 30 derniers jours)
Rudi Martin
Rudi Martin le 21 Mar 2013
Réponse apportée : ARNAB le 23 Fév 2015
I'm trying to make a GA code for welding optimization with three parameters, each parameter has a different interval limit. Need guidance here...
===========================================================================
My parameters:
current: 80 - 100 ampere => x(1)
voltage: 80 - 90 volt => x(2)
weld time: 13 - 15 second => x(3)
since this is my first time using GA, I'm a little bit confused how to do it. In my code, I represent my parameters as a variable of a fitness function. Not sure if this is right.
============================================================================
My fitness function:
function y = weldingfcn {x(1),x(2),x(3)}
for x(1)=80:100
for x(2)=80:90
for x(3)=13:15
y = -(1-0.01)*{(x(1)-5)^2+(x(2)-5)^2+(x(3)-5)^2};
end
end
end
============================================================================
My function for initializing population:
%popsize = population size
%stringlength = number of bits that represent individual
%dimension = number of variable
function pop = encoding(popsize,stringlength,dimension)
pop = round(rand(popsize,dimension*stringlength+1));
===========================================================================
My function for decoding population binary number to decimal number:
function pop=decoding(pop,stringlength,dimension,x_bound)
popsize=size(pop,1);
temp=2.^(stringlength-1:-1:0)/(2^stringlength-1);
for i=1:dimension
bound(i)=x_bound(i,2)-x_bound(i,1);
end
for i=1:popsize
for j=1:dimension
m(:,j)=pop(i,stringlength*(j-1)+1:stringlength*j);
end
x=temp*m;
x=x.*bound+x_bound(:,1)';
pop(i,dimension*stringlength+1)=funname(x);
end
========================================================================
Function for selection procedure:
function selected = selection(pop,popsize,stringlength,dimension)
popsize_new = size(pop,1);
r=rand(1,popsize);
fitness=pop(:,dimension*stringlength+1);
fitness=fitness/sum(fitness);
fitness=cumsum(fitness);
for i=1:popsize
for j=1:popsize_new
if r(i)<=fitness(j)
selected(i,:)=pop(j,:);
break;
end
end
end
============================================================================
Function for crossover procedure:
function new_pop = cross_over(pop,popsize,stringlength,dimension)
match = round(rand(1,popsize)*(popsize-1))+1;
for i = 1:popsize
[child1,child2] = cross_running(pop(i,:),pop(match(i),:),stringlength,dimension);
new_pop(2*i-1:2*i,:)= [child1;child2];
end
function [child1,child2] = cross_running(parent1,parent2,stringlength,dimension)
cpoint = round((stringlength-1)*rand(1,dimension))+1;
for j=1:dimension
child1((j-1)*stringlength+1:j*stringlength)=[parent1((j-1)*stringlength+1: (j-1)*stringlength+cpoint(j))
parent2((j-1)*stringlength+cpoint(j)+1:j*stringlength)];
child2((j-1)*stringlength+1:j*stringlength)=[parent2((j-1)*stringlength+1: (j-1)*stringlength+cpoint(j))
parent1((j-1)*stringlength+cpoint(j)+1:j*stringlength)];
end
===========================================================================
Function for mutation procedure:
function new_pop = mutation(new_pop,stringlength,dimension,pm)
new_popsize = size(new_pop,1);
for i=1:new_popsize
if rand<pm mpoint=round(rand(1,dimension)*(stringlength-1))+1;
for j=1:dimension
new_pop(i,(j-1)*stringlength+mpoint(j))=1-new_pop(i,(j-1)*stringlength+mpoint(j));
end
end
end
===========================================================================
Function for starting the GA:
%GA parameter
popsize=10; %population size
dimension=3; %number of variables
stringlength=8; %length of binary number
x_bound=[80,100;80,90;13,15]; %x limit interval [lb,ub]
%where: lb = lower bound
% ub = upper bound
pm=0.05; %mutation probability
%Genetic Algorithm
pop=encoding_guo(popsize,stringlength,dimension);
pop=decoding_guo(pop,stringlength,dimension,x_bound);
[choice_number,choice_k]=max(pop(:,stringlength*dimension+1));
choice=pop(choice_k,:);
for i=1:1000 new_pop=cross_over(pop,popsize,stringlength,dimension);
pop=mutation_guo(new_pop,stringlength,dimension,pm);
pop=decoding_guo(pop,stringlength,dimension,x_bound);
[number,k]=max(pop(:,stringlength*dimension+1));
if choice_number<number
choice_number=number;
choice_k=k;
choice=pop(choice_k,:);
end
pop=selection_guo(pop,popsize,stringlength,dimension);
[number,m]=min(pop(:,stringlength*dimension+1));
pop(m,:)=choice;
end
[value,x]=result_guo(pop,stringlength,dimension,x_bound);
============================================================================
Function for displaying the result:
function [value,x] = result_guo(pop,stringlength,dimension,x_bound)
[value,x] = max(pop(:,stringlength*dimension+1));
temp=2.^(stringlength-1:-1:0)/(2^stringlength-1);
for i=1:dimension
bound(i)=x_bound(i,2)-x_bound(i,1);
end
============================================================================
When i run the GA, an error showing up. after i fixed it, another comes up. It's always error. Someone please guide me. Thank you for your concern =)

Réponse acceptée

Alan Weiss
Alan Weiss le 21 Mar 2013
I'm sorry you are having trouble. it seems to me that you need to slow down, and learn the correct syntax for the various things you are trying to do. Please take some time to read the documentation.
For example, here is how you write a fitness function. It should be a function of one vector x, not a cell array of {x1,x2,x3}.
Are you using "bitstring" population type? If so, did you set your options to indicate that? Did you know that "bitstring" population is not a string at all, despite the misleading name? Here is some recent documentation about various population types.
Are you sure that you need custom crossover and mutation functions? Why not start with built-in ones? But perhaps you are using a custom population type.
There are a few examples that might help you:
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Plus de réponses (1)

ARNAB
ARNAB le 23 Fév 2015
what is the meaning of rep([-0.1;0.1],[1,13])

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by