fuzzyrb 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +4 -0
- data/README.txt +2 -2
- data/examples/compare_models.rb +59 -33
- data/lib/fuzzy.rb +4 -2
- data/lib/fuzzy_implication.rb +8 -6
- data/lib/fuzzy_rule.rb +2 -1
- data/test/test_fuzzy_implication.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +3 -3
- metadata.gz.sig +0 -0
data/History.txt
CHANGED
data/README.txt
CHANGED
@@ -13,8 +13,8 @@ Implements Fuzzy Sets in Ruby. I am very beginner at this topic, so it is very b
|
|
13
13
|
* No error handling.
|
14
14
|
* Defuzzification as center of gravity and first maximum
|
15
15
|
* Minimum and Multiplication T-Norms.
|
16
|
-
*
|
17
|
-
* Reasoning - apply matching rule and combine the results.
|
16
|
+
* Mamdani and Larsen aggregation methods.
|
17
|
+
* Reasoning - apply matching rule and combine the results. Mamdani or Takagi-Sugeno system.
|
18
18
|
|
19
19
|
== SYNOPSIS:
|
20
20
|
|
data/examples/compare_models.rb
CHANGED
@@ -17,9 +17,32 @@
|
|
17
17
|
# In this example we try to model the function f(x, y) = x + y using different
|
18
18
|
# parameters. As a result we get couple graphs of difference between the model
|
19
19
|
# and the function and sum of errors (differences)
|
20
|
+
#
|
21
|
+
# To use gnuplot you have to apply a little diff on gnuplot 2.2 from
|
22
|
+
# RubyForge:
|
23
|
+
# *** 250,256 ****
|
24
|
+
# y.each_with_index do |yv, j|
|
25
|
+
# f << [ xv, yv, d[i][j] ].join(" ") << "\n"
|
26
|
+
# end
|
27
|
+
# ! # f << "\n"
|
28
|
+
# end
|
29
|
+
# elsif ( self[0].kind_of? Numeric ) then
|
30
|
+
# self.length.times do |i| f << "#{self[i]}\n" end
|
31
|
+
# --- 250,256 ----
|
32
|
+
# y.each_with_index do |yv, j|
|
33
|
+
# f << [ xv, yv, d[i][j] ].join(" ") << "\n"
|
34
|
+
# end
|
35
|
+
# ! f << "\n"
|
36
|
+
# end
|
37
|
+
# elsif ( self[0].kind_of? Numeric ) then
|
38
|
+
# self.length.times do |i| f << "#{self[i]}\n" end
|
39
|
+
#
|
40
|
+
# Without uncomenting this line 3dplot seems not to work.
|
41
|
+
|
20
42
|
|
21
43
|
$:.unshift(File.dirname(__FILE__) + "/../lib/")
|
22
44
|
require 'fuzzy'
|
45
|
+
include Fuzzyrb
|
23
46
|
@smallInput = FuzzySet.trapezoid([0, 0, 0, 10])
|
24
47
|
@largeInput = FuzzySet.trapezoid([0, 10, 10, 10])
|
25
48
|
@smallOutput = FuzzySet.trapezoid([0, 0, 0, 10])
|
@@ -29,7 +52,7 @@ require 'fuzzy'
|
|
29
52
|
@rule2 = FuzzyRule.new([@smallInput, @largeInput], @mediumOutput)
|
30
53
|
@rule3 = FuzzyRule.new([@largeInput, @smallInput], @mediumOutput)
|
31
54
|
@rule4 = FuzzyRule.new([@largeInput, @largeInput], @largeOutput)
|
32
|
-
@ms =
|
55
|
+
@ms = MamdaniImplication.new([@rule1, @rule2, @rule3, @rule4])
|
33
56
|
|
34
57
|
@rulets1 = FuzzyRule.new([@smallInput, @smallInput], Proc.new { |a, b| 0})
|
35
58
|
@rulets2 = FuzzyRule.new([@smallInput, @largeInput], Proc.new { |a, b| 10})
|
@@ -37,38 +60,46 @@ require 'fuzzy'
|
|
37
60
|
@rulets4 = FuzzyRule.new([@largeInput, @largeInput], Proc.new { |a, b| 20})
|
38
61
|
@tss = TakagiSugenoImplication.new([@rulets1, @rulets2, @rulets3, @rulets4])
|
39
62
|
|
40
|
-
|
41
|
-
require '
|
63
|
+
begin
|
64
|
+
require 'rubygems'
|
65
|
+
require 'gnuplot'
|
66
|
+
@makePlot = true
|
67
|
+
rescue LoadError
|
68
|
+
@makePlot = false
|
69
|
+
STDERR.puts "If you want to see graphical output you have to install gnuplot from Rubyforge!"
|
70
|
+
end
|
42
71
|
@sum_of_diffs = {}
|
43
72
|
def count_error(implication, params)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
73
|
+
range = 0..10
|
74
|
+
x = range.collect {|v| v.to_f}
|
75
|
+
y = x
|
76
|
+
z = []
|
77
|
+
pp params
|
78
|
+
for i in range do
|
79
|
+
z << []
|
80
|
+
for j in range do
|
81
|
+
# print " T_Norm: #{t_norm}, implication: #{imp_type}, deffuzification: #{def_type}, difference: "
|
82
|
+
res = implication.evaluate([i, j],params)
|
83
|
+
diff = (res - (i+j)).abs
|
84
|
+
@sum_of_diffs[params] ||= 0
|
85
|
+
@sum_of_diffs[params] += diff
|
86
|
+
puts "[#{i}, #{j}] #{res} - #{i+j} = #{res - (i+j)}"
|
87
|
+
z[i] << diff
|
88
|
+
end
|
89
|
+
end
|
90
|
+
if @makePlot
|
91
|
+
Gnuplot.open do |gp|
|
92
|
+
Gnuplot::SPlot.new( gp ) do |plot|
|
93
|
+
plot.title "#{params.inspect}"
|
94
|
+
plot.xlabel "a"
|
95
|
+
plot.ylabel "b"
|
96
|
+
plot.set("ticslevel", "0.8")
|
97
|
+
plot.set("isosample", "40,40")
|
51
98
|
|
52
|
-
|
53
|
-
|
54
|
-
y = x
|
55
|
-
z = []
|
56
|
-
pp params
|
57
|
-
for i in range do
|
58
|
-
z << []
|
59
|
-
for j in range do
|
60
|
-
# print " T_Norm: #{t_norm}, implication: #{imp_type}, deffuzification: #{def_type}, difference: "
|
61
|
-
res = implication.evaluate([i, j],params)
|
62
|
-
diff = (res - (i+j)).abs
|
63
|
-
@sum_of_diffs[params] ||= 0
|
64
|
-
@sum_of_diffs[params] += diff
|
65
|
-
puts "[#{i}, #{j}] #{res} - #{i+j} = #{res - (i+j)}"
|
66
|
-
z[i] << diff
|
99
|
+
plot.data << Gnuplot::DataSet.new( [x, y, z] ) do |ds|
|
100
|
+
ds.with = "lines"
|
67
101
|
end
|
68
102
|
end
|
69
|
-
plot.data << Gnuplot::DataSet.new( [x, y, z] ) do |ds|
|
70
|
-
ds.with = "lines"
|
71
|
-
end
|
72
103
|
end
|
73
104
|
end
|
74
105
|
end
|
@@ -80,10 +111,5 @@ end
|
|
80
111
|
count_error(@ms, :t_norm => t_norm, :implication => imp_type, :defuzzification => def_type)
|
81
112
|
end
|
82
113
|
end
|
83
|
-
# print "[#{i}, #{j}] T_norm: #{t_norm}, implication: Takagi Sugeno, difference: "
|
84
|
-
# res = @tss.evaluate([i, j], t_norm)
|
85
|
-
# sum_of_diffs[[t_norm, :ts]] ||= 0
|
86
|
-
# sum_of_diffs[[t_norm, :ts]] += (res - (i+j)).abs
|
87
|
-
# puts "#{res} - #{i+j} = #{res - (i+j)}"
|
88
114
|
end
|
89
115
|
pp @sum_of_diffs
|
data/lib/fuzzy.rb
CHANGED
@@ -29,7 +29,7 @@ class Array
|
|
29
29
|
}
|
30
30
|
uniqArray
|
31
31
|
end
|
32
|
-
|
32
|
+
# Removes duplicate elements from self. Duplicates by means of eql?.
|
33
33
|
def uniq_values!()
|
34
34
|
test = self.dup
|
35
35
|
self.clear
|
@@ -39,6 +39,8 @@ class Array
|
|
39
39
|
self
|
40
40
|
end
|
41
41
|
|
42
|
+
# Returns a new array by removing duplicate values in self. Duplicates by
|
43
|
+
# means of eql?.
|
42
44
|
def non_decreasing
|
43
45
|
for i in 1..self.length-1
|
44
46
|
return false if self[i-1] > self[i]
|
@@ -54,5 +56,5 @@ require 'fuzzy_rule'
|
|
54
56
|
require 'fuzzy_implication'
|
55
57
|
|
56
58
|
module Fuzzyrb
|
57
|
-
VERSION = "1.2.
|
59
|
+
VERSION = "1.2.1"
|
58
60
|
end
|
data/lib/fuzzy_implication.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
module Fuzzyrb
|
2
2
|
# Should not be used directly. Use <tt>TakagiSugenoImplication</tt> or
|
3
|
-
# <tt>
|
3
|
+
# <tt>MamdaniImplication</tt>
|
4
4
|
class FuzzyImplication
|
5
5
|
def initialize(rules)
|
6
6
|
@rules = rules
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
# Uses Takegi-Sugeno system to compute implication. Each rule
|
11
|
-
#
|
10
|
+
# Uses Takegi-Sugeno system to compute implication. Each rule's conclusion
|
11
|
+
# is a function of rule's arguments.
|
12
12
|
class TakagiSugenoImplication < FuzzyImplication
|
13
13
|
# Evaluates the rule. Params are the same as for
|
14
14
|
# <tt>FuzzyRule</tt>
|
@@ -25,10 +25,12 @@ module Fuzzyrb
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
# Regular implication. Result is defuzzification of fuzzy set composed of
|
29
|
+
# rules' conclusions scaled by T-norm.
|
30
|
+
class MamdaniImplication < FuzzyImplication
|
29
31
|
# Evaluates the rule. Params includes <tt>FuzzyRule</tt> params and:
|
30
|
-
# * <tt>:defuzzification</tt> which can be <tt>:CoG</tt> for center of
|
31
|
-
# <tt>:firstMaximum</tt> for first maximum.
|
32
|
+
# * <tt>:defuzzification</tt> which can be <tt>:CoG</tt> for center of
|
33
|
+
# gravity of <tt>:firstMaximum</tt> for first maximum.
|
32
34
|
def evaluate(values, params)
|
33
35
|
result = @rules.map { |rule|
|
34
36
|
rule.evaluate(values, params)
|
data/lib/fuzzy_rule.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module Fuzzyrb
|
2
2
|
class FuzzyRule
|
3
3
|
# Creates rule.
|
4
|
-
# First argument is an Array of premises (<tt>FuzzySet</tt>). Second is
|
4
|
+
# First argument is an Array of premises (<tt>FuzzySet</tt>). Second is
|
5
|
+
# result. It is either <tt>FuzzySet</tt> or <tt>Proc</tt>.
|
5
6
|
def initialize(arguments, result)
|
6
7
|
@arguments = arguments
|
7
8
|
@result = result
|
@@ -12,7 +12,7 @@ class TestFuzzyImplication < Test::Unit::TestCase
|
|
12
12
|
b2 = FuzzySet.trapezoid([30, 45, 45, 50])
|
13
13
|
r1 = FuzzyRule.new([a11, a12], b1)
|
14
14
|
r2 = FuzzyRule.new([a21, a22], b2)
|
15
|
-
@fi =
|
15
|
+
@fi = MamdaniImplication.new([r1, r2])
|
16
16
|
|
17
17
|
tr1 = FuzzyRule.new([a11, a12], Proc.new { |a, b| a+b })
|
18
18
|
tr2 = FuzzyRule.new([a21, a22], Proc.new { |a, b| a-b })
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -3,15 +3,15 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: fuzzyrb
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.2.
|
7
|
-
date: 2007-11-
|
6
|
+
version: 1.2.1
|
7
|
+
date: 2007-11-19 00:00:00 +01:00
|
8
8
|
summary: Fuzzy Sets for Ruby
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: roman.kamyk@gmail.com
|
12
12
|
homepage: " by Roman Kamyk"
|
13
13
|
rubyforge_project: fuzzyrb
|
14
|
-
description: "== DESCRIPTION: Implements Fuzzy Sets in Ruby. I am very beginner at this topic, so it is very basic now. Any help will be appreciated. == FEATURES/PROBLEMS: * Fuzzy Sets defined as sequence of line segments. * Fuzzy Rules. Only conjunction of arguments is possible. * No error handling. * Defuzzification as center of gravity and first maximum * Minimum and Multiplication T-Norms. *
|
14
|
+
description: "== DESCRIPTION: Implements Fuzzy Sets in Ruby. I am very beginner at this topic, so it is very basic now. Any help will be appreciated. == FEATURES/PROBLEMS: * Fuzzy Sets defined as sequence of line segments. * Fuzzy Rules. Only conjunction of arguments is possible. * No error handling. * Defuzzification as center of gravity and first maximum * Minimum and Multiplication T-Norms. * Mamdani and Larsen aggregation methods. * Reasoning - apply matching rule and combine the results. Mamdani or Takagi-Sugeno system."
|
15
15
|
autorequire:
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|
metadata.gz.sig
CHANGED
Binary file
|