Class: FuzzyAssociativeMemory::Trapezoid

Inherits:
FuzzySet
  • Object
show all
Defined in:
lib/fuzzy_associative_memory/trapezoid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, top_left, top_right, right, height = 1.0) ⇒ Trapezoid

Returns a new instance of Trapezoid.



14
15
16
17
18
19
20
21
22
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 14

def initialize(left, top_left, top_right, right, height=1.0)
  # TODO validations

  @left      = left.to_f
  @top_left  = top_left.to_f
  @top_right = top_right.to_f
  @right     = right.to_f
  @height    = height.to_f
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



12
13
14
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 12

def height
  @height
end

#leftObject (readonly)

Returns the value of attribute left.



12
13
14
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 12

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



12
13
14
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 12

def right
  @right
end

#top_leftObject (readonly)

Returns the value of attribute top_left.



12
13
14
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 12

def top_left
  @top_left
end

#top_rightObject (readonly)

Returns the value of attribute top_right.



12
13
14
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 12

def top_right
  @top_right
end

Instance Method Details

#centroid_xObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 37

def centroid_x
  a = @top_right - @top_left
  b = @right - @left
  c = @top_left - @left

  cx = (2.0*a*c + a**2 + c*b + a*b + b**2.0) / (3.0 * (a+b))
  cx + @left
  # cy = (@height * (2.0*a + b)) / (3.0 * (a+b))
  # [cx+@left, cy]
end

#larsen(ratio) ⇒ Object



52
53
54
55
56
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 52

def larsen(ratio)
  t = self.dup
  t.height=(t.height * ratio)
  t
end

#mamdani(clip_height) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 58

def mamdani(clip_height)
  left      = @left
  top_left  = @left + (clip_height * (@top_left - @left))
  top_right = @right - (clip_height * (@right - @top_right))
  right     = @right

  FuzzyAssociativeMemory::Trapezoid.new(left, top_left, top_right, right, clip_height)
end

#mu(value) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 24

def mu(value)
  raise ArgumentError, "value passed to Trapezoid::mu() cannot be nil" if value.nil?
  if value < @left || value > @right
    0.0
  elsif value >= @left && value < @top_left
    (value - @left) / (@top_left - @left)
  elsif value >= @top_left && value <= @top_right
    1.0
  else
    (@right - value) / (@right - @top_right)
  end
end

#to_sObject



67
68
69
# File 'lib/fuzzy_associative_memory/trapezoid.rb', line 67

def to_s
  "Trapezoid {#{@left}/#{@top_left}/#{@top_right}/#{@right}, height #{@height}}"
end