-
Notifications
You must be signed in to change notification settings - Fork 0
/
pseudocode.txt
235 lines (216 loc) · 7.03 KB
/
pseudocode.txt
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
- Limitations
- No intermediate output
- Structs: line, range, rule, assignment
Token = {
Assignment(Assignment)
Operator(Operator)
Value(f64)
}
Rule = Token[]
Range = {
begin: float,
end: float
}
Line = {
slope: float,
y_intercept: float,
x_range: Range
}
Assignment = {
not: bool,
var: string
set: string
}
rules = Rule[]
variables = {
"var_name": {
"set_name": Line[],
...
},
...
}
fuzzy_values = {
"var_name": {
"set_name": float from 0 to 1,
...
} (map for input, multimap for output),
...
}
- choose_name_from_map(map)
- i = 1
- variable_names = []
- for name in map
- variable_names.append(name)
- print("{i}. {name}")
- i++
- number = input() - 1
- return variable_names[number]
# input var if in lhs - output var if in rhs
# Is/is not
# set
# And/or/then if in lhs - x/, if in rhs
# rule is [assignment, op, assignment, op, assignment, ..., op(then), assignment, assignment, ....]
- add_rule(input_variables, output_variables, rules)
- if input_variables.size() == 0 || output_variables.size() == 0
- print insufficient input and/or output variables to create a rule
- rule = []
- in_lhs = true
- operators = [and, or, then]
- i = 1
- while true
- turn = i % 2
- if turn == 1
- var_pool = in_lhs ? input_variables : output_variables
- var_name = choose_name_from_map(var_pool)
- print("1. is\n2. is not")
- not_choice = input in range 1, 2
- not = not_choice == 2
- set_name = choose_name_from_map(var_pool[var_name])
- rule.append(Token::Assignment({not, var_name, set_name}))
- else if turn == 0
- if in lhs
- print("1. and\n2. or\n3. then")
- operator_choice = input() in range 1, 3
- operator = operators[operator_choice - 1]
- rule.append(Token::Operator(operator))
- if operator == then
- in_lhs = false
- else
- print("1. more\n 2. done")
- operator_choice = input() in range 1, 2
- if operator_choice == 2
- break
- i++
- rules.append(rule)
- compute_y(line, value)
- if line.slope == inf
- return 1
- return line.slope * value + line.y_intercept
- calculate_fuzzy_value_for_variable(variables, variable_name, set_name, crisp_value)
- lines = variables[variable_name][set_name]
- for line in lines
- if crisp_value >= line.range_x.begin && crisp_value <= line.range_x.end
- return compute_y(line, crisp_value)
- return 0
- add_variable(variables)
- var_name = input()
- variables.set(var_name, {});
- no_sets = input()
- for 1..=no_sets
- set_name = input()
- set_type = input()
- no_points = set_type == 'trapezoid' ? 4 : 3
- xs = []
- for j=1..=no_points
- xs.append(input())
- lines = []
- ys = [0]
- for i=2..no_points
- ys.append(1)
- ys.append(0)
- for j=1..no_points
- slope = (ys[j] - ys[j - 1])/(xs[j] - xs[j - 1])
- lines.append(new Line{
slope,
y_intercept: ys[j] - slope * xs[j],
range: [xs[j - 1], xs[j]]
})
- variables['var_name'].set('set_name', lines)
# for each rule
# change lhs assignments to fuzzy values
# change value1 and value2 to min(value1, value2)
# change value1 or value2 to max(value1, value2)
# only one fuzzy value will be left in lhs, call it fuzzy result
# for every assignment in rhs, output_fuzzy_values[set_name] = fuzzy result
# for each var name in output fuzzy values
# for each set name in output_fuzzy_values[var_name], do the crisp value stuff
- do_operation_on_tokens(operator_token, operation, tokens)
- new_tokens = []
- idx = 0
- while (tokens[idx] != Operator.THEN)
- if tokens[idx] matches operator(operator_token)
- new_tokens.push(operation(tokens[idx - 1], tokens[idx + 1]))
- else if tokens[idx] matches operator(_)
- new_tokens.push(tokens[idx - 1])
- new_tokens.push(tokens[idx])
- new_tokens.push(tokens[idx + 1])
- idx++
- copy rest of tokens to new_tokens
- return new_tokens
- calculate_crisp_output(input_variables, output_variables, rules)
- if rules.size() == 0
- Need at least one rule to calculate crisp output
- input_crisp_values = {}
- output_fuzzy_values = {} (contains multimap)
- for rule in rules
- tokens_list = rule.clone()
- idx = 0
- while (tokens_list[idx] != Operator.THEN)
- if tokens_list[idx] matches Token::Assignment
- assignment = tokens_list[idx]
- crisp = input_crisp_values.has(assignment.var)
? input_crisp_values[assignment.var]
: input('{assignment.var} crisp value')
- input_crisp_values[assignment.var] = crisp
- fuzzy = calculate_fuzzy_value_for_variable(input_variables, assignment.var, assignment.set, crisp)
- if (assignment.not) fuzzy = 1 - fuzzy
- tokens_list[idx] = Token::value(fuzzy)
- idx++
- tokens_anded = do_operation_on_tokens(Operator::AND, min, tokens_list)
- tokens_ored = do_operation_on_tokens(Operator::OR, max, tokens_list)
- fuzzy_result = tokens_ored[0]
- idx = 0
- while (idx < tokens_ored.size())
- if (tokens_ored[idx] matches Token::Assignment)
- assignment = tokens_ored[idx]
- assignment_result = fuzzy_result
- if (assignment.not) assignment_result = 1 - assignment_result
- if (!output_fuzzy_values.has(assignment.var))
- output_fuzzy_values.set(assignment.var, {})
- output_fuzzy_values[assignment.var].set(assignment.set, assignment_result)
- idx++
- for (var_name, set) in output_fuzzy_values
- numerator = 0
- denominator = 0
- for (set_name, fuzzy_value) in set (beware of multimap)
- centroid = 0
- lines = output_variables[var_name][set_name]
- for line in lines
- centroid += line.x_range.begin
- centroid += lines[lines.size() - 1].end
- centroid /= lines.size() + 1
- numerator += centroid * fuzzy_value
- denominator += fuzzy_value
- crisp_value = numerator / denominator
# Check which set it lies in
- set_name = "invalid set"
- max_y = -1
- for (s, lines) in output_variables[var_name]
- for line in lines
- if crisp_value >= line.range_x.begin && crisp_value <= line.range_x.end
- prev_max_y = max_y
- max_y = max(max_y, compute_y(line, crisp_value))
- if max_y != prev_max_y
- set_name = s
- print("{var_name}: {crisp_value} (set_name)")
- main()
- input("system name")
- input("system description")
- input_variables = {}
- output_variables = {}
- rules = []
- while True
- print(
"
1. Add input variable
2. Add output variable
3. Add rule
4. Calculate crisp values for output values
"
)
- choice = input() in range 1 4
- switch input
- case 1: add_variable(input_variables)
- case 2: add_variable(output_variables)
- case 3: add_rule(input_variables, output_variable, rules)
- case 4: calculate_crisp_output(input_variables, output_variables, rules)