Class: Fuzzyrb::FuzzySet
- Inherits:
-
Object
- Object
- Fuzzyrb::FuzzySet
- Defined in:
- lib/fuzzy_set.rb
Instance Attribute Summary collapse
-
#points ⇒ Object
readonly
Returns the value of attribute points.
Class Method Summary collapse
Instance Method Summary collapse
-
#&(other) ⇒ Object
Choose min of current and other set.
- #+(other) ⇒ Object
- #[](value) ⇒ Object
- #centerOfGravity ⇒ Object
- #firstMaximum ⇒ Object
-
#initialize(points) ⇒ FuzzySet
constructor
A new instance of FuzzySet.
- #min(value) ⇒ Object
- #scale(factor) ⇒ Object
- #scale!(factor) ⇒ Object
- #toLines ⇒ Object
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.uniq_values end |
Instance Attribute Details
#points ⇒ Object (readonly)
Returns the value of attribute points.
43 44 45 |
# File 'lib/fuzzy_set.rb', line 43 def points @points end |
Class Method Details
.trapezoid(array) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/fuzzy_set.rb', line 3 def self.trapezoid(array) raise Exception.new("Trapezoid must have array length 4") if array.length != 4 raise Exception.new("Trapezoid arguments must be nondecreasing") unless array.non_decreasing points = [] points << Point.new(array[0], 0) unless array[1] == array[0] points << Point.new(array[1], SCALE) points << Point.new(array[2], SCALE) unless array[2] == array[1] points << Point.new(array[3], 0) unless array[3] == array[2] FuzzySet.new(points) end |
Instance Method Details
#&(other) ⇒ Object
Choose min of current and other set
35 36 37 38 39 40 41 42 |
# File 'lib/fuzzy_set.rb', line 35 def &(other) points = [@points, crosspoints(other)].flatten.uniq_values.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_values.sort res = points.reject { |point| self[point.x]-EPSILON > point.y or other[point.x]-EPSILON > point.y } FuzzySet.new(res) end |
#[](value) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/fuzzy_set.rb', line 84 def [](value) if value<@points[0].x return @points[0].y elsif value > @points.last.x return @points.last.y 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 |
#centerOfGravity ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/fuzzy_set.rb', line 45 def centerOfGravity() nominator = 0.0 denominator = 0.0 # puts "Length: #{@points.length}" for i in 1..@points.length-1 line = Line.new(@points[i], @points[i-1]) # puts "#{nominator}, #{denominator}, #{line.a}, #{line.b}" # pp @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 return 0 if nominator.abs <= EPSILON raise Exception.new("Runtime exception: denominator cannot be null") if denominator == 0 nominator/denominator end |
#firstMaximum ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/fuzzy_set.rb', line 64 def firstMaximum() 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 32 |
# 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_values.sort.map { |p| Point.new(p.x, [p.y, value].min)} # reject all points that has higher membership than this fuzzy set res = points.reject { |p| self[p.x] - EPSILON > value } FuzzySet.new(res) end |
#scale(factor) ⇒ Object
74 75 76 77 |
# File 'lib/fuzzy_set.rb', line 74 def scale(factor) fs = FuzzySet.new(@points.map { |p| p.clone() }) fs.scale!(factor) end |
#scale!(factor) ⇒ Object
79 80 81 82 |
# File 'lib/fuzzy_set.rb', line 79 def scale!(factor) @points.each { |point| point.y *= factor / SCALE } self end |