Class: Function

Inherits:
Object
  • Object
show all
Includes:
Defuzz, Norm
Defined in:
lib/rfuzzy/function.rb

Overview

Basic 2d adherence function

Direct Known Subclasses

Trapezoidal, Triangular

Constant Summary collapse

EPSILON =
1.0e-10
@@step_size =
nil

Constants included from Defuzz

Defuzz::DEFUZZ_METHODS

Constants included from Norm

Norm::NORM_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Defuzz

defuzz_method

Methods included from Norm

norm_method, #s_norm_max_min, #s_norm_prob, #t_norm_max_min, #t_norm_prob

Constructor Details

#initialize(name, *args, &block) ⇒ Function

Default constructor. Usage: .new(String name, Point *points)



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

def initialize(name,*args, &block)
	@name = name
	@points = args
	if block_given?
		yield self
	end
	@points.sort
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/rfuzzy/function.rb', line 19

def name
  @name
end

#pointsObject

Returns the value of attribute points.



19
20
21
# File 'lib/rfuzzy/function.rb', line 19

def points
  @points
end

Class Method Details

.step_size(s) ⇒ Object

Sets the step resolution used if automatic step detection is used



11
12
13
14
15
16
17
# File 'lib/rfuzzy/function.rb', line 11

def self.step_size(s)
	if s <= 0
		raise ArgumentError, "Step size must be greater than 0"
	else
		@@step_size = s
	end
end

Instance Method Details

#*(fl) ⇒ Object

Returns a new Function with y coordinate multiplied by the given factor. If the new y value is greater than one 1.0 is assigned. This can be used for product-like Mamdani rules. Usage: .*(Float factor)



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rfuzzy/function.rb', line 59

def *(fl)
	fl = fl.to_f
	unless fl >= 0
		raise ArgumentError, "Argument should be positive"
	end
	new_function = Function.new("#{@name}*#{fl}")
	@points.each do |p|
		new_function.points.push Point.new(p.x, [p.y*fl, 1.0].min)
	end
	return new_function
end

#add(*points) ⇒ Object

Adds points to the function. Usage: .add(Point *points)



89
90
91
92
93
94
95
96
97
# File 'lib/rfuzzy/function.rb', line 89

def add(*points)
	points.each do |p|
		if @points.include? p
			@points.delete p
		end
	end
	@points.concat points
	@points.sort!
end

#and(other, step = :auto, method = :auto) ⇒ Object

Method for performing and (t-norm) operation. Usage: <tt>.and(Function f, Float step, Symbol method) <tt>.and(Function f, Float step) <tt>.and(Function f)



132
133
134
135
136
137
138
# File 'lib/rfuzzy/function.rb', line 132

def and(other, step = :auto, method = :auto)
	res = apply_norm(:t, other, step, method)
	if block_given?
		yield res
	end
	return res
end

#at(x) ⇒ Object

Calculates function value at x using linear approximation. Function boundaries are from -Infinity to +Infinity. Usage: .at(Float x)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rfuzzy/function.rb', line 110

def at(x)
	if @points.first.x >= x
		return @points.first.y
	elsif @points.last.x <= x
		return @points.last.y
	else
		@points.each_index do |i|
			if x >= @points[i].x && x < @points[i+1].x
				dy = @points[i+1].y - @points[i].y
				dx = @points[i+1].x - @points[i].x
				du = (dy/dx)*(x - @points[i].x)
				return @points[i].y + du 
			end
		end
	end
end

#cut_at(fl) ⇒ Object

Returns a new Function with y coordinate less than given factor. This can be used for max-min-like Mamdani rules. Usage: .cut_at(Float factor)



76
77
78
79
80
81
82
83
84
85
# File 'lib/rfuzzy/function.rb', line 76

def cut_at(fl)
	unless fl >= 0 && fl <= 1
		raise ArgumentError, "Argument should be from [0;1]"
	end
	new_function = Function.new("#{@name} MAX #{fl}")
	@points.each do |p|
		new_function.points.push Point.new(p.x, [p.y,fl].min)
	end
	return new_function
end

#defuzz(method = :auto) ⇒ Object

defuzzification



167
168
169
170
171
172
173
174
175
176
# File 'lib/rfuzzy/function.rb', line 167

def defuzz(method = :auto)
	if(method == :auto)
		unless @@defuzz_method.nil?
			method = @@defuzz_method
		else
			raise ArgumentError, "Specify method, or call Norm::norm_method"
		end
	end
	method(DEFUZZ_METHODS[method]).call(self)
end

#initialize_copy(other, &block) ⇒ Object

Copy constructor.



33
34
35
# File 'lib/rfuzzy/function.rb', line 33

def initialize_copy(other, &block)
	initialize(other.name, other.points, block)
end

#not(&block) ⇒ Object

Negation. Usage: .not



155
156
157
158
159
160
161
162
163
164
# File 'lib/rfuzzy/function.rb', line 155

def not(&block)
	new_function = Function.new("not #{@name}")
	@points.each_index do |i|
		new_function.points[i] = Point.new(@points[i].x, 1 - @points[i].y)
	end
	if block_given?
		yield new_function
	end
	return new_function
end

#or(other, step = :auto, method = :auto) ⇒ Object

Method for performing or (s-norm) operation. Usage: <tt>.or(Function f, Float step, Symbol method) <tt>.or(Function f, Float step) <tt>.or(Function f)



145
146
147
148
149
150
151
# File 'lib/rfuzzy/function.rb', line 145

def or(other, step = :auto, method = :auto)
	res = apply_norm(:s, other, step, method)
	if block_given?
		yield res
	end
	return res
end

#remove(*points) ⇒ Object

Removes points from the function. Usage: .remove(Point *points)



101
102
103
104
105
# File 'lib/rfuzzy/function.rb', line 101

def remove(*points)
	points.each do |p|
		@points.delete p
	end
end

#to_gplotObject

For use with GNUplot



46
47
48
49
50
51
52
# File 'lib/rfuzzy/function.rb', line 46

def to_gplot
	str = ""
	@points.each do |p|
		str << "#{p.x} #{p.y}\n"
	end
	return str
end

#to_sObject



37
38
39
40
41
42
43
# File 'lib/rfuzzy/function.rb', line 37

def to_s
	str = "ADHERENCE FUNCTION\n\tPoints:\n"
	@points.each do |point|
		str << "\t#{point}\n"
	end
	return str
end