Class: MM::Space

Inherits:
Object
  • Object
show all
Defined in:
lib/mm/space.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric, opts = {}) ⇒ Space

Initialization method for MM::Space

metric - Array of MM::Metrics, where each metric corresponds to a dimension

in the MM::Space.

opts - Hash with additional parameters. (default: {})

:delta - The delta of the MM::Search function used in #morph.
  (default: 0.001)
:boundaries - Array of same size as metric containing pairs [low,
  high], which should be the bounding vectors of a given MM::Space.
:adjacent_points_function - Proc to use as the
  adjacent_points_function for MM::Search in #morph.
:cost_function - Proc to use for cost_function for MM::Search in
  #morph.

Returns an MM::Space object



25
26
27
28
29
30
31
32
# File 'lib/mm/space.rb', line 25

def initialize metric, opts = {}
  @metric = metric
  @search_klass = opts[:search_klass] || MM::Search
  @delta = opts[:delta] || 0.001
  @boundaries = opts[:boundaries]
  @adjacent_points_function = opts[:adjacent_points_function]
  @cost_function = opts[:cost_function]
end

Instance Attribute Details

#adjacent_points_functionObject

Default adjacent_points_function. It takes all repeated permutations of a given morph.

Returns the adjacent_points_function Proc.



107
108
109
110
111
112
# File 'lib/mm/space.rb', line 107

def adjacent_points_function
  @adjacent_points_function ||
  ->(current_point) {
    current_point.repeated_permutation(current_point.size)
  }
end

#boundariesObject

Returns the value of attribute boundaries.



7
8
9
# File 'lib/mm/space.rb', line 7

def boundaries
  @boundaries
end

#cost_function(start_morph, to) ⇒ Object

Default cost_function to use if no other one is specified. Takes the root of the sum of the squares, or the generalized Euclidean distance.

start_morph - morph to begin the morph from. This should be a valid morph in

the space (i.e., not out of bounds), and should also work with MM::Metric.

to - Destination vector. There should be one dimension in the Array for each

element in @metric

Returns a Proc that calculates how much the current difference vector differs from the requested difference vector.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mm/space.rb', line 86

def cost_function start_morph, to
  @cost_function ||
  ->(current_point) {
    @metric.zip(to).inject(0) {|memo, x|
      distance = x[0].call(start_morph, current_point)
      unless @boundaries.nil?
        start_to_lowest = x[0].call(start_morph, @boundaries[0][0])
        current_to_lowest = x[0].call(current_point, @boundaries[0][0])
        if start_to_lowest > current_to_lowest
          distance = distance * -1.0
        end
      end
      memo = memo + (distance - x[1]).abs ** 2
    } ** 0.5
  }
end

#deltaObject

Returns the value of attribute delta.



6
7
8
# File 'lib/mm/space.rb', line 6

def delta
  @delta
end

#max_distanceObject

Returns the value of attribute max_distance.



7
8
9
# File 'lib/mm/space.rb', line 7

def max_distance
  @max_distance
end

#metricObject

Returns the value of attribute metric.



7
8
9
# File 'lib/mm/space.rb', line 7

def metric
  @metric
end

#search_klassObject

Returns the value of attribute search_klass.



6
7
8
# File 'lib/mm/space.rb', line 6

def search_klass
  @search_klass
end

Instance Method Details

#enter(locals = {}, &block) ⇒ Object

Allows for morphing within a given block.

locals - Hash of key-value pairs where the key is the name of the local

variable to be created and the value is the value of that variable. Note
that it actually creates *methods*, rather than *variables*, and that these
methods refer to instance variables that are then removed. It could get
buggy if you were to try to create a variable that was the same name as an
existing method or class variable. (default: {})

block - Block to evaluate within the context of the instance.

Returns the last element returned by the block.



132
133
134
135
136
137
# File 'lib/mm/space.rb', line 132

def enter locals = {}, &block
  create_local_variables locals
  output = instance_eval &block
  remove_local_variables locals
  output
end

#morph(start_morph, to: nil, current_point: nil) ⇒ Object

Morphs to a given point within the space

start_morph - Enumerable object of things to morph from to - Array to morph to, with one element for each dimension

Returns Array of resulting MM::Ratio objects



40
41
42
43
44
45
46
47
# File 'lib/mm/space.rb', line 40

def morph start_morph, to: nil, current_point: nil
  if current_point
    # puts "Finding from #{current_point.map {|r| r.join ' '}}"
    searcher(start_morph, to).find_from_point current_point
  else
    searcher(start_morph, to).find
  end
end