nirvdrum-ai4r 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,29 @@
1
+ # Author:: Sergio Fierens
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://ai4r.rubyforge.org/
5
+ #
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Mozilla Public License version 1.1 as published by the
8
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
9
+
10
+ require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/id3'
11
+
12
+ # Load data from data_set.csv
13
+ data_filename = "#{File.dirname(__FILE__)}/id3_data.csv"
14
+ data_set = Ai4r::Data::DataSet.new.load_csv_with_labels data_filename
15
+
16
+ # Build ID3 tree
17
+ id3 = Ai4r::Classifiers::ID3.new.build(data_set)
18
+
19
+ # Show rules
20
+ puts "Discovered rules are:"
21
+ puts id3.get_rules
22
+ puts
23
+
24
+ # Try to predict some values
25
+ puts "Prediction samples:"
26
+ puts "['Moron Sur (GBA)','4','[86 m2 - 100 m2]'] => " + id3.eval(['Moron Sur (GBA)','4','[86 m2 - 100 m2]'])
27
+ puts "['Moron Sur (GBA)','3','[101 m2 - 125 m2]'] => " + id3.eval(['Moron Sur (GBA)','3','[101 m2 - 125 m2]'])
28
+ puts "['Recoleta (CABA)','3','[86 m2 - 100 m2]'] => " + id3.eval(['Recoleta (CABA)','3','[86 m2 - 100 m2]',])
29
+ puts "['Tigre (GBA)','3','[71 m2 - 85 m2]'] => " + id3.eval(['Tigre (GBA)','3','[71 m2 - 85 m2]',])
@@ -0,0 +1,11 @@
1
+ "Color","Type","Origin","Stolen?"
2
+ "Red","Sports","Domestic","Yes"
3
+ "Red","Sports","Domestic","No"
4
+ "Red","Sports","Domestic","Yes"
5
+ "Yellow","Sports","Domestic","No"
6
+ "Yellow","Sports","Imported","Yes"
7
+ "Yellow","SUV","Imported","No"
8
+ "Yellow","SUV","Imported","Yes"
9
+ "Yellow","Sports","Domestic","No"
10
+ "Red","SUV","Imported","No"
11
+ "Red","Sports","Imported","Yes"
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/naive_bayes'
2
+ require File.dirname(__FILE__) + '/../../lib/ai4r/data/data_set'
3
+ require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/id3'
4
+ require 'benchmark'
5
+
6
+ include Ai4r::Classifiers
7
+ include Ai4r::Data
8
+
9
+ data_set = DataSet.new
10
+ data_set.load_csv_with_labels File.dirname(__FILE__) + "/naive_bayes_data.csv"
11
+
12
+ b = NaiveBayes.new.
13
+ set_parameters({:m=>3}).
14
+ build data_set
15
+ p b.eval(["Red", "SUV", "Domestic"])
16
+ p b.get_probability_map(["Red", "SUV", "Domestic"])
@@ -0,0 +1,31 @@
1
+ Discovered rules are:
2
+ if size=='[28 m2 - 39 m2]' and zone=='Moron Sur (GBA)' then price='[29K-35K]'
3
+ elsif size=='[28 m2 - 39 m2]' and zone=='Recoleta (CABA)' then price='[46K-55K]'
4
+ elsif size=='[28 m2 - 39 m2]' and zone=='Tigre (GBA)' then price='[56K-75K]'
5
+ elsif size=='[40 m2 - 50 m2]' and zone=='Moron Sur (GBA)' then price='[29K-35K]'
6
+ elsif size=='[40 m2 - 50 m2]' and zone=='Recoleta (CABA)' then price='[56K-75K]'
7
+ elsif size=='[40 m2 - 50 m2]' and zone=='Tigre (GBA)' then price='[56K-75K]'
8
+ elsif size=='[51 m2 - 59 m2]' and zone=='Moron Sur (GBA)' then price='[36K-45K]'
9
+ elsif size=='[51 m2 - 59 m2]' and zone=='Recoleta (CABA)' then price='[76K-100K]'
10
+ elsif size=='[51 m2 - 59 m2]' and zone=='Tigre (GBA)' then price='[101K-125K]'
11
+ elsif size=='[60 m2 - 70 m2]' and zone=='Moron Sur (GBA)' then price='[56K-75K]'
12
+ elsif size=='[60 m2 - 70 m2]' and zone=='Recoleta (CABA)' then price='[76K-100K]'
13
+ elsif size=='[71 m2 - 85 m2]' and zone=='Moron Sur (GBA)' then price='[46K-55K]'
14
+ elsif size=='[71 m2 - 85 m2]' and zone=='Recoleta (CABA)' then price='[126K-150K]'
15
+ elsif size=='[71 m2 - 85 m2]' and zone=='Tigre (GBA)' then price='[126K-150K]'
16
+ elsif size=='[86 m2 - 100 m2]' and zone=='Moron Sur (GBA)' then price='[46K-55K]'
17
+ elsif size=='[86 m2 - 100 m2]' and zone=='Recoleta (CABA)' then price='[126K-150K]'
18
+ elsif size=='[86 m2 - 100 m2]' and zone=='Tigre (GBA)' then price='[152K-169K]'
19
+ elsif size=='[101 m2 - 125 m2]' and zone=='Moron Sur (GBA)' then price='[76K-100K]'
20
+ elsif size=='[101 m2 - 125 m2]' and zone=='Recoleta (CABA)' then price='[200K-275K]'
21
+ elsif size=='[101 m2 - 125 m2]' and zone=='Tigre (GBA)' then price='[170K-200K]'
22
+ elsif size=='[126 m2 - 160 m2]' and zone=='Moron Sur (GBA)' then price='[56K-75K]'
23
+ elsif size=='[126 m2 - 160 m2]' and zone=='Recoleta (CABA)' then price='[200K-275K]'
24
+ elsif size=='[126 m2 - 160 m2]' and zone=='Tigre (GBA)' then price='>275K'
25
+ else raise 'There was not enough information during training to do a proper induction for this data element' end
26
+
27
+ Prediction samples:
28
+ ['Moron Sur (GBA)','4','[86 m2 - 100 m2]'] => [46K-55K]
29
+ ['Moron Sur (GBA)','3','[101 m2 - 125 m2]'] => [76K-100K]
30
+ ['Recoleta (CABA)','3','[86 m2 - 100 m2]'] => [126K-150K]
31
+ ['Tigre (GBA)','3','[71 m2 - 85 m2]'] => [126K-150K]
@@ -0,0 +1,37 @@
1
+ # Author:: Sergio Fierens
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://ai4r.rubyforge.org/
5
+ #
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Mozilla Public License version 1.1 as published by the
8
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
9
+
10
+ require File.dirname(__FILE__) + '/../../lib/ai4r/genetic_algorithm/genetic_algorithm'
11
+ require File.dirname(__FILE__) + '/../../lib/ai4r/data/data_set'
12
+ require 'csv'
13
+
14
+ # Load data from data_set.csv
15
+ data_filename = "#{File.dirname(__FILE__)}/travel_cost.csv"
16
+ data_set = Ai4r::Data::DataSet.new.load_csv_with_labels data_filename
17
+ data_set.data_items.collect! {|column| column.collect {|element| element.to_f}}
18
+
19
+ Ai4r::GeneticAlgorithm::Chromosome.set_cost_matrix(data_set.data_items)
20
+
21
+ puts "Some random selected tours costs: "
22
+ 3.times do
23
+ c = Ai4r::GeneticAlgorithm::Chromosome.seed
24
+ puts "COST #{-1 * c.fitness} TOUR: "+
25
+ "#{c.data.collect{|c| data_set.data_labels[c]} * ', '}"
26
+ end
27
+
28
+ puts "Beginning genetic search, please wait... "
29
+ search = Ai4r::GeneticAlgorithm::GeneticSearch.new(800, 100)
30
+ result = search.run
31
+ puts "COST #{-1 * result.fitness} TOUR: "+
32
+ "#{result.data.collect{|c| data_set.data_labels[c]} * ', '}"
33
+
34
+ # $7611.99 TOUR: Moscow, Kiev, Warsaw, Hamburg, Berlin, Vienna, Munich, Milan, Rome, Barcelona, Madrid, Paris, Brussels, London, Dublin
35
+ # $7659.81 TOUR: Moscow, Kiev, Warsaw, Vienna, Munich, Berlin, Hamburg, Brussels, Dublin, London, Paris, Milan, Rome, Barcelona, Madrid
36
+ # $7596.74 TOUR: Moscow, Kiev, Warsaw, Berlin, Hamburg, Vienna, Munich, Milan, Rome, Barcelona, Madrid, Paris, Brussels, London Dublin
37
+
@@ -0,0 +1,16 @@
1
+ "Barcelona","Berlin","Brussels","Dublin","Hamburg","Kiev","London","Madrid","Milan","Moscow","Munich","Paris","Rome","Vienna","Warsaw"
2
+ 0,"1497.61","1062.89","1469.29","1471.78","2391.06","1137.67","504.64","725.12","3006.93","1054.55","831.59","856.69","1347.43","1862.33"
3
+ "1497.61",0,"651.62","1315.16","254.51","1204.00","929.97","1867.69","840.72","1607.99","501.97","876.96","1181.67","523.61","516.06"
4
+ "1062.89","651.62",0,"773.20","489.76","1836.20","318.72","1314.30","696.61","2253.26","601.87","261.29","1171.34","914.81","1159.85"
5
+ "1469.29","1315.16","773.20",0,"1073.36","2513.69","462.60","1449.96","1413.37","2792.41","1374.91","776.83","1882.22","1680.00","1823.72"
6
+ "1471.78","254.51","489.76","1073.36",0,"1440.34","720.12","1785.33","900.01","1779.93","610.17","744.63","1307.51","742.79","750.49"
7
+ "2391.06","1204.00","1836.20","2513.69","1440.34",0,"2131.20","2859.32","1672.69","756.61","1391.36","2022.76","1673.74","1052.76","690.12"
8
+ "1137.67","929.97","318.72","462.60","720.12","2131.20",0,"1263.37","957.91","2498.32","916.23","340.55","1431.21","1233.48","1445.85"
9
+ "504.64","1867.69","1314.30","1449.96","1785.33","2859.32","1263.37",0,"1187.73","3437.70","1484.53","1053.40","1360.80","1807.09","2288.42"
10
+ "725.12","840.72","696.61","1413.37","900.01","1672.69","957.91","1187.73",0,"2283.19","348.89","641.31","476.00","623.36","1143.01"
11
+ "3006.93","1607.99","2253.26","2792.41","1779.93","756.61","2498.32","3437.70","2283.19",0,"1957.15","2484.92","2374.26","1669.22","1149.41"
12
+ "1054.55","501.97","601.87","1374.91","610.17","1391.36","916.23","1484.53","348.89","1957.15",0,"685.14","698.04","354.42","809.02"
13
+ "831.59","876.96","261.29","776.83","744.63","2022.76","340.55","1053.40","641.31","2484.92","685.14",0,"1105.76","1033.73","1365.91"
14
+ "856.69","1181.67","1171.34","1882.22","1307.51","1673.74","1431.21","1360.80","476.00","2374.26","698.04","1105.76",0,"763.26","1316.24"
15
+ "1347.43","523.61","914.81","1680.00","742.79","1052.76","1233.48","1807.09","623.36","1669.22","354.42","1033.73","763.26",0,"557.43"
16
+ "1862.33","516.06","1159.85","1823.72","750.49","690.12","1445.85","2288.42","1143.01","1149.41","809.02","1365.91","1316.24","557.43",0
@@ -0,0 +1,67 @@
1
+ # Author:: Sergio Fierens
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://ai4r.rubyforge.org/
5
+ #
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Mozilla Public License version 1.1 as published by the
8
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
9
+
10
+ require File.dirname(__FILE__) + '/training_patterns'
11
+ require File.dirname(__FILE__) + '/patterns_with_noise'
12
+ require File.dirname(__FILE__) + '/patterns_with_base_noise'
13
+ require File.dirname(__FILE__) + '/../../lib/ai4r/neural_network/backpropagation'
14
+ require 'benchmark'
15
+
16
+ times = Benchmark.measure do
17
+
18
+ srand 1
19
+
20
+ net = Ai4r::NeuralNetwork::Backpropagation.new([256, 3])
21
+
22
+ tr_input = TRIANGLE.flatten.collect { |input| input.to_f / 5.0}
23
+ sq_input = SQUARE.flatten.collect { |input| input.to_f / 5.0}
24
+ cr_input = CROSS.flatten.collect { |input| input.to_f / 5.0}
25
+
26
+ tr_with_noise = TRIANGLE_WITH_NOISE.flatten.collect { |input| input.to_f / 5.0}
27
+ sq_with_noise = SQUARE_WITH_NOISE.flatten.collect { |input| input.to_f / 5.0}
28
+ cr_with_noise = CROSS_WITH_NOISE.flatten.collect { |input| input.to_f / 5.0}
29
+
30
+ tr_with_base_noise = TRIANGLE_WITH_BASE_NOISE.flatten.collect { |input| input.to_f / 5.0}
31
+ sq_with_base_noise = SQUARE_WITH_BASE_NOISE.flatten.collect { |input| input.to_f / 5.0}
32
+ cr_with_base_noise = CROSS_WITH_BASE_NOISE.flatten.collect { |input| input.to_f / 5.0}
33
+
34
+ puts "Training the network, please wait."
35
+ 101.times do |i|
36
+ error = net.train(tr_input, [1,0,0])
37
+ error = net.train(sq_input, [0,1,0])
38
+ error = net.train(cr_input, [0,0,1])
39
+ puts "Error after iteration #{i}:\t#{error}" if i%20 == 0
40
+ end
41
+
42
+ def result_label(result)
43
+ if result[0] > result[1] && result[0] > result[2]
44
+ "TRIANGLE"
45
+ elsif result[1] > result[2]
46
+ "SQUARE"
47
+ else
48
+ "CROSS"
49
+ end
50
+ end
51
+
52
+ puts "Training Examples"
53
+ puts "#{net.eval(tr_input).inspect} => #{result_label(net.eval(tr_input))}"
54
+ puts "#{net.eval(sq_input).inspect} => #{result_label(net.eval(sq_input))}"
55
+ puts "#{net.eval(cr_input).inspect} => #{result_label(net.eval(cr_input))}"
56
+ puts "Examples with noise"
57
+ puts "#{net.eval(tr_with_noise).inspect} => #{result_label(net.eval(tr_with_noise))}"
58
+ puts "#{net.eval(sq_with_noise).inspect} => #{result_label(net.eval(sq_with_noise))}"
59
+ puts "#{net.eval(cr_with_noise).inspect} => #{result_label(net.eval(cr_with_noise))}"
60
+ puts "Examples with base noise"
61
+ puts "#{net.eval(tr_with_base_noise).inspect} => #{result_label(net.eval(tr_with_base_noise))}"
62
+ puts "#{net.eval(sq_with_base_noise).inspect} => #{result_label(net.eval(sq_with_base_noise))}"
63
+ puts "#{net.eval(cr_with_base_noise).inspect} => #{result_label(net.eval(cr_with_base_noise))}"
64
+
65
+ end
66
+
67
+ puts "Elapsed time: #{times}"
@@ -0,0 +1,68 @@
1
+ # Author:: Sergio Fierens
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://ai4r.rubyforge.org/
5
+ #
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Mozilla Public License version 1.1 as published by the
8
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
9
+
10
+
11
+ TRIANGLE_WITH_BASE_NOISE = [
12
+ [ 3, 3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, 3, 3, 3, 3, 3],
56
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3],
57
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3],
58
+ [ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
59
+ [ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
60
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3],
61
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3],
62
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3],
63
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3],
64
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3],
65
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3],
66
+ [ 3, 3, 3, 3, 3, 3, 3, 8, 8, 3, 3, 3, 3, 3, 3, 3]
67
+ ]
68
+
@@ -0,0 +1,66 @@
1
+ # Author:: Sergio Fierens
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://ai4r.rubyforge.org/
5
+ #
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Mozilla Public License version 1.1 as published by the
8
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
9
+
10
+ TRIANGLE_WITH_NOISE = [
11
+ [ 1, 0, 0, 0, 0, 0, 0, 1, 5, 0, 0, 1, 0, 0, 0, 0],
12
+ [ 0, 0, 0, 0, 3, 0, 1, 9, 9, 1, 0, 0, 0, 0, 3, 0],
13
+ [ 0, 3, 0, 0, 0, 0, 5, 1, 5, 3, 0, 0, 0, 0, 0, 7],
14
+ [ 0, 0, 0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0, 5],
58
+ [ 5, 5, 5, 5, 5, 5, 5, 5, 1, 5, 5, 5, 5, 1, 0, 0],
59
+ [ 0, 0, 0, 8, 0, 0, 0, 4, 5, 0, 0, 0, 0, 0, 0, 0],
60
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 4, 0, 0, 0, 0, 0, 0],
61
+ [ 0, 0, 0, 0, 0, 4, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0],
62
+ [ 4, 0, 0, 4, 0, 0, 0, 5, 5, 0, 0, 0, 1, 0, 0, 0],
63
+ [ 0, 0, 0, 0, 0, 1, 0, 5, 4, 4, 3, 0, 0, 0, 0, 0],
64
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 10, 0, 0, 0],
65
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0]
66
+ ]
@@ -0,0 +1,68 @@
1
+ # Author:: Sergio Fierens
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://ai4r.rubyforge.org/
5
+ #
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Mozilla Public License version 1.1 as published by the
8
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
9
+
10
+
11
+ TRIANGLE = [
12
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0],
13
+ [ 0, 0, 0, 0, 0, 0, 1, 9, 9, 1, 0, 0, 0, 0, 0, 0],
14
+ [ 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0],
15
+ [ 0, 0, 0, 0, 0, 1, 9, 1, 1, 9, 1, 0, 0, 0, 0, 0],
16
+ [ 0, 0, 0, 0, 0, 5, 5, 0, 0, 5, 5, 0, 0, 0, 0, 0],
17
+ [ 0, 0, 0, 0, 1, 9, 1, 0, 0, 1, 9, 1, 0, 0, 0, 0],
18
+ [ 0, 0, 0, 0, 5, 5, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0],
63
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0],
64
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0],
65
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0],
66
+ [ 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0]
67
+ ]
68
+
@@ -0,0 +1,35 @@
1
+ # Author:: Sergio Fierens
2
+ # License:: MPL 1.1
3
+ # Project:: ai4r
4
+ # Url:: http://ai4r.rubyforge.org/
5
+ #
6
+ # You can redistribute it and/or modify it under the terms of
7
+ # the Mozilla Public License version 1.1 as published by the
8
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
9
+
10
+ require File.dirname(__FILE__) + '/../../lib/ai4r/neural_network/backpropagation'
11
+ require 'benchmark'
12
+
13
+ times = Benchmark.measure do
14
+
15
+ srand 1
16
+
17
+ net = Ai4r::NeuralNetwork::Backpropagation.new([2, 2, 1])
18
+
19
+ puts "Training the network, please wait."
20
+ 2001.times do |i|
21
+ net.train([0,0], [0])
22
+ net.train([0,1], [1])
23
+ net.train([1,0], [1])
24
+ error = net.train([1,1], [0])
25
+ puts "Error after iteration #{i}:\t#{error}" if i%200 == 0
26
+ end
27
+
28
+ puts "Test data"
29
+ puts "[0,0] = > #{net.eval([0,0]).inspect}"
30
+ puts "[0,1] = > #{net.eval([0,1]).inspect}"
31
+ puts "[1,0] = > #{net.eval([1,0]).inspect}"
32
+ puts "[1,1] = > #{net.eval([1,1]).inspect}"
33
+ end
34
+
35
+ puts "Elapsed time: #{times}"