APPENDIX II
MATLAB Code
MATLAB is a commonly used program for computer modeling. Its code is
relatively straightforward. So even though you may not use MATLAB, it has
a pseudocode flavor that should be easy to translate into your favorite pro-
gramming language. If you wish to learn about MATLAB or reference all the
manuals on line, go to www.mathworks.com for help. There are many demos,
free software, and other useful items as well as all the MATLAB documenta-
tion you would ever need. An excellent version is also available for students.
Many of the programs we have used in this book are listed in this appendix
and come on the included CD. All the plots and graphs in this book were
created with MATLAB version 6.5. We have listed the MATLAB code in the
appendix in case the CD gets separated from the book.
PROGRAM 1: BINARY GENETIC ALGORITHM
% Binary Genetic Algorithm
%
% minimizes the objective function designated in ff
% Before beginning, set all the parameters in parts
I, II, and III
% Haupt & Haupt
% 2003
clear
%_______________________________________________________
% I. Setup the GA
ff=’testfunction’; % objective function
npar=2; % number of optimization variables
%_______________________________________________________
% II. Stopping criteria
maxit=100; % max number of iterations
mincost=-9999999; % minimum cost
Practical Genetic Algorithms, Second Edition, by Randy L. Haupt and Sue Ellen Haupt.
ISBN 0-471-45565-2 Copyright © 2004 John Wiley & Sons, Inc.
211