ai4ruby 1.11

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 (79) hide show
  1. data/README.rdoc +47 -0
  2. data/examples/classifiers/id3_data.csv +121 -0
  3. data/examples/classifiers/id3_example.rb +29 -0
  4. data/examples/classifiers/naive_bayes_data.csv +11 -0
  5. data/examples/classifiers/naive_bayes_example.rb +16 -0
  6. data/examples/classifiers/results.txt +31 -0
  7. data/examples/genetic_algorithm/genetic_algorithm_example.rb +37 -0
  8. data/examples/genetic_algorithm/travel_cost.csv +16 -0
  9. data/examples/neural_network/backpropagation_example.rb +67 -0
  10. data/examples/neural_network/patterns_with_base_noise.rb +68 -0
  11. data/examples/neural_network/patterns_with_noise.rb +66 -0
  12. data/examples/neural_network/training_patterns.rb +68 -0
  13. data/examples/neural_network/xor_example.rb +35 -0
  14. data/examples/som/som_data.rb +156 -0
  15. data/examples/som/som_multi_node_example.rb +22 -0
  16. data/examples/som/som_single_example.rb +24 -0
  17. data/lib/ai4r.rb +33 -0
  18. data/lib/ai4r/classifiers/classifier.rb +62 -0
  19. data/lib/ai4r/classifiers/hyperpipes.rb +118 -0
  20. data/lib/ai4r/classifiers/ib1.rb +121 -0
  21. data/lib/ai4r/classifiers/id3.rb +326 -0
  22. data/lib/ai4r/classifiers/multilayer_perceptron.rb +135 -0
  23. data/lib/ai4r/classifiers/naive_bayes.rb +259 -0
  24. data/lib/ai4r/classifiers/one_r.rb +110 -0
  25. data/lib/ai4r/classifiers/prism.rb +197 -0
  26. data/lib/ai4r/classifiers/zero_r.rb +73 -0
  27. data/lib/ai4r/clusterers/average_linkage.rb +59 -0
  28. data/lib/ai4r/clusterers/bisecting_k_means.rb +93 -0
  29. data/lib/ai4r/clusterers/centroid_linkage.rb +66 -0
  30. data/lib/ai4r/clusterers/clusterer.rb +61 -0
  31. data/lib/ai4r/clusterers/complete_linkage.rb +67 -0
  32. data/lib/ai4r/clusterers/diana.rb +139 -0
  33. data/lib/ai4r/clusterers/k_means.rb +126 -0
  34. data/lib/ai4r/clusterers/median_linkage.rb +61 -0
  35. data/lib/ai4r/clusterers/single_linkage.rb +194 -0
  36. data/lib/ai4r/clusterers/ward_linkage.rb +64 -0
  37. data/lib/ai4r/clusterers/ward_linkage_hierarchical.rb +31 -0
  38. data/lib/ai4r/clusterers/weighted_average_linkage.rb +61 -0
  39. data/lib/ai4r/data/data_set.rb +266 -0
  40. data/lib/ai4r/data/parameterizable.rb +64 -0
  41. data/lib/ai4r/data/proximity.rb +100 -0
  42. data/lib/ai4r/data/statistics.rb +77 -0
  43. data/lib/ai4r/experiment/classifier_evaluator.rb +95 -0
  44. data/lib/ai4r/genetic_algorithm/genetic_algorithm.rb +270 -0
  45. data/lib/ai4r/neural_network/backpropagation.rb +326 -0
  46. data/lib/ai4r/neural_network/hopfield.rb +149 -0
  47. data/lib/ai4r/som/layer.rb +68 -0
  48. data/lib/ai4r/som/node.rb +96 -0
  49. data/lib/ai4r/som/som.rb +155 -0
  50. data/lib/ai4r/som/two_phase_layer.rb +90 -0
  51. data/test/classifiers/hyperpipes_test.rb +84 -0
  52. data/test/classifiers/ib1_test.rb +78 -0
  53. data/test/classifiers/id3_test.rb +208 -0
  54. data/test/classifiers/multilayer_perceptron_test.rb +79 -0
  55. data/test/classifiers/naive_bayes_test.rb +43 -0
  56. data/test/classifiers/one_r_test.rb +62 -0
  57. data/test/classifiers/prism_test.rb +85 -0
  58. data/test/classifiers/zero_r_test.rb +49 -0
  59. data/test/clusterers/average_linkage_test.rb +51 -0
  60. data/test/clusterers/bisecting_k_means_test.rb +66 -0
  61. data/test/clusterers/centroid_linkage_test.rb +53 -0
  62. data/test/clusterers/complete_linkage_test.rb +57 -0
  63. data/test/clusterers/diana_test.rb +69 -0
  64. data/test/clusterers/k_means_test.rb +100 -0
  65. data/test/clusterers/median_linkage_test.rb +53 -0
  66. data/test/clusterers/single_linkage_test.rb +122 -0
  67. data/test/clusterers/ward_linkage_hierarchical_test.rb +61 -0
  68. data/test/clusterers/ward_linkage_test.rb +53 -0
  69. data/test/clusterers/weighted_average_linkage_test.rb +53 -0
  70. data/test/data/data_set_test.rb +96 -0
  71. data/test/data/proximity_test.rb +81 -0
  72. data/test/data/statistics_test.rb +65 -0
  73. data/test/experiment/classifier_evaluator_test.rb +76 -0
  74. data/test/genetic_algorithm/chromosome_test.rb +58 -0
  75. data/test/genetic_algorithm/genetic_algorithm_test.rb +81 -0
  76. data/test/neural_network/backpropagation_test.rb +82 -0
  77. data/test/neural_network/hopfield_test.rb +72 -0
  78. data/test/som/som_test.rb +97 -0
  79. metadata +168 -0
@@ -0,0 +1,65 @@
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 'test/unit'
11
+ require File.dirname(__FILE__) + '/../../lib/ai4r/data/statistics'
12
+
13
+ module Ai4r
14
+ module Data
15
+ class StatisticsTest < Test::Unit::TestCase
16
+
17
+ DELTA = 0.00001
18
+
19
+ def setup
20
+ @data_set = DataSet.new.
21
+ parse_csv "#{File.dirname(__FILE__)}/statistics_data_set.csv"
22
+ end
23
+
24
+ def test_mean
25
+ assert_equal 2, Statistics.mean(@data_set, 1)
26
+ assert_equal 2.502, Statistics.mean(@data_set, 0)
27
+ end
28
+
29
+ def test_variance
30
+ assert_equal 0, Statistics.variance(@data_set, 1)
31
+ assert_in_delta 4.47302, Statistics.variance(@data_set, 0), DELTA
32
+ end
33
+
34
+ def test_standard_deviation
35
+ assert_equal 0, Statistics.standard_deviation(@data_set, 1)
36
+ assert_in_delta 2.11495, Statistics.standard_deviation(@data_set, 0), DELTA
37
+ end
38
+
39
+ def test_mode
40
+ items = [ [ "New York", 25, "Y"],
41
+ [ "New York", 55, "Y"],
42
+ [ "Chicago", 23, "Y"],
43
+ [ "Boston", 23, "N"],
44
+ [ "Chicago", 12, "N"],
45
+ [ "Chicago", 87, "Y"] ]
46
+ set = DataSet.new.set_data_items(items)
47
+ assert_equal "Chicago", Statistics.mode(set,0)
48
+ assert_equal 23, Statistics.mode(set,1)
49
+ assert_equal "Y", Statistics.mode(set,2)
50
+ end
51
+
52
+ def test_min
53
+ assert_equal 2, Statistics.min(@data_set, 1)
54
+ assert_equal 1, Statistics.min(@data_set, 0)
55
+ end
56
+
57
+ def test_max
58
+ assert_equal 2, Statistics.max(@data_set, 1)
59
+ assert_equal 6, Statistics.max(@data_set, 0)
60
+ assert_equal 3.7, Statistics.max(@data_set, 2)
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,76 @@
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 'test/unit'
11
+ require File.dirname(__FILE__) + '/../../lib/ai4r/experiment/classifier_evaluator'
12
+ require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/classifier'
13
+
14
+ class MockClassifier < Ai4r::Classifiers::Classifier
15
+
16
+ attr_accessor :built
17
+
18
+ def initialize(class_value)
19
+ @built = false
20
+ @class_value = class_value
21
+ end
22
+
23
+ def build(data_set)
24
+ @built = true
25
+ end
26
+
27
+ def eval(data)
28
+ @class_value
29
+ end
30
+
31
+ end
32
+
33
+ module Ai4r
34
+ module Experiment
35
+ class ClassifierEvaluatorTest < Test::Unit::TestCase
36
+
37
+ def test_add_classifier
38
+ evaluator = ClassifierEvaluator.new
39
+ evaluator << MockClassifier.new(nil) << MockClassifier.new(nil)
40
+ evaluator.add_classifier(MockClassifier.new(nil)).
41
+ add_classifier(MockClassifier.new(nil)).
42
+ add_classifier(MockClassifier.new(nil))
43
+ assert_equal 5, evaluator.classifiers.length
44
+ end
45
+
46
+ def test_build
47
+ evaluator = ClassifierEvaluator.new
48
+ 5.times { evaluator << MockClassifier.new(nil) }
49
+ evaluator.classifiers.each {|c| assert !c.built}
50
+ evaluator.build(Ai4r::Data::DataSet.new)
51
+ evaluator.classifiers.each {|c| assert c.built}
52
+ end
53
+
54
+ def test_eval
55
+ evaluator = ClassifierEvaluator.new
56
+ 5.times { |x| evaluator << MockClassifier.new(x) }
57
+ assert_equal [0, 1, 2, 3, 4], evaluator.eval([])
58
+ end
59
+
60
+ def test_test
61
+ evaluator = ClassifierEvaluator.new
62
+ 5.times { |x| evaluator << MockClassifier.new(x) }
63
+ input = Ai4r::Data::DataSet.new :data_items =>
64
+ [[0],[0],[0],[1],[2],[3]]
65
+ output = evaluator.test input
66
+ assert_equal 5, output.data_items.length # 5 classifiers, 5 result rows
67
+ output.data_items.each { |result| assert result[1]>=0 && result[1]<1} # eval time
68
+ assert_equal 3, output.data_items.first[2] # 3 errors for the 1st classifier
69
+ assert_equal 0.5, output.data_items.first[3] # succes rate
70
+ assert_equal 6, output.data_items.last[2] # 6 errors for the last classifier
71
+ assert_equal 0, output.data_items.last[3] # succes rate
72
+ end
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,58 @@
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
+ require File.dirname(__FILE__) + '/../../lib/ai4r/genetic_algorithm/genetic_algorithm'
12
+
13
+ require 'test/unit'
14
+
15
+ module Ai4r
16
+
17
+ module GeneticAlgorithm
18
+
19
+ COST = [
20
+ [ 0, 10, 12, 21, 25, 25, 34, 26, 28, 11],
21
+ [ 10, 0, 12, 21, 19, 21, 18, 12, 22, 11],
22
+ [ 10, 12, 0, 24, 18, 16, 36, 29, 17, 22],
23
+ [ 20, 12, 22, 0, 32, 34, 28, 24, 31, 9],
24
+ [ 23, 20, 19, 31, 0, 25, 29, 25, 31, 28],
25
+ [ 24, 20, 15, 33, 24, 0, 38, 34, 17, 25],
26
+ [ 33, 19, 35, 29, 24, 34, 0, 9, 38, 28],
27
+ [ 25, 13, 28, 25, 25, 34, 9, 0, 33, 19],
28
+ [ 30, 23, 18, 29, 31, 18, 38, 34, 0, 23],
29
+ [ 11, 11, 22, 9, 28, 26, 27, 19, 22, 0]
30
+ ]
31
+
32
+ class ChromosomeTest < Test::Unit::TestCase
33
+
34
+ def test_chromosome_seed
35
+ Chromosome.set_cost_matrix(COST)
36
+ chromosome = Chromosome.seed
37
+ assert_equal [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], chromosome.data.sort
38
+ end
39
+
40
+ def test_fitness
41
+ Chromosome.set_cost_matrix(COST)
42
+ chromosome = Chromosome.new([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
43
+ assert_equal( -206, chromosome.fitness)
44
+ end
45
+
46
+ def test_reproduce
47
+ Chromosome.set_cost_matrix(COST)
48
+ c1 = Chromosome.new([2, 8, 5, 3, 6, 7, 1, 9, 0, 4])
49
+ c2 = Chromosome.new([3, 2, 0, 1, 5, 4, 6, 7, 9, 8])
50
+ c3 = Chromosome.reproduce(c1, c2)
51
+ assert_equal([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], c3.data.sort)
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,81 @@
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 'test/unit'
12
+
13
+ module Ai4r
14
+
15
+ module GeneticAlgorithm
16
+
17
+ COUNTRY = %w"Belgium France Germany Ireland Italy Poland Portugal Spain Sweden UK"
18
+
19
+ # Belgium, France, Germany, Ireland, Italy, Poland, Portugal, Spain, Sweden, UK
20
+ COSTS = [
21
+ [ 0, 10, 12, 21, 25, 25, 34, 26, 28, 11],
22
+ [ 10, 0, 12, 21, 19, 21, 18, 12, 22, 11],
23
+ [ 10, 12, 0, 24, 18, 16, 36, 29, 17, 22],
24
+ [ 20, 12, 22, 0, 32, 34, 28, 24, 31, 9],
25
+ [ 23, 20, 19, 31, 0, 25, 29, 25, 31, 28],
26
+ [ 24, 20, 15, 33, 24, 0, 38, 34, 17, 25],
27
+ [ 33, 19, 35, 29, 24, 34, 0, 9, 38, 28],
28
+ [ 25, 13, 28, 25, 25, 34, 9, 0, 33, 19],
29
+ [ 30, 23, 18, 29, 31, 18, 38, 34, 0, 23],
30
+ [ 11, 11, 22, 9, 28, 26, 27, 19, 22, 0]
31
+ ]
32
+
33
+
34
+ class GeneticAlgorithmTest < Test::Unit::TestCase
35
+
36
+ def test_chromosome_seed
37
+ Chromosome.set_cost_matrix(COSTS)
38
+ chromosome = Chromosome.seed
39
+ assert_equal [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], chromosome.data.sort
40
+ end
41
+
42
+ def test_fitness
43
+ Chromosome.set_cost_matrix(COSTS)
44
+ chromosome = Chromosome.new([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
45
+ assert_equal( -206, chromosome.fitness)
46
+ end
47
+
48
+ def test_selection
49
+ search = GeneticSearch.new(10, 5)
50
+ search.generate_initial_population
51
+ selected = search.selection
52
+ selected.each { |c| assert c!=nil }
53
+ assert_equal 6, selected.length
54
+ assert_equal 1, search.population[0].normalized_fitness
55
+ assert_equal 0, search.population.last.normalized_fitness
56
+ assert_equal 10, search.population.length
57
+ end
58
+
59
+ def test_reproduction
60
+ search = GeneticSearch.new(10, 5)
61
+ search.generate_initial_population
62
+ selected = search.selection
63
+ offsprings = search.reproduction selected
64
+ assert_equal 3, offsprings.length
65
+ end
66
+
67
+ def test_replace_worst_ranked
68
+ search = GeneticSearch.new(10, 5)
69
+ search.generate_initial_population
70
+ selected = search.selection
71
+ offsprings = search.reproduction selected
72
+ search.replace_worst_ranked offsprings
73
+ assert_equal 10, search.population.length
74
+ offsprings.each { |c| assert search.population.include?(c)}
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,82 @@
1
+ #
2
+ # neural_network_test.rb
3
+ #
4
+ # This is a unit test file for the backpropagation neural network implemented
5
+ # in ai4r
6
+ #
7
+ # Author:: Sergio Fierens
8
+ # License:: MPL 1.1
9
+ # Project:: ai4r
10
+ # Url:: http://ai4r.rubyforge.org/
11
+ #
12
+ # You can redistribute it and/or modify it under the terms of
13
+ # the Mozilla Public License version 1.1 as published by the
14
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
15
+ #
16
+
17
+
18
+ require File.dirname(__FILE__) + '/../../lib/ai4r/neural_network/backpropagation'
19
+ require 'test/unit'
20
+
21
+ Ai4r::NeuralNetwork::Backpropagation.send(:public, *Ai4r::NeuralNetwork::Backpropagation.protected_instance_methods)
22
+ Ai4r::NeuralNetwork::Backpropagation.send(:public, *Ai4r::NeuralNetwork::Backpropagation.private_instance_methods)
23
+
24
+ module Ai4r
25
+
26
+ module NeuralNetwork
27
+
28
+
29
+ class BackpropagationTest < Test::Unit::TestCase
30
+
31
+
32
+ def test_init_network
33
+ net_4_2 = Backpropagation.new([4, 2]).init_network
34
+ assert_equal [[1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0]],
35
+ net_4_2.activation_nodes
36
+ assert_equal 1, net_4_2.weights.size
37
+ assert_equal 5, net_4_2.weights.first.size
38
+ net_4_2.weights.first.each do |weights_n|
39
+ assert_equal 2, weights_n.size
40
+ end
41
+
42
+ net_2_2_1 = Backpropagation.new([2, 2, 1]).init_network
43
+ assert_equal [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0]],
44
+ net_2_2_1.activation_nodes
45
+ assert_equal 2, net_2_2_1.weights.size
46
+ assert_equal 3, net_2_2_1.weights.first.size
47
+
48
+ net_2_2_1.disable_bias = true
49
+ net_2_2_1_no_bias = net_2_2_1.init_network
50
+ assert_equal [[1.0, 1.0], [1.0, 1.0], [1.0]],
51
+ net_2_2_1_no_bias.activation_nodes
52
+ end
53
+
54
+ def test_eval
55
+ #Test set 1
56
+ net = Backpropagation.new([3, 2])
57
+ y = net.eval([3, 2, 3])
58
+ assert y.length == 2
59
+ #Test set 2
60
+ net = Backpropagation.new([2, 4, 8, 10, 7])
61
+ y = net.eval([2, 3])
62
+ assert y.length == 7
63
+ end
64
+
65
+ def test_dump
66
+ net = Backpropagation.new([3, 2]).init_network
67
+ s = Marshal.dump(net)
68
+ x = Marshal.load(s)
69
+ assert_equal net.structure, x.structure
70
+ assert_equal net.disable_bias, x.disable_bias
71
+ assert_equal net.learning_rate, x.learning_rate
72
+ assert_equal net.momentum, x.momentum
73
+ assert_equal net.weights, x.weights
74
+ assert_equal net.last_changes, x.last_changes
75
+ assert_equal net.activation_nodes, x.activation_nodes
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,72 @@
1
+ # This is a unit test file for the hopfield neural network AI4r implementation
2
+ #
3
+ # Author:: Sergio Fierens
4
+ # License:: MPL 1.1
5
+ # Project:: ai4r
6
+ # Url:: http://ai4r.rubyforge.org/
7
+ #
8
+ # You can redistribute it and/or modify it under the terms of
9
+ # the Mozilla Public License version 1.1 as published by the
10
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
11
+
12
+ require File.dirname(__FILE__) + '/../../lib/ai4r'
13
+ require 'test/unit'
14
+
15
+ Ai4r::NeuralNetwork::Hopfield.send(:public, *Ai4r::NeuralNetwork::Hopfield.protected_instance_methods)
16
+
17
+ module Ai4r
18
+
19
+ module NeuralNetwork
20
+
21
+
22
+ class HopfieldTest < Test::Unit::TestCase
23
+
24
+ def setup
25
+ @data_set = Ai4r::Data::DataSet.new :data_items => [
26
+ [1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1],
27
+ [-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1],
28
+ [-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1,1],
29
+ [1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1],
30
+ ]
31
+ end
32
+
33
+ def test_initialize_nodes
34
+ net = Hopfield.new
35
+ data_set = Ai4r::Data::DataSet.new :data_items => [[1,1,0,0,1,1,0,0]]
36
+ assert_equal [-1,-1,-1,-1,-1,-1,-1,-1], net.initialize_nodes(data_set)
37
+ end
38
+
39
+ def test_initialize_weights
40
+ net = Hopfield.new
41
+ net.initialize_nodes @data_set
42
+ net.initialize_weights(@data_set)
43
+ assert_equal 15, net.weights.length
44
+ net.weights.each_with_index {|w_row, i| assert_equal i+1, w_row.length}
45
+ end
46
+
47
+ def test_run
48
+ net = Hopfield.new
49
+ net.train @data_set
50
+ pattern = [1,1,-1,1,1,1,-1,-1,1,1,-1,-1,1,1,1,-1]
51
+ 100.times do
52
+ pattern = net.run(pattern)
53
+ end
54
+ assert_equal [1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1], pattern
55
+ end
56
+
57
+ def test_eval
58
+ net = Hopfield.new
59
+ net.train @data_set
60
+ p = [1,1,-1,1,1,1,-1,-1,1,1,-1,-1,1,1,1,-1]
61
+ assert_equal @data_set.data_items[0], net.eval(p)
62
+ p = [-1,-1,1,1,1,-1,1,1,-1,-1,1,-1,-1,-1,1,1]
63
+ assert_equal @data_set.data_items[1], net.eval(p)
64
+ p = [-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,-1,-1]
65
+ assert_equal @data_set.data_items[2], net.eval(p)
66
+ p = [-1,-1,1,1,1,1,1,1,-1,-1,-1,-1,1,-1,-1,-1]
67
+ assert_equal @data_set.data_items[3], net.eval(p)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,97 @@
1
+ # This is a unit test file for the SOM algorithm implemented
2
+ # in ai4r
3
+ #
4
+ # Author:: Thomas Kern
5
+ # License:: MPL 1.1
6
+ # Project:: ai4r
7
+ # Url:: http://ai4r.rubyforge.org/
8
+ #
9
+ # You can redistribute it and/or modify it under the terms of
10
+ # the Mozilla Public License version 1.1 as published by the
11
+ # Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
12
+
13
+ require File.dirname(__FILE__) + '/../../lib/ai4r/som/som'
14
+ require 'test/unit'
15
+
16
+ module Ai4r
17
+
18
+ module Som
19
+
20
+ class SomTest < Test::Unit::TestCase
21
+
22
+ def setup
23
+ @som = Som.new 2, 5, Layer.new(3, 3)
24
+ @som.initiate_map
25
+ end
26
+
27
+ def test_random_initiation
28
+ assert_equal 25, @som.nodes.length
29
+
30
+ @som.nodes.each do |node|
31
+ assert_equal 2, node.weights.length
32
+
33
+ node.weights.each do |weight|
34
+ assert weight < 1
35
+ assert weight > 0
36
+ end
37
+
38
+ end
39
+ end
40
+
41
+
42
+ # bmu
43
+
44
+ def test_find_bmu
45
+ bmu = @som.find_bmu([0.5, 0.5])
46
+ end
47
+
48
+ def test_adjust_nodes
49
+ @som.adjust_nodes [1, 2], @som.find_bmu([0.5, 0.5]), 2, 0.1
50
+ end
51
+
52
+ def test_access_to_nodes
53
+ assert_raise Exception do
54
+ @som.get_node(5, 5)
55
+ end
56
+
57
+ assert_raise Exception do
58
+ @som.get_node(5, -3)
59
+ end
60
+
61
+ assert_equal Node, @som.get_node(0, 0).class
62
+ end
63
+
64
+ def test_distance_for_same_row
65
+ assert_equal 2, distancer(0, 0, 0, 2)
66
+ assert_equal 2, distancer(0, 4, 0, 2)
67
+ assert_equal 0, distancer(0, 0, 0, 0)
68
+ end
69
+
70
+ def test_distance_for_same_column
71
+ assert_equal 1, distancer(0, 0, 1, 0)
72
+ assert_equal 2, distancer(2, 0, 0, 0)
73
+ end
74
+
75
+ def test_distance_for_diagonally_point
76
+ assert_equal 1, distancer(1, 0, 0, 1)
77
+ assert_equal 2, distancer(2, 2, 0, 0)
78
+ assert_equal 2, distancer(3, 2, 1, 4)
79
+ end
80
+
81
+ def test_distance_for_screwed_diagonally_point
82
+ assert_equal 2, distancer(0, 0, 2, 1)
83
+ assert_equal 4, distancer(3, 4, 1, 0)
84
+ assert_equal 2, distancer(3, 2, 1, 3)
85
+ end
86
+
87
+ private
88
+
89
+ def distancer(x1, y1, x2, y2)
90
+ @som.get_node(x1, y1).distance_to_node(@som.get_node(x2, y2))
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
97
+ end