Class: Ai4r::Som::Node

Inherits:
Object
  • Object
show all
Includes:
Data::Parameterizable
Defined in:
lib/ai4r/som/node.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Class Method Details

.create(id, total, dimensions) ⇒ Object

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


42
43
44
45
46
47
48
49
# 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

Instance Method Details

#distance_to_input(input) ⇒ Object

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


65
66
67
68
69
70
71
72
# 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) ⇒ Object

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


82
83
84
# 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) ⇒ Object

instantiates the weights to the dimension (of the input vector) for backup reasons, the instantiated weight is stored into @instantiated_weight as well


53
54
55
56
57
58
59
60
# 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