blob: 60223e8a333e0b0c77767c0c853b834ce66143b8 (
plain)
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
|
(.module:
[lux #*
[control
[monad (#+ do Monad)]]
[data
[bit ("bit/." equivalence)]
["." number]
[text
format]
[collection
["." list]
["." set]]]
[math
["r" random]
[logic
["&" fuzzy]
["_" continuous]]]]
lux/test)
(do-template [<desc> <hash> <gen> <triangle> <lt> <lte> <gt> <gte>]
[(context: (format "[" <desc> "] " "Triangles")
(<| (times 100)
(do @
[values (r.set <hash> 3 <gen>)
#let [[x y z] (case (set.to-list values)
(^ (list x y z))
[x y z]
_
(undefined))]
sample <gen>
#let [[bottom middle top] (case (list.sort <lt> (list x y z))
(^ (list bottom middle top))
[bottom middle top]
_
(undefined))
triangle (<triangle> x y z)]]
($_ seq
(test "The middle value will always have maximum membership."
(r/= _.true (&.membership middle triangle)))
(test "Boundary values will always have 0 membership."
(and (r/= _.false (&.membership bottom triangle))
(r/= _.false (&.membership top triangle))))
(test "Values within range, will have membership > 0."
(bit/= (r/> _.false (&.membership sample triangle))
(and (<gt> bottom sample)
(<lt> top sample))))
(test "Values outside of range, will have membership = 0."
(bit/= (r/= _.false (&.membership sample triangle))
(or (<lte> bottom sample)
(<gte> top sample))))
))))]
["Rev" number.hash r.rev &.triangle r/< r/<= r/> r/>=]
)
(do-template [<desc> <hash> <gen> <trapezoid> <lt> <lte> <gt> <gte>]
[(context: (format "[" <desc> "] " "Trapezoids")
(<| (times 100)
(do @
[values (r.set <hash> 4 <gen>)
#let [[w x y z] (case (set.to-list values)
(^ (list w x y z))
[w x y z]
_
(undefined))]
sample <gen>
#let [[bottom middle-bottom middle-top top] (case (list.sort <lt> (list w x y z))
(^ (list bottom middle-bottom middle-top top))
[bottom middle-bottom middle-top top]
_
(undefined))
trapezoid (<trapezoid> w x y z)]]
($_ seq
(test "The middle values will always have maximum membership."
(and (r/= _.true (&.membership middle-bottom trapezoid))
(r/= _.true (&.membership middle-top trapezoid))))
(test "Boundary values will always have 0 membership."
(and (r/= _.false (&.membership bottom trapezoid))
(r/= _.false (&.membership top trapezoid))))
(test "Values within inner range will have membership = 1"
(bit/= (r/= _.true (&.membership sample trapezoid))
(and (<gte> middle-bottom sample)
(<lte> middle-top sample))))
(test "Values within range, will have membership > 0."
(bit/= (r/> _.false (&.membership sample trapezoid))
(and (<gt> bottom sample)
(<lt> top sample))))
(test "Values outside of range, will have membership = 0."
(bit/= (r/= _.false (&.membership sample trapezoid))
(or (<lte> bottom sample)
(<gte> top sample))))
))))]
["Rev" number.hash r.rev &.trapezoid r/< r/<= r/> r/>=]
)
(def: gen-triangle
(r.Random (&.Fuzzy Rev))
(do r.monad
[x r.rev
y r.rev
z r.rev]
(wrap (&.triangle x y z))))
(context: "Combinators"
(<| (times 100)
(do @
[left gen-triangle
right gen-triangle
sample r.rev]
($_ seq
(test "Union membership as as high as membership in any of its members."
(let [combined (&.union left right)
combined-membership (&.membership sample combined)]
(and (r/>= (&.membership sample left)
combined-membership)
(r/>= (&.membership sample right)
combined-membership))))
(test "Intersection membership as as low as membership in any of its members."
(let [combined (&.intersection left right)
combined-membership (&.membership sample combined)]
(and (r/<= (&.membership sample left)
combined-membership)
(r/<= (&.membership sample right)
combined-membership))))
(test "Complement membership is the opposite of normal membership."
(r/= (&.membership sample left)
(_.not (&.membership sample (&.complement left)))))
(test "Membership in the difference will never be higher than in the set being subtracted."
(bit/= (r/> (&.membership sample right)
(&.membership sample left))
(r/< (&.membership sample left)
(&.membership sample (&.difference left right)))))
))))
(context: "From predicates and sets"
(<| (times 100)
(do @
[#let [set-10 (set.from-list number.hash (list.n/range 0 10))]
sample (|> r.nat (:: @ map (n/% 20)))]
($_ seq
(test (format "Values that satisfy a predicate have membership = 1."
"Values that don't have membership = 0.")
(bit/= (r/= _.true (&.membership sample (&.from-predicate n/even?)))
(n/even? sample)))
(test (format "Values that belong to a set have membership = 1."
"Values that don't have membership = 0.")
(bit/= (r/= _.true (&.membership sample (&.from-set set-10)))
(set.member? set-10 sample)))
))))
(context: "Thresholds"
(<| (times 100)
(do @
[fuzzy gen-triangle
sample r.rev
threshold r.rev
#let [vip-fuzzy (&.cut threshold fuzzy)
member? (&.to-predicate threshold fuzzy)]]
($_ seq
(test "Can increase the threshold of membership of a fuzzy set."
(bit/= (r/> _.false (&.membership sample vip-fuzzy))
(r/> threshold (&.membership sample fuzzy))))
(test "Can turn fuzzy sets into predicates through a threshold."
(bit/= (member? sample)
(r/> threshold (&.membership sample fuzzy))))
))))
|