How to loop GA to give constraints to each gene in the chromosome

1 次查看(过去 30 天)
hello, I am trying to create a ga in matlab, my chromosome has 5 genes but i need to put limits into these genes. Can anyone give me a tip on how to do it? This is my code so far:
Where can i loop it to add the constraints of each gene and how would i go about doing it? thanks.
%Parameters of the GA
generations=100;
pop_size=100;
elitism=.25;
mut=.01;
%lower number
l_number=5;
%larger number
h_number=600;
%length of the chromosome
lengthofvector=5;
%generate initial population
[ pop ] = pop_gen(pop_size, l_number,h_number,lengthofvector );
%evaluate the sum of each chromosome (objective value)
[ OB] = evaluation( pop )
for i=1:generations
%Rank solutions
[ ranked_sol ] = rank_solutions( pop,OB );
%get Elite Solutions
elit_sol= ranked_sol(1:round(pop_size*elitism),:)
%get childs
[children] = reproduction(ranked_sol,pop_size,elitism );
%mutation
[children]=mutation(children,mut);
pop=[elit_sol;children];
[ OB] = evaluation( pop );
localmin(i)=min(OB);
end
[ ranked_sol ] = rank_solutions( pop,OB );
bestsol=ranked_sol(1,:)
best_value=localmin(end)
  1 个评论
Brendan Hamm
Brendan Hamm 2018-11-29
It looks like you are trying to define your own Genetic Algorithm routine. There is a function called ga in the Global Optimization toolbox which can solve such problems and this would likely be the path of least resitance.
In any case, you would need to ensure the corssover, mutation and population functions are generating samples which satisfy the constraints.

请先登录,再进行评论。

回答(1 个)

Rishabh Gupta
Rishabh Gupta 2018-12-10
Hi Ximena,
I would recomend you to use ga function in MATLB avalaible for Genetic Algorithms which also have the option to set constraints for the soultion.
If you want to add the contraint checking in your code, then you could write a function which takes in a chromosome and check all the constraints you wish to check and returns True/False corresponding to whether this is a valid chromosome or not. Call this function for every children created in you algorithm and keep the particular children in the next generation only if it satisfies the contraints. This would ensure that contraints are always satisfied by all the chromosomes in the algorithm.
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by