Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C
Tip/Trick

Simple C genetic algorithm example

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
3 Oct 2011CPOL 34.5K   3  
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)


Written By
Technical Lead
Morocco Morocco
Adrabi!, Just another Ghost in the Shell =)

Comments and Discussions

 
-- There are no messages in this forum --