Class: Ai4r::Som::Node

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

Overview

Represents a node in the self-organizing map grid.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Class Method Details

.create(id, _rows, columns, dimensions, options = {}) ⇒ Object

creates an instance of Node and instantiates the weights

id

unique identifier for this node

rows

number of rows of the SOM grid

columns

number of columns of the SOM grid

dimensions

dimension of the input vector

Options Hash (options):

  • :range (Range) — default: 0..1

    range used to initialize weights

  • :random_seed (Integer)

    Seed for Ruby’s RNG. The deprecated :seed key is supported for backward compatibility.



56
57
58
59
60
61
62
63
64
# File 'lib/ai4r/som/node.rb', line 56

def self.create(id, _rows, columns, dimensions, options = {})
  n = Node.new
  n.id = id
  n.distance_metric = options[:distance_metric] || :chebyshev
  n.instantiate_weight dimensions, options
  n.x = id % columns
  n.y = (id / columns.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



98
99
100
101
102
103
104
105
# File 'lib/ai4r/som/node.rb', line 98

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



117
118
119
120
121
122
# File 'lib/ai4r/som/node.rb', line 117

def distance_to_node(node)
  dx = x - node.x
  dy = y - node.y
  metric = @distance_metric || :chebyshev
  DistanceMetrics.send(metric, dx, dy)
end

#instantiate_weight(dimensions, options = {}) ⇒ Object

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

Options Hash (options):

  • :range (Range) — default: 0..1

    range used to initialize weights

  • :random_seed (Integer)

    Seed for Ruby’s RNG. The deprecated :seed key is supported for backward compatibility.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ai4r/som/node.rb', line 74

def instantiate_weight(dimensions, options = {})
  opts = { range: 0..1, random_seed: nil, seed: nil, rng: nil }.merge(options)
  rng = opts[:rng]
  unless rng
    seed = opts[:random_seed] || opts[:seed]
    rng = seed.nil? ? Random.new : Random.new(seed)
  end
  range = opts[:range] || (0..1)
  min = range.first.to_f
  max = range.last.to_f
  span = max - min
  @weights = Array.new dimensions
  @instantiated_weight = Array.new dimensions
  @weights.each_index do |index|
    @weights[index] = min + (rng.rand * span)
    @instantiated_weight[index] = @weights[index]
  end
end