Class: FuzzyAssociativeMemory::Triangle

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, center, right, height = 1.0) ⇒ Triangle

Returns a new instance of Triangle.



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

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

  @center = center.to_f
  @left   = left.to_f
  @right  = right.to_f
  @height = height.to_f
end

Instance Attribute Details

#centerObject (readonly)

Returns the value of attribute center.



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

def center
  @center
end

#heightObject

Returns the value of attribute height.



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

def height
  @height
end

#leftObject (readonly)

Returns the value of attribute left.



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

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



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

def right
  @right
end

Instance Method Details

#centroid_xObject



32
33
34
# File 'lib/fuzzy_associative_memory/triangle.rb', line 32

def centroid_x
  (@left + @right + @center) / 3.0
end

#larsen(ratio) ⇒ Object



40
41
42
43
44
# File 'lib/fuzzy_associative_memory/triangle.rb', line 40

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

#mamdani(clip_height) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/fuzzy_associative_memory/triangle.rb', line 46

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

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

#mu(value) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/fuzzy_associative_memory/triangle.rb', line 23

def mu(value)
  raise ArgumentError, "value passed to Triangle::mu() cannot be nil" if value.nil?
  if value < @left || value > @right
    0.0
  else
    1 - ((@center-value).abs / ((@right - @left) / 2.0 ))
  end
end

#to_sObject



55
56
57
# File 'lib/fuzzy_associative_memory/triangle.rb', line 55

def to_s
  "Triangle {#{@left}/#{@center}/#{@right}, height #{@height}}"
end