forked from YOMOGItaro/rubyfuzzylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiu.rb
131 lines (100 loc) · 3.39 KB
/
fiu.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: undecided -*-
=begin rdoc
2011/07/04
YOMOGI
=end
require 'MembershipFunction.rb'
class FuzzyInferenceUnit
@antecedent_fuzzy_set1
@antecedent_fuzzy_set2
@fuzzy_conditional_proposition
@consequent_fuzzy_set
def initialize(
antecedent_fuzzy_set1,
antecedent_fuzzy_set2,
fuzzy_conditional_proposition,
consequent_fuzzy_set
)
@antecedent_fuzzy_set1 = antecedent_fuzzy_set1
@antecedent_fuzzy_set2 = antecedent_fuzzy_set2
@fuzzy_conditional_proposition = fuzzy_conditional_proposition
@consequent_fuzzy_set = consequent_fuzzy_set
end
def to_gnuplot_data
ret = ""
MembershipFunction::PARTITIONED_ELEMENT_NUMBER.times{ |idx1|
MembershipFunction::PARTITIONED_ELEMENT_NUMBER.times{ |idx2|
ret += idx1.to_s + " ";
ret += idx2.to_s + " ";
ret += call(idx1, idx2).to_s + " ";
ret += "\n"
}
}
return ret
end
def call(x1, x2)
omf = output_membership_function(x1, x2)
omf.centroid
end
def output_membership_function(x1, x2)
ret = MembershipFunction.valued_zero
each_membership_function_couple{ |amf1, amf2, cmf|
alpha = [amf1.call(x1), amf2.call(x2)].min
ret = ret.supremum(cmf.alpha_cut(alpha))
}
ret
end
def each_membership_function_couple
@antecedent_fuzzy_set1.each{ |amf1|
@antecedent_fuzzy_set2.each{ |amf2|
cmf = consequent_membership_function_from_labels(amf1.label, amf2.label)
yield amf1, amf2, cmf
}
}
end
def consequent_membership_function_from_labels(label1, label2)
consequent_label = @fuzzy_conditional_proposition.reference(label1, label2)
@consequent_fuzzy_set.reference(consequent_label)
end
def consequent_label(label1, label2)
@fuzzy_conditional_proposition.reference(label1, label2)
end
def consequent_membership_function(label1, label2)
@consequent_fuzzy_set.at(consequent_label(label1, label2))
end
end
if __FILE__ == $0
require 'yaml'
require 'FuzzySet.rb'
require 'FuzzyConditionalProposition.rb'
require 'MembershipFunction.rb'
# ct = {
# "low" => { "low" => "low", "mid" => "low", "top" => "mid"},
# "mid" => { "low" => "low", "mid" => "mid", "top" => "mid"},
# "top" => { "low" => "mid", "mid" => "top", "top" => "top"}
# }
# fcp = FuzzyConditionalProposition.new(ct)
# mfs1 = {
# "low" => MembershipFunctionTrapezoid.new( 1, 10, 10, 20, "low"),
# "mid" => MembershipFunctionTrapezoid.new( 15, 20, 20, 25, "mid"),
# "top" => MembershipFunctionTrapezoid.new( 20, 25, 25,32, "top")
# }
# fs1 = FuzzySet.new("hoge", mfs1)
# mfs2 = {
# "low" => MembershipFunctionTrapezoid.new( 1, 10, 10, 20, "low"),
# "mid" => MembershipFunctionTrapezoid.new( 15, 20, 20, 25, "mid"),
# "top" => MembershipFunctionTrapezoid.new( 20, 25, 25,32, "top")
# }
# fs2 = FuzzySet.new("pero", mfs2)
# cmfs = {
# "low" => MembershipFunctionTrapezoid.new( 1, 10, 10, 20, "low"),
# "mid" => MembershipFunctionTrapezoid.new( 15, 20, 20, 25, "mid"),
# "top" => MembershipFunctionTrapezoid.new( 20, 25, 25,32, "top")
# }
# cfs = FuzzySet.new("ushiro", cmfs)
# fiu = FuzzyInferenceUnit.new(fs1, fs2, fcp, cfs)
# YAML.dump(fiu, File.open('fiu_sample.yaml', 'w'))
fiu = YAML.load_file('fiu_sample.yaml')
#p fiu.call(50, 100)
fiu.to_gnuplot_data
end