Package fuzzy :: Module Adjective
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.Adjective

 1  # -*- coding: utf-8 -*- 
 2  # 
 3  # Copyright (C) 2009  Rene Liebscher 
 4  # 
 5  # This program is free software; you can redistribute it and/or modify it under 
 6  # the terms of the GNU Lesser General Public License as published by the Free  
 7  # Software Foundation; either version 3 of the License, or (at your option) any 
 8  # later version. 
 9  # 
10  # This program is distributed in the hope that it will be useful, but WITHOUT  
11  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
12  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
13  # details. 
14  #  
15  # You should have received a copy of the GNU Lesser General Public License 
16  # along with this program; if not, see <http://www.gnu.org/licenses/>.  
17  # 
18  """Describes a ... of a variable.""" 
19  __revision__ = "$Id: Adjective.py,v 1.16 2010-02-17 19:57:13 rliebscher Exp $" 
20   
21   
22  from fuzzy.norm.Max import Max 
23  from fuzzy.set.Set import Set 
24   
25 -class Adjective(object):
26 """Describes a ... of a variable. 27 28 @ivar set: fuzzy set 29 @type set: L{fuzzy.set.Set.Set} 30 @ivar COM: norm (if None the class default _COM is used.) 31 @type COM: L{fuzzy.norm.Norm.Norm} 32 @ivar membership: set or calculated membership 33 @type membership: float 34 @cvar _COM: class default is instance variable is None 35 @type _COM: L{fuzzy.norm.Norm.Norm} 36 """ 37 38 # default if not set in instance 39 _COM = Max() 40 _set = Set() 41
42 - def __init__(self, set=None, COM=None):
43 """Initialize adjective. 44 45 @param set: fuzzy set 46 @type set: L{fuzzy.set.Set.Set} 47 @param COM: norm (if None the class default _COM is used.) 48 @type COM: L{fuzzy.norm.Norm.Norm} 49 """ 50 self.set = set or Adjective._set 51 self.membership = None 52 self.COM = COM
53
54 - def setMembershipForValue(self, value):
55 """Get membership for an input value from the fuzzy set.""" 56 self.membership = self.set(value)
57
58 - def getMembership(self):
59 """Return membership set in this adjective.""" 60 if self.membership is None: 61 return 0.0 62 else: 63 return self.membership
64
65 - def setMembership(self, value):
66 """Set membership of this adjective as result 67 of a rule calculation, 68 if already set use COM norm to merge 69 old and new value.""" 70 71 if self.membership is None: 72 self.membership = value 73 else: 74 self.membership = (self.COM or self._COM)( 75 self.membership, # consider already set value 76 value 77 )
78
79 - def reset(self):
80 """Reset membership to unknown value (None).""" 81 self.membership = None
82
83 - def getName(self, system):
84 """Find own name in given system. 85 Returns a tuple (var_name,adj_name) of None.""" 86 return system.findAdjectiveName(self)
87
88 - def __repr__(self):
89 """Return representation of instance. 90 91 @return: representation of instance 92 @rtype: string 93 """ 94 params = [] 95 if self.set is not Adjective._set: params.append("set=%s" % repr(self.set)) 96 if self.COM: params.append("COM=%s" % repr(self.COM)) 97 return "%s.%s(%s)" % (self.__class__.__module__, self.__class__.__name__, ", ".join(params))
98