Class: FuzzySet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points) ⇒ FuzzySet

Returns a new instance of FuzzySet.



14
15
16
# File 'lib/fuzzy_set.rb', line 14

def initialize(points)
  @points = points.sort
end

Instance Attribute Details

#pointsObject (readonly)

Returns the value of attribute points.



42
43
44
# File 'lib/fuzzy_set.rb', line 42

def points
  @points
end

Class Method Details

.trapezoid(array) ⇒ Object

Raises:

  • (Exception)


4
5
6
7
8
9
10
11
12
# File 'lib/fuzzy_set.rb', line 4

def self.trapezoid(array)
  raise Exception.new("Trapezoid must have array length 4") if array.length != 4
  points = []
  points << Point.new(array[0], 0)
  points << Point.new(array[1], SCALE)
  points << Point.new(array[2], SCALE) unless array[2] == array[1]
  points << Point.new(array[3], 0)
  FuzzySet.new(points)
end

Instance Method Details

#&(other) ⇒ Object

Choose min of current and other set



34
35
36
37
38
39
40
41
# File 'lib/fuzzy_set.rb', line 34

def &(other)
  points = [@points, crosspoints(other)].flatten.uniq.sort
  res = []
  points.each { |point|
    res << point if self[point.x]-EPSILON < point.y and self[point.x]+EPSILON > point.y and other[point.x]+EPSILON >= self[point.x]
  }
  FuzzySet.new(res)
end

#+(other) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/fuzzy_set.rb', line 18

def +(other)
  points = [@points, other.points, intersections(other)].flatten.uniq.sort
  res = points.reject { |point|
     self[point.x]-EPSILON > point.y or other[point.x]-EPSILON > point.y
  }
  FuzzySet.new(res)
end

#[](value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fuzzy_set.rb', line 77

def [](value)
  if value<@points[0].x
    return 0
  elsif value > @points.last.x
    return 0
  end
  idx = 0
  while (@points[idx].x < value)
    idx += 1
  end
  return @points[idx].y if @points[idx].x == value
  x1 = @points[idx-1].x
  x2 = @points[idx].x
  y1 = @points[idx-1].y
  y2 = @points[idx].y
  return 1.0*(y2 - y1)/(x2-x1) * (value - x1) + y1
end

#centerOfGravityObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fuzzy_set.rb', line 44

def centerOfGravity()
  nominator = 0.0
  denominator = 0.0
  for i in 1..@points.length-1
    line = Line.new(@points[i], @points[i-1])
    x2 = points[i].x
    x1 = points[i-1].x
    nominator += line.a * x2**3/3 + line.b * x2**2/2 - line.a * x1**3/3 - line.b * x1**2/2
    denominator += line.a * x2**2/2 + line.b * x2 - line.a * x1**2/2 - line.b * x1
  end
  nominator/denominator
end

#firstMinimumObject



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

def firstMinimum()
  maxIdx = 0
  for i in 0..@points.length-1
    if @points[i].y > @points[maxIdx].y
      maxIdx = i
    end
  end
  @points[maxIdx].x
end

#min(value) ⇒ Object



26
27
28
29
30
31
# File 'lib/fuzzy_set.rb', line 26

def min(value)
  line = Line.new(Point.new(0, value), Point.new(1, value))
  points = [@points, intersections(line)].flatten.uniq.sort
  res = points.reject { |p| self[p.x] - EPSILON > value }
  FuzzySet.new(res)
end

#scale(factor) ⇒ Object



67
68
69
70
# File 'lib/fuzzy_set.rb', line 67

def scale(factor)
  fs = FuzzySet.new(@points.map { |p| p.clone() })
  fs.scale!(factor)
end

#scale!(factor) ⇒ Object



72
73
74
75
# File 'lib/fuzzy_set.rb', line 72

def scale!(factor)
  @points.each { |point| point.y *= factor / SCALE }
  self
end

#toLinesObject



95
96
97
98
99
100
101
# File 'lib/fuzzy_set.rb', line 95

def toLines
  lines = []
  for i in 1..@points.length-1
    lines << Line.new(@points[i], @points[i-1])
  end
  lines
end