File Exchange

## Basic Genetic Algorithm

version 1.2.0.0 (5.52 KB) by
An easy to use Genetic Algorithm

Updated 10 May 2018

View Version History

These scritps implement the version of the Genetic Algorithm decribed in
"Control predictivo basado en modelos mediante técnica de optimización heurística. Aplicación a procesos no lineales y multivariables. F. Xavier Blasco Ferragud. PhD Tesis 1999 (in Spanish). Editorial UPV. ISBN 84-699-5429-6.
It is an easy to use GA and basic instructions are supplied.
Available at: http://hdl.handle.net/10251/15995

### Cite As

Xavier Blasco (2020). Basic Genetic Algorithm (https://www.mathworks.com/matlabcentral/fileexchange/39021-basic-genetic-algorithm), MATLAB Central File Exchange. Retrieved .

Murali Tejas

Xavier Blasco

Indeed, currently the beta parameters are in the range [-0.5, 0.5]. With beta in range [0,1] you get a pure linear combination between the parents (P1 parent 1, P2 Parent2, H1 child 1):

H1= beta1*P1+(1-beta1)*P2

Choosing beta in the [-0.5, 0.5] extends the linear combination beyond the limits set by the parents. This expands the limits of exploration established by two parents (increasing the area were the child can be positioned)

As I said in a previous post (May 29th, 2020 in answer to Tom) I am evaluating the possibility of setting betas en el rango [0, 1]. But my impression now is that we would not notice significant variations in the behavior of the algorithm.

Hello. I have the next issue: the beta1 parameter "% Child1 = beta1*Parent1+(1-beta1)*Parent2" I think should be inside inside [0, 1] (for linear recombination). However, the matlab code generates betas<0 due to "betas=rand(2,1)-0.5;" (alpha=0). What do you think.

soheil bahrami

How can I use KH algorithm to normalize problems like Ackley Function ?

Xavier Blasco

In the same way used in the answer to Heonyong. Using penalty terms.
For instances:
function J=cost(x)
%x=[x1,x2] 2D search space.
J=x(1)^2+5*x(2);
%nonlinear penalty term: x(1)^2-0.3*x(2)^3>=1
p=abs(min(x(1)^2-0.3*x(2)^3-1,0))*1e5;
J=J+p;

Sunghyun Jeon

How can I introduce 2 variable nonlinear constraint?

Xavier Blasco

An easy way to include restrictions is to add a term to the objective function that penalizes when the restriction is not satisfied.
For instance, if you have to minimize f(x)=x^2 s.t. x>=1, the cost function could be:
function J=cost(x)
J=x^2;
%penalty term when x>=1
p=abs(min(x-1,0))*1e5;
J=J+p;

Heonyong Lim

How can I introduce constraint at this problem?

Xavier Blasco

In an extreme case where, for instance, P1=1000 and P2=1005 (in the boundaries of the search space you propose), and beta1=-0.4 (betas have been randomly generated in range [-0.5, 0.5], I'm thinking to change the range to [0 1])
H1= beta1*P1+(1-beta1)*P2
H1= -0.4*1000+1.4*1005=1007 (it isn't 1400)
Anyway, this value is outside the boundaries, but after crossover the population pass the mutation operation (mutbga function). As you can see at the end of this function there is some code to prevent points outside boundaries.

% Coerce points outside bounds
aux = ones(Nind,1);
auxf1=aux*FieldDR(1,:);
auxf2=aux*FieldDR(2,:);
NewChrom = (NewChrom>auxf2).*auxf2+(NewChrom<auxf1).*auxf1+(NewChrom<=auxf2 & NewChrom>=auxf1).*NewChrom;

Of course, it is not the unique way to implement crossover and mutation, and your proposal could be valid. But I don't know if is better or not.

Tom

Suppose 1D, but you can extent my idea to multiple dimensions. Suppose the lower boundary is 1000, the upper boudary is 1005. Thus only values between 1000 and 1005 are allowed. A parents value is thus between 1000 and 1005. When I multiply a value between 1000 and 1005 with the beta of 1.4 I end up at around 1400 which is far outside the boundary. would it not be more logic to first take the value of the parent, substract 1000 from it then doing the A multiplication and then adding the 1000 again?

Xavier Blasco

Dear Tom, I'm so pleased that my code can be useful.

As for the question you are asking me, I am not sure I understood the question but I will try to answer it.
The operation you describe is performed in the crossover operand (not in the mutation operand).
It is a linear crossover, two children (NewChrom(pin:pin+1,:) in the code) are generated from two parents (OldChrom(pin:pin+1,:) in the code).
For instances, if we call the children H1 and H2, and the fathers P1 and P2, and we consider alpha=0 (to simplify):
H1= beta1*P1+(1-beta1)*P2
H2= beta2*P1+(1-beta2)*P2

In the code, matrix A (2x2) contains the recombination factor (randomly generated "betas") for each of the variables.

For example, for a case with three 3d:
>> alpha= 0 % default value
alpha =
0
>> betas=rand(2,1)*(1+2*alpha)-(0.5+alpha)
betas =
0.1324
-0.4025
>> A=[betas(1) 1-betas(1); 1-betas(2) betas(2)]
A =
0.1324 0.8676
1.4025 -0.4025
>> OldChrom=[5 10 2;3 14 4]
OldChrom =
5 10 2
3 14 4
>> NewChrom=A*OldChrom
NewChrom =
3.2647 13.4706 3.7353
5.8049 8.3902 1.1951

Tom

I like the code very much. I was just wondering: why did you include the range in the mutation but not in the crossover? Is there a reason for that?

betas=rand(2,1)*(1+2*alpha)-(0.5+alpha);
A=[betas(1) 1-betas(1); 1-betas(2) betas(2)];
NewChrom(pin:pin+1,:)=A*OldChrom(pin:pin+1,:);

suppose you have a really close Upper and lower boundary (for eexample 1000 and 1005. then the range is 1 but you mutiply A*1000). is it not more logic to substract 1000, then A*5 and add the 1000 back?
Or is there a statistical logic behind the above equations instead of what I am suggesting?

Tom Wambecq

Matthew Rice

xi

Thank you for your answer, I have a problem in the process of my own changes, and now I have solved

Xavier Blasco

If you define a function of 1 parameter.
function value=objfun_y(x)
value=x.^2+sin(x)+3*x+x.^3-1;

And execute:
lb=[-1];
ub=;

it works.

If you define the function as a 4 parameter function:
function value=objfun_y4(x)
value=x(1).^2+sin(x(2))+3*x(3)+x(4).^3-1;

and execute:
lb=[-1 -1 -1 -1];
ub=[1 1 1 1];
% Execute GA

It also works. I don't understand what is the problem.

xi

I want to use this code to solve a quaternary equation, how should I modify it? for example y=x(1)^2+sin(x(2))+3*x(3)+x(4)^3-1. When I modified the target parameters. After modifying the upper and lower limits of the parameter, it still cannot be solved. the error in function chrom=crtrp(Nind,FieldDR);

Ranran Xu

Islam Samy

please, i want to the code og genetic algorithm with the fitness function

Xavier Blasco

I can't reproduce the error. I suppose your are running example.m, but everything works well when I run it. Your comment suggest me you you don't have executed the line 1 to 4 of the example or you have delete the variable gaDat before executing line 5.

Error in ga (line 5)

hello Xavier thank you so much for the code, my problem is to maximize the power of PV system using genetic algorithm.thank you so much

Md Alamgir Hossain

Burak Ersin

Burak Ersin

Xavier Blasco

You have to build a cost function for your problem. There is a short tutorial where you can see examples of use.

lydia hamis

My study regarding image reconstruction using constrained least squares filter. In order to get better result, i would like to implement GA. How do i implement this code to the current results?

tayueyue

Harry Smith

Thanks for the code, quick conversion from Matlab solver GA to yours

Xavier Blasco

No heuristic algorithm can guarantee to have found the global optimum. In the current version of the algorithm the stop is done with a fixed number of iterations, but the user can add his own criterion of stop in the function gaiteration.m. This function is executed at each iteration of the algorithm.
if yourStopCriterionIsSatisfied
end
Change yourStopCriterionIsSatisfied by your own condition.

Thank you for the code.
I have a question, While applying the ga alg. my optimal function converges to a fix value starting from an iteration (iteration: 50). does it correspond to the optimal point? is it possible to stop the iterations and assume it as the minimum ?

sawon pratiher

New SLM scheme to reduce the PAPR of OFDM signals using
a genetic algorithm

S. Garcia-Nieto

##### MATLAB Release Compatibility
Created with R2009b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux