Example #1
0
def complement(COMPLEMENT, set,segment_size=None):
    """Returns a new fuzzy set which ist this complement of the given set.
    (Where the membership of the result set is equal to C{COMPLEMENT(set(x))}.

    For meaning of segment_size see also L{fuzzy.set.operations.merge}.
    
    @param COMPLEMENT: fuzzy complement to use. For example Zadeh(), ...
        Also possible as one param function, eg. C{lambda x: 1.-x}.
    @type COMPLEMENT: L{fuzzy.complement.Base.Base}
    @param set: fuzzy set
    @type set: L{fuzzy.set.Set}
    @param segment_size: maximum size of a segment
    @type segment_size: float/None
    @return: resulting fuzzy set
    @rtype: L{fuzzy.set.Polygon.Polygon}
    """
    from fuzzy.set.Polygon import Polygon
    ret = Polygon()

    prev_x,prev_y = None,None
    for x,y in _complement_generator(COMPLEMENT,set):
        if (segment_size is not None) and (prev_x is not None) and (abs(y-prev_y)>0.01):
            diff = x-prev_x
            if  diff > 2.*segment_size:
                n = int(diff/segment_size)
                dx = diff/n
                for i in range(1,n):
                    x_ = prev_x+i*dx
                    ret.add(x_,COMPLEMENT(set(x_)))
        ret.add(x,y)
        prev_x,prev_y = x,y

    return ret
Example #2
0
def complement(COMPLEMENT, set, segment_size=None):
    """Returns a new fuzzy set which is this complement of the given set.
    (Where the membership of the result set is equal to C{COMPLEMENT(set(x))}.

    For meaning of segment_size see also L{fuzzy.set.operations.merge}.
    
    @param COMPLEMENT: fuzzy complement to use. For example Zadeh(), ...
        Also possible as one param function, eg. C{lambda x: 1.-x}.
    @type COMPLEMENT: L{fuzzy.complement.Base.Base}
    @param set: fuzzy set
    @type set: L{fuzzy.set.Set}
    @param segment_size: maximum size of a segment
    @type segment_size: float/None
    @return: resulting fuzzy set
    @rtype: L{fuzzy.set.Polygon.Polygon}
    """
    from fuzzy.set.Polygon import Polygon
    ret = Polygon()

    prev_x, prev_y = None, None
    for x, y in _complement_generator(COMPLEMENT, set):
        if (segment_size
                is not None) and (prev_x
                                  is not None) and (abs(y - prev_y) > 0.01):
            diff = x - prev_x
            if diff > 2. * segment_size:
                n = int(diff / segment_size)
                dx = diff / n
                for i in range(1, n):
                    x_ = prev_x + i * dx
                    ret.add(x_, COMPLEMENT(set(x_)))
        ret.add(x, y)
        prev_x, prev_y = x, y

    return ret
Example #3
0
def norm(NORM, set, value,segment_size=None):
    """Returns a new fuzzy set which ist this set normed with value.
    where the membership of the result set is equal to C{NORM(set(x),value)}.

    For meaning of segment_size see also L{fuzzy.set.operations.merge}.
    
    @param NORM: fuzzy norm to calculate set's values with value. For example Min(), Max(), ...
        Also possible as two params function, eg. C{lambda a,b: (a+b)/2.}.
    @type NORM: L{fuzzy.norm.Norm.Norm}
    @param set: fuzzy set
    @type set: L{fuzzy.set.Set}
    @param value: value
    @type value: float
    @param segment_size: maximum size of a segment
    @type segment_size: float/None
    @return: resulting fuzzy set
    @rtype: L{fuzzy.set.Polygon.Polygon}
    """
    from fuzzy.set.Polygon import Polygon
    ret = Polygon()

    prev_x,prev_y = None,None
    for x,y in _norm_generator(NORM,set,value):
        if (segment_size is not None) and (prev_x is not None) and (abs(y-prev_y)>0.01):
            diff = x-prev_x
            if  diff > 2.*segment_size:
                n = int(diff/segment_size)
                dx = diff/n
                for i in range(1,n):
                    x_ = prev_x+i*dx
                    ret.add(x_,NORM(set(x_),value))
        ret.add(x,y)
        prev_x,prev_y = x,y

    return ret
Example #4
0
def merge(NORM, set1, set2, segment_size=None):
    """Returns a new fuzzy set which is the merger of set1 and set2,
    where the membership of the result set is equal to C{NORM(set1(x),set2(x))}.
    
    For nonlinear operations you might want set the segment size to a value 
    which controls how large a linear segment of the result can be. 
    See also the following examples:
      - U{http://pyfuzzy.sourceforge.net/demo/merge/AlgebraicProduct_d_d.png} - The algebraic product is M{x*y}, so using it on the same set, it calculates the square of it.
      - U{http://pyfuzzy.sourceforge.net/demo/merge/AlgebraicSum_d_d.png} - The algebraic sum is M{x+y-x*y}.
    
    @param NORM: fuzzy norm to calculate both sets values. For example Min(), Max(), ...
        Also possible as two params function, eg. C{lambda a,b: (a+b)/2.}.
    @type NORM: L{fuzzy.norm.Norm.Norm}
    @param set1: fuzzy set
    @type set1: L{fuzzy.set.Set}
    @param set2: fuzzy set
    @type set2: L{fuzzy.set.Set}
    @param segment_size: maximum size of a segment
    @type segment_size: float/None
    @return: resulting fuzzy set
    @rtype: L{fuzzy.set.Polygon.Polygon}
       """
    from fuzzy.set.Polygon import Polygon
    ret = Polygon()

    prev_x, prev_y = None, None
    for x, y in _merge_generator(NORM, set1, set2):
        if (segment_size
                is not None) and (prev_x
                                  is not None) and (abs(y - prev_y) > 0.01):
            diff = x - prev_x
            if diff > 2. * segment_size:
                n = int(diff / segment_size)
                dx = diff / n
                for i in range(1, n):
                    x_ = prev_x + i * dx
                    ret.add(x_, NORM(set1(x_), set2(x_)))
        ret.add(x, y)
        prev_x, prev_y = x, y

    return ret
Example #5
0
def saw(interval, n):
    """
    Splits an ``interval`` into ``n`` triangle functions.

    :Parameters:
      interval
        A tuple containing the start and the end of the interval, in the format
        ``(start, end)``;
      n
        The number of functions in which the interval must be split.

    :Returns:
      A list of triangle membership functions, in order.
    """
    xo, xf = interval
    dx = float(xf - xo) / float(n + 1)
    mfs = []
    for i in range(n):
        mft = Polygon()
        mft.add(x=xo, y=0.0)
        mft.add(x=xo + dx, y=1.0)
        mft.add(x=xo + 2 * dx, y=0.0)
        mfs.append(mft)
        xo += dx
    return mfs
Example #6
0
def merge(NORM, set1, set2, segment_size=None):
    """Returns a new fuzzy set which ist the merger of set1 and set2,
    where the membership of the result set is equal to C{NORM(set1(x),set2(x))}.
    
    For nonlinear operations you might want set the segment size to a value 
    which controls how large a linear segment of the result can be. 
    See also the following examples:
      - U{http://pyfuzzy.sourceforge.net/test/merge/AlgebraicProduct_d_d.png} - The algebraic product is M{x*y}, so using it on the same set, it calculates the square of it.
      - U{http://pyfuzzy.sourceforge.net/test/merge/AlgebraicSum_d_d.png} - The algebraic sum is M{x+y-x*y}.
       
    @param NORM: fuzzy norm to calculate both sets values. For example Min(), Max(), ...
        Also possible as two params function, eg. C{lambda a,b: (a+b)/2.}.
    @type NORM: L{fuzzy.norm.Norm.Norm}
    @param set1: fuzzy set
    @type set1: L{fuzzy.set.Set}
    @param set2: fuzzy set
    @type set2: L{fuzzy.set.Set}
    @param segment_size: maximum size of a segment
    @type segment_size: float/None
    @return: resulting fuzzy set
    @rtype: L{fuzzy.set.Polygon.Polygon}
       """
    from fuzzy.set.Polygon import Polygon
    ret = Polygon()

    prev_x,prev_y = None,None
    for x,y in _merge_generator(NORM,set1,set2):
        if (segment_size is not None) and (prev_x is not None) and (abs(y-prev_y)>0.01):
            diff = x-prev_x
            if  diff > 2.*segment_size:
                n = int(diff/segment_size)
                dx = diff/n
                for i in range(1,n):
                    x_ = prev_x+i*dx
                    ret.add(x_,NORM(set1(x_),set2(x_)))
        ret.add(x,y)
        prev_x,prev_y = x,y

    return ret
Example #7
0
def norm(NORM, set, value, segment_size=None):
    """Returns a new fuzzy set which ist this set normed with value.
    where the membership of the result set is equal to C{NORM(set(x),value)}.

    For meaning of segment_size see also L{fuzzy.set.operations.merge}.
    
    @param NORM: fuzzy norm to calculate set's values with value. For example Min(), Max(), ...
        Also possible as two params function, eg. C{lambda a,b: (a+b)/2.}.
    @type NORM: L{fuzzy.norm.Norm.Norm}
    @param set: fuzzy set
    @type set: L{fuzzy.set.Set}
    @param value: value
    @type value: float
    @param segment_size: maximum size of a segment
    @type segment_size: float/None
    @return: resulting fuzzy set
    @rtype: L{fuzzy.set.Polygon.Polygon}
    """
    from fuzzy.set.Polygon import Polygon
    ret = Polygon()

    prev_x, prev_y = None, None
    for x, y in _norm_generator(NORM, set, value):
        if (segment_size
                is not None) and (prev_x
                                  is not None) and (abs(y - prev_y) > 0.01):
            diff = x - prev_x
            if diff > 2. * segment_size:
                n = int(diff / segment_size)
                dx = diff / n
                for i in range(1, n):
                    x_ = prev_x + i * dx
                    ret.add(x_, NORM(set(x_), value))
        ret.add(x, y)
        prev_x, prev_y = x, y

    return ret
Example #8
0
def _createSystem():
    """
    \brief Crea el conjunto de reglas y valores para el controlador
    \details Este comando no es necesario utilizarlo
    """
    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()

    #----------------------------------------------------------------------------------------------------------------
    # Valores de entrada
    #----------------------------------------------------------------------------------------------------------------
    input_temp = InputVariable(fuzzify=Plain())

    system.variables["error"] = input_temp

    in1_set = Polygon()
    in1_set.add(x=-6000, y=0.0)  # porque debe iniciar y finalizar en cero
    in1_set.add(x=-5000, y=1.0)
    in1_set.add(x=-4000, y=0.5)
    in1_set.add(x=-3000, y=0.0)
    in1 = Adjective(in1_set)
    input_temp.adjectives["MGN"] = in1

    in2_set = Polygon()
    in2_set.add(x=-4500, y=0.0)
    in2_set.add(x=-3000, y=1.0)
    in2_set.add(x=-2000, y=0.0)
    in2 = Adjective(in2_set)
    input_temp.adjectives["GN"] = in2

    in3_set = Polygon()
    in3_set.add(x=-3000, y=0.0)
    in3_set.add(x=-2000, y=1.0)
    in3_set.add(x=-1000, y=0.0)
    in3 = Adjective(in3_set)
    input_temp.adjectives["N"] = in3

    in4_set = Polygon()
    in4_set.add(x=-2000, y=0.0)
    in4_set.add(x=-1000, y=1.0)
    in4_set.add(x=0, y=0.0)
    in4 = Adjective(in4_set)
    input_temp.adjectives["Z"] = in4

    in5_set = Polygon()
    in5_set.add(x=-1000, y=0.0)
    in5_set.add(x=1000, y=1.0)
    in5_set.add(x=5000, y=1.0)
    in5_set.add(x=6000, y=0.0)  # porque debe iniciar y finalizar en cero
    in5 = Adjective(in5_set)
    input_temp.adjectives["P"] = in5

    #----------------------------------------------------------------------------------------------------------------
    # Valores de salida
    #----------------------------------------------------------------------------------------------------------------
    output_temp = OutputVariable(defuzzify=COG())

    system.variables["vel"] = output_temp
    output_temp.failsafe = 0.0  # let it output 0.0 if no COG available

    out1_set = Polygon()
    out1_set.add(x=-200, y=0.0)  # porque debe iniciar y finalizar en cero
    out1_set.add(x=-120, y=1.0)
    out1_set.add(x=250, y=0.0)
    out1 = Adjective(out1_set)
    output_temp.adjectives["VMB"] = out1

    out2_set = Polygon()
    out2_set.add(x=30, y=0.0)
    out2_set.add(x=355, y=1.0)
    out2_set.add(x=600, y=0.0)
    out2 = Adjective(out2_set)
    output_temp.adjectives["VB"] = out2

    out3_set = Polygon()
    out3_set.add(x=255, y=0.0)
    out3_set.add(x=600, y=1.0)
    out3_set.add(x=950, y=0.0)
    out3 = Adjective(out3_set)
    output_temp.adjectives["VM"] = out3

    out4_set = Polygon()
    out4_set.add(x=600, y=0.0)
    out4_set.add(x=875, y=1.0)
    out4_set.add(x=1130, y=0.0)
    out4 = Adjective(out4_set)
    output_temp.adjectives["VA"] = out4

    out5_set = Polygon()
    out5_set.add(x=950, y=0.0)
    out5_set.add(x=1300, y=1.0)
    out5_set.add(x=1500, y=0.0)  # porque debe iniciar y finalizar en cero
    out5 = Adjective(out5_set)
    output_temp.adjectives["VMA"] = out5

    #----------------------------------------------------------------------------------------------------------------
    # Reglas
    #----------------------------------------------------------------------------------------------------------------
    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["vel"].adjectives["VMA"],
        operator=Input(system.variables["error"].adjectives["MGN"]),
    )
    system.rules["rule1"] = rule1

    rule2 = Rule(
        adjective=system.variables["vel"].adjectives["VA"],
        operator=Input(system.variables["error"].adjectives["GN"], ),
    )
    system.rules["rule2"] = rule2

    rule3 = Rule(
        adjective=system.variables["vel"].adjectives["VM"],
        operator=Input(system.variables["error"].adjectives["N"], ),
    )
    system.rules["rule3"] = rule3

    rule4 = Rule(
        adjective=system.variables["vel"].adjectives["VB"],
        operator=Input(system.variables["error"].adjectives["Z"], ),
    )
    system.rules["rule4"] = rule4

    rule5 = Rule(
        adjective=system.variables["vel"].adjectives["VMB"],
        operator=Input(system.variables["error"].adjectives["P"], ),
    )
    system.rules["rule5"] = rule5

    return system
Example #9
0
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
Example #10
0
def flat_saw(interval, n):
    """
    Splits an ``interval`` into a decreasing ramp, ``n-2`` triangle functions
    and an increasing ramp.

    :Parameters:
      interval
        A tuple containing the start and the end of the interval, in the format
        ``(start, end)``;
      n
        The number of functions in which the interval must be split.

    :Returns:
      A list of corresponding functions, in order.
    """
    xo, xf = interval
    dx = float(xf - xo) / float(n + 1)

    # Decreasing ramp
    mf1 = Polygon()
    mf1.add(x=xo, y=1.0)
    mf1.add(x=xo + dx, y=1.0)
    mf1.add(x=xo + 2 * dx, y=0.0)

    # ``n-2`` triangle
    mfs = saw((xo + dx, xf - dx), n - 2)

    # Increasing ramp
    mf2 = Polygon()
    mf2.add(x=xf - 2 * dx, y=0.0)
    mf2.add(x=xf - dx, y=1.0)
    mf2.add(x=xf, y=1.0)

    return [mf1] + mfs + [mf2]
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
Example #12
0
def _createSystem():
    """
    \brief Crea el conjunto de reglas y valores para el controlador
    \details Este comando no es necesario utilizarlo
    """
    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()

    #----------------------------------------------------------------------------------------------------------------
    # Valores de entrada
    #----------------------------------------------------------------------------------------------------------------
    input_temp = InputVariable(fuzzify=Plain())

    system.variables["error"] = input_temp

    in1_set = Polygon()
    in1_set.add(x =-6000, y= 0.0) # porque debe iniciar y finalizar en cero
    in1_set.add(x =-5000, y= 1.0)
    in1_set.add(x =-4000, y= 0.5)
    in1_set.add(x =-3000, y= 0.0)
    in1 = Adjective(in1_set)
    input_temp.adjectives["MGN"] = in1

    in2_set = Polygon()
    in2_set.add(x =-4500, y= 0.0)
    in2_set.add(x =-3000, y= 1.0)
    in2_set.add(x =-2000, y= 0.0)
    in2 = Adjective(in2_set)
    input_temp.adjectives["GN"] = in2

    in3_set = Polygon()
    in3_set.add(x =-3000, y= 0.0)
    in3_set.add(x =-2000, y= 1.0)
    in3_set.add(x =-1000, y= 0.0)
    in3 = Adjective(in3_set)
    input_temp.adjectives["N"] = in3

    in4_set = Polygon()
    in4_set.add(x = -2000, y= 0.0)
    in4_set.add(x =-1000, y= 1.0)
    in4_set.add(x =0, y= 0.0)
    in4 = Adjective(in4_set)
    input_temp.adjectives["Z"] = in4

    in5_set = Polygon()
    in5_set.add(x = -1000, y= 0.0)
    in5_set.add(x = 1000, y= 1.0)
    in5_set.add(x = 5000, y= 1.0)
    in5_set.add(x =6000, y= 0.0) # porque debe iniciar y finalizar en cero
    in5 = Adjective(in5_set)
    input_temp.adjectives["P"] = in5

    #----------------------------------------------------------------------------------------------------------------
    # Valores de salida
    #----------------------------------------------------------------------------------------------------------------
    output_temp = OutputVariable(defuzzify=COG())

    system.variables["vel"] = output_temp
    output_temp.failsafe = 0.0 # let it output 0.0 if no COG available

    out1_set = Polygon()
    out1_set.add(x =-200, y= 0.0) # porque debe iniciar y finalizar en cero
    out1_set.add(x =-120, y= 1.0)
    out1_set.add(x =250, y= 0.0)
    out1 = Adjective(out1_set)  
    output_temp.adjectives["VMB"] = out1

    out2_set = Polygon()
    out2_set.add(x =30, y= 0.0)
    out2_set.add(x =355, y= 1.0)
    out2_set.add(x =600, y= 0.0)
    out2 = Adjective(out2_set)
    output_temp.adjectives["VB"] = out2

    out3_set = Polygon()
    out3_set.add(x = 255, y= 0.0)
    out3_set.add(x = 600, y= 1.0)
    out3_set.add(x = 950, y= 0.0)
    out3 = Adjective(out3_set)
    output_temp.adjectives["VM"] = out3

    out4_set = Polygon()
    out4_set.add(x = 600, y= 0.0)
    out4_set.add(x = 875, y= 1.0)
    out4_set.add(x = 1130,  y= 0.0)
    out4 = Adjective(out4_set)
    output_temp.adjectives["VA"] = out4

    out5_set = Polygon()
    out5_set.add(x = 950, y= 0.0)
    out5_set.add(x = 1300, y= 1.0)
    out5_set.add(x =1500, y= 0.0) # porque debe iniciar y finalizar en cero
    out5 = Adjective(out5_set)
    output_temp.adjectives["VMA"] = out5

    #----------------------------------------------------------------------------------------------------------------
    # Reglas
    #----------------------------------------------------------------------------------------------------------------
    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["vel"].adjectives["VMA"],
                                operator=Input(system.variables["error"].adjectives["MGN"]),
                                )
    system.rules["rule1"]=rule1


    rule2 = Rule(adjective=system.variables["vel"].adjectives["VA"],
                            operator=Input(system.variables["error"].adjectives["GN"],),
                            )
    system.rules["rule2"]=rule2 

    rule3 = Rule(adjective=system.variables["vel"].adjectives["VM"],
                            operator=Input(system.variables["error"].adjectives["N"],),
                            )
    system.rules["rule3"]=rule3 

    rule4 = Rule(adjective=system.variables["vel"].adjectives["VB"],
                            operator=Input(system.variables["error"].adjectives["Z"],),
                            )
    system.rules["rule4"]=rule4 

    rule5 = Rule(adjective=system.variables["vel"].adjectives["VMB"],
                            operator=Input(system.variables["error"].adjectives["P"],),
                            )
    system.rules["rule5"]=rule5 

    return system