Skip to content

Commit 8635ca1

Browse files
committed
added Domain.MAX
1 parent e66e1c7 commit 8635ca1

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

fuzzy/classes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ def MIN(self, x):
121121
and calculate the min from that.
122122
"""
123123
return min(f(x) for f in self._sets.values())
124+
125+
def MAX(self, x):
126+
"""Standard way to get the max over all membership funcs.
127+
"""
128+
return max(f(x) for f in self._sets.values())
124129

125130
class Set:
126131
"""
@@ -283,12 +288,10 @@ def plot(self):
283288
plt.plot(R, V)
284289

285290
def array(self):
286-
# arange may not be ideal for this
287291
if self.domain is None:
288292
raise FuzzyWarning("No domain assigned.")
289-
return fromiter((self.func(x) for x in arange(self.domain.low,
290-
self.domain.high,
291-
self.domain.res)), float)
293+
return fromiter((self.func(x) for x in self.domain.range()),
294+
float)
292295

293296
def __repr__(self):
294297
"""

test_fuzzy_functionality.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,39 @@
33
Functional test of the fuzzylogic lib 'fuzzy'.
44
"""
55

6-
import unittest
6+
from unittest import TestCase, skip
77

8-
from fuzzy.classes import Domain, Set, Rule
9-
from fuzzy.functions import R, S
8+
from fuzzy.classes import Domain, Set
9+
from fuzzy.rules import scale
10+
from fuzzy.functions import R, S, bounded_linear
1011

11-
class TestTop(unittest.TestCase):
12+
class TestTop(TestCase):
1213
def setUp(self):
13-
"""
14-
Lucy tells her robot that it is good to heat when it's cold and not when it's hot."""
14+
"""Lucy tells her robot that it is good to heat when it's cold and not when it's hot."""
1515
self.temp = Domain('temperature', -100, 100) # in Celsius
1616
self.temp.cold = Set(S(0, 15))
1717
self.temp.hot = Set(R(10, 30))
1818
self.temp.warm = ~self.temp.cold & ~self.temp.hot
1919

2020
def test_temp(self):
2121
assert(self.temp(6) == {'cold': 0.6, 'hot': 0, 'warm': 0.4})
22+
23+
class Test_Meal(TestCase):
24+
def setUp(self):
25+
"""Tom is surveying restaurants.
26+
He doesn't need fancy logic but rather uses a simple approach with weights.
27+
He went into a small, dirty bar that served some delicious drink and food.
28+
"""
29+
self.R = Domain("rating", 1, 10, res=0.1)
30+
self.R.norm = Set(bounded_linear(1, 10))
31+
self.weights = weights = {"beverage": 0.3,
32+
"atmosphere": 0.2,
33+
"arrangement":0.2,
34+
"taste": 0.3}
35+
self.ratings = {"beverage": self.R.MIN(7),
36+
"atmosphere": self.R.MIN(3),
37+
"arrangement": self.R.MIN(2),
38+
"taste": self.R.MIN(9)}
2239

2340
if __name__ == '__main__':
2441
unittest.main()

0 commit comments

Comments
 (0)