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

2 views (last 30 days)
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 Comment
Brendan Hamm
Brendan Hamm on 29 Nov 2018
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.

Sign in to comment.

Answers (1)

Rishabh Gupta
Rishabh Gupta on 10 Dec 2018
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.

Products

Community Treasure Hunt

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

Start Hunting!