nirvdrum-ai4r 1.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/.gitignore +1 -0
  2. data/.rakeTasks +7 -0
  3. data/README.rdoc +56 -0
  4. data/Rakefile.rb +42 -0
  5. data/VERSION +1 -0
  6. data/ai4r.gemspec +221 -0
  7. data/change_log +49 -0
  8. data/examples/classifiers/id3_data.csv +121 -0
  9. data/examples/classifiers/id3_example.rb +29 -0
  10. data/examples/classifiers/naive_bayes_data.csv +11 -0
  11. data/examples/classifiers/naive_bayes_example.rb +16 -0
  12. data/examples/classifiers/results.txt +31 -0
  13. data/examples/genetic_algorithm/genetic_algorithm_example.rb +37 -0
  14. data/examples/genetic_algorithm/travel_cost.csv +16 -0
  15. data/examples/neural_network/backpropagation_example.rb +67 -0
  16. data/examples/neural_network/patterns_with_base_noise.rb +68 -0
  17. data/examples/neural_network/patterns_with_noise.rb +66 -0
  18. data/examples/neural_network/training_patterns.rb +68 -0
  19. data/examples/neural_network/xor_example.rb +35 -0
  20. data/examples/som/som_data.rb +156 -0
  21. data/examples/som/som_multi_node_example.rb +22 -0
  22. data/examples/som/som_single_example.rb +24 -0
  23. data/lib/ai4r.rb +32 -0
  24. data/lib/ai4r/classifiers/classifier.rb +59 -0
  25. data/lib/ai4r/classifiers/hyperpipes.rb +118 -0
  26. data/lib/ai4r/classifiers/id3.rb +326 -0
  27. data/lib/ai4r/classifiers/multilayer_perceptron.rb +135 -0
  28. data/lib/ai4r/classifiers/naive_bayes.rb +259 -0
  29. data/lib/ai4r/classifiers/one_r.rb +110 -0
  30. data/lib/ai4r/classifiers/prism.rb +197 -0
  31. data/lib/ai4r/classifiers/zero_r.rb +73 -0
  32. data/lib/ai4r/clusterers/average_linkage.rb +59 -0
  33. data/lib/ai4r/clusterers/bisecting_k_means.rb +93 -0
  34. data/lib/ai4r/clusterers/centroid_linkage.rb +66 -0
  35. data/lib/ai4r/clusterers/clusterer.rb +61 -0
  36. data/lib/ai4r/clusterers/complete_linkage.rb +67 -0
  37. data/lib/ai4r/clusterers/diana.rb +139 -0
  38. data/lib/ai4r/clusterers/k_means.rb +126 -0
  39. data/lib/ai4r/clusterers/median_linkage.rb +61 -0
  40. data/lib/ai4r/clusterers/single_linkage.rb +194 -0
  41. data/lib/ai4r/clusterers/ward_linkage.rb +64 -0
  42. data/lib/ai4r/clusterers/weighted_average_linkage.rb +61 -0
  43. data/lib/ai4r/data/data_set.rb +266 -0
  44. data/lib/ai4r/data/parameterizable.rb +64 -0
  45. data/lib/ai4r/data/proximity.rb +100 -0
  46. data/lib/ai4r/data/statistics.rb +77 -0
  47. data/lib/ai4r/experiment/classifier_evaluator.rb +95 -0
  48. data/lib/ai4r/genetic_algorithm/genetic_algorithm.rb +270 -0
  49. data/lib/ai4r/neural_network/backpropagation.rb +293 -0
  50. data/lib/ai4r/neural_network/hopfield.rb +149 -0
  51. data/lib/ai4r/som/layer.rb +68 -0
  52. data/lib/ai4r/som/node.rb +96 -0
  53. data/lib/ai4r/som/som.rb +155 -0
  54. data/lib/ai4r/som/two_phase_layer.rb +90 -0
  55. data/site/forrest.properties +152 -0
  56. data/site/forrest.properties.dispatcher.properties +25 -0
  57. data/site/forrest.properties.xml +29 -0
  58. data/site/src/documentation/README.txt +7 -0
  59. data/site/src/documentation/classes/CatalogManager.properties +62 -0
  60. data/site/src/documentation/content/locationmap.xml +72 -0
  61. data/site/src/documentation/content/xdocs/downloads.html +9 -0
  62. data/site/src/documentation/content/xdocs/geneticAlgorithms.xml +294 -0
  63. data/site/src/documentation/content/xdocs/index.xml +155 -0
  64. data/site/src/documentation/content/xdocs/machineLearning.xml +131 -0
  65. data/site/src/documentation/content/xdocs/neuralNetworks.xml +270 -0
  66. data/site/src/documentation/content/xdocs/site.xml +54 -0
  67. data/site/src/documentation/content/xdocs/sourceCode.xml +43 -0
  68. data/site/src/documentation/content/xdocs/tabs.xml +35 -0
  69. data/site/src/documentation/resources/images/ai4r-logo.png +0 -0
  70. data/site/src/documentation/resources/images/c.png +0 -0
  71. data/site/src/documentation/resources/images/c_wbn.png +0 -0
  72. data/site/src/documentation/resources/images/c_wn.png +0 -0
  73. data/site/src/documentation/resources/images/ellipse-2.svg +30 -0
  74. data/site/src/documentation/resources/images/ero.gif +0 -0
  75. data/site/src/documentation/resources/images/europe2.png +0 -0
  76. data/site/src/documentation/resources/images/europe3.png +0 -0
  77. data/site/src/documentation/resources/images/fitness.png +0 -0
  78. data/site/src/documentation/resources/images/genetic_algorithms_example.png +0 -0
  79. data/site/src/documentation/resources/images/icon-a.png +0 -0
  80. data/site/src/documentation/resources/images/icon-b.png +0 -0
  81. data/site/src/documentation/resources/images/icon.png +0 -0
  82. data/site/src/documentation/resources/images/jadeferret.png +0 -0
  83. data/site/src/documentation/resources/images/my_email.png +0 -0
  84. data/site/src/documentation/resources/images/neural_network_example.png +0 -0
  85. data/site/src/documentation/resources/images/project-logo.png +0 -0
  86. data/site/src/documentation/resources/images/rubyforge.png +0 -0
  87. data/site/src/documentation/resources/images/s.png +0 -0
  88. data/site/src/documentation/resources/images/s_wbn.png +0 -0
  89. data/site/src/documentation/resources/images/s_wn.png +0 -0
  90. data/site/src/documentation/resources/images/sigmoid.png +0 -0
  91. data/site/src/documentation/resources/images/sub-dir/icon-c.png +0 -0
  92. data/site/src/documentation/resources/images/t.png +0 -0
  93. data/site/src/documentation/resources/images/t_wbn.png +0 -0
  94. data/site/src/documentation/resources/images/t_wn.png +0 -0
  95. data/site/src/documentation/resources/schema/catalog.xcat +29 -0
  96. data/site/src/documentation/resources/schema/hello-v10.dtd +51 -0
  97. data/site/src/documentation/resources/schema/symbols-project-v10.ent +26 -0
  98. data/site/src/documentation/resources/stylesheets/hello2document.xsl +33 -0
  99. data/site/src/documentation/sitemap.xmap +66 -0
  100. data/site/src/documentation/skinconf.xml +418 -0
  101. data/site/src/documentation/translations/langcode.xml +29 -0
  102. data/site/src/documentation/translations/languages_de.xml +24 -0
  103. data/site/src/documentation/translations/languages_en.xml +24 -0
  104. data/site/src/documentation/translations/languages_es.xml +22 -0
  105. data/site/src/documentation/translations/languages_fr.xml +24 -0
  106. data/site/src/documentation/translations/languages_nl.xml +24 -0
  107. data/site/src/documentation/translations/menu.xml +33 -0
  108. data/site/src/documentation/translations/menu_af.xml +33 -0
  109. data/site/src/documentation/translations/menu_de.xml +33 -0
  110. data/site/src/documentation/translations/menu_es.xml +33 -0
  111. data/site/src/documentation/translations/menu_fr.xml +33 -0
  112. data/site/src/documentation/translations/menu_it.xml +33 -0
  113. data/site/src/documentation/translations/menu_nl.xml +33 -0
  114. data/site/src/documentation/translations/menu_no.xml +33 -0
  115. data/site/src/documentation/translations/menu_ru.xml +33 -0
  116. data/site/src/documentation/translations/menu_sk.xml +33 -0
  117. data/site/src/documentation/translations/tabs.xml +22 -0
  118. data/site/src/documentation/translations/tabs_de.xml +22 -0
  119. data/site/src/documentation/translations/tabs_es.xml +22 -0
  120. data/site/src/documentation/translations/tabs_fr.xml +22 -0
  121. data/site/src/documentation/translations/tabs_nl.xml +22 -0
  122. data/test/classifiers/hyperpipes_test.rb +84 -0
  123. data/test/classifiers/id3_test.rb +208 -0
  124. data/test/classifiers/multilayer_perceptron_test.rb +79 -0
  125. data/test/classifiers/naive_bayes_test.rb +43 -0
  126. data/test/classifiers/one_r_test.rb +62 -0
  127. data/test/classifiers/prism_test.rb +85 -0
  128. data/test/classifiers/zero_r_test.rb +50 -0
  129. data/test/clusterers/average_linkage_test.rb +51 -0
  130. data/test/clusterers/bisecting_k_means_test.rb +66 -0
  131. data/test/clusterers/centroid_linkage_test.rb +53 -0
  132. data/test/clusterers/complete_linkage_test.rb +57 -0
  133. data/test/clusterers/diana_test.rb +69 -0
  134. data/test/clusterers/k_means_test.rb +100 -0
  135. data/test/clusterers/median_linkage_test.rb +53 -0
  136. data/test/clusterers/single_linkage_test.rb +122 -0
  137. data/test/clusterers/ward_linkage_test.rb +53 -0
  138. data/test/clusterers/weighted_average_linkage_test.rb +53 -0
  139. data/test/data/data_set.csv +121 -0
  140. data/test/data/data_set_test.rb +96 -0
  141. data/test/data/proximity_test.rb +81 -0
  142. data/test/data/statistics_data_set.csv +5 -0
  143. data/test/data/statistics_test.rb +65 -0
  144. data/test/experiment/classifier_evaluator_test.rb +76 -0
  145. data/test/genetic_algorithm/chromosome_test.rb +58 -0
  146. data/test/genetic_algorithm/genetic_algorithm_test.rb +81 -0
  147. data/test/neural_network/backpropagation_test.rb +69 -0
  148. data/test/neural_network/hopfield_test.rb +72 -0
  149. data/test/som/som_test.rb +97 -0
  150. metadata +238 -0
@@ -0,0 +1,25 @@
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
+ # Note: The reason for this "forrest.properties.dispatcher.properties"
18
+ # is to assist with automated testing (main/build.sh test).
19
+ # Its content redefines the project.required.plugins property which defines
20
+ # the list of required plugins for the dispatcher.
21
+ # To test the Dispatcher in development, simply replace the
22
+ # project.required.plugins property in the forrest.properties file by the one
23
+ # defined in this file.
24
+ #
25
+ project.required.plugins=org.apache.forrest.plugin.output.pdf,org.apache.forrest.plugin.internal.dispatcher,org.apache.forrest.themes.core,org.apache.forrest.plugin.output.inputModule
@@ -0,0 +1,29 @@
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
+ <properties>
19
+ <!--
20
+ # codename: Dispatcher
21
+ # Dispatcher is using a fallback mechanism for theming.
22
+ # You can configure the theme name and its extension here
23
+ -->
24
+ <property name="dispatcher.theme" value="pelt"/>
25
+ <property name="dispatcher.theme-ext" value=".fv"/>
26
+ <!--SVG example for project/group logo-->
27
+ <property name="group-name" value="group"/>
28
+ <property name="project-name" value="project"/>
29
+ </properties>
@@ -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,294 @@
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="Example">
9
+ <title>The European Rock Tour Problem (Also known as the Travelling salesman problem)</title>
10
+ <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>
11
+ <p><img src="images/europe2.png" alt="European Tour" /></p>
12
+ <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>
13
+ <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>
14
+ </p>
15
+ <p><em><strong>Band member #1</strong> – Oh man! So it is going to take us all day!</em>
16
+ </p>
17
+ <p><em><strong>Band member #2</strong> – And maybe more, 'cause this internet connection sucks...</em>
18
+ </p>
19
+ <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>
20
+ </p>
21
+ <p><em><strong>Band</strong> – (Silence)</em>
22
+ </p>
23
+ <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>
24
+ </p>
25
+ <p><em><strong>Band</strong> – (Silence)</em>
26
+ </p>
27
+ <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>
28
+ </p>
29
+ <p><em><strong>Band</strong> – YEAH! THAT'S THE MAN! LET'S DO IT!</em>
30
+ </p>
31
+ <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.
32
+ </p>
33
+ </section>
34
+
35
+ <section id="Introduction">
36
+ <title>Introduction to Genetics Algorithms in Ruby</title>
37
+ <p>A Genetic Algorithm is a particular class of evolutionary algorithm and stochastic search.
38
+ It aims to find the best possible solution in a solution domain, by selecting a simple
39
+ set of solutions (chosed randomly or with a simple heuristic), and making it "evolve".
40
+ </p>
41
+ <p>It works based on the following pseudocode:</p>
42
+ <ol>
43
+ <li>Choose initial population</li>
44
+ <li>Evaluate the fitness of each individual in the population</li>
45
+ <li>Repeat as many times as generations we allow
46
+ <ol>
47
+ <li>Select randomly best-ranking individuals to reproduce</li>
48
+ <li>Breed new generation through crossover and mutation (genetic operations) and give birth to offspring</li>
49
+ <li>Evaluate the individual fitnesses of the offspring</li>
50
+ <li>Replace worst ranked part of population with offspring</li>
51
+ </ol>
52
+ </li>
53
+ </ol>
54
+ </section>
55
+
56
+ <section id="chromosome-impl">
57
+ <title>Implementation of Chromosome class for the Travelling salesman problem</title>
58
+ <p>In AI4R, the GeneticAlgorithm module implements the GeneticSearch and Chromosome classes.
59
+ GeneticSearch is a generic class, and can be used to solved any kind of problems.
60
+ The GeneticSearch class performs a stochastic search following the algorithm mentioned in the
61
+ previous section.
62
+ </p>
63
+ <p>
64
+ However, the Chromosome class implementation is problem specific.
65
+ Ai4r built-in Chromosomeclass was designed to model the
66
+ <a href="http://en.wikipedia.org/wiki/Traveling_salesman_problem" title="Link to Wikipedia">
67
+ Travelling salesman problem.
68
+ </a>
69
+ You have to provide a matrix with the cost of traveling from one point to another
70
+ (array of arrays of float values).
71
+ If you want to solve other type of problem, you will have to modify the Chromosome class,
72
+ by overwriting its fitness, reproduce, and mutate functions, to model you specific problem.
73
+ </p>
74
+ <section id="chromosome-impl-data">
75
+ <title>Data representation</title>
76
+ <p>Each chromosome must represent a posible solution for the problem. This class conatins an array
77
+ 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>
78
+ <source>
79
+ <![CDATA[
80
+ data_set = [ [ 0, 10, 5],
81
+ [ 6, 0, 4],
82
+ [25, 4, 0]
83
+ ]
84
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
85
+ ]]>
86
+ </source>
87
+ </section>
88
+ <section id="chromosome-impl-fitness">
89
+ <title>Fitness function</title>
90
+ <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.
91
+ 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>
92
+ <p>The fitness function will return the complete tour cost represented by the chromosome, multiplied by -1.
93
+ For example:</p>
94
+
95
+ <source>
96
+ <![CDATA[
97
+ data_set = [ [ 0, 10, 5],
98
+ [ 6, 0, 4],
99
+ [25, 4, 0]
100
+ ]
101
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
102
+ chromosome = Ai4r::GeneticAlgorithm::Chromosome.new([0, 2, 1])
103
+ chromosome.fitness
104
+ # => -9
105
+ ]]>
106
+ </source>
107
+ <p>That is: From 0 to 2 costs 5. From 2 to 1 costs 4. Total cost is 9.</p>
108
+ </section>
109
+ <section id="chromosome-impl-reproduce">
110
+ <title>Reproduce function</title>
111
+ <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>
112
+ <p><img src="images/ero.gif" alt="Edge recombination" /></p>
113
+ <p>The previous image was taken from the wikipedia, so hail to the author: Koala man (not me).</p>
114
+ </section>
115
+ <section id="chromosome-impl-mutation">
116
+ <title>Mutation function</title>
117
+ <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>
118
+ <source>
119
+ <![CDATA[
120
+ def self.mutate(chromosome)
121
+ if chromosome.normalized_fitness && rand < ((1 - chromosome.normalized_fitness) * 0.3)
122
+ data = chromosome.data
123
+ index = rand(data.length-1)
124
+ data[index], data[index+1] = data[index+1], data[index]
125
+ chromosome.data = data
126
+ @fitness = nil
127
+ end
128
+ end
129
+ ]]>
130
+ </source>
131
+ </section>
132
+ <section id="chromosome-impl-seed">
133
+ <title>Seed function</title>
134
+ <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>
135
+ <source>
136
+ <![CDATA[
137
+ def self.seed
138
+ data_size = @@costs[0].length
139
+ available = []
140
+ 0.upto(data_size-1) { |n| available << n }
141
+ seed = []
142
+ while available.length > 0 do
143
+ index = rand(available.length)
144
+ seed << available.delete_at(index)
145
+ end
146
+ return Chromosome.new(seed)
147
+ end
148
+ ]]>
149
+ </source>
150
+ </section>
151
+ </section>
152
+
153
+ <section id="search-impl">
154
+ <title>Implementation of GeneticSearch</title>
155
+ <p>The GeneticSearch class is an generic class to try to solve any kind of problem using genetic algorithms. If you
156
+ want to model another type of problem, you will have to modify the Chromosome class, defining its fitness, mutate, and reproduce functions.</p>
157
+
158
+ <section id="search-impl-init">
159
+
160
+ <title>Initialize the search</title>
161
+ <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>
162
+ <source>
163
+ <![CDATA[
164
+ search = Ai4r::GeneticAlgorithm::GeneticSearch.new(10, 20)
165
+ result = search.run
166
+ ]]>
167
+ </source>
168
+ </section>
169
+ <section id="search-impl-run">
170
+ <title>Run method</title>
171
+ <p>Once you initialize an instance of GeneticSearch class, you can perform the search executing the run method.
172
+ This method will:</p>
173
+ <ol>
174
+ <li>Choose initial population</li>
175
+ <li>Evaluate the fitness of each individual in the population</li>
176
+ <li>Repeat as many times as generations we allow
177
+ <ol>
178
+ <li>Select randomly the best-ranking individuals to reproduce</li>
179
+ <li>Breed new generation through crossover and mutation (genetic operations) and give birth to offspring</li>
180
+ <li>Evaluate the individual fitnesses of the offspring</li>
181
+ <li>Replace worst ranked part of population with offspring</li>
182
+ </ol>
183
+ </li>
184
+ </ol>
185
+ </section>
186
+
187
+ <section id="search-impl-selection">
188
+ <title>Selection</title>
189
+ <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>
190
+ <ol>
191
+ <li>The fitness function is evaluated for each individual, providing fitness values</li>
192
+ <li>The population is sorted by descending fitness values.</li>
193
+ <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>
194
+ <li> A random number R is chosen. R is between 0 and the accumulated normalized value (all the normalized fitness values added togheter).</li>
195
+ <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>
196
+ <li>We repeat steps 4 and 5, 2/3 times the population size.</li>
197
+ </ol>
198
+ <p><img src="images/fitness.png" alt="Edge recombination" /></p>
199
+ <p>The previous image was taken from the wikipedia, so hail to the author: Simon Hatthon.</p>
200
+ </section>
201
+
202
+
203
+ <section id="search-impl-reproduction">
204
+ <title>Reproduction</title>
205
+ <p>The reproduction function combines each pair of selected chromosomes using the method Chromosome.reproduce.</p>
206
+ <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>
207
+ <source>
208
+ <![CDATA[
209
+ rand < ((1 - chromosome.normalized_fitness) * 0.4)
210
+ ]]>
211
+ </source>
212
+ </section>
213
+
214
+
215
+ </section>
216
+ <section id="how-to-run">
217
+ <title>How to use AI4R Genetic Search implementation</title>
218
+ <source>
219
+ <![CDATA[
220
+ #Cost of traveling from one point to another. E.g. Travelling from Node 0 to Node 2 costs 5.
221
+ data_set = [ [ 0, 10, 5],
222
+ [ 6, 0, 4],
223
+ [25, 4, 0]
224
+ ]
225
+
226
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
227
+
228
+ search = Ai4r::GeneticAlgorithm::GeneticSearch.new(10, 20)
229
+ result = search.run
230
+ puts "Result cost: #{result.fitness}"
231
+ puts "Result nodes: #{result.data.inspect}"
232
+ ]]>
233
+ </source>
234
+ </section>
235
+
236
+ <section id="example-run">
237
+ <title>How to run the example</title>
238
+ <p>You can run the example with "ruby genetic_algorithm_example.rb". The genetic_algorithm_example.rb file
239
+ contains:</p>
240
+ <source>
241
+ <![CDATA[
242
+ require "rubygems"
243
+ require "ai4r/genetic_algorithm/genetic_algorithm"
244
+ require "csv"
245
+
246
+ # Load data from data_set.csv
247
+ data_set = []
248
+ CSV::Reader.parse(File.open("#{File.dirname(__FILE__)}/travel_cost.csv", 'r')) do |row|
249
+ data_set << row
250
+ end
251
+ data_labels = data_set.shift
252
+ data_set.collect! do |column|
253
+ column.collect { |element| element.to_f}
254
+ end
255
+
256
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set)
257
+
258
+ puts "Beginning genetic search, please wait... "
259
+ search = Ai4r::GeneticAlgorithm::GeneticSearch.new(800, 100)
260
+ result = search.run
261
+ puts "Result cost: #{result.fitness}"
262
+ puts "Result tour: "
263
+ result.data.each { |c| print " #{data_labels[c]}"}
264
+ ]]>
265
+ </source>
266
+
267
+ </section>
268
+
269
+ <section id="Results">
270
+ <title>Results of using Genetic Algorithms to the The European Rock Tour Problem (or Travelling salesman problem)</title>
271
+ <p>The cost of 3 randomly selected tours:</p>
272
+ <ul>
273
+ <li>$17486.01 : Madrid Vienna Moscow Berlin Brussels Munich Milan Barcelona London Hamburg Warsaw Dublin Kiev Paris Rome</li>
274
+ <li>$20198.92 : London Rome Brussels Kiev Hamburg Warsaw Barcelona Paris Munich Dublin Vienna Moscow Madrid Milan Berlin</li>
275
+ <li>$17799.34 : Madrid Milan Kiev Vienna Warsaw London Barcelona Hamburg Paris Munich Dublin Berlin Moscow Rome Brussels</li>
276
+ </ul>
277
+ <p>3 tours obtained with an initial population of 800, and after 100 generations:</p>
278
+ <ul>
279
+ <li>$7611.99 : Moscow Kiev Warsaw Hamburg Berlin Munich Vienna Milan Rome Barcelona Madrid Paris Brussels London Dublin</li>
280
+ <li>$7596.74 : Moscow Kiev Warsaw Berlin Hamburg Munich Vienna Milan Rome Barcelona Madrid Paris Brussels London Dublin (See Image)</li>
281
+ <li>$7641.61 : Madrid Barcelona Rome Milan Paris Dublin London Brussels Hamburg Berlin Vienna Munich Warsaw Kiev Moscow</li>
282
+ </ul>
283
+ <p><img src="images/europe3.png" alt="Best tour result using Genetic Algorithms in ruby" /></p>
284
+ <p>The GeneticSearch class is an generic class to try to solve any kind of problem using genetic algorithms. If you
285
+ want to model another type of problem, you will have to modify the Chromosome class, defining its fitness, mutate, and reproduce functions.</p>
286
+ </section>
287
+
288
+ <section id="more-genetic-run">
289
+ <title>More about Genetic Algorithms and the Travelling salesman problem</title>
290
+ <p><a href="http://en.wikipedia.org/wiki/Traveling_salesman_problem" title="Link to Wikipedia">Travelling salesman problem at Wikipedia</a></p>
291
+ <p><a href="http://en.wikipedia.org/wiki/Genetic_algorithm" title="Link to Wikipedia">Genetic Algorithms at Wikipedia</a></p>
292
+ </section>
293
+ </body>
294
+ </document>