Geneticalgoinc
Geneticalgoinc
Geneticalgoinc
Kumara Sastry
June 5, 2007
Abstract
This report provides documentation for the general purpose genetic algorithm toolbox for
matlab in C++. The fitness function used in the toolbox is written in matlab. The toolbox pro-
vides different selection, recombination, mutation, niching, and constraint-handling operators.
Problems with single and multiple objectives can be solved with the toolbox. Moreover, the
toolbox is easily extensible and customizable for incorporating other operators and for solving
user-defined search problems.
1 Introduction
This document briefly describes how to download, compile, and run the GA toolbox for matlab.
The source code is an extension of the GA toolbox implementation in C++ (Sastry, 2007) so that
it can be used inside matlab with fitness functions written in matlab. Interested user should refer
to the extensive comments in the code for an explanation of the more intricate GA-implementation
and the code structure details. This report also explains how to modify the objective function that
comes with the distribution of the code. The source is written in C++ but knowledge of matlab
and C is sufficient to modify the objective function so that you can try the toolbox for your own
problems.
At this point you should have in your directory the following files:
2
DISCLAIMER README chromosome.cpp chromosome.hpp
crossover.cpp crossover.hpp ga.cpp ga.hpp
globalSetup.cpp globalSetup.hpp individual.cpp individual.hpp
localsearch.cpp localsearch.hpp nsgapopulation.cpp population.cpp
population.hpp random.cpp random.hpp selection.cpp
selection.hpp userDefinables.cpp sgaFitnessFunction.m mogaFitnessFunction.m
plotFunction.m
The cxxopts.sh is a C++ options file needed to let matlab know that you are compiling
C/C++ source code. Once the compiling is successfully completed, a Matlab executable file,
GAtbxm.mexglx, is created.
In Windows-based systems, launch the matlab application and compile the source code using
mex in the Matlab command window as follows:
Note that depending on your Matlab installation, you might need to use a specific C++ options
file with the -f option. The C++ options file in Windows is a .bat file and is typically in the
%Matlab%\bin or %Matlab%\bin\mexopts directory, where %Matlab% is the Matlab root directory.
For example, %Matlab% can be C:\Program Files\MATLAB704\. Once the compiling is successfully
completed, a Matlab executable file, GAtbxm.mexw32 or GAtbxm.dll, is created.
An alternative way of compiling the source code in Microsoft Visual Studio is given here:
http://www.mathworks.com/support/solutions/data/1-180D4.html?solution=1-180D4.
3
with the following two constraints
The multiobjective test function is the Kurosawe test function (Deb, Pratap, Agrawal, & Me-
yarivan, 2002):
2
X q
f1 (x1 , x2 , x3 ) = −10 ∗ exp −0.2 x2i + x2i+1 , (4)
i=1
3 h i
|xi |0.8 + 5 sin x3i .
X
f2 (x1 , x2 , x3 ) = (5)
i=1
The steps required to write your own fitness function are described in section 7.
To run the GA toolbox, at the command prompt, type
For example to use input sga maxSpec as the input file type GAtbxm(’input sga maxSpec’).
Relevant statistics are plotted, depending on the input file specifications, at the end of each gener-
ation. The output plotted is handled by the matlab function plotFunction.m. The input to the
function is the decision variable values, objective function values, and constraint violation values
of the individuals in the current population. plotFunction.m is called within the C++ in the
function
1. Tournament selection with replacement (Goldberg, Korb, & Deb, 1989; Sastry & Gold-
berg, 2001)
2. Tournament selection without replacement (Goldberg, Korb, & Deb, 1989; Sastry &
Goldberg, 2001)
4
3. Truncation selection (Mühlenbein & Schlierkamp-Voosen, 1993)
4. Roulette-Wheel selection (Goldberg, 1989)
5. Stochastic universal selection (SUS) (Baker, 1985; Baker, 1987; Grefenstette & Baker,
1989; Goldberg, 1989)
1. One point crossover (Goldberg, 1989; Sastry, Goldberg, & Kendall, 2005)
2. Two point crossover (Goldberg, 1989; Sastry, Goldberg, & Kendall, 2005)
3. Uniform crossover (Goldberg, 1989; Sastry, Goldberg, & Kendall, 2005)
4. Simulated binary crossover (SBX) (Deb & Agarwal, 1995; Deb & Kumar, 1995)
Constraint handling Three constraint handling methods are implemented in the GA toolbox:
5
It is important to note that the constraint handling methods use the constraint violation
value and not constraint function value. For example, if the constraint is
10x1 + x2 ≤ 20,
Local search The local search method implemented in the GA toolbox is the Nelder-Mead algo-
rithm (Nelder & Mead, 1965; Press, Flannery, Teukolsky, & Vettering, 1989). Local search is
applied to an individual with probability pls . The solution obtained through the local search is
incorporated using the Baldwinian strategy—where the local search solution replaces the indi-
vidual, but the fitness of the individual is not modified—with probability, pb , and Lamarckian
strategy—where both the fitness and the individual are replaced with local search solution
and its fitness—with probability, 1 − pb .
Elitist Replacement In GA toolbox, both the old and new population is sorted according to
their fitness and constraint violation. A user specified proportion, pe , of top individuals are
retained and the rest, n(1 − pe ) individuals are replaced by the top individuals in the new
population. Here n is the population size.
5.1 Statistics
Various statistics are collected from the population every generation. These not only aid the
user with the progress of genetic search, but are also used in many operators and functionalities.
Statistics are also used to determine if the genetic search should be terminated or not. The statistics
collected are the mean, minimum, and maximum fitness and objective values. Other statistics
include, fitness variance and objective variance in the population. For the multiobjective case, the
statistics are collected separately for each objective.
1. Number of Function Evaluations: This option terminates the run after certain number
of function evaluations have been performed by the GA.
2. Fitness Variance: This option stops the once the fitness variance of the population reaches
below a specified value.
3. Best Fitness: This criterion terminates the run once the fitness of the best individual in the
population exceeds a specified value.
4. Average Fitness: This option stops the run once the average fitness of the population
exceeds a specified value.
5. Average Objective: This option stops the run once the average objective function value of
the population exceeds a specified value.
6. Change in Best Fitness: This option terminates the run once the change in fitness of the
best individual in the population over the previous generation is below a specified value.
6
7. Change in Average Fitness: This option terminates the run once the change in average
fitness of the population over the previous generation is below a specified value.
8. Change in Fitness Variance: This option terminates the run once the change in fitness
variance of the population over the previous generation is below a specified value.
9. Change in Best Objective: This option terminates the run once the change in change in
objective value of the best individual in the population over the previous generation is below
a specified value.
10. Change in Average Objective: This option terminates the run once the change in change
in average objective value the population over the previous generation is below a specified
value.
11. Number of Fronts: This option is available only for NSGA. It terminates the run after the
number of non-dominated fronts in the population goes below a specified value.
12. Number of guys in first front: This option terminates the run when the number of
individuals in the first exceeds a specified value. It is only available only for NSGA.
13. Change in number of fronts: This options stops the run when the change in the number
of fronts over the previous generating is below a specified value.
1 #
2 # GA type: SGA or NSGA
3 #
4 SGA
5
6 #
7 # Number of decision variables
8 #
9 2
10
11 #
12 # For each decision variable, enter:
13 # decision variable type, Lower bound, Upper bound
14 # Decision variable type can be double or int
15 #
16 double 0.0 6.0
17 double 0.0 6.0
7
18
19 #
20 # Objectives:
21 # Number of objectives
22 # For each objective enter the optimization type: Max or Min
23 #
24 1
25 Min
26
27 #
28 # Constraints:
29 # Number of constraints
30 # For each constraint enter a penalty weight
31 #
32 2
33 1.0
34 1.0
35 #
36 # General parameters: If these parameters are not entered default
37 # values will be chosen. However you must enter
38 # "default" in the place of the parameter.
40 # [population size]
41 # [maximum generations]
42 # [replace proportion]
43 #
44 100
45 100
46 0.9
47
48 #
49 # Niching (for maintaining multiple solutions)
50 # To use default setting type "default"
51 # Usage: Niching type, [parameter(s)...]
52 # Valid Niching types and optional parameters are:
53 # NoNiching
54 # Sharing [niching radius] [scaling factor]
55 # RTS [Window size]
56 # DeterministicCrowding
57 #
58 # When using NSGA, it must be NoNiching (OFF).
59 #
60 NoNiching
61
62 #
63 # Selection
64 # Usage: Selection type, [parameter(s)...]
65 # To use the default setting type "default"
66 #
8
67 # Valid selection types and optional parameters are:
68 # RouletteWheel
69 # SUS
70 # TournamentWOR [tournament size]
71 # TournamentWR [tournament size]
72 # Truncation [# copies]
73 #
74 # When using NSGA, it can be neither SUS nor RouletteWheel.
75 #
76 TournamentWOR 2
77
78 #
79 # Crossover
80 # Crossover probability
81 # To use the default setting type "default"
82 #
83 # Usage: Crossover type, [parameter(s)...]
84 # To use the default crossover method type "default"
85 # Valid crossover types and optional parameters are
86 # OnePoint
87 # TwoPoint
88 # Uniform [genewise swap probability]
89 # SBX [genewise swap probability][order of the polynomial]
90 #
91 0.9
92 SBX 0.5 10
93
94 #
95 # Mutation
96 # Mutation probability
97 # To use the default setting type "default"
98 #
99 # Usage: Mutation type, [parameter(s)...]
100 # Valid mutation types and the optional parameters are:
101 # Selective
102 # Polynomial [order of the polynomial]
103 # Genewise [sigma for gene #1][sigma for gene #2]...[sigma for gene #ell]
104 #
105 0.1
106 Polynomial 20
107
108 #
109 # Scaling method
110 # To use the default setting type "default"
111 #
112 # Usage: Scaling method, [parameter(s)...]
113 # Valid scaling methods and optional parameters are:
114 # NoScaling
9
115 # Ranking
116 # SigmaScaling [scaling parameter]
117 #
118 NoScaling
119
120 #
121 # Constraint-handling method
122 # To use the default setting type "default"
123 #
124 # Usage: Constraint handling method, [parameters(s)...]
125 # Valid constraint handling methods and optional parameters are
126 # NoConstraints
127 # Tournament
128 # Penalty [Linear|Quadratic]
129 #
130 Tournament
131
132 #
133 # Local search method
134 # To use the default setting type "default"
135 #
136 # Usage: localSearchMethod, [maxLocalTolerance], [maxLocalEvaluations],
137 # [initialLocalPenaltyParameter], [localUpdateParameter],
138 # [lamarckianProbability], [localSearchProbability]
139 #
140 # Valid local search methods are: NoLocalSearch and SimplexSearch
141 #
142 # For example, SimplexSearch 0.001000 20 0.500000 2.000000 0.000000 0.000000
143 NoLocalSearch
144
145 #
146 # Stopping criteria
147 # To use the default setting type "default"
148 #
149 # Number of stopping criterias
150 #
151 # If the number is greater than zero
152 # Number of generation window
153 # Stopping criterion, Criterion parameter
154 #
155 # Valid stopping criterias and the associated parameters are
156 # NoOfEvaluations, Maximum number of function evaluations
157 # FitnessVariance, Minimum fitness variance
158 # AverageFitness, Maximum value
159 # AverageObjective, Max/Min value
160 # ChangeInBestFitness, Minimum change
161 # ChangeInAvgFitness, Minimum change
162 # ChangeInFitnessVar, Minimum change
10
163 # ChangeInBestObjective, Minimum change
164 # ChangeInAvgObjective, Minimum change
165 # NoOfFronts (NSGA only), Minimum number
166 # NoOfGuysInFirstFront (NSGA only), Minimum number
167 # ChangeInNoOfFronts (NSGA only), Minimum change
168 # BestFitness (SGA with NoNiching only), Maximum value
169 #
170 0
171
172 #
173 # Load the initial population from a file or not
174 # To use the default setting type "default"
175 #
176 # Usage: Load population (0|1)
177 #
178 # For example, if you want random initialization type 0
179 # On the other and if you want to load the initial population from a
180 # file, type
181 # 1 <population file name> [0|1]
182 #
183 # Valid options for "Load population" are 0/1
184 # If you type "1" you must specify the name of the file to load the
185 # population from. The second optional parameter which indicates
186 # whether to evaluate the individuals of the loaded population or not.
187 0
188
189 # Save the evaluated individuals to a file
190 #
191 # To use default setting type "default".
192 #
193 # Here by default all evaluated individuals are stored and you will be
194 # asked for a file name later when you run the executable.
195 #
196 # Usage: Save population (0|1)
197 # For example, if you don’t want to save the evaluated solutions type 0
198 # On the other and if you want to save the evaluated solutions
199 # 1 <save file name>
200 #
201 # Note that the evaluated solutions will be appended to the file.
202 #
203 # Valid options for "Save population" are 0/1
204 # If you type "1" you must specify the name of the file to save the
205 # population to.
206 1 evaluatedSolutions.txt
207
208 #END
11
7 How to plug-in your own objective function?
The code for the objective function is in the matlab function sgaFitnessFunction.m (for single-
objective) and mogaFitnessFunction.m (for multiobjective) and it is called within C++ library in
the file userDefinables.cpp. These are the only files that you need to rewrite in order to try your
own fitness function. The function header which calls the matlab fitness function is as follows:
globalEvaluate takes as argument an array x, whose ` elements contains the decision variables of
a candidate solution whose fitness is being evaluated. Here, ` is the problem length (# of genes).
The decision variables are then passed on to the matlab function sgaFitnessFunction.m (or
mogaFitnessFunction.m) which evaluates the objective and constraint violation values of the given
candidate solution. The function header is as follows:
crossover.cpp contains the implementation of the class crossover. Different crossover operators
are implemented here.
12
selection.cpp contains the implementation of the class selection. Different selection operators
are implemented here.
localsearch.cpp contains the implementation of the class localsearch. A local search operator
is implemented here.
ga.cpp contains the implementation of the class ga. Different GA architectures (GA and NSGA-II)
are implemented here.
globalSetup.cpp contains the implementation of the class globalSetup. Global data needed
across different classes are implemented here.
userDefinables.cpp contains the code for the objective function. If you want to try the GAtbxm
on your own problem, you might have to modify the function globalEvaluate() contained
in this file.
plotFunction.m contains the implementation of graphical display of results obtained through the
GA toolbox.
9 Disclaimer
This code is distributed for academic purposes only. It has no warranty implied or given, and the
authors assume no liability for damage resulting from its use or misuse. If you have any comments
or find any bugs, please send an email to [email protected].
10 Commercial use
For the commercial use of this code please contact Prof. David E. Goldberg at [email protected]
Acknowledgments
This work was also sponsored by the Air Force Office of Scientific Research, Air Force Materiel
Command, USAF, under grant FA9550-06-1-0096, the National Science Foundation under grant
ITR grant DMR-03-25939 at the Materials Computation Center. The U.S. Government is autho-
rized to reproduce and distribute reprints for government purposes notwithstanding any copyright
notation thereon.
The views and conclusions contained herein are those of the authors and should not be inter-
preted as necessarily representing the official policies or endorsements, either expressed or implied,
of the Air Force Office of Scientific Research, the National Science Foundation, or the U.S. Gov-
ernment.
I thank Prof. Duane D. Johnson and David E. Goldberg for encouraging me to write this
report.
13
References
Baker, J. E. (1985). Adaptive selection methods for genetic algorithms. Proceedings of the Inter-
national Conference on Genetic Algorithms and Their Applications, 101–111.
Baker, J. E. (1987). Reducing bias and inefficiency in the selection algorithm. Proceedings of the
Second International Conference on Genetic Algorithms.
Deb, K. (2001). Multi-objective optimization using evolutionary algorithms. Chichester, UK: John
Wiley and Sons.
Deb, K., & Agarwal, R. (1995). Simulated binary crossover for continuous search space. Complex
Systems, 9 , 115–148.
Deb, K., & Kumar, A. (1995). Real-coded genetic algorithms with simulated binary crossover:
Studies on multimodal and multiobjective problems. Complex Systems, 9 , 431–454.
Deb, K., Pratap, A., Agrawal, S., & Meyarivan, T. (2002). A fast elitist non-dominated sorting
genetic algorithm for multi-objective optimization: NSGA-II. IEEE Transactions on Evolu-
tionary Computation, 6 (2), 182–197. (Also KanGAL Report No. 2000001).
Forrest, S. (1985). Documentation fo PRISONERS DILEMMA and NORMS programs that use
genetic algorithms. Unpublished manuscript, University of Michichan, Ann Arbor.
Goldberg, D. E. (1989). Genetic algorithms in search optimization and machine learning. Read-
ing, MA: Addison-Wesley.
Goldberg, D. E., Korb, B., & Deb, K. (1989). Messy genetic algorithms: Motivation, analysis,
and first results. Complex Systems, 3 (5), 493–530. (Also IlliGAL Report No. 89003).
Goldberg, D. E., & Richardson, J. J. (1987). Genetic algorithms with sharing for multimodal
function optimization. Proceedings of the Second International Conference on Genetic Algo-
rithms, 41–49.
Grefenstette, J. J., & Baker, J. E. (1989). How genetic algorithms work: A critical look at implicit
parallelism. Proceedings of the Third International Conference on Genetic Algorithms, 20–27.
Harik, G. R. (1995). Finding multimodal solutions using restricted tournament selection. Pro-
ceedings of the Sixth International Conference on Genetic Algorithms, 24–31. (Also IlliGAL
Report No. 94002).
Mahfoud, S. W. (1992). Crowding and preselection revisited. Parallel Problem Solving from
Nature, 2 , 27–36. (Also IlliGAL Report No. 92004).
Mühlenbein, H., & Schlierkamp-Voosen, D. (1993). Predictive models for the breeder genetic
algorithm: I. continous parameter optimization. Evolutionary Computation, 1 (1), 25–49.
Nelder, J. A., & Mead, R. (1965). A simplex method for function minimization. The Computer
Journal , 8 , 308–313.
Press, W., Flannery, B., Teukolsky, S., & Vettering, W. (1989). Numerical recipes in C. Cam-
bridge: Cambridge University Press.
Rekalaitis, G., Ravindran, A., & Ragsdell, K. (1983). Engineering optimization: Methods and
applications. New York, NY: Wiley.
Sastry, K. (2007, June). Single and multiobjective genetic algorithm toolbox in C++ (IlliGAL
Report No. 2007016). Urbana, IL: University of Illinois at Urbana-Champaign.
14
Sastry, K., & Goldberg, D. E. (2001). Modeling tournament selection with replacement using ap-
parent added noise. Intelligent Engineering Systems Through Artificial Neural Networks, 11 ,
129–134. (Also IlliGAL Report No. 2001014).
Sastry, K., Goldberg, D. E., & Kendall, G. (2005). Genetic algorithms: A tuto-
rial. In Burke, E., & Kendall, G. (Eds.), Introductory Tutorials in Optimization,
Search, and Decision Support Methodologies (Chapter 4, pp. 97–125). Berlin: Springer.
http://www.asap.cs.nott.ac.uk/publications/pdf/gxk introsch4.pdf.
15