{"id":202,"date":"2013-12-17T03:43:12","date_gmt":"2013-12-17T03:43:12","guid":{"rendered":"http:\/\/phoenixgamedevelopment.com\/blog\/?p=202"},"modified":"2013-12-19T22:01:23","modified_gmt":"2013-12-19T22:01:23","slug":"genetic-algorithm-example-program-and-source-code-download","status":"publish","type":"post","link":"https:\/\/phoenixgamedevelopment.com\/blog\/genetic-algorithm-example-program-and-source-code-download\/","title":{"rendered":"Genetic Algorithm Example Program and Source code download"},"content":{"rendered":"<p>I have prepared my Genetic Algorithm Example application for upload. I used the QT library to develop the application, but the executable should run on almost any Windows system, whether QT installed or not.<\/p>\n<p>The executable is located<a href=\"http:\/\/www.phoenixgamedevelopment.com\/blog\/wp-content\/uploads\/2013\/12\/GAExample.rar\"> HERE<\/a>.<\/p>\n<p>The source files are located <a href=\"http:\/\/www.phoenixgamedevelopment.com\/blog\/wp-content\/uploads\/2013\/12\/GAExampleSource.rar\">HERE<\/a>.<\/p>\n<p>A code listing with comments follows, refer to my earlier post on this topic for more information. To build and execute this code, simple create a new QT gui application, and add the relevant files from the source directory.<\/p>\n<p>gaentity.h:<\/p>\n<p><code><br \/>\n#ifndef GAENTITY_H<\/code><\/p>\n<p>#define GAENTITY_H<\/p>\n<p>class GAEntity<\/p>\n<p>{<\/p>\n<p>public:<\/p>\n<p>GAEntity(int maxnumber);<\/p>\n<p>GAEntity();<\/p>\n<p>int solution; \/\/This is the first, and so far only, &#8220;chromasome&#8221; of the GA entity<\/p>\n<p>int fitness;<\/p>\n<p>};<\/p>\n<p>#endif \/\/ GAENTITY_H<\/p>\n<p>gaentity.cpp:<br \/>\n<code><br \/>\n<\/code><\/p>\n<p>#include &#8220;gaentity.h&#8221;<\/p>\n<p>GAEntity::GAEntity(int maxnumber)<br \/>\n{<br \/>\nsolution = rand() % maxnumber + 1; \/\/All GA entities are initialised with a random value from 1 to the max number.<br \/>\nfitness = 999; \/\/lower fitness values are better, so initialise entity with an impossibly high value<br \/>\n}<\/p>\n<p>GAEntity::GAEntity()<br \/>\n{<br \/>\nsolution = rand() % 1000 + 1; \/\/ default constructor, assumes max value is 1000<br \/>\nfitness = 999;<br \/>\n}<\/p>\n<p>Main Logic Function:<br \/>\n<code><br \/>\n<\/code><\/p>\n<p>void MainWindow::runbtnpushed(){<\/p>\n<p>srand ( time(NULL) ); \/\/init time for random number function<br \/>\nQString s = &#8220;&#8221;;<br \/>\n\/\/init variables from gui:<br \/>\nint targetnumber = ui-&gt;targetIN-&gt;value(); \/\/The number that the AI is trying to guess<br \/>\nint maxgenerations = ui-&gt;maxgenerationsIN-&gt;value(); \/\/The max number of generations that the algorithm will run for.<br \/>\nint populationsize = ui-&gt;popsizeIN-&gt;value(); \/\/The number of entities to create in each generation, the more there are, the more chance they will solve the problem<\/p>\n<p>int bestfitness = 999;<\/p>\n<p>for(int i = 0; i &lt; populationsize;i++){ GAEntity ent = GAEntity(ui-&gt;maxnumberIN-&gt;value()); \/\/Create I entities and initialise to random value<br \/>\nent.fitness = abs(targetnumber &#8211; ent.solution); \/\/determine fitness (Simply subtract the solution from the targetnumber, and ignore the sign)<br \/>\npopulation.push_back(ent); \/\/add to population vector<br \/>\n}<br \/>\nint count = 0;<\/p>\n<p>\/\/pick the best two candidates, mate them, produce new population<br \/>\nGAEntity parent1;<br \/>\nGAEntity parent2;<\/p>\n<p>while(bestfitness &gt; 0 &amp;&amp; count &lt; maxgenerations){<br \/>\ncount++;<\/p>\n<p>parent1.fitness = 999;<br \/>\nparent1.solution = 0;<\/p>\n<p>\/\/choose parents:<br \/>\nfor(size_t i = 0; i &lt; population.size();i++){ \/\/lesser fitness is better<\/p>\n<p>GAEntity ent = population[i];<br \/>\n\/\/fitness:<br \/>\nint fitness = abs(targetnumber &#8211; abs(ent.solution));<br \/>\n\/\/this code finds the two entities with the highest fitness. This would be an excellent place for improvement!<br \/>\nif(fitness &lt; parent1.fitness){<br \/>\nparent2.fitness = parent1.fitness;<br \/>\nparent2.solution = parent1.solution;<\/p>\n<p>parent1.fitness = ent.fitness;<br \/>\nparent1.solution = ent.solution;<\/p>\n<p>}else if(fitness &lt; parent2.fitness){ parent2.fitness = ent.fitness; parent2.solution = ent.solution; } } \/\/mate parents: GAEntity child; child.solution = (parent1.solution + parent2.solution)\/2; \/\/This is the &#8220;seed&#8221; for the next generation \/\/print data to gui s = &#8220;Generation: &#8220;; s.append(QString::number(count)); s.append(&#8221; Parent 1 Fitness: &#8220;); s.append(QString::number(abs(targetnumber &#8211; abs(parent1.solution)))); s.append(&#8221; Parent 2 Fitness: &#8220;); s.append(QString::number(abs(targetnumber &#8211; abs(parent2.solution)))); ui-&gt;textBrowser-&gt;append(s);<\/p>\n<p>if(abs(targetnumber &#8211; abs(child.solution) == 0)){ \/\/Solution has been found!<br \/>\n\/\/qDebug() &lt;&lt; &#8220;Solution found!&#8221; &lt;&lt; child.solution &lt;&lt; &#8221; Generation: &#8221; &lt;&lt; count; ui-&gt;textBrowser-&gt;append(&#8220;&#8221;);<br \/>\ns = &#8220;Solution found! Solution: &#8220;;<br \/>\ns.append(QString::number(child.solution));<br \/>\ns.append(&#8221; Generation: &#8220;);<br \/>\ns.append(QString::number(count));<br \/>\nui-&gt;textBrowser-&gt;append(s);<br \/>\nbestfitness = 0;<br \/>\n}<\/p>\n<p>population.clear(); \/\/delete old population<br \/>\n\/\/mutate child and produce new population:<br \/>\nfor(int i = 0; i &lt; populationsize;i++){<\/p>\n<p>GAEntity ent;<\/p>\n<p>\/\/Random Mutation:<br \/>\nfloat rnd1 = static_cast (rand()) \/ static_cast (RAND_MAX);<br \/>\nint rnd = rand() % 2 + 0;<\/p>\n<p>if(rnd1 &gt; 0.5 &amp;&amp; child.solution-rnd &gt;0)<br \/>\nent.solution = child.solution-rnd;<br \/>\nelse<br \/>\nent.solution = child.solution+rnd;<\/p>\n<p>\/\/eval fitness here:<br \/>\n\/\/ent.fitness = abs(targetnumber &#8211; ent.solution);<br \/>\npopulation.push_back(ent);<\/p>\n<p>}<br \/>\n}<\/p>\n<p>\/\/print out all entities:<br \/>\nfor(size_t i = 0; i &lt; population.size();i++){<br \/>\n\/\/ qDebug() &lt;&lt; &#8220;ID: &#8221; &lt;&lt; i &lt;&lt; &#8221; Solution: &#8221; &lt;&lt; population[i].solution &lt;&lt; &#8221; Fitness: &#8221; &lt;&lt; abs(targetnumber &#8211; population[i].solution);\/\/population[i].fitness;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have prepared my Genetic Algorithm Example application for upload. I used the QT library to develop the application, but the executable should run on almost any Windows system, whether QT installed or not. The executable is located HERE. The source files are located HERE. A code listing with comments follows, refer to my earlier [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,12],"tags":[],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-software-and-games","category-tutorials","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"aioseo_notices":[],"builder_content":"","_links":{"self":[{"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":15,"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phoenixgamedevelopment.com\/blog\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}