ai4r 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (196) hide show
  1. data/README.rdoc +48 -0
  2. data/examples/decision_trees/data_set.csv +121 -0
  3. data/examples/decision_trees/id3_example.rb +31 -0
  4. data/examples/decision_trees/results.txt +29 -0
  5. data/examples/genetic_algorithm/genetic_algorithm_example.rb +39 -0
  6. data/examples/genetic_algorithm/travel_cost.csv +16 -0
  7. data/examples/neural_network/backpropagation_example.rb +65 -0
  8. data/examples/neural_network/patterns_with_base_noise.rb +68 -0
  9. data/examples/neural_network/patterns_with_noise.rb +66 -0
  10. data/examples/neural_network/training_patterns.rb +68 -0
  11. data/lib/decision_tree/id3.rb +354 -0
  12. data/lib/genetic_algorithm/genetic_algorithm.rb +268 -0
  13. data/lib/neural_network/backpropagation.rb +259 -0
  14. data/site/build/site/en/broken-links.xml +2 -0
  15. data/site/build/site/en/downloads.html +187 -0
  16. data/site/build/site/en/downloads.pdf +151 -0
  17. data/site/build/site/en/geneticAlgorithms.html +564 -0
  18. data/site/build/site/en/geneticAlgorithms.pdf +911 -0
  19. data/site/build/site/en/images/ai4r-logo.png +0 -0
  20. data/site/build/site/en/images/built-with-forrest-button.png +0 -0
  21. data/site/build/site/en/images/c.png +0 -0
  22. data/site/build/site/en/images/c_wbn.png +0 -0
  23. data/site/build/site/en/images/c_wn.png +0 -0
  24. data/site/build/site/en/images/ero.gif +0 -0
  25. data/site/build/site/en/images/europe2.png +0 -0
  26. data/site/build/site/en/images/europe3.png +0 -0
  27. data/site/build/site/en/images/fitness.png +0 -0
  28. data/site/build/site/en/images/instruction_arrow.png +0 -0
  29. data/site/build/site/en/images/my_email.png +0 -0
  30. data/site/build/site/en/images/rubyforge.png +0 -0
  31. data/site/build/site/en/images/s.png +0 -0
  32. data/site/build/site/en/images/s_wbn.png +0 -0
  33. data/site/build/site/en/images/s_wn.png +0 -0
  34. data/site/build/site/en/images/sigmoid.png +0 -0
  35. data/site/build/site/en/images/t.png +0 -0
  36. data/site/build/site/en/images/t_wbn.png +0 -0
  37. data/site/build/site/en/images/t_wn.png +0 -0
  38. data/site/build/site/en/index.html +258 -0
  39. data/site/build/site/en/index.pdf +306 -0
  40. data/site/build/site/en/linkmap.html +231 -0
  41. data/site/build/site/en/linkmap.pdf +94 -0
  42. data/site/build/site/en/locationmap.xml +72 -0
  43. data/site/build/site/en/machineLearning.html +325 -0
  44. data/site/build/site/en/machineLearning.pdf +337 -0
  45. data/site/build/site/en/neuralNetworks.html +446 -0
  46. data/site/build/site/en/neuralNetworks.pdf +604 -0
  47. data/site/build/site/en/skin/CommonMessages_de.xml +23 -0
  48. data/site/build/site/en/skin/CommonMessages_en_US.xml +23 -0
  49. data/site/build/site/en/skin/CommonMessages_es.xml +23 -0
  50. data/site/build/site/en/skin/CommonMessages_fr.xml +23 -0
  51. data/site/build/site/en/skin/basic.css +166 -0
  52. data/site/build/site/en/skin/breadcrumbs-optimized.js +90 -0
  53. data/site/build/site/en/skin/breadcrumbs.js +237 -0
  54. data/site/build/site/en/skin/fontsize.js +166 -0
  55. data/site/build/site/en/skin/getBlank.js +40 -0
  56. data/site/build/site/en/skin/getMenu.js +45 -0
  57. data/site/build/site/en/skin/images/README.txt +1 -0
  58. data/site/build/site/en/skin/images/add.jpg +0 -0
  59. data/site/build/site/en/skin/images/built-with-forrest-button.png +0 -0
  60. data/site/build/site/en/skin/images/chapter.gif +0 -0
  61. data/site/build/site/en/skin/images/chapter_open.gif +0 -0
  62. data/site/build/site/en/skin/images/current.gif +0 -0
  63. data/site/build/site/en/skin/images/error.png +0 -0
  64. data/site/build/site/en/skin/images/external-link.gif +0 -0
  65. data/site/build/site/en/skin/images/fix.jpg +0 -0
  66. data/site/build/site/en/skin/images/forrest-credit-logo.png +0 -0
  67. data/site/build/site/en/skin/images/hack.jpg +0 -0
  68. data/site/build/site/en/skin/images/header_white_line.gif +0 -0
  69. data/site/build/site/en/skin/images/info.png +0 -0
  70. data/site/build/site/en/skin/images/instruction_arrow.png +0 -0
  71. data/site/build/site/en/skin/images/label.gif +0 -0
  72. data/site/build/site/en/skin/images/page.gif +0 -0
  73. data/site/build/site/en/skin/images/pdfdoc.gif +0 -0
  74. data/site/build/site/en/skin/images/poddoc.png +0 -0
  75. data/site/build/site/en/skin/images/printer.gif +0 -0
  76. data/site/build/site/en/skin/images/rc-b-l-15-1body-2menu-3menu.png +0 -0
  77. data/site/build/site/en/skin/images/rc-b-r-15-1body-2menu-3menu.png +0 -0
  78. data/site/build/site/en/skin/images/rc-b-r-5-1header-2tab-selected-3tab-selected.png +0 -0
  79. data/site/build/site/en/skin/images/rc-t-l-5-1header-2searchbox-3searchbox.png +0 -0
  80. data/site/build/site/en/skin/images/rc-t-l-5-1header-2tab-selected-3tab-selected.png +0 -0
  81. data/site/build/site/en/skin/images/rc-t-l-5-1header-2tab-unselected-3tab-unselected.png +0 -0
  82. data/site/build/site/en/skin/images/rc-t-r-15-1body-2menu-3menu.png +0 -0
  83. data/site/build/site/en/skin/images/rc-t-r-5-1header-2searchbox-3searchbox.png +0 -0
  84. data/site/build/site/en/skin/images/rc-t-r-5-1header-2tab-selected-3tab-selected.png +0 -0
  85. data/site/build/site/en/skin/images/rc-t-r-5-1header-2tab-unselected-3tab-unselected.png +0 -0
  86. data/site/build/site/en/skin/images/remove.jpg +0 -0
  87. data/site/build/site/en/skin/images/rss.png +0 -0
  88. data/site/build/site/en/skin/images/spacer.gif +0 -0
  89. data/site/build/site/en/skin/images/success.png +0 -0
  90. data/site/build/site/en/skin/images/txtdoc.png +0 -0
  91. data/site/build/site/en/skin/images/update.jpg +0 -0
  92. data/site/build/site/en/skin/images/valid-html401.png +0 -0
  93. data/site/build/site/en/skin/images/vcss.png +0 -0
  94. data/site/build/site/en/skin/images/warning.png +0 -0
  95. data/site/build/site/en/skin/images/xmldoc.gif +0 -0
  96. data/site/build/site/en/skin/menu.js +48 -0
  97. data/site/build/site/en/skin/note.txt +50 -0
  98. data/site/build/site/en/skin/print.css +54 -0
  99. data/site/build/site/en/skin/profile.css +163 -0
  100. data/site/build/site/en/skin/prototype.js +1257 -0
  101. data/site/build/site/en/skin/screen.css +587 -0
  102. data/site/build/site/en/svn.html +223 -0
  103. data/site/build/site/en/svn.pdf +239 -0
  104. data/site/build/site/en/wholesite.pdf +1686 -0
  105. data/site/build/tmp/brokenlinks.xml +2 -0
  106. data/site/build/tmp/build-info.xml +5 -0
  107. data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.data +0 -0
  108. data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.index +0 -0
  109. data/site/build/tmp/input.xmap +32 -0
  110. data/site/build/tmp/internal.xmap +32 -0
  111. data/site/build/tmp/locationmap.xml +29 -0
  112. data/site/build/tmp/output.xmap +38 -0
  113. data/site/build/tmp/pluginlist2fetchbuild.xml +144 -0
  114. data/site/build/tmp/plugins-1.xml +212 -0
  115. data/site/build/tmp/plugins-2.xml +347 -0
  116. data/site/build/tmp/projfilters.properties +41 -0
  117. data/site/build/tmp/resources.xmap +32 -0
  118. data/site/build/webapp/WEB-INF/logs/access.log +0 -0
  119. data/site/build/webapp/WEB-INF/logs/core.log +788 -0
  120. data/site/build/webapp/WEB-INF/logs/debug.log +0 -0
  121. data/site/build/webapp/WEB-INF/logs/error.log +248 -0
  122. data/site/build/webapp/WEB-INF/logs/flow.log +0 -0
  123. data/site/build/webapp/WEB-INF/logs/idgen.log +0 -0
  124. data/site/build/webapp/WEB-INF/logs/linkrewriter.log +0 -0
  125. data/site/build/webapp/WEB-INF/logs/locationmap.log +0 -0
  126. data/site/build/webapp/WEB-INF/logs/sitemap.log +0 -0
  127. data/site/build/webapp/WEB-INF/logs/xmlform.log +0 -0
  128. data/site/forrest.properties +152 -0
  129. data/site/forrest.properties.dispatcher.properties +25 -0
  130. data/site/forrest.properties.xml +29 -0
  131. data/site/src/documentation/README.txt +7 -0
  132. data/site/src/documentation/classes/CatalogManager.properties +62 -0
  133. data/site/src/documentation/content/locationmap.xml +72 -0
  134. data/site/src/documentation/content/xdocs/downloads.html +9 -0
  135. data/site/src/documentation/content/xdocs/geneticAlgorithms.xml +280 -0
  136. data/site/src/documentation/content/xdocs/index.xml +73 -0
  137. data/site/src/documentation/content/xdocs/machineLearning.xml +129 -0
  138. data/site/src/documentation/content/xdocs/neuralNetworks.xml +218 -0
  139. data/site/src/documentation/content/xdocs/site.xml +51 -0
  140. data/site/src/documentation/content/xdocs/svn.xml +31 -0
  141. data/site/src/documentation/content/xdocs/tabs.xml +35 -0
  142. data/site/src/documentation/resources/images/ai4r-logo.png +0 -0
  143. data/site/src/documentation/resources/images/c.png +0 -0
  144. data/site/src/documentation/resources/images/c_wbn.png +0 -0
  145. data/site/src/documentation/resources/images/c_wn.png +0 -0
  146. data/site/src/documentation/resources/images/ellipse-2.svg +30 -0
  147. data/site/src/documentation/resources/images/ero.gif +0 -0
  148. data/site/src/documentation/resources/images/europe2.png +0 -0
  149. data/site/src/documentation/resources/images/europe3.png +0 -0
  150. data/site/src/documentation/resources/images/fitness.png +0 -0
  151. data/site/src/documentation/resources/images/icon-a.png +0 -0
  152. data/site/src/documentation/resources/images/icon-b.png +0 -0
  153. data/site/src/documentation/resources/images/icon.png +0 -0
  154. data/site/src/documentation/resources/images/my_email.png +0 -0
  155. data/site/src/documentation/resources/images/project-logo.png +0 -0
  156. data/site/src/documentation/resources/images/rubyforge.png +0 -0
  157. data/site/src/documentation/resources/images/s.png +0 -0
  158. data/site/src/documentation/resources/images/s_wbn.png +0 -0
  159. data/site/src/documentation/resources/images/s_wn.png +0 -0
  160. data/site/src/documentation/resources/images/sigmoid.png +0 -0
  161. data/site/src/documentation/resources/images/sub-dir/icon-c.png +0 -0
  162. data/site/src/documentation/resources/images/t.png +0 -0
  163. data/site/src/documentation/resources/images/t_wbn.png +0 -0
  164. data/site/src/documentation/resources/images/t_wn.png +0 -0
  165. data/site/src/documentation/resources/schema/catalog.xcat +29 -0
  166. data/site/src/documentation/resources/schema/hello-v10.dtd +51 -0
  167. data/site/src/documentation/resources/schema/symbols-project-v10.ent +26 -0
  168. data/site/src/documentation/resources/stylesheets/hello2document.xsl +33 -0
  169. data/site/src/documentation/sitemap.xmap +66 -0
  170. data/site/src/documentation/skinconf.xml +418 -0
  171. data/site/src/documentation/translations/langcode.xml +29 -0
  172. data/site/src/documentation/translations/languages_de.xml +24 -0
  173. data/site/src/documentation/translations/languages_en.xml +24 -0
  174. data/site/src/documentation/translations/languages_es.xml +22 -0
  175. data/site/src/documentation/translations/languages_fr.xml +24 -0
  176. data/site/src/documentation/translations/languages_nl.xml +24 -0
  177. data/site/src/documentation/translations/menu.xml +33 -0
  178. data/site/src/documentation/translations/menu_af.xml +33 -0
  179. data/site/src/documentation/translations/menu_de.xml +33 -0
  180. data/site/src/documentation/translations/menu_es.xml +33 -0
  181. data/site/src/documentation/translations/menu_fr.xml +33 -0
  182. data/site/src/documentation/translations/menu_it.xml +33 -0
  183. data/site/src/documentation/translations/menu_nl.xml +33 -0
  184. data/site/src/documentation/translations/menu_no.xml +33 -0
  185. data/site/src/documentation/translations/menu_ru.xml +33 -0
  186. data/site/src/documentation/translations/menu_sk.xml +33 -0
  187. data/site/src/documentation/translations/tabs.xml +22 -0
  188. data/site/src/documentation/translations/tabs_de.xml +22 -0
  189. data/site/src/documentation/translations/tabs_es.xml +22 -0
  190. data/site/src/documentation/translations/tabs_fr.xml +22 -0
  191. data/site/src/documentation/translations/tabs_nl.xml +22 -0
  192. data/test/decision_tree/id3_test.rb +209 -0
  193. data/test/genetic_algorithm/chromosome_test.rb +55 -0
  194. data/test/genetic_algorithm/genetic_algorithm_test.rb +78 -0
  195. data/test/neural_network/backpropagation_test.rb +44 -0
  196. metadata +274 -0
@@ -0,0 +1,7 @@
1
+ This is the base documentation directory.
2
+
3
+ skinconf.xml # This file customizes Forrest for your project. In it, you
4
+ # tell forrest the project name, logo, copyright info, etc
5
+
6
+ sitemap.xmap # Optional. This sitemap is consulted before all core sitemaps.
7
+ # See http://forrest.apache.org/docs/project-sitemap.html
@@ -0,0 +1,62 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright ownership.
4
+ # The ASF licenses this file to You under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with
6
+ # the License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ #=======================================================================
17
+ # CatalogManager.properties for Catalog Entity Resolver.
18
+ #
19
+ # This is the default properties file for your project.
20
+ # This facilitates local configuration of application-specific catalogs.
21
+ # If you have defined any local catalogs, then they will be loaded
22
+ # before Forrest's core catalogs.
23
+ #
24
+ # See the Apache Forrest documentation:
25
+ # http://forrest.apache.org/docs/your-project.html
26
+ # http://forrest.apache.org/docs/validation.html
27
+
28
+ # verbosity:
29
+ # The level of messages for status/debug (messages go to standard output).
30
+ # The setting here is for your own local catalogs.
31
+ # The verbosity of Forrest's core catalogs is controlled via
32
+ # main/webapp/WEB-INF/cocoon.xconf
33
+ #
34
+ # The following messages are provided ...
35
+ # 0 = none
36
+ # 1 = ? (... not sure yet)
37
+ # 2 = 1+, Loading catalog, Resolved public, Resolved system
38
+ # 3 = 2+, Catalog does not exist, resolvePublic, resolveSystem
39
+ # 10 = 3+, List all catalog entries when loading a catalog
40
+ # (Cocoon also logs the "Resolved public" messages.)
41
+ verbosity=1
42
+
43
+ # catalogs ... list of additional catalogs to load
44
+ # (Note that Apache Forrest will automatically load its own default catalog
45
+ # from main/webapp/resources/schema/catalog.xcat)
46
+ # Use either full pathnames or relative pathnames.
47
+ # pathname separator is always semi-colon (;) regardless of operating system
48
+ # directory separator is always slash (/) regardless of operating system
49
+ # The project catalog is expected to be at ../resources/schema/catalog.xcat
50
+ #catalogs=../resources/schema/catalog.xcat
51
+ # FIXME: Workaround FOR-548 "project DTD catalogs are not included
52
+ # when running as a servlet WAR".
53
+ # Same catalog, different path
54
+ catalogs=../resources/schema/catalog.xcat;../../project/src/documentation/resources/schema/catalog.xcat
55
+
56
+ # relative-catalogs
57
+ # If false, relative catalog URIs are made absolute with respect to the
58
+ # base URI of the CatalogManager.properties file. This setting only
59
+ # applies to catalog URIs obtained from the catalogs property in the
60
+ # CatalogManager.properties file
61
+ # Example: relative-catalogs=[yes|no]
62
+ relative-catalogs=no
@@ -0,0 +1,72 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ Licensed to the Apache Software Foundation (ASF) under one or more
4
+ contributor license agreements. See the NOTICE file distributed with
5
+ this work for additional information regarding copyright ownership.
6
+ The ASF licenses this file to You under the Apache License, Version 2.0
7
+ (the "License"); you may not use this file except in compliance with
8
+ the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ -->
18
+ <locationmap xmlns="http://apache.org/forrest/locationmap/1.0">
19
+ <components>
20
+ <matchers default="lm">
21
+ <matcher name="lm" src="org.apache.forrest.locationmap.WildcardLocationMapHintMatcher"/>
22
+ </matchers>
23
+ <!--
24
+ * Can contain any sitemap selector with the following syntax. *
25
+ <selectors default="exists">
26
+ <selector name="exists" logger="sitemap.selector.exists"
27
+ src="org.apache.forrest.sourceexists.SourceExistsSelector" />
28
+ </selectors>
29
+ -->
30
+ </components>
31
+ <!--
32
+ * Can contain a mount statement as a sibling to components and locator *
33
+ <mount src="somelocation.xml"/>
34
+ -->
35
+ <locator>
36
+ <!--
37
+ * Can contain a mount within a selector where a selector is valid.
38
+ <select>
39
+ <mount src="somelocation.xml"/>
40
+ </select>
41
+ -->
42
+ <match pattern="project.rewrite.**">
43
+ <location src="http://cocoon.apache.org/{1}.html"/>
44
+ </match>
45
+ <match pattern="project.remote.**.xml">
46
+ <location src="http://svn.apache.org/repos/asf/forrest/trunk/main/fresh-site/src/documentation/content/xdocs/{1}.xml"/>
47
+ </match>
48
+ <!--
49
+ * Can use a selector inside a match. *
50
+ <match pattern="somepattern/**">
51
+ <select>
52
+ <location src="first-location-attempted"/>
53
+ <location src="second-location-attempted"/>
54
+ <location src="third-location-attepted"/>
55
+ </select>
56
+ </match>
57
+ -->
58
+ <!--
59
+ To locate all your source documents in a slide repository you can do:
60
+
61
+ <match pattern="tabs.xml">
62
+ <location src="http://127.0.0.1:8080/slide/files/tabs.xml"/>
63
+ </match>
64
+ <match pattern="site.xml">
65
+ <location src="http://127.0.0.1:8080/slide/files/site.xml"/>
66
+ </match>
67
+ <match pattern="**.xml">
68
+ <location src="http://127.0.0.1:8080/slide/files/{1}.xml"/>
69
+ </match>
70
+ -->
71
+ </locator>
72
+ </locationmap>
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <head>
3
+ <title>Artificial intelligence for ruby :: ai4r :: download page at RubyForge</title>
4
+ </head>
5
+ <body>
6
+ <h1><a name="intro" />Artificial intelligence for ruby - ai4r - download page at RubyForge</h1>
7
+ <iframe width="100%" height="800px" frameborder="0" src="http://rubyforge.org/frs/?group_id=4942" />
8
+ </body>
9
+ </html>
@@ -0,0 +1,280 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
3
+ <document>
4
+ <header>
5
+ <title>Genetics Algorithms in Ruby :: ai4r</title>
6
+ </header>
7
+ <body>
8
+ <section id="Introduction">
9
+ <title>Introduction to Genetics Algorithms in Ruby</title>
10
+ <p>The GeneticAlgorithm module implements the GeneticSearch and Chromosome classes. The GeneticSearch is a generic class, and can be used to solved any kind of problems. The GeneticSearch class performs a stochastic search
11
+ of the solution of a given problem. It uses the following pseudocode:
12
+ </p>
13
+ <ol>
14
+ <li>Choose initial population</li>
15
+ <li>Evaluate the fitness of each individual in the population</li>
16
+ <li>Repeat as many times as generations we allow
17
+ <ol>
18
+ <li>Select randomly best-ranking individuals to reproduce</li>
19
+ <li>Breed new generation through crossover and mutation (genetic operations) and give birth to offspring</li>
20
+ <li>Evaluate the individual fitnesses of the offspring</li>
21
+ <li>Replace worst ranked part of population with offspring</li>
22
+ </ol>
23
+ </li>
24
+ </ol>
25
+ <p>The Chromosome is "problem specific". Ai4r built-in Chromosomeclass was designed to model the <a href="http://en.wikipedia.org/wiki/Traveling_salesman_problem" title="Link to Wikipedia">Travelling salesman problem</a>. You have to provide a matrix with the cost of traveling from one point to another (array of arrays of float values). If you want to solve other type of problem, you will have to modify the Chromosome class, by overwriting its fitness, reproduce, and mutate functions, to model you specific problem.</p>
26
+ </section>
27
+
28
+ <section id="how-to-run">
29
+ <title>How to use it</title>
30
+ <source>
31
+ <![CDATA[
32
+ #Cost of traveling from one point to another. E.g. Travelling from Node 0 to Node 2 costs 5.
33
+ data_set = [ [ 0, 10, 5],
34
+ [ 6, 0, 4],
35
+ [25, 4, 0]
36
+ ]
37
+
38
+ GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
39
+
40
+ search = GeneticAlgorithm::GeneticSearch.new(10, 20)
41
+ result = search.run
42
+ puts "Result cost: #{result.fitness}"
43
+ puts "Result nodes: #{result.data.inspect}"
44
+ ]]>
45
+ </source>
46
+ </section>
47
+
48
+ <section id="Example">
49
+ <title>The European Rock Tour Problem (Also known as the Travelling salesman problem)</title>
50
+ <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>
51
+ <p><img src="images/europe2.png" alt="European Tour" /></p>
52
+ <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>
53
+ <p><em><strong>Drummer</strong> – Boys, if you continue, you will have to try 1,307,674,368,000 combinations. You are in front of a NP Problem.</em>
54
+ </p>
55
+ <p><em><strong>Band member #1</strong> – Oh man! So it is going to take us all day!</em>
56
+ </p>
57
+ <p><em><strong>Band member #2</strong> – And maybe more, 'cause this internet connection sucks...</em>
58
+ </p>
59
+ <p><em><strong>Drummer</strong> – 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>
60
+ </p>
61
+ <p><em><strong>Band</strong> – (Silence)</em>
62
+ </p>
63
+ <p><em><strong>Drummer</strong> – .. 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>
64
+ </p>
65
+ <p><em><strong>Band</strong> – (Silence)</em>
66
+ </p>
67
+ <p><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>
68
+ </p>
69
+ <p><em><strong>Band</strong> – YEAH! THAT'S THE MAN! LET'S DO IT!</em>
70
+ </p>
71
+ <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.
72
+ </p>
73
+ </section>
74
+
75
+
76
+ <section id="Results">
77
+ <title>Results of using Genetic Algorithms to the The European Rock Tour Problem (or Travelling salesman problem)</title>
78
+ <p>The cost of 3 randomly selected tours:</p>
79
+ <ul>
80
+ <li>$17486.01 : Madrid Vienna Moscow Berlin Brussels Munich Milan Barcelona London Hamburg Warsaw Dublin Kiev Paris Rome</li>
81
+ <li>$20198.92 : London Rome Brussels Kiev Hamburg Warsaw Barcelona Paris Munich Dublin Vienna Moscow Madrid Milan Berlin</li>
82
+ <li>$17799.34 : Madrid Milan Kiev Vienna Warsaw London Barcelona Hamburg Paris Munich Dublin Berlin Moscow Rome Brussels</li>
83
+ </ul>
84
+ <p>3 tours obtained with an initial population of 800, and after 100 generations:</p>
85
+ <ul>
86
+ <li>$7611.99 : Moscow Kiev Warsaw Hamburg Berlin Munich Vienna Milan Rome Barcelona Madrid Paris Brussels London Dublin</li>
87
+ <li>$7596.74 : Moscow Kiev Warsaw Berlin Hamburg Munich Vienna Milan Rome Barcelona Madrid Paris Brussels London Dublin (See Image)</li>
88
+ <li>$7641.61 : Madrid Barcelona Rome Milan Paris Dublin London Brussels Hamburg Berlin Vienna Munich Warsaw Kiev Moscow</li>
89
+ </ul>
90
+ <p><img src="images/europe3.png" alt="Best tour result using Genetic Algorithms in ruby" /></p>
91
+ <p>The GeneticSearch class is an generic class to try to solve any kind of problem using genetic algorithms. If you
92
+ want to model another type of problem, you will have to modify the Chromosome class, defining its fitness, mutate, and reproduce functions.</p>
93
+ </section>
94
+
95
+ <section id="chromosome-impl">
96
+ <title>Implementation of Chromosome class for the Travelling salesman problem</title>
97
+ <p>Although the GeneticSearch class is an generic class to try to solve any kind of problem using genetic algorithms, the Chromosome class is problem specific.</p>
98
+ <section id="chromosome-impl-data">
99
+ <title>Data representation</title>
100
+ <p>Each chromosome must represent a posible solution for the problem. This class conatins an array
101
+ 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>
102
+ <source>
103
+ <![CDATA[
104
+ data_set = [ [ 0, 10, 5],
105
+ [ 6, 0, 4],
106
+ [25, 4, 0]
107
+ ]
108
+ GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
109
+ ]]>
110
+ </source>
111
+ </section>
112
+ <section id="chromosome-impl-fitness">
113
+ <title>Fitness function</title>
114
+ <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.
115
+ 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>
116
+ <p>The fitness function will return the complete tour cost represented by the chromosome, multiplied by -1.
117
+ For example:</p>
118
+
119
+ <source>
120
+ <![CDATA[
121
+ data_set = [ [ 0, 10, 5],
122
+ [ 6, 0, 4],
123
+ [25, 4, 0]
124
+ ]
125
+ GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
126
+ chromosome = GeneticAlgorithm::Chromosome.new([0, 2, 1])
127
+ chromosome.fitness
128
+ # => -9
129
+ ]]>
130
+ </source>
131
+ <p>That is: From 0 to 2 costs 5. From 2 to 1 costs 4. Total cost is 9.</p>
132
+ </section>
133
+ <section id="chromosome-impl-reproduce">
134
+ <title>Reproduce function</title>
135
+ <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>
136
+ <p><img src="images/ero.gif" alt="Edge recombination" /></p>
137
+ <p>The previous image was taken from the wikipedia, so hail to the author: Koala man (not me).</p>
138
+ </section>
139
+ <section id="chromosome-impl-mutation">
140
+ <title>Mutation function</title>
141
+ <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>
142
+ <source>
143
+ <![CDATA[
144
+ def self.mutate(chromosome)
145
+ if chromosome.normalized_fitness && rand < ((1 - chromosome.normalized_fitness) * 0.3)
146
+ data = chromosome.data
147
+ index = rand(data.length-1)
148
+ data[index], data[index+1] = data[index+1], data[index]
149
+ chromosome.data = data
150
+ @fitness = nil
151
+ end
152
+ end
153
+ ]]>
154
+ </source>
155
+ </section>
156
+ <section id="chromosome-impl-seed">
157
+ <title>Seed function</title>
158
+ <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>
159
+ <source>
160
+ <![CDATA[
161
+ def self.seed
162
+ data_size = @@costs[0].length
163
+ available = []
164
+ 0.upto(data_size-1) { |n| available << n }
165
+ seed = []
166
+ while available.length > 0 do
167
+ index = rand(available.length)
168
+ seed << available.delete_at(index)
169
+ end
170
+ return Chromosome.new(seed)
171
+ end
172
+ ]]>
173
+ </source>
174
+ </section>
175
+ </section>
176
+
177
+ <section id="search-impl">
178
+ <title>Implementation of GeneticSearch</title>
179
+ <p>The GeneticSearch class is an generic class to try to solve any kind of problem using genetic algorithms. If you
180
+ want to model another type of problem, you will have to modify the Chromosome class, defining its fitness, mutate, and reproduce functions.</p>
181
+
182
+ <section id="search-impl-init">
183
+
184
+ <title>Initialize the search</title>
185
+ <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>
186
+ <source>
187
+ <![CDATA[
188
+ search = GeneticAlgorithm::GeneticSearch.new(10, 20)
189
+ result = search.run
190
+ ]]>
191
+ </source>
192
+ </section>
193
+ <section id="search-impl-run">
194
+ <title>Run method</title>
195
+ <p>Once you initialize an instance of GeneticSearch class, you can perform the search executing the run method.
196
+ This method will:</p>
197
+ <ol>
198
+ <li>Choose initial population</li>
199
+ <li>Evaluate the fitness of each individual in the population</li>
200
+ <li>Repeat as many times as generations we allow
201
+ <ol>
202
+ <li>Select randomly the best-ranking individuals to reproduce</li>
203
+ <li>Breed new generation through crossover and mutation (genetic operations) and give birth to offspring</li>
204
+ <li>Evaluate the individual fitnesses of the offspring</li>
205
+ <li>Replace worst ranked part of population with offspring</li>
206
+ </ol>
207
+ </li>
208
+ </ol>
209
+ </section>
210
+
211
+ <section id="search-impl-selection">
212
+ <title>Selection</title>
213
+ <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>
214
+ <ol>
215
+ <li>The fitness function is evaluated for each individual, providing fitness values</li>
216
+ <li>The population is sorted by descending fitness values.</li>
217
+ <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>
218
+ <li> A random number R is chosen. R is between 0 and the accumulated normalized value (all the normalized fitness values added togheter).</li>
219
+ <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>
220
+ <li>We repeat steps 4 and 5, 2/3 times the population size.</li>
221
+ </ol>
222
+ <p><img src="images/fitness.png" alt="Edge recombination" /></p>
223
+ <p>The previous image was taken from the wikipedia, so hail to the author: Simon Hatthon.</p>
224
+ </section>
225
+
226
+
227
+ <section id="search-impl-reproduction">
228
+ <title>Reproduction</title>
229
+ <p>The reproduction function combines each pair of selected chromosomes using the method Chromosome.reproduce.</p>
230
+ <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>
231
+ <source>
232
+ <![CDATA[
233
+ rand < ((1 - chromosome.normalized_fitness) * 0.4)
234
+ ]]>
235
+ </source>
236
+ </section>
237
+
238
+
239
+ </section>
240
+
241
+
242
+ <section id="example-run">
243
+ <title>How to run the example</title>
244
+ <p>You can run the example with "ruby genetic_algorithm_example.rb". The genetic_algorithm_example.rb file
245
+ contains:</p>
246
+ <source>
247
+ <![CDATA[
248
+ require File.dirname(__FILE__) + '/../../lib/genetic_algorithm/genetic_algorithm'
249
+ require 'csv'
250
+
251
+ # Load data from data_set.csv
252
+ data_set = []
253
+ CSV::Reader.parse(File.open("#{File.dirname(__FILE__)}/travel_cost.csv", 'r')) do |row|
254
+ data_set << row
255
+ end
256
+ data_labels = data_set.shift
257
+ data_set.collect! do |column|
258
+ column.collect { |element| element.to_f}
259
+ end
260
+
261
+ GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
262
+
263
+ puts "Beginning genetic search, please wait... "
264
+ search = GeneticAlgorithm::GeneticSearch.new(800, 100)
265
+ result = search.run
266
+ puts "Result cost: #{result.fitness}"
267
+ puts "Result tour: "
268
+ result.data.each { |c| print " #{data_labels[c]}"}
269
+ ]]>
270
+ </source>
271
+
272
+ </section>
273
+
274
+ <section id="more-genetic-run">
275
+ <title>More about Genetic Algorithms and the Travelling salesman problem</title>
276
+ <p><a href="http://en.wikipedia.org/wiki/Traveling_salesman_problem" title="Link to Wikipedia">Travelling salesman problem at Wikipedia</a></p>
277
+ <p><a href="http://en.wikipedia.org/wiki/Genetic_algorithm" title="Link to Wikipedia">Genetic Algorithms at Wikipedia</a></p>
278
+ </section>
279
+ </body>
280
+ </document>
@@ -0,0 +1,73 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ Licensed to the Apache Software Foundation (ASF) under one or more
4
+ contributor license agreements. See the NOTICE file distributed with
5
+ this work for additional information regarding copyright ownership.
6
+ The ASF licenses this file to You under the Apache License, Version 2.0
7
+ (the "License"); you may not use this file except in compliance with
8
+ the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+
18
+ -->
19
+ <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
20
+ <document>
21
+ <header>
22
+ <title>Artificial Intelligence for Ruby :: ai4r</title>
23
+ </header>
24
+ <body>
25
+ <section id="Introduction">
26
+ <title>Introduction</title>
27
+ <p>
28
+ This project aims to produce ruby
29
+ implementations of algorithms covering several Artificial
30
+ intelligence fields, including:
31
+ </p>
32
+ <ul>
33
+ <li><a href="geneticAlgorithms.html">Genetic algorithms</a></li>
34
+ <li><a href="machineLearning.html">Machine Learning (ID3, Decision Trees)</a></li>
35
+ <li><a href="neuralNetworks.html">Neural networks (Backpropagation networks)</a></li>
36
+ </ul>
37
+ </section>
38
+
39
+ <section id="howtoinstall">
40
+ <title>How to install</title>
41
+ <p>1. Install the gem:</p>
42
+ <source>gem install http://rubyforge.org/frs/download.php/32923/ai4r-1.0.gem</source>
43
+ <p>Include require statements in your code:</p>
44
+ <source>
45
+ require "rubygems"
46
+ require "decision_tree/id3"
47
+ require "neural_network/backpropagation"
48
+ require "genetic_algorithm/genetic_algorithm"
49
+ </source>
50
+
51
+ </section>
52
+
53
+ <section id="ContactMe">
54
+ <title>Contact me</title>
55
+ <p>
56
+ If you have some constructive comments about this project, please do send those to me:
57
+ Sergio Fierens, email address: <img src="images/my_email.png" alt="(sergio (dot) fierens (at) gmail (dot) com)"
58
+ title="Sergio Fierens contact email" />
59
+ </p>
60
+ </section>
61
+
62
+ <section id="warranty">
63
+ <title>Disclaimer Note</title>
64
+ <p>
65
+ This software is
66
+ provided "as is" and without any
67
+ express or implied warranties, including, without limitation,
68
+ the implied warranties of merchantibility and fitness for a
69
+ particular purpose.
70
+ </p>
71
+ </section>
72
+ </body>
73
+ </document>