File Exchange

image thumbnail

SpeedyGA: A Fast Simple Genetic Algorithm

version 1.22 (4.13 KB) by

A vectorized implementation of a simple genetic algorithm in Matlab

111 Downloads

Updated

View License

SpeedyGA is a vectorized implementation of a genetic algorithm in the Matlab programming language. Without bells and whistles, it faithfully implements the specification for a Simple GA given on pgs 10, 11 of M. Mitchell's GA book. See comments in code for details.

This script has played a crucial part in the development of a new, unified explanation for the adaptive capacity of genetic algorithms (including ones with uniform crossover) called the Generative Fixation Hypothesis. See http://cs.brandeis.edu/~kekib/dissertation.html

Matlab is optimized for performing operations on arrays. Loops, especially nested loops, tend to run slowly in Matlab. It is possible to significantly improve the performance of Matlab programs by converting loops into array operations. This process is called vectorization. Matlab provides a rich set of functions and many expressive indexing schemes that make it possible to vectorize code. Such code not only runs faster, it is also shorter, and simpler to understand and change (provided that you know a little about Matlab of course).

Genetic Algorithms that are implemented in C/C++ or Java typically have multiple nested loops. Therefore direct ports of such implementations to Matlab will run very slowly. Many of the nested loops found in a typical GA implementation have been eliminated from SpeedyGA. The resulting code is short, fast and simple. It is indeed a delightful coincidence when the constructs of a programming language match a programming task so well that a program can be written this succinctly.

SpeedyGA is proof that Matlab is a useful language for the rapid prototyping of Genetic Algorithms. This, in addition to Matlab's extensive data visualization capabilities, make Matlab an extremely useful platform for the experimental analysis of GAs.

SpeedyGA has been created and tested under Matlab 7 (R14). Out of the box it evolves a population against the one-max fitness function. The royal-roads fitness function has also been included but is not currently being called. If you find SpeedyGA useful or find any bugs please let me know.

Enjoy!

p.s. For an experimental genetic algorithm which might significantly improve the quality of the solutions returned check out TurboGA (http://www.mathworks.com/matlabcentral/fileexchange/24053 )

Comments and Ratings (21)

as soni

Good evening everyone, I need an example of programming an algorithm but without using the optimization toolbox, can someone help me

naidu talada

HELLO
i how to use this code for minimizing a function which is in simulation model?
thank u

Do-woon Kim

topolowa

Could be usefull, but how to implement your own fitness function...

Hi,

Could you please let us know how it can be used for a custom fitness function?
Thanks.

Wendy

Wendy (view profile)

Hi all,

I would like to try this speedyGA algorithm, but I am not sure how to use it. For example, I tried comment
SpeedyGA(@myfun, nvars), it didn't work. Any advice is greatly appreciated!

Thanks.

Please tell me how to use this. I am new to Matlab.
I want to minimize fitness function like

function y = fitness_f(x)
y= x^2-x+1;

Pablo Estevez

Hey, nice code.
I got an error for small populations and small genomes, and solved it by changing line 132 from "fitnessVals=ones(popSize,1);" to "fitnessVals=ones(1,popSize);". Please let me know if that change is valid or Im braking something in the code!

sofiane

very helpful work, thanks

Aji D

Aji D (view profile)

super....

Patroklus

Very nice work!!

mohan l

mohan l

jia

jia (view profile)

Eastor Lee

good thanks

Good work, but can be improved more,

samaksh kumar

awesome!!!!!!!!!

Krish Reddy

very helpful. Thanks!

Updates

1.22

No updates to the code. Changed the description to mention the Generative Fixation Hypothesis

1.21

updated description

1.20

Updated description

1.19

SpeedyGA now runs as a script (easier to work with for research purposes). Without bells and whistles, it now faithfully implements the specification for a Simple GA given on pgs 10, 11 of M. Mitchell's GA book. See comments in code for details.

1.13

.

1.12

1) The best individual of each generation is no longer displayed
2) Plots the maximum and average fitness of each generation at the end of a run
3) Upon completion, returns the best individual of a run, and its fitness

1.5

corrected a typo in the description field

1.4

Added the option of visualizing bit frequencies (handy for studying GA dynamics)

1.3

pre-generated crossover and mutation masks, which significantly improves performance

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video