class Rule(object): """This is realizes an important part of the inference engine. It represents and calculates the value of a fuzzy rule and sets the given adjective to the appropriate value. @cvar _CER: the default value (=Min()) for the norm used to calculate the certainty of a rule. @type _CER: L{fuzzy.norm.Norm.Norm} @ivar adjective: fuzzy adjective to set @type adjective: L{fuzzy.Adjective.Adjective} @ivar operator: Operator which provides the value to set @type operator: L{fuzzy.operator.Operator.Operator} @ivar certainty: how sure are we about this rule @type certainty: float @ivar CER: fuzzy norm to use with certainty (normally a t-norm) @type CER: L{fuzzy.norm.Norm.Norm} """ # default if not set in instance _CER = Min() def __init__(self, adjective, operator, certainty=1.0, CER=None): """Initialize instance. @param adjective: fuzzy adjective to set @type adjective: L{fuzzy.Adjective.Adjective} @param operator: Operator which provides the value to set @type operator: L{fuzzy.operator.Operator.Operator} @param certainty: how sure are we about this rule @type certainty: float @param CER: fuzzy norm to use with certainty (normally a t-norm) @type CER: L{fuzzy.norm.Norm.Norm} """ self.adjective = adjective self.operator = operator self.certainty = certainty self.CER = CER def compute(self): """Compute and set value for given fuzzy adjective.""" import fuzzy.Adjective if isinstance(self.adjective, fuzzy.Adjective.Adjective): self.adjective.setMembership((self.CER or self._CER)( self.certainty, # how sure are we about this rule self.operator() # value from input )) elif isinstance(self.adjective, list): for adj in self.adjective: adj.setMembership((self.CER or self._CER)( self.certainty, # how sure are we about this rule self.operator() # value from input )) else: raise Exception("rule target not set.") def getName(self, system): """Lookup the name given this rule in the given system""" return system.findRuleName(self)
class Function_norm(unittest.TestCase): patterns = { "1": [[(1., 0.), (1., 1.), (1., 0.)], [(1., 0.), (1., 0.5), (1., 0.)], Min(), 0.5], "2": [[(1., 0.), (1., 1.)], [(1., 0.), (1., 0.5)], Min(), 0.5], "3": [[(1., 1.), (1., 0.)], [(1., 0.5), (1., 0.)], Min(), 0.5], "4": [[(1., 0.), (2., 1.)], [(1., 0.), (1.5, 0.5), (2., 0.5)], Min(), 0.5], "5": [[(1., 1.), (2., 0.)], [(1., 0.5), (1.5, 0.5), (2., 0.)], Min(), 0.5], } def template(self, i, o, n, p): r = norm(n, Polygon(i), p) self.assertEqual(o, r.points)
class Function_merge(unittest.TestCase): patterns = { "1": [[(1., 0.), (2., 1.)], [(1., 0.), (2., 1.)], [(1., 0.), (2., 1.)], Min()], "2": [[(1., 0.), (2., 1.)], [(2., 0.), (3., 1.)], [(1., 0.), (2., 0.), (3., 1.)], Min()], "3": [[(1., 0.), (2., 1.)], [(1., 1.), (2., 1.)], [(1., 0.), (2., 1.)], Min()], "4": [[(1., 0.), (2., 1.)], [(1., 1.), (2., 0.)], [(1., 0.), (1.5, 0.5), (2., 0.)], Min()], "5": [[(1., 0.), (2., 1.)], [(1., 1.), (2., 0.)], [(1., 0.), (1.5, 0.25), (2., 0.)], AlgebraicProduct()], } def template(self, x1, x2, y, n): r = merge(n, Polygon(x1), Polygon(x2)) self.assertEqual(y, r.points)
class Base(object): """Abstract base class for defuzzification which results in a numeric value. @ivar INF: inference norm, used with set of adjective and given value for it @type INF: L{fuzzy.norm.Norm.Norm} @ivar ACC: norm for accumulation of set of adjectives @type ACC: L{fuzzy.norm.Norm.Norm} @cvar _INF: default value when INF is None @type _INF: L{fuzzy.norm.Norm.Norm} @cvar _ACC: default value when ACC is None @type _ACC: L{fuzzy.norm.Norm.Norm} @ivar activated_sets: results of activation of adjectives of variable. @type activated_sets: {string:L{fuzzy.set.Polygon.Polygon}} @ivar accumulated_set: result of accumulation of activated sets @type accumulated_set: L{fuzzy.set.Polygon.Polygon} """ # default values if instance values are not set _INF = Min() _ACC = Max() def __init__(self, INF=None, ACC=None): """ @param INF: inference norm, used with set of adjective and given value for it @type INF: L{fuzzy.norm.Norm.Norm} @param ACC: norm for accumulation of set of adjectives @type ACC: L{fuzzy.norm.Norm.Norm} """ self.ACC = ACC # accumulation self.INF = INF # inference self.activated_sets = {} self.accumulated_set = None def getValue(self, variable): """Defuzzyfication.""" raise DefuzzificationException("don't use the abstract base class") # helper methods for sub classes def accumulate(self, variable, segment_size=None): """combining adjective values into one set""" self.activated_sets = {} temp = None for name, adjective in variable.adjectives.items(): # get precomputed adjective set temp2 = norm((self.INF or self._INF), adjective.set, adjective.getMembership(), segment_size) self.activated_sets[name] = temp2 # accumulate all adjectives if temp is None: temp = temp2 else: temp = merge((self.ACC or self._ACC), temp, temp2, segment_size) self.accumulated_set = temp return temp def value_table(self, set): """get a value table of the polygon representation""" # get polygon representation ig = set.getIntervalGenerator() next = ig.nextInterval(None, None) while next is not None: x = next y = set(x) yield (x, y) # get next point from polygon next = ig.nextInterval(next, None)
class Rule(object): """This is realizes an important part of the inference engine. It represents and calculates the value of a fuzzy rule and sets the given adjective to the appropriate value. @cvar _CER: the default value (=Min()) for the norm used to calculate the certainty of a rule. @type _CER: L{fuzzy.norm.Norm.Norm} @ivar adjective: fuzzy adjective to set @type adjective: L{fuzzy.Adjective.Adjective} @ivar operator: Operator which provides the value to set @type operator: L{fuzzy.operator.Operator.Operator} @ivar certainty: how sure are we about this rule @type certainty: float @ivar CER: fuzzy norm to use with certainty (normally a t-norm) @type CER: L{fuzzy.norm.Norm.Norm} """ # default if not set in instance _CER = Min() def __init__(self, adjective, operator, certainty=1.0, CER=None): """Initialize instance. @param adjective: fuzzy adjective to set @type adjective: L{fuzzy.Adjective.Adjective} @param operator: Operator which provides the value to set @type operator: L{fuzzy.operator.Operator.Operator} @param certainty: how sure are we about this rule @type certainty: float @param CER: fuzzy norm to use with certainty (normally a t-norm) @type CER: L{fuzzy.norm.Norm.Norm} """ self.adjective = adjective self.operator = operator self.certainty = certainty self.CER = CER def compute(self): """Compute and set value for given fuzzy adjective.""" import fuzzy.Adjective if isinstance(self.adjective, fuzzy.Adjective.Adjective): self.adjective.setMembership( (self.CER or self._CER)( self.certainty, # how sure are we about this rule self.operator() # value from input ) ) elif isinstance(self.adjective, list): for adj in self.adjective: adj.setMembership( (self.CER or self._CER)( self.certainty, # how sure are we about this rule self.operator() # value from input ) ) else: from fuzzy.Exception import FuzzyException raise FuzzyException("rule target not set.") def getName(self, system): """Lookup the name given this rule in the given system""" return system.findRuleName(self) def __repr__(self): """Return representation of instance. @return: representation of instance @rtype: string """ params = [] params.append("adjective=%s" % object.__repr__(self.adjective)) params.append("operator=%s" % repr(self.operator)) if self.certainty != 1.0: params.append("certainty=%s" % self.certainty) if self.CER: params.append("CER=%s" % repr(self.CER)) return "%s.%s(%s)" % (self.__class__.__module__, self.__class__.__name__, ", ".join(params))
def _createSystem(): from fuzzy.InputVariable import InputVariable from fuzzy.OutputVariable import OutputVariable from fuzzy.fuzzify.Plain import Plain from fuzzy.defuzzify.COG import COG from fuzzy.defuzzify.LM import LM from fuzzy.Adjective import Adjective from fuzzy.set.Polygon import Polygon system = fuzzy.System.System() #---------------------------------------------------------------------------------------------------------------- # input temperature #---------------------------------------------------------------------------------------------------------------- input_temp = InputVariable(fuzzify=Plain()) system.variables["InputTemp"] = input_temp int1_set = Polygon() int1_set.add(x=-80, y=0.0) int1_set.add(x=0.0, y=1.0) int1_set.add(x=10, y=0.0) int1 = Adjective(int1_set) input_temp.adjectives["z"] = int1 int2_set = Polygon() int2_set.add(x=0, y=0.0) int2_set.add(x=15, y=1.0) int2_set.add(x=30, y=0.0) int2 = Adjective(int2_set) input_temp.adjectives["s"] = int2 int3_set = Polygon() int3_set.add(x=15, y=0.0) int3_set.add(x=30, y=1.0) int3_set.add(x=80, y=0.0) int3 = Adjective(int3_set) input_temp.adjectives["t"] = int3 #---------------------------------------------------------------------------------------------------------------- # input wind #---------------------------------------------------------------------------------------------------------------- input_wind = InputVariable(fuzzify=Plain()) system.variables["InputWind"] = input_wind inw1_set = Polygon() inw1_set.add(x=0, y=0.0) inw1_set.add(x=10.0, y=1.0) inw1_set.add(x=20, y=0.0) inw1 = Adjective(inw1_set) input_wind.adjectives["s"] = inw1 inw2_set = Polygon() inw2_set.add(x=10, y=0.0) inw2_set.add(x=30, y=1.0) inw2_set.add(x=50, y=0.0) inw2 = Adjective(inw2_set) input_wind.adjectives["m"] = inw2 inw3_set = Polygon() inw3_set.add(x=30, y=0.0) inw3_set.add(x=50, y=1.0) inw3_set.add(x=150, y=0.0) inw3 = Adjective(inw3_set) input_wind.adjectives["v"] = inw3 #---------------------------------------------------------------------------------------------------------------- # output => pocitova teplota #---------------------------------------------------------------------------------------------------------------- output_clothes = OutputVariable(defuzzify=COG()) system.variables["o"] = output_clothes output_clothes.failsafe = 0.0 # let it output 0.0 if no COG available # out1_set = Polygon() # out1_set.add(x = -80, y= 0.0) # out1_set.add(x = 0, y= 1.0) # out1_set.add(x = 10, y= 0.0) # out1 = Adjective(out1_set) # output_clothes.adjectives["vz"] = out1 out2_set = Polygon() out2_set.add(x=-80, y=0.0) out2_set.add(x=0, y=1.0) out2_set.add(x=10, y=0.0) out2 = Adjective(out2_set) output_clothes.adjectives["z"] = out2 out3_set = Polygon() out3_set.add(x=0, y=0.0) out3_set.add(x=15, y=1.0) out3_set.add(x=30, y=0.0) out3 = Adjective(out3_set) output_clothes.adjectives["s"] = out3 out4_set = Polygon() out4_set.add(x=15, y=0.0) out4_set.add(x=30, y=1.0) out4_set.add(x=80, y=0.0) out4 = Adjective(out4_set) output_clothes.adjectives["t"] = out4 # out5_set = Polygon() # out5_set.add(x = 30, y= 0.0) # out5_set.add(x = 40, y= 1.0) # out5_set.add(x = 50, y= 0.0) # out5 = Adjective(out5_set) # output_clothes.adjectives["vt"] = out5 #---------------------------------------------------------------------------------------------------------------- # Definition der Regeln #---------------------------------------------------------------------------------------------------------------- from fuzzy.Rule import Rule from fuzzy.norm.Min import Min from fuzzy.operator.Input import Input from fuzzy.operator.Compound import Compound rule1 = Rule(adjective=system.variables["o"].adjectives["z"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["z"]), Input(system.variables["InputWind"].adjectives["v"]))) system.rules["rule1"] = rule1 rule2 = Rule(adjective=system.variables["o"].adjectives["z"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["z"]), Input(system.variables["InputWind"].adjectives["m"]))) system.rules["rule2"] = rule2 rule3 = Rule(adjective=system.variables["o"].adjectives["z"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["z"]), Input(system.variables["InputWind"].adjectives["s"]))) system.rules["rule3"] = rule3 rule4 = Rule(adjective=system.variables["o"].adjectives["z"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["s"]), Input(system.variables["InputWind"].adjectives["v"]))) system.rules["rule4"] = rule4 rule5 = Rule(adjective=system.variables["o"].adjectives["s"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["s"]), Input(system.variables["InputWind"].adjectives["m"]))) system.rules["rule5"] = rule5 rule6 = Rule(adjective=system.variables["o"].adjectives["s"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["s"]), Input(system.variables["InputWind"].adjectives["s"]))) system.rules["rule6"] = rule6 rule7 = Rule(adjective=system.variables["o"].adjectives["s"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["t"]), Input(system.variables["InputWind"].adjectives["v"]))) system.rules["rule7"] = rule7 rule8 = Rule(adjective=system.variables["o"].adjectives["t"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["t"]), Input(system.variables["InputWind"].adjectives["m"]))) system.rules["rule8"] = rule8 rule9 = Rule(adjective=system.variables["o"].adjectives["t"], operator=Compound( Min(), Input(system.variables["InputTemp"].adjectives["t"]), Input(system.variables["InputWind"].adjectives["s"]))) system.rules["rule9"] = rule9 return system
def _createSystem(): ''' Definition des Fuzzy-Systems: 1. Eingangsvariable Phi 2. Eingangsvariable dPhi_dT 3. Ausgangsvariable a 4. Definition der Regeln ''' from fuzzy.InputVariable import InputVariable from fuzzy.OutputVariable import OutputVariable from fuzzy.fuzzify.Plain import Plain from fuzzy.defuzzify.COG import COG from fuzzy.Adjective import Adjective from fuzzy.set.Polygon import Polygon system = fuzzy.System.System() #---------------------------------------------------------------------------------------------------------------- # Definition des Drehwinkels als Eingang #---------------------------------------------------------------------------------------------------------------- input_temp = InputVariable(fuzzify=Plain()) system.variables["Phi"] = input_temp in1_set = Polygon() in1_set.add(x=-22.5, y=0.0) in1_set.add(x=0.0, y=1.0) in1_set.add(x=22.5, y=0.0) in1 = Adjective(in1_set) input_temp.adjectives["eN"] = in1 in2_set = Polygon() in2_set.add(x=0.0, y=0.0) in2_set.add(x=22.5, y=1.0) in2_set.add(x=45.0, y=0.0) in2 = Adjective(in2_set) input_temp.adjectives["pk"] = in2 in3_set = Polygon() in3_set.add(x=22.5, y=0.0) in3_set.add(x=45.0, y=1.0) in3_set.add(x=67.5, y=0.0) in3 = Adjective(in3_set) input_temp.adjectives["pm"] = in3 in4_set = Polygon() in4_set.add(x=45.0, y=0.0) in4_set.add(x=67.5, y=1.0) in4_set.add(x=90.0, y=1.0) in4_set.add(x=180.0, y=0.0) in4 = Adjective(in4_set) input_temp.adjectives["pg"] = in4 in5_set = Polygon() in5_set.add(x=-45.0, y=0.0) in5_set.add(x=-67.5, y=1.0) in5_set.add(x=-90.0, y=1.0) in5_set.add(x=-180.0, y=0.0) in5 = Adjective(in5_set) input_temp.adjectives["ng"] = in5 in6_set = Polygon() in6_set.add(x=-22.5, y=0.0) in6_set.add(x=-45.0, y=1.0) in6_set.add(x=-67.5, y=0.0) in6 = Adjective(in6_set) input_temp.adjectives["nm"] = in6 in7_set = Polygon() in7_set.add(x=-0.0, y=0.0) in7_set.add(x=-22.5, y=1.0) in7_set.add(x=-45.0, y=0.0) in7 = Adjective(in7_set) input_temp.adjectives["nk"] = in7 #---------------------------------------------------------------------------------------------------------------- # Definition der Winkelgeschwindigkeit als Eingang #---------------------------------------------------------------------------------------------------------------- input_tempa = InputVariable(fuzzify=Plain()) system.variables["dPhi_dT"] = input_tempa in1a_set = Polygon() in1a_set.add(x=-11.25, y=0.0) in1a_set.add(x=0.0, y=1.0) in1a_set.add(x=11.25, y=0.0) in1a = Adjective(in1a_set) input_tempa.adjectives["eN"] = in1a in2a_set = Polygon() in2a_set.add(x=0.0, y=0.0) in2a_set.add(x=11.25, y=1.0) in2a_set.add(x=22.5, y=0.0) in2a = Adjective(in2a_set) input_tempa.adjectives["pk"] = in2a in3a_set = Polygon() in3a_set.add(x=11.25, y=0.0) in3a_set.add(x=22.50, y=1.0) in3a_set.add(x=33.75, y=0.0) in3a = Adjective(in3a_set) input_tempa.adjectives["pm"] = in3a in4a_set = Polygon() in4a_set.add(x=22.5, y=0.0) in4a_set.add(x=33.75, y=1.0) in4a_set.add(x=45.0, y=1.0) in4a_set.add(x=90.0, y=0.0) in4a = Adjective(in4a_set) input_tempa.adjectives["pg"] = in4a in5a_set = Polygon() in5a_set.add(x=-0.0, y=0.0) in5a_set.add(x=-11.25, y=1.0) in5a_set.add(x=-22.5, y=0.0) in5a = Adjective(in5a_set) input_tempa.adjectives["nk"] = in5a in6a_set = Polygon() in6a_set.add(x=-11.25, y=0.0) in6a_set.add(x=-22.50, y=1.0) in6a_set.add(x=-33.75, y=0.0) in6a = Adjective(in6a_set) input_tempa.adjectives["nm"] = in6a in7a_set = Polygon() in7a_set.add(x=-22.5, y=0.0) in7a_set.add(x=-33.75, y=1.0) in7a_set.add(x=-45.0, y=1.0) in7a_set.add(x=-90.0, y=0.0) in7a = Adjective(in7a_set) input_tempa.adjectives["ng"] = in7a #---------------------------------------------------------------------------------------------------------------- # Definition der Horizontalbeschleunigung als Ausgang #---------------------------------------------------------------------------------------------------------------- output_temp = OutputVariable(defuzzify=COG()) system.variables["a"] = output_temp output_temp.failsafe = 0.0 # let it output 0.0 if no COG available out1_set = Polygon() out1_set.add(x=-0.25, y=0.0) out1_set.add(x=0.0, y=1.0) out1_set.add(x=0.25, y=0.0) out1 = Adjective(out1_set) output_temp.adjectives["eN"] = out1 out2_set = Polygon() out2_set.add(x=0.00, y=0.0) out2_set.add(x=0.25, y=1.0) out2_set.add(x=0.50, y=0.0) out2 = Adjective(out2_set) output_temp.adjectives["pk"] = out2 out3_set = Polygon() out3_set.add(x=0.25, y=0.0) out3_set.add(x=0.50, y=1.0) out3_set.add(x=0.75, y=0.0) out3 = Adjective(out3_set) output_temp.adjectives["pm"] = out3 out4_set = Polygon() out4_set.add(x=0.50, y=0.0) out4_set.add(x=0.75, y=1.0) out4_set.add(x=1.0, y=1.0) out4_set.add(x=2.0, y=0.0) out4 = Adjective(out4_set) output_temp.adjectives["pg"] = out4 out5_set = Polygon() out5_set.add(x=0.00, y=0.0) out5_set.add(x=-0.25, y=1.0) out5_set.add(x=-0.50, y=0.0) out5 = Adjective(out5_set) output_temp.adjectives["nk"] = out5 out6_set = Polygon() out6_set.add(x=-0.25, y=0.0) out6_set.add(x=-0.50, y=1.0) out6_set.add(x=-0.75, y=0.0) out6 = Adjective(out6_set) output_temp.adjectives["nm"] = out6 out7_set = Polygon() out7_set.add(x=-0.50, y=0.0) out7_set.add(x=-0.75, y=1.0) out7_set.add(x=-1.0, y=1.0) out7_set.add(x=-2.0, y=0.0) out7 = Adjective(out7_set) output_temp.adjectives["ng"] = out7 #---------------------------------------------------------------------------------------------------------------- # Definition der Regeln #---------------------------------------------------------------------------------------------------------------- from fuzzy.Rule import Rule from fuzzy.norm.Min import Min from fuzzy.operator.Input import Input from fuzzy.operator.Compound import Compound rule1 = Rule(adjective=system.variables["a"].adjectives["pk"], operator=Compound( Min(), Input(system.variables["Phi"].adjectives["nk"]), Input(system.variables["dPhi_dT"].adjectives["eN"]))) system.rules["rule1"] = rule1 rule2 = Rule( adjective=system.variables["a"].adjectives["nk"], operator=Input(system.variables["Phi"].adjectives["pk"], ), ) system.rules["rule2"] = rule2 rule3 = Rule( adjective=system.variables["a"].adjectives["pm"], operator=Input(system.variables["Phi"].adjectives["nm"], ), ) system.rules["rule3"] = rule3 rule4 = Rule( adjective=system.variables["a"].adjectives["nm"], operator=Input(system.variables["Phi"].adjectives["pm"], ), ) system.rules["rule4"] = rule4 rule5 = Rule( adjective=system.variables["a"].adjectives["pg"], operator=Input(system.variables["Phi"].adjectives["ng"], ), ) system.rules["rule5"] = rule5 rule6 = Rule( adjective=system.variables["a"].adjectives["ng"], operator=Input(system.variables["Phi"].adjectives["pg"], ), ) system.rules["rule6"] = rule6 return system
class Base(object): """Abstract base class for defuzzification which results in a numeric value. @ivar INF: inference norm, used with set of adjective and given value for it @type INF: L{fuzzy.norm.Norm.Norm} @ivar ACC: norm for accumulation of set of adjectives @type ACC: L{fuzzy.norm.Norm.Norm} @cvar _INF: default value when INF is None @type _INF: L{fuzzy.norm.Norm.Norm} @cvar _ACC: default value when ACC is None @type _ACC: L{fuzzy.norm.Norm.Norm} @ivar activated_sets: results of activation of adjectives of variable. @type activated_sets: {string:L{fuzzy.set.Polygon.Polygon}} @ivar accumulated_set: result of accumulation of activated sets @type accumulated_set: L{fuzzy.set.Polygon.Polygon} """ # default values if instance values are not set _INF = Min() _ACC = Max() def __init__(self, INF=None, ACC=None): """ @param INF: inference norm, used with set of adjective and given value for it @type INF: L{fuzzy.norm.Norm.Norm} @param ACC: norm for accumulation of set of adjectives @type ACC: L{fuzzy.norm.Norm.Norm} """ self.ACC = ACC # accumulation self.INF = INF # inference self.activated_sets = {} self.accumulated_set = None def getValue(self, variable): """Defuzzification.""" raise NotImplementedError("don't use the abstract base class") # helper methods for sub classes def accumulate(self, variable, segment_size=None): """combining adjective values into one set""" self.activated_sets = {} temp = None for name, adjective in variable.adjectives.items(): # get precomputed adjective set temp2 = norm((self.INF or self._INF), adjective.set, adjective.getMembership(), segment_size) self.activated_sets[name] = temp2 # accumulate all adjectives if temp is None: temp = temp2 else: temp = merge((self.ACC or self._ACC), temp, temp2, segment_size) self.accumulated_set = temp return temp def value_table(self, set): """get a value table of the polygon representation""" # get polygon representation return set.getValuesXY() def __repr__(self): """Return representation of instance. @return: representation of instance @rtype: string """ params = [] self._repr_params(params) return "%s.%s(%s)" % (self.__class__.__module__, self.__class__.__name__, ", ".join(params)) def _repr_params(self, params): """Helper for representation of instance. Add all own params to given list in params. """ if self.INF: params.append("INF=%s" % repr(self.INF)) if self.ACC: params.append("ACC=%s" % repr(self.ACC))