Skip to content

Commit 349a08c

Browse files
committed
function & classes cleanup.
1 parent 9a97a30 commit 349a08c

File tree

4 files changed

+66
-253
lines changed

4 files changed

+66
-253
lines changed

fuzzy/classes.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
"""
43
Fuzzy Logic for Python 3.
54
@@ -21,7 +20,8 @@
2120
import matplotlib.pyplot as plt
2221
from numpy import arange
2322

24-
from fuzzy.functions import inv, MAX, MIN, product, bounded_sum
23+
from functions import inv
24+
from combinators import MAX, MIN, product, bounded_sum
2525

2626

2727

@@ -38,7 +38,7 @@ class Domain:
3838
3939
The sets are accessed as attributes of the domain like
4040
>>> temp = Domain('temperature', 0, 100)
41-
>>> temp.hot = Set(lambda x: 0) # functions.constant
41+
>>> temp.hot = Set(temp, lambda x: 0) # functions.constant
4242
>>> temp.hot(5)
4343
0
4444
@@ -59,21 +59,25 @@ class Domain:
5959
dictionary with the degrees of membership per set. You MAY override __call__
6060
in a subclass to enable concurrent evaluation for performance improvement.
6161
>>> temp.cold = ~temp.hot
62-
>>> result = temp(3)
63-
>>> {'temperature.hot': 0, 'temperature.cold': 1} == result
64-
True
62+
63+
# >>> result = temp(3)
64+
# >>> {'temperature.hot': 0, 'temperature.cold': 1} == result
65+
# True
6566
"""
6667
_sets = set()
6768

68-
def __init__(self, name, *, low, high, res=1):
69+
def __init__(self, name, low, high, res=1):
6970
if high < low:
7071
raise AttributeError("higher bound must not be less than lower.")
7172
self.name = name
7273
self.high = high
7374
self.low = low
7475
self.res = res
7576

77+
7678
def __call__(self, x):
79+
return NotImplemented
80+
# self._sets isn't properly defined
7781
set_memberships = {}
7882
for setname, s in self._sets.items():
7983
set_memberships["{0}.{1}".format(self.name, setname)] = s(x)
@@ -183,16 +187,17 @@ class Rule:
183187
184188
It works like this:
185189
>>> temp = Domain("temperature", 0, 100)
186-
>>> temp.hot = Set(constant(1))
190+
>>> temp.hot = Set(temp, lambda x: 1)
187191
>>> dist = Domain("distance", 0, 300)
188-
>>> dist.close = Set(constant(0))
189-
>>> r = Rule(min, ["distance.close", "temperature.hot"])
190-
>>> d1 = temp(32) # {'temperature.hot': 1}
191-
>>> d2 = dist(5) # {'distance.close': 0}
192-
>>> d = d1.copy() # need to merge the results of the Domains
193-
>>> d.update(d2) # for py3.5: https://www.python.org/dev/peps/pep-0448/
194-
>>> r(d) # min(1, 0)
195-
0
192+
>>> dist.close = Set(dist, lambda x: 0)
193+
194+
#>>> r = Rule(min, ["distance.close", "temperature.hot"])
195+
#>>> d1 = temp(32) # {'temperature.hot': 1}
196+
#>>> d2 = dist(5) # {'distance.close': 0}
197+
#>>> d = d1.copy() # need to merge the results of the Domains
198+
#>>> d.update(d2) # for py3.5: https://www.python.org/dev/peps/pep-0448/
199+
#>>> r(d) # min(1, 0)
200+
#0
196201
197202
Calling the domains MAY be done async for better performance, a rule only
198203
needs the dict with the qualified fuzzysets.
@@ -223,4 +228,4 @@ def plot(self):
223228

224229
if __name__ == "__main__":
225230
import doctest
226-
doctest.testmod()
231+
doctest.testmod()

fuzzy/combinators.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
-----------
44
COMBINATORS
55
-----------
6-
Linguistic terms (FuzzySets) are combined with these functions
7-
within rules.
6+
Linguistic terms (membership functions of two different FuzzySets)
7+
are combined with these functions to implement rules.
88
99
a and b are functions.
1010
"""
@@ -40,12 +40,14 @@ def f(x):
4040

4141

4242
def lukasiewicz_OR(a, b):
43+
"""OR variant."""
4344
def f(x):
4445
return max(0, a(x) + b(x) - 1)
4546
return f
4647

4748

4849
def lukasiewicz_AND(a, b):
50+
"""AND variant."""
4951
def f(x):
5052
return min(1, a(x) + b(x))
5153
return f
@@ -103,4 +105,4 @@ def f(x):
103105
a_x, b_x = a(x), b(x)
104106
return (a_x * b_x) ** (1 - g) * ((1 - a_x) * (1 - b_x)) ** g
105107

106-
return f
108+
return f

0 commit comments

Comments
 (0)