Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C

Simple C genetic algorithm example

1.00/5 (1 vote)
3 Oct 2011CPOL 34.7K  
Simple C genetic algorithm example
This is a simple C genetic algorithm example, it's not perfect but works fine without chaos errors in memory nor results, it is tested in fedora 14 with Valgrind-3.5.0 and compiled by gcc version 4.5.1.

This program finds an equation composed by addition/subtraction for the given target number.

An example output for target number 27:

binary string: 01111010011010110110101000011000 - fitness: 2
binary string: 01111010011010110110101000011000 - fitness: 2
binary string: 01111010011010110110101000011000 - fitness: 2
binary string: 01111010011010110110101000011000 - fitness: 2
binary string: 00110010101100101011011010100100 - fitness: 1
binary string: 00101010001001001011010110100110 - fitness: 0
2+24-5+6


To change target number, just edit line #define TARGET 27

The full source code has been given here: http://pastebin.com/40Y5U1sY[^].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)