You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
genetic algorithm run time and result analysis
9 views (last 30 days)
Show older comments
Hello
i am using Genetic algorithm. my problem has
nvars=30,
population size = 400
size of initial population matrix = 50x30
max. generation = 200
the following is a plot of best fitness value optained at each generation. the dark dots are ''best value across a generation '' the lighter dots are ''mean value across a generation''
question 1 is; it takes very long time to reach this figure, more than 10 hours, is that normal for my code that has around 300 lines?
question 2: whould any one comment on the shape of output result, this is scattered but on the same area. may be the search space does not mutate enogh? i used the default options for mutation and crossover.
question 3: what options could be useful to reduce running time but get an accurate result, i can reduce the max generations and population size, but i want to lmite the run time to 15 min.

15 Comments
Walter Roberson
on 30 Sep 2020
is that normal for my code that has around 300 lines?
Did you implement genetic algorithm yourself, in about 300 lines of code? Or are you using ga() ?
Nourhan Elsayed
on 30 Sep 2020
i use this function
[x,fval,exitflag,output,population,scores] =ga(@FittingFunction,nvars,[],[],[],[],lb,ub,@NonLinearConstraints,[],options);
Walter Roberson
on 30 Sep 2020
What kinds of operations do you do in those 300 lines?
Are the nonlinear constraints only inequalities or are there equalities as well? You can end up having to test a lot of differerent values to find matches for nonlinear equalities.
Star Strider
on 30 Sep 2020
One possibility is that ga is chasing a moving target, and the fitness function is changing arbitrarily, perhaps due to a random number generator call. A static fitness function — where only the parameters are changing — should converge.
Nourhan Elsayed
on 30 Sep 2020
thank you Strider
would you please explaine more about a moving target?
in addition, i want to ask about the last numer '0'
i got when i interrupt the run. as i got this massage
Read of fitting fn successeful
127 1005600 1.55399e+11 1211 0
Optimization terminated: Stopped in inner iterations.
Nourhan Elsayed
on 30 Sep 2020
thank you Walter Roberson
i have only nonlinear equality constraints
What kinds of operations do you do in those 300 lines?
i have to make a sequance of calculations to define dependent variables that participate on the fitting function. in addition, some lines calculate the initial population matrix. and other lines read and load input data from excel file
Walter Roberson
on 30 Sep 2020
How large is the fitting function and what kinds of operations are you doing in it?
Are you loading a file inside the fitting function?
Walter Roberson
on 30 Sep 2020
I do not recognize that final message about inner iterations. Which MATLAB version are you using?
Walter Roberson
on 30 Sep 2020
Nonlinear equality constraints effectively require the equivalent of an fsolve to try find a position that meets the constraints. That can be tricky since the constraints are permitted to be discontinuous. Also sometimes the constraints involve considerable computation.
Nourhan Elsayed
on 30 Sep 2020
Edited: Nourhan Elsayed
on 30 Sep 2020
ok,
my project has 4 files
file 1: it loads input data from excel file and creat initial population matrix based on input data. it contains lower and upper bounds, some independent variables that are defined as Global and most of them are vectors. (i run this file before i run the ga file , i used to do this step befor i run the ga to overcome the error of undefined variable).
file 2 : Fitting Function: this file containes 4 sections doing next tasks:
%%Task 1: Decision variables : define vector X [x1, x2, x3, ..... x30] of 30 Decision variables for the optimization process
%% Task 2: range of suitable Engines
the code access ''excel file'' to look up for data within a defined range and store them on a matrix,
each element of this matrix is compare to value of x1 and choose the closest then the code acces the excel file again and extract data coressponding to this value.
this step repated for x2 and x3
%% Task 3 : calculate dependent variables (V1, V2, PTO, PTI)
V1 = a* Ld^2 + b * Ld + c * ones(1,9)
V2 = u* LF^2 + d * LF + cw* ones(2,9)
given that
Ld = [x(2:12)]
LF = [x(13 :21); x(22:30)]
% PTO and PTI
PTI = P_Prop_left - x1 * Ld;
PTI (PTIt<0)= 0
PTO= x1* Ld - P_Prop_left;
PTO(PTOt<0) = 0 ;
%% Task 4 objective function:
TFC = (( x1 * (Ld.* V1)+ x1 * (Ld .* V1)) * T') + ((x2* (LF .* V2) * T')
Note that operations are matrix operations.
file 3: NonLinearConstraints:
c = [];
ceq = x1 * Ld+ 0.5 * x2* LF - P_Prop_left - 0.5 * P_EL
file 4: it contains only the ga function and optimization options.
i provided here only a sample and conclusion for the code operations for more clarification.
when i interrupt the run. as i got this massage
Read of fitting fn successeful
127 1005600 1.55399e+11 1211 0
Optimization terminated: Stopped in inner iterations.
i want to ask about the meaning of last naumber of this massage. i suppose that the algorithm can not satisfy the constraint.
i hope you can help
thank you a lot
later.
Walter Roberson
on 30 Sep 2020
Edited: Walter Roberson
on 4 Oct 2020
Are you reading a file inside your objective function (task 2)? That is expensive to process. https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Nourhan Elsayed
on 1 Oct 2020
yes Walter, it sounds you are right. parametrizing function could be one solution .
i suggest also to use bilevel concept. x1, x2, x3 are the upper level variables which pass their values to lower level optimization process. in that case, lower level optimization problem has a fixed x1, x2, x3 and that will reduce search space significantly.
thank you a lot
Nourhan Elsayed
on 4 Oct 2020
that one was helpful https://www.mathworks.com/matlabcentral/answers/602392-genetic-algorithm-run-time-and-result-analysis#comment_1031587
i used function handle on my task and i re-formated the whole problrm.
i devided it into two optimization problems, or two layers, one inside the other.
the outer layer i used ga algorithm and the inner layer i used fmincon algorithm. that gave me the figure below.
would you please check that. the process terminated because it reached maximum no of generations. i am asking , on the light of this output, do my problem is going on the right way. i know this information is not enough to judge. but i was watching the code during running and i noticed that my variables are changing , but unfortunatlty, i coudn't get out information about each generation

Walter Roberson
on 4 Oct 2020
You might want to add an additional PlotFcn to plot the generation information. Or since you are only running 50 generations, change the 'Display' option to 'iter'
Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)