66from unittest import TestCase , skip
77
88from fuzzy .classes import Domain , Set
9- from fuzzy .rules import scale
9+ from fuzzy .rules import rescale , weighted_sum
1010from fuzzy .functions import R , S , bounded_linear
1111
1212class TestTop (TestCase ):
@@ -20,22 +20,38 @@ def setUp(self):
2020 def test_temp (self ):
2121 assert (self .temp (6 ) == {'cold' : 0.6 , 'hot' : 0 , 'warm' : 0.4 })
2222
23- class Test_Meal (TestCase ):
23+ class Test_Weighted (TestCase ):
2424 def setUp (self ):
2525 """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.
26+ He doesn't need fancy logic but rather uses a simple approach
27+ with weights.
28+ He went into a small, dirty bar that served some
29+ really good drink and food that wasn't nicely arranged but still
30+ yummmy. He rates the different factors on a scale from 1 to 10,
31+ uses a bounded_linear function to normalize over [0,1] and
32+ passes both the weights (how much each aspect should weigh in total)
33+ and the domain as parameters into weighted_sum.
34+ However, he can't just use Domain(value) because that would return
35+ a dict of memberships, instead he uses Domain.min(value) which
36+ returns the minimum of all memberships no matter how many sets
37+ there are. He creates a dict of membership values corresponding to
38+ the weights and passes that into the parametrized weighted_sum func
39+ as argument to get the final rating for this restaurant.
2840 """
2941 self .R = Domain ("rating" , 1 , 10 , res = 0.1 )
3042 self .R .norm = Set (bounded_linear (1 , 10 ))
3143 self .weights = weights = {"beverage" : 0.3 ,
3244 "atmosphere" : 0.2 ,
33- "arrangement " :0.2 ,
45+ "looks " :0.2 ,
3446 "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 )}
47+ self .w_func = weighted_sum (weights = self .weights , target = self .R )
48+
49+ def test_rating (self ):
50+ ratings = {"beverage" : self .R .min (9 ),
51+ "atmosphere" : self .R .min (5 ),
52+ "looks" : self .R .min (4 ),
53+ "taste" : self .R .min (8 )}
54+ assert (self .w_func (ratings ) == 6.9 )
3955
4056if __name__ == '__main__' :
4157 unittest .main ()
0 commit comments