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.



4
5
6
# File 'lib/fuzzy_set.rb', line 4

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

Instance Attribute Details

#pointsObject (readonly)

Returns the value of attribute points.



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

def points
  @points
end

Class Method Details

.trapezoid(array) ⇒ Object

Raises:

  • (Exception)


8
9
10
11
12
13
14
15
16
# File 'lib/fuzzy_set.rb', line 8

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



65
66
67
68
69
70
71
72
# File 'lib/fuzzy_set.rb', line 65

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



49
50
51
52
53
54
55
# File 'lib/fuzzy_set.rb', line 49

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fuzzy_set.rb', line 98

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

#intersections(other) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fuzzy_set.rb', line 26

def intersections(other)
  if other.is_a?(Line)
    points = []
    toLines.each { |mline| 
      points << mline.intersect(other)
    }
    points.select { |point| (self[point.x]-point.y).abs <= EPSILON }
  elsif other.is_a?(FuzzySet)
    myLines = toLines
    points = []
    other.toLines.each { |line| 
      myLines.each { |mline|
        points << mline.intersect(line)
      }
    }
    points.select { |point|
      (self[point.x]-point.y).abs + (other[point.x]-point.y).abs <= EPSILON
    }
  else
    raise Exception.new("Unable to count intersection")
  end
end

#min(value) ⇒ Object



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

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



88
89
90
91
# File 'lib/fuzzy_set.rb', line 88

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

#scale!(factor) ⇒ Object



93
94
95
96
# File 'lib/fuzzy_set.rb', line 93

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

#toLinesObject



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

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

#weigthCenterObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fuzzy_set.rb', line 75

def weigthCenter()
  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