class Ai4r::Som::Node
this class is used for the individual node and will be (nodes * nodes)-time instantiated
attributes¶ ↑
-
direct access to the x and y values is granted, those show the position of the node in
the square map
-
id => is the uniq and sequential ID of the node
-
weights => values of the current weights are stored in an array of dimension 'dimensions'.
Weights are of type float
-
instantiated_weight => the values of the first instantiation of weights. these values are
never changed
Public Class Methods
creates an instance of Node and instantiates the weights the parameters is a uniq and sequential ID as well as the number of total nodes dimensions signals the dimension of the input vector
# File lib/ai4r/som/node.rb, line 42 def self.create(id, total, dimensions) n = Node.new n.id = id n.instantiate_weight dimensions n.x = id % total n.y = (id / total.to_f).to_i n end
Public Instance Methods
returns the square distance between the current weights and the input the input is a vector/array of the same size as weights at the end, the square root is extracted from the sum of differences
# File lib/ai4r/som/node.rb, line 65 def distance_to_input(input) dist = 0 input.each_with_index do |i, index| dist += (i - @weights[index]) ** 2 end Math.sqrt(dist) end
returns the distance in square-form from the instance node to the passed node example: 2 2 2 2 2 2 1 1 1 2 2 1 0 1 2 2 1 1 1 2 2 2 2 2 2 0 being the current node
# File lib/ai4r/som/node.rb, line 82 def distance_to_node(node) max((self.x - node.x).abs, (self.y - node.y).abs) end
instantiates the weights to the dimension (of the input vector) for backup reasons, the instantiated weight is stored into @instantiated_weight as well
# File lib/ai4r/som/node.rb, line 53 def instantiate_weight(dimensions) @weights = Array.new dimensions @instantiated_weight = Array.new dimensions @weights.each_with_index do |weight, index| @weights[index] = rand @instantiated_weight[index] = @weights[index] end end
Private Instance Methods
# File lib/ai4r/som/node.rb, line 88 def max(a, b) a > b ? a : b end