ai4r 1.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. data/README.rdoc +12 -25
  2. data/examples/decision_trees/id3_example.rb +6 -9
  3. data/examples/decision_trees/results.txt +2 -0
  4. data/examples/genetic_algorithm/genetic_algorithm_example.rb +11 -13
  5. data/examples/neural_network/xor_example.rb +25 -0
  6. data/lib/ai4r.rb +10 -0
  7. data/lib/ai4r/classifiers/classifier.rb +46 -0
  8. data/lib/ai4r/classifiers/id3.rb +27 -58
  9. data/lib/ai4r/classifiers/one_r.rb +19 -58
  10. data/lib/ai4r/classifiers/prism.rb +21 -57
  11. data/lib/ai4r/classifiers/zero_r.rb +16 -48
  12. data/lib/ai4r/clusterers/bisecting_k_means.rb +115 -0
  13. data/lib/ai4r/clusterers/clusterer.rb +55 -0
  14. data/lib/ai4r/clusterers/k_means.rb +164 -0
  15. data/lib/ai4r/data/data_set.rb +250 -0
  16. data/lib/ai4r/genetic_algorithm/genetic_algorithm.rb +19 -19
  17. data/lib/ai4r/neural_network/backpropagation.rb +23 -24
  18. data/site/build/site/en/broken-links.xml +2 -0
  19. data/site/build/site/en/downloads.html +200 -0
  20. data/site/build/site/en/downloads.pdf +151 -0
  21. data/site/build/site/en/forum.html +197 -0
  22. data/site/build/site/en/forum.pdf +151 -0
  23. data/site/build/site/en/geneticAlgorithms.html +591 -0
  24. data/site/build/site/en/geneticAlgorithms.pdf +934 -0
  25. data/site/build/site/en/images/ai4r-logo.png +0 -0
  26. data/site/build/site/en/images/built-with-forrest-button.png +0 -0
  27. data/site/build/site/en/images/c.png +0 -0
  28. data/site/build/site/en/images/c_wbn.png +0 -0
  29. data/site/build/site/en/images/c_wn.png +0 -0
  30. data/site/build/site/en/images/ero.gif +0 -0
  31. data/site/build/site/en/images/europe2.png +0 -0
  32. data/site/build/site/en/images/europe3.png +0 -0
  33. data/site/build/site/en/images/fitness.png +0 -0
  34. data/site/build/site/en/images/genetic_algorithms_example.png +0 -0
  35. data/site/build/site/en/images/instruction_arrow.png +0 -0
  36. data/site/build/site/en/images/jadeferret.png +0 -0
  37. data/site/build/site/en/images/my_email.png +0 -0
  38. data/site/build/site/en/images/neural_network_example.png +0 -0
  39. data/site/build/site/en/images/rubyforge.png +0 -0
  40. data/site/build/site/en/images/s.png +0 -0
  41. data/site/build/site/en/images/s_wbn.png +0 -0
  42. data/site/build/site/en/images/s_wn.png +0 -0
  43. data/site/build/site/en/images/sigmoid.png +0 -0
  44. data/site/build/site/en/images/t.png +0 -0
  45. data/site/build/site/en/images/t_wbn.png +0 -0
  46. data/site/build/site/en/images/t_wn.png +0 -0
  47. data/site/build/site/en/index.html +336 -0
  48. data/site/build/site/en/index.pdf +508 -0
  49. data/site/build/site/en/linkmap.html +263 -0
  50. data/site/build/site/en/linkmap.pdf +94 -0
  51. data/site/build/site/en/locationmap.xml +72 -0
  52. data/site/build/site/en/machineLearning.html +339 -0
  53. data/site/build/site/en/machineLearning.pdf +337 -0
  54. data/site/build/site/en/neuralNetworks.html +484 -0
  55. data/site/build/site/en/neuralNetworks.pdf +604 -0
  56. data/site/build/site/en/skin/CommonMessages_de.xml +23 -0
  57. data/site/build/site/en/skin/CommonMessages_en_US.xml +23 -0
  58. data/site/build/site/en/skin/CommonMessages_es.xml +23 -0
  59. data/site/build/site/en/skin/CommonMessages_fr.xml +23 -0
  60. data/site/build/site/en/skin/basic.css +166 -0
  61. data/site/build/site/en/skin/breadcrumbs-optimized.js +90 -0
  62. data/site/build/site/en/skin/breadcrumbs.js +237 -0
  63. data/site/build/site/en/skin/fontsize.js +166 -0
  64. data/site/build/site/en/skin/getBlank.js +40 -0
  65. data/site/build/site/en/skin/getMenu.js +45 -0
  66. data/site/build/site/en/skin/images/README.txt +1 -0
  67. data/site/build/site/en/skin/images/add.jpg +0 -0
  68. data/site/build/site/en/skin/images/built-with-forrest-button.png +0 -0
  69. data/site/build/site/en/skin/images/chapter.gif +0 -0
  70. data/site/build/site/en/skin/images/chapter_open.gif +0 -0
  71. data/site/build/site/en/skin/images/current.gif +0 -0
  72. data/site/build/site/en/skin/images/error.png +0 -0
  73. data/site/build/site/en/skin/images/external-link.gif +0 -0
  74. data/site/build/site/en/skin/images/fix.jpg +0 -0
  75. data/site/build/site/en/skin/images/forrest-credit-logo.png +0 -0
  76. data/site/build/site/en/skin/images/hack.jpg +0 -0
  77. data/site/build/site/en/skin/images/header_white_line.gif +0 -0
  78. data/site/build/site/en/skin/images/info.png +0 -0
  79. data/site/build/site/en/skin/images/instruction_arrow.png +0 -0
  80. data/site/build/site/en/skin/images/label.gif +0 -0
  81. data/site/build/site/en/skin/images/page.gif +0 -0
  82. data/site/build/site/en/skin/images/pdfdoc.gif +0 -0
  83. data/site/build/site/en/skin/images/poddoc.png +0 -0
  84. data/site/build/site/en/skin/images/printer.gif +0 -0
  85. data/site/build/site/en/skin/images/rc-b-l-15-1body-2menu-3menu.png +0 -0
  86. data/site/build/site/en/skin/images/rc-b-r-15-1body-2menu-3menu.png +0 -0
  87. data/site/build/site/en/skin/images/rc-b-r-5-1header-2tab-selected-3tab-selected.png +0 -0
  88. data/site/build/site/en/skin/images/rc-t-l-5-1header-2searchbox-3searchbox.png +0 -0
  89. data/site/build/site/en/skin/images/rc-t-l-5-1header-2tab-selected-3tab-selected.png +0 -0
  90. data/site/build/site/en/skin/images/rc-t-l-5-1header-2tab-unselected-3tab-unselected.png +0 -0
  91. data/site/build/site/en/skin/images/rc-t-r-15-1body-2menu-3menu.png +0 -0
  92. data/site/build/site/en/skin/images/rc-t-r-5-1header-2searchbox-3searchbox.png +0 -0
  93. data/site/build/site/en/skin/images/rc-t-r-5-1header-2tab-selected-3tab-selected.png +0 -0
  94. data/site/build/site/en/skin/images/rc-t-r-5-1header-2tab-unselected-3tab-unselected.png +0 -0
  95. data/site/build/site/en/skin/images/remove.jpg +0 -0
  96. data/site/build/site/en/skin/images/rss.png +0 -0
  97. data/site/build/site/en/skin/images/spacer.gif +0 -0
  98. data/site/build/site/en/skin/images/success.png +0 -0
  99. data/site/build/site/en/skin/images/txtdoc.png +0 -0
  100. data/site/build/site/en/skin/images/update.jpg +0 -0
  101. data/site/build/site/en/skin/images/valid-html401.png +0 -0
  102. data/site/build/site/en/skin/images/vcss.png +0 -0
  103. data/site/build/site/en/skin/images/warning.png +0 -0
  104. data/site/build/site/en/skin/images/xmldoc.gif +0 -0
  105. data/site/build/site/en/skin/menu.js +48 -0
  106. data/site/build/site/en/skin/note.txt +50 -0
  107. data/site/build/site/en/skin/print.css +54 -0
  108. data/site/build/site/en/skin/profile.css +163 -0
  109. data/site/build/site/en/skin/prototype.js +1257 -0
  110. data/site/build/site/en/skin/screen.css +587 -0
  111. data/site/build/site/en/svn.html +252 -0
  112. data/site/build/site/en/svn.pdf +306 -0
  113. data/site/build/site/en/wholesite.pdf +1915 -0
  114. data/site/build/tmp/brokenlinks.xml +2 -0
  115. data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.data +0 -0
  116. data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.index +0 -0
  117. data/site/build/tmp/locationmap.xml +14 -14
  118. data/site/build/tmp/output.xmap +23 -23
  119. data/site/build/tmp/pluginlist2fetchbuild.xml +144 -144
  120. data/site/build/tmp/projfilters.properties +41 -41
  121. data/site/build/webapp/WEB-INF/logs/core.log +593 -679
  122. data/site/build/webapp/WEB-INF/logs/error.log +362 -279
  123. data/site/build/webapp/WEB-INF/logs/sitemap.log +368 -1015
  124. data/site/src/documentation/content/xdocs/index.xml +18 -10
  125. data/site/src/documentation/content/xdocs/machineLearning.xml +4 -3
  126. data/site/src/documentation/content/xdocs/site.xml +2 -1
  127. data/site/src/documentation/resources/images/sigmoid.png +0 -0
  128. data/test/classifiers/id3_test.rb +45 -44
  129. data/test/classifiers/one_r_test.rb +19 -17
  130. data/test/classifiers/prism_test.rb +22 -20
  131. data/test/classifiers/zero_r_test.rb +15 -12
  132. data/test/clusterers/bisecting_k_means_test.rb +59 -0
  133. data/test/clusterers/k_means_test.rb +93 -0
  134. data/test/data/data_set_test.rb +92 -0
  135. metadata +252 -128
  136. data/lib/ai4r/classifiers/classifier_helper.rb +0 -54
  137. data/site/src/documentation/content/xdocs/forum.html +0 -9
  138. data/site/src/documentation/resources/images/Thumbs.db +0 -0
  139. data/site/src/documentation/resources/images/sub-dir/Thumbs.db +0 -0
@@ -0,0 +1,591 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2
+ <html>
3
+ <head>
4
+ <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
+ <meta content="Apache Forrest" name="Generator">
6
+ <meta name="Forrest-version" content="0.8">
7
+ <meta name="Forrest-skin-name" content="pelt">
8
+ <title>Genetics Algorithms in Ruby :: ai4r</title>
9
+ <link type="text/css" href="skin/basic.css" rel="stylesheet">
10
+ <link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
11
+ <link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
12
+ <link type="text/css" href="skin/profile.css" rel="stylesheet">
13
+ <script src="skin/getBlank.js" language="javascript" type="text/javascript"></script><script src="skin/getMenu.js" language="javascript" type="text/javascript"></script><script src="skin/fontsize.js" language="javascript" type="text/javascript"></script>
14
+ <link rel="shortcut icon" href="">
15
+ </head>
16
+ <body onload="init()">
17
+ <script type="text/javascript">ndeSetTextSize();</script>
18
+ <div id="top">
19
+ <!--+
20
+ |header
21
+ +-->
22
+ <div class="header">
23
+ <!--+
24
+ |start group logo
25
+ +-->
26
+ <div class="grouplogo">
27
+ <a href="http://www.jadeferret.com"><img class="logoImage" alt="Jade Ferret" src="images/jadeferret.png" title="Jade Ferret"></a>
28
+ </div>
29
+ <!--+
30
+ |end group logo
31
+ +-->
32
+ <!--+
33
+ |start Project Logo
34
+ +-->
35
+ <div class="projectlogo">
36
+ <a href="http://ai4r.rubyforge.org/"><img class="logoImage" alt="ai4r" src="images/ai4r-logo.png" title="AI4R - Artificial Intelligence for Ruby"></a>
37
+ </div>
38
+ <!--+
39
+ |end Project Logo
40
+ +-->
41
+ <!--+
42
+ |start Search
43
+ +-->
44
+ <div class="searchbox">
45
+ <div class="roundtopsmall">
46
+ <form target="_top" action="http://www.google.com/custom" method="get">
47
+ <input value="ai4r.rubyforge.org;raa.ruby-lang.org;rubyforge.org" name="domains" type="hidden"><label style="display: none" for="sbi">Enter your search terms</label><input id="sbi" value="" maxlength="255" size="31" name="q" type="text"><label style="display: none" for="sbb">Submit search form</label><input id="sbb" value="Search" name="sa" type="submit"><span style="display:none"><input id="ss0" value="" name="sitesearch" style="display=none" type="radio"><input id="ss1" checked value="ai4r.rubyforge.org" name="sitesearch" style="display=none" type="radio"><input id="ss2" value="raa.ruby-lang.org" name="sitesearch" style="display=none" type="radio"><input id="ss3" value="rubyforge.org" name="sitesearch" style="display=none" type="radio"></span><input value="pub-2578934938711895" name="client" type="hidden"><input value="1" name="forid" type="hidden"><input value="ISO-8859-1" name="ie" type="hidden"><input value="ISO-8859-1" name="oe" type="hidden"><input value="active" name="safe" type="hidden"><input value="0000" name="flav" type="hidden"><input value="XEZZoGuMPMQqvUG_" name="sig" type="hidden"><input value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;LH:50;LW:78;L:http://ai4r.rubyforge.org/images/ai4r-logo.png;S:http://ai4r.rubyforge.org/;FORID:1" name="cof" type="hidden"><input value="en" name="hl" type="hidden">
48
+ </form>
49
+ </div>
50
+ </div>
51
+ <!--+
52
+ |end search
53
+ +-->
54
+ <!--+
55
+ |start Tabs
56
+ +-->
57
+ <ul id="tabs">
58
+ <li class="current">
59
+ <a class="selected" href="index.html">Home</a>
60
+ </li>
61
+ </ul>
62
+ <!--+
63
+ |end Tabs
64
+ +-->
65
+ </div>
66
+ </div>
67
+ <div id="main">
68
+ <div id="publishedStrip">
69
+ <!--+
70
+ |start Subtabs
71
+ +-->
72
+ <div id="level2tabs"></div>
73
+ <!--+
74
+ |end Endtabs
75
+ +-->
76
+ <script type="text/javascript"><!--
77
+ document.write("Last Published: " + document.lastModified);
78
+ // --></script>
79
+ </div>
80
+ <!--+
81
+ |breadtrail
82
+ +-->
83
+ <div class="breadtrail">
84
+
85
+ &nbsp;
86
+ </div>
87
+ <!--+
88
+ |start Menu, mainarea
89
+ +-->
90
+ <!--+
91
+ |start Menu
92
+ +-->
93
+ <div id="menu">
94
+ <div onclick="SwitchMenu('menu_selected_1.1', 'skin/')" id="menu_selected_1.1Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Home</div>
95
+ <div id="menu_selected_1.1" class="selectedmenuitemgroup" style="display: block;">
96
+ <div class="menuitem">
97
+ <a href="index.html" title="ai4r - Artificial Intelligence for Ruby">Index</a>
98
+ </div>
99
+ <div onclick="SwitchMenu('menu_selected_1.1.2', 'skin/')" id="menu_selected_1.1.2Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Practical Examples</div>
100
+ <div id="menu_selected_1.1.2" class="selectedmenuitemgroup" style="display: block;">
101
+ <div class="menupage">
102
+ <div class="menupagetitle">Genetic Algorithms</div>
103
+ </div>
104
+ <div class="menuitem">
105
+ <a href="machineLearning.html" title="ID3 Decision Trees in ruby">Machine Learning</a>
106
+ </div>
107
+ <div class="menuitem">
108
+ <a href="neuralNetworks.html" title="Backpropagation Neural Network in ruby">Neural Networks</a>
109
+ </div>
110
+ </div>
111
+ <div class="menuitem">
112
+ <a href="downloads.html" title="ai4r - Download Files">Downloads</a>
113
+ </div>
114
+ <div class="menuitem">
115
+ <a href="http://forum.jadeferret.com/viewforum.php?f=3" title="Ai4r Forum">Forum</a>
116
+ </div>
117
+ <div class="menuitem">
118
+ <a href="http://ai4r.rubyforge.org/rdoc/index.html" title="Ai4 RDoc docs">API doc (Rdoc)</a>
119
+ </div>
120
+ <div class="menuitem">
121
+ <a href="svn.html" title="ai4r Subversion repository">Source Code repository</a>
122
+ </div>
123
+ <div class="menuitem">
124
+ <a href="wholesite.pdf">ai4r project PDF</a>
125
+ </div>
126
+ </div>
127
+ <div id="credit">
128
+ <script type="text/javascript">
129
+ google_ad_client = "pub-2578934938711895";
130
+ google_ad_slot = "6111091580";
131
+ google_ad_width = 125;
132
+ google_ad_height = 125;
133
+ </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
134
+ <hr>
135
+ </div>
136
+ <div id="roundbottom">
137
+ <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
138
+ <!--+
139
+ |alternative credits
140
+ +-->
141
+ <div id="credit2"></div>
142
+ </div>
143
+ <!--+
144
+ |end Menu
145
+ +-->
146
+ <!--+
147
+ |start content
148
+ +-->
149
+ <div id="content">
150
+ <div title="Portable Document Format" class="pdflink">
151
+ <a class="dida" href="geneticAlgorithms.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
152
+ PDF</a>
153
+ </div>
154
+ <h1>Genetics Algorithms in Ruby :: ai4r</h1>
155
+ <div id="minitoc-area">
156
+ <ul class="minitoc">
157
+ <li>
158
+ <a href="#Example">The European Rock Tour Problem (Also known as the Travelling salesman problem)</a>
159
+ </li>
160
+ <li>
161
+ <a href="#Introduction">Introduction to Genetics Algorithms in Ruby</a>
162
+ </li>
163
+ <li>
164
+ <a href="#chromosome-impl">Implementation of Chromosome class for the Travelling salesman problem</a>
165
+ <ul class="minitoc">
166
+ <li>
167
+ <a href="#chromosome-impl-data">Data representation</a>
168
+ </li>
169
+ <li>
170
+ <a href="#chromosome-impl-fitness">Fitness function</a>
171
+ </li>
172
+ <li>
173
+ <a href="#chromosome-impl-reproduce">Reproduce function</a>
174
+ </li>
175
+ <li>
176
+ <a href="#chromosome-impl-mutation">Mutation function</a>
177
+ </li>
178
+ <li>
179
+ <a href="#chromosome-impl-seed">Seed function</a>
180
+ </li>
181
+ </ul>
182
+ </li>
183
+ <li>
184
+ <a href="#search-impl">Implementation of GeneticSearch</a>
185
+ <ul class="minitoc">
186
+ <li>
187
+ <a href="#search-impl-init">Initialize the search</a>
188
+ </li>
189
+ <li>
190
+ <a href="#search-impl-run">Run method</a>
191
+ </li>
192
+ <li>
193
+ <a href="#search-impl-selection">Selection</a>
194
+ </li>
195
+ <li>
196
+ <a href="#search-impl-reproduction">Reproduction</a>
197
+ </li>
198
+ </ul>
199
+ </li>
200
+ <li>
201
+ <a href="#how-to-run">How to use AI4R Genetic Search implementation</a>
202
+ </li>
203
+ <li>
204
+ <a href="#example-run">How to run the example</a>
205
+ </li>
206
+ <li>
207
+ <a href="#Results">Results of using Genetic Algorithms to the The European Rock Tour Problem (or Travelling salesman problem)</a>
208
+ </li>
209
+ <li>
210
+ <a href="#more-genetic-run">More about Genetic Algorithms and the Travelling salesman problem</a>
211
+ </li>
212
+ </ul>
213
+ </div>
214
+
215
+ <a name="N1000C"></a><a name="Example"></a>
216
+ <h2 class="boxed">The European Rock Tour Problem (Also known as the Travelling salesman problem)</h2>
217
+ <div class="section">
218
+ <p>An ageing rock band was planning its (hopefully) last european tour. They were planning to visite 15 european cities: Barcelona, Berlin, Brussels, Dublin, Hamburg, Kiev, London, Madrid, Milan, Moscow, Munich, Paris, Rome, Vienna, and Warsaw.</p>
219
+ <p>
220
+ <img alt="European Tour" src="images/europe2.png"></p>
221
+ <p>They start planning the trip, when they realize that they could save a lot of money, if they ordered the cities to minimize the traveling cost. So they decided to try all possible combinations. The sat in front of the computer, visited they favorite traveling site, and started typing. 53 hours and several liters of coffee later, they realized it was a little bit more complicated than what they expected. They called their drummer (who was on vacations) and explained the problem to him. Fortunately, their drummer had a Master in Computer Science degree.</p>
222
+ <p>
223
+ <em><strong>Drummer</strong> &ndash; Boys, if you continue, you will have to try 1,307,674,368,000 combinations. You are in front of a NP Problem.</em>
224
+
225
+ </p>
226
+ <p>
227
+ <em><strong>Band member #1</strong> &ndash; Oh man! So it is going to take us all day!</em>
228
+
229
+ </p>
230
+ <p>
231
+ <em><strong>Band member #2</strong> &ndash; And maybe more, 'cause this internet connection sucks...</em>
232
+
233
+ </p>
234
+ <p>
235
+ <em><strong>Drummer</strong> &ndash; err... yes, it would take a while. But don't worry, I am sure we can get to a good solution using stochastic search algorithms applied to this problem..</em>
236
+
237
+ </p>
238
+ <p>
239
+ <em><strong>Band</strong> &ndash; (Silence)</em>
240
+
241
+ </p>
242
+ <p>
243
+ <em><strong>Drummer</strong> &ndash; .. that is, we are going to move from solution to solution in the space of candidate solutions, using techniques similar to what nature use for evolution, these are known as genetic algorithms.</em>
244
+
245
+ </p>
246
+ <p>
247
+ <em><strong>Band</strong> &ndash; (Silence)</em>
248
+
249
+ </p>
250
+ <p>
251
+ <em><strong>Drummer</strong> - ... What I mean is, we will pick some of them randomly, leave the ugly ones behind, and mate with the good looking ones...</em>
252
+
253
+ </p>
254
+ <p>
255
+ <em><strong>Band</strong> &ndash; YEAH! THAT'S THE MAN! LET'S DO IT!</em>
256
+
257
+ </p>
258
+ <p>I forgot to tell another restriction of this problem: This band is really bad (What did you expect? Their drummer is a computer geek!) so once they visited a city, they cannot go back there.
259
+ </p>
260
+ </div>
261
+
262
+
263
+ <a name="N10060"></a><a name="Introduction"></a>
264
+ <h2 class="boxed">Introduction to Genetics Algorithms in Ruby</h2>
265
+ <div class="section">
266
+ <p>A Genetic Algorithm is a particular class of evolutionary algorithm and stochastic search.
267
+ It aims to find the best possible solution in a solution domain, by selecting a simple
268
+ set of solutions (chosed randomly or with a simple heuristic), and making it "evolve".
269
+ </p>
270
+ <p>It works based on the following pseudocode:</p>
271
+ <ol>
272
+
273
+ <li>Choose initial population</li>
274
+
275
+ <li>Evaluate the fitness of each individual in the population</li>
276
+
277
+ <li>Repeat as many times as generations we allow
278
+ <ol>
279
+
280
+ <li>Select randomly best-ranking individuals to reproduce</li>
281
+
282
+ <li>Breed new generation through crossover and mutation (genetic operations) and give birth to offspring</li>
283
+
284
+ <li>Evaluate the individual fitnesses of the offspring</li>
285
+
286
+ <li>Replace worst ranked part of population with offspring</li>
287
+
288
+ </ol>
289
+
290
+ </li>
291
+
292
+ </ol>
293
+ </div>
294
+
295
+
296
+ <a name="N10088"></a><a name="chromosome-impl"></a>
297
+ <h2 class="boxed">Implementation of Chromosome class for the Travelling salesman problem</h2>
298
+ <div class="section">
299
+ <p>In AI4R, the GeneticAlgorithm module implements the GeneticSearch and Chromosome classes.
300
+ GeneticSearch is a generic class, and can be used to solved any kind of problems.
301
+ The GeneticSearch class performs a stochastic search following the algorithm mentioned in the
302
+ previous section.
303
+ </p>
304
+ <p>
305
+ However, the Chromosome class implementation is problem specific.
306
+ Ai4r built-in Chromosomeclass was designed to model the
307
+ <a class="external" href="http://en.wikipedia.org/wiki/Traveling_salesman_problem">
308
+ Travelling salesman problem.
309
+ </a>
310
+ You have to provide a matrix with the cost of traveling from one point to another
311
+ (array of arrays of float values).
312
+ If you want to solve other type of problem, you will have to modify the Chromosome class,
313
+ by overwriting its fitness, reproduce, and mutate functions, to model you specific problem.
314
+ </p>
315
+ <a name="N10099"></a><a name="chromosome-impl-data"></a>
316
+ <h3 class="boxed">Data representation</h3>
317
+ <p>Each chromosome must represent a posible solution for the problem. This class conatins an array
318
+ with the list of visited nodes (cities of the tour). The size of the tour is obtained automatically from the traveling costs matrix. You have to assign the costs matrix BEFORE you run the genetic search. The following costs matrix could be used to solve the problem with only 3 cities:</p>
319
+ <pre class="code">
320
+
321
+ data_set = [ [ 0, 10, 5],
322
+ [ 6, 0, 4],
323
+ [25, 4, 0]
324
+ ]
325
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
326
+
327
+ </pre>
328
+ <a name="N100A7"></a><a name="chromosome-impl-fitness"></a>
329
+ <h3 class="boxed">Fitness function</h3>
330
+ <p>The fitness function quantifies the optimality of a solution (that is, a chromosome) in a genetic algorithm so that that particular chromosome may be ranked against all the other chromosomes.
331
+ Optimal chromosomes, or at least chromosomes which are more optimal, are allowed to breed and mix their datasets by any of several techniques, producing a new generation that will (hopefully) be even better.</p>
332
+ <p>The fitness function will return the complete tour cost represented by the chromosome, multiplied by -1.
333
+ For example:</p>
334
+ <pre class="code">
335
+
336
+ data_set = [ [ 0, 10, 5],
337
+ [ 6, 0, 4],
338
+ [25, 4, 0]
339
+ ]
340
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
341
+ chromosome = Ai4r::GeneticAlgorithm::Chromosome.new([0, 2, 1])
342
+ chromosome.fitness
343
+ # =&gt; -9
344
+
345
+ </pre>
346
+ <p>That is: From 0 to 2 costs 5. From 2 to 1 costs 4. Total cost is 9.</p>
347
+ <a name="N100BB"></a><a name="chromosome-impl-reproduce"></a>
348
+ <h3 class="boxed">Reproduce function</h3>
349
+ <p>Reproduction is used to vary the programming of a chromosome or chromosomes from one generation to the next. There are several ways to combine two chromosomes: One-point crossover, Two-point crossover, "Cut and splice", edge recombination, and more. The method is usually dependant of the problem domain. In this case, we have implemented edge recombination, wich is the most used reproduction algorithm for the Travelling salesman problem. The edge recombination operator (ERO) is an operator that creates a path that is similar to a set of existing paths (parents) by looking at the edges rather than the vertices.</p>
350
+ <p>
351
+ <img alt="Edge recombination" src="images/ero.gif"></p>
352
+ <p>The previous image was taken from the wikipedia, so hail to the author: Koala man (not me).</p>
353
+ <a name="N100CD"></a><a name="chromosome-impl-mutation"></a>
354
+ <h3 class="boxed">Mutation function</h3>
355
+ <p>Mutation funtion will be called fro every member of the population, on each generations. But you do not want to mutate your chromosomes every time, specialy if the are very fit. This is how it is currently implemented: With a probabilty of changing inversely proportional to its fitness, we swap 2 consecutive random nodes.</p>
356
+ <pre class="code">
357
+
358
+ def self.mutate(chromosome)
359
+ if chromosome.normalized_fitness &amp;&amp; rand &lt; ((1 - chromosome.normalized_fitness) * 0.3)
360
+ data = chromosome.data
361
+ index = rand(data.length-1)
362
+ data[index], data[index+1] = data[index+1], data[index]
363
+ chromosome.data = data
364
+ @fitness = nil
365
+ end
366
+ end
367
+
368
+ </pre>
369
+ <a name="N100DB"></a><a name="chromosome-impl-seed"></a>
370
+ <h3 class="boxed">Seed function</h3>
371
+ <p>Initializes an individual solution (chromosome) for the initial population. The built in seed function generates a chromosome randomly, but you can use some problem domain knowledge, to generate better initial solutions (although this not always deliver better results, it improves convergency times).</p>
372
+ <pre class="code">
373
+
374
+ def self.seed
375
+ data_size = @@costs[0].length
376
+ available = []
377
+ 0.upto(data_size-1) { |n| available &lt;&lt; n }
378
+ seed = []
379
+ while available.length &gt; 0 do
380
+ index = rand(available.length)
381
+ seed &lt;&lt; available.delete_at(index)
382
+ end
383
+ return Chromosome.new(seed)
384
+ end
385
+
386
+ </pre>
387
+ </div>
388
+
389
+
390
+ <a name="N100EA"></a><a name="search-impl"></a>
391
+ <h2 class="boxed">Implementation of GeneticSearch</h2>
392
+ <div class="section">
393
+ <p>The GeneticSearch class is an generic class to try to solve any kind of problem using genetic algorithms. If you
394
+ want to model another type of problem, you will have to modify the Chromosome class, defining its fitness, mutate, and reproduce functions.</p>
395
+ <a name="N100F3"></a><a name="search-impl-init"></a>
396
+ <h3 class="boxed">Initialize the search</h3>
397
+ <p>You have to provide two parameters during instantiation: The initial population size, and the how many generations produce. Large numbers will usually converge to better results, while small numbers will have better performance.</p>
398
+ <pre class="code">
399
+
400
+ search = Ai4r::GeneticAlgorithm::GeneticSearch.new(10, 20)
401
+ result = search.run
402
+
403
+ </pre>
404
+ <a name="N10101"></a><a name="search-impl-run"></a>
405
+ <h3 class="boxed">Run method</h3>
406
+ <p>Once you initialize an instance of GeneticSearch class, you can perform the search executing the run method.
407
+ This method will:</p>
408
+ <ol>
409
+
410
+ <li>Choose initial population</li>
411
+
412
+ <li>Evaluate the fitness of each individual in the population</li>
413
+
414
+ <li>Repeat as many times as generations we allow
415
+ <ol>
416
+
417
+ <li>Select randomly the best-ranking individuals to reproduce</li>
418
+
419
+ <li>Breed new generation through crossover and mutation (genetic operations) and give birth to offspring</li>
420
+
421
+ <li>Evaluate the individual fitnesses of the offspring</li>
422
+
423
+ <li>Replace worst ranked part of population with offspring</li>
424
+
425
+ </ol>
426
+
427
+ </li>
428
+
429
+ </ol>
430
+ <a name="N10126"></a><a name="search-impl-selection"></a>
431
+ <h3 class="boxed">Selection</h3>
432
+ <p>Selection is the stage of a genetic algorithm in which individual genomes are chosen from a population for later breeding. There are several generic selection algorithms, such as tournament selection and roulette wheel selection. We implemented the latest.</p>
433
+ <ol>
434
+
435
+ <li>The fitness function is evaluated for each individual, providing fitness values</li>
436
+
437
+ <li>The population is sorted by descending fitness values.</li>
438
+
439
+ <li>The fitness values ar then normalized. (Highest fitness gets 1, lowest fitness gets 0). The normalized value is stored in the "normalized_fitness" attribute of the chromosomes.</li>
440
+
441
+ <li> A random number R is chosen. R is between 0 and the accumulated normalized value (all the normalized fitness values added togheter).</li>
442
+
443
+ <li>The selected individual is the first one whose accumulated normalized value (its is normalized value plus the normalized values of the chromosomes prior it) greater than R.</li>
444
+
445
+ <li>We repeat steps 4 and 5, 2/3 times the population size.</li>
446
+
447
+ </ol>
448
+ <p>
449
+ <img alt="Edge recombination" src="images/fitness.png"></p>
450
+ <p>The previous image was taken from the wikipedia, so hail to the author: Simon Hatthon.</p>
451
+ <a name="N1014D"></a><a name="search-impl-reproduction"></a>
452
+ <h3 class="boxed">Reproduction</h3>
453
+ <p>The reproduction function combines each pair of selected chromosomes using the method Chromosome.reproduce.</p>
454
+ <p>The reproduction will also call the Chromosome.mutate method with each member of the population. You should implement Chromosome.mutate to only change (mutate) randomly. E.g. You could effectivly change the chromosome only if: </p>
455
+ <pre class="code">
456
+
457
+ rand &lt; ((1 - chromosome.normalized_fitness) * 0.4)
458
+
459
+ </pre>
460
+ </div>
461
+
462
+ <a name="N1015F"></a><a name="how-to-run"></a>
463
+ <h2 class="boxed">How to use AI4R Genetic Search implementation</h2>
464
+ <div class="section">
465
+ <pre class="code">
466
+
467
+ #Cost of traveling from one point to another. E.g. Travelling from Node 0 to Node 2 costs 5.
468
+ data_set = [ [ 0, 10, 5],
469
+ [ 6, 0, 4],
470
+ [25, 4, 0]
471
+ ]
472
+
473
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
474
+
475
+ search = Ai4r::GeneticAlgorithm::GeneticSearch.new(10, 20)
476
+ result = search.run
477
+ puts "Result cost: #{result.fitness}"
478
+ puts "Result nodes: #{result.data.inspect}"
479
+
480
+ </pre>
481
+ </div>
482
+
483
+
484
+ <a name="N1016A"></a><a name="example-run"></a>
485
+ <h2 class="boxed">How to run the example</h2>
486
+ <div class="section">
487
+ <p>You can run the example with "ruby genetic_algorithm_example.rb". The genetic_algorithm_example.rb file
488
+ contains:</p>
489
+ <pre class="code">
490
+
491
+ require "rubygems"
492
+ require "ai4r/genetic_algorithm/genetic_algorithm"
493
+ require "csv"
494
+
495
+ # Load data from data_set.csv
496
+ data_set = []
497
+ CSV::Reader.parse(File.open("#{File.dirname(__FILE__)}/travel_cost.csv", 'r')) do |row|
498
+ data_set &lt;&lt; row
499
+ end
500
+ data_labels = data_set.shift
501
+ data_set.collect! do |column|
502
+ column.collect { |element| element.to_f}
503
+ end
504
+
505
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
506
+
507
+ puts "Beginning genetic search, please wait... "
508
+ search = Ai4r::GeneticAlgorithm::GeneticSearch.new(800, 100)
509
+ result = search.run
510
+ puts "Result cost: #{result.fitness}"
511
+ puts "Result tour: "
512
+ result.data.each { |c| print " #{data_labels[c]}"}
513
+
514
+ </pre>
515
+ </div>
516
+
517
+
518
+ <a name="N10178"></a><a name="Results"></a>
519
+ <h2 class="boxed">Results of using Genetic Algorithms to the The European Rock Tour Problem (or Travelling salesman problem)</h2>
520
+ <div class="section">
521
+ <p>The cost of 3 randomly selected tours:</p>
522
+ <ul>
523
+
524
+ <li>$17486.01 : Madrid Vienna Moscow Berlin Brussels Munich Milan Barcelona London Hamburg Warsaw Dublin Kiev Paris Rome</li>
525
+
526
+ <li>$20198.92 : London Rome Brussels Kiev Hamburg Warsaw Barcelona Paris Munich Dublin Vienna Moscow Madrid Milan Berlin</li>
527
+
528
+ <li>$17799.34 : Madrid Milan Kiev Vienna Warsaw London Barcelona Hamburg Paris Munich Dublin Berlin Moscow Rome Brussels</li>
529
+
530
+ </ul>
531
+ <p>3 tours obtained with an initial population of 800, and after 100 generations:</p>
532
+ <ul>
533
+
534
+ <li>$7611.99 : Moscow Kiev Warsaw Hamburg Berlin Munich Vienna Milan Rome Barcelona Madrid Paris Brussels London Dublin</li>
535
+
536
+ <li>$7596.74 : Moscow Kiev Warsaw Berlin Hamburg Munich Vienna Milan Rome Barcelona Madrid Paris Brussels London Dublin (See Image)</li>
537
+
538
+ <li>$7641.61 : Madrid Barcelona Rome Milan Paris Dublin London Brussels Hamburg Berlin Vienna Munich Warsaw Kiev Moscow</li>
539
+
540
+ </ul>
541
+ <p>
542
+ <img alt="Best tour result using Genetic Algorithms in ruby" src="images/europe3.png"></p>
543
+ <p>The GeneticSearch class is an generic class to try to solve any kind of problem using genetic algorithms. If you
544
+ want to model another type of problem, you will have to modify the Chromosome class, defining its fitness, mutate, and reproduce functions.</p>
545
+ </div>
546
+
547
+
548
+ <a name="N101A5"></a><a name="more-genetic-run"></a>
549
+ <h2 class="boxed">More about Genetic Algorithms and the Travelling salesman problem</h2>
550
+ <div class="section">
551
+ <p>
552
+ <a class="external" href="http://en.wikipedia.org/wiki/Traveling_salesman_problem">Travelling salesman problem at Wikipedia</a>
553
+ </p>
554
+ <p>
555
+ <a class="external" href="http://en.wikipedia.org/wiki/Genetic_algorithm">Genetic Algorithms at Wikipedia</a>
556
+ </p>
557
+ </div>
558
+
559
+ </div>
560
+ <!--+
561
+ |end content
562
+ +-->
563
+ <div class="clearboth">&nbsp;</div>
564
+ </div>
565
+ <div id="footer">
566
+ <!--+
567
+ |start bottomstrip
568
+ +-->
569
+ <div class="lastmodified">
570
+ <script type="text/javascript"><!--
571
+ document.write("Last Published: " + document.lastModified);
572
+ // --></script>
573
+ </div>
574
+ <div class="copyright">
575
+ Copyright &copy;
576
+ 2007-2009 <a href="http://www.jadeferret.com/">Sergio Fierens, Jade Ferret</a>
577
+ </div>
578
+ <div class="host">
579
+ <a href="http://rubyforge.org/projects/ai4r/"><img class="logoImage" alt="" src="images/rubyforge.png"></a>
580
+ </div>
581
+ <!--+
582
+ |end bottomstrip
583
+ +-->
584
+ </div>
585
+ <script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script><script type="text/javascript">
586
+ var pageTracker = _gat._getTracker("UA-3234625-1");
587
+ pageTracker._initData();
588
+ pageTracker._trackPageview();
589
+ </script>
590
+ </body>
591
+ </html>