class Ai4r::Som::Node

this class is used for the individual node and will be (nodes * nodes)-time instantiated

attributes

the square map

Weights are of type float

never changed

Public Class Methods

create(id, total, dimensions) click to toggle source

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

distance_to_input(input) click to toggle source

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
distance_to_node(node) click to toggle source

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
instantiate_weight(dimensions) click to toggle source

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

max(a, b) click to toggle source
# File lib/ai4r/som/node.rb, line 88
def max(a, b)
  a > b ? a : b
end