Beispiel #1
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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
0
    def test_set_get_pyfuzzy_for_polygon_type(self):
        " shoud return the correct corresponding pyfuzzy object for the Polygon type "

        new_set = self._mock_setModel('fuzzy.set.Polygon.Polygon')

        points = [(0.,0.),(30.,1.),(60.,0.)]
        points_value = str(points)

        self.parameters_mock = [
                self._parameters_mock(name="points", value=points_value)
            ]

        # mocking parameters (queryset)
        parameters_queryset = mock.Mock()
        parameters_queryset.all = lambda : self.parameters_mock
        self.set_pre_mock(SetModel,'parameters')
        SetModel.parameters = parameters_queryset


        new_pyfuzzy_set = new_set.get_pyfuzzy()

        # the expected pyfuzzy system
        pyfuzzy_set_expected = Polygon(points=points)

        # are from the same class
        self.assertEquals(type(pyfuzzy_set_expected), type(new_pyfuzzy_set))

        # have the same points
        self.assertEquals(pyfuzzy_set_expected.points, new_pyfuzzy_set.points)
Beispiel #5
0
    def test_output_variable_changing_one_set_and_rule(self):
        " should return the correct output when changing all the sets to the new instance of same value, and changing the corresponding rule "

        from fuzzy_modeling.models import OutputVariableModel

        from fuzzy.norm.AlgebraicSum import AlgebraicSum
        from fuzzy.norm.AlgebraicProduct import AlgebraicProduct
        from fuzzy.Adjective import Adjective
        from fuzzy.set.Polygon import Polygon
        from fuzzy.defuzzify.COG import COG

        pyfuzzy_system_expected = self._createSystem()

        new_pyfuzzy_system = self._createSystem()

        INF = AlgebraicProduct()
        ACC = AlgebraicSum()
        COM = AlgebraicSum()
        COG = COG(INF=INF, ACC=ACC, failsafe=0., segment_size=0.5)

        acceleration = new_pyfuzzy_system.variables['a']

        acceleration.adjectives['right_fast'] = a_right_fast = Adjective(
            Polygon([(10., 0.), (20., 1.), (50., 0.)]), COM=COM)

        new_pyfuzzy_system.rules['far right'].adjective = a_right_fast

        import math
        input_dict = {}
        input_dict["X"] = 0.0  #: position [m]
        input_dict["dX_dT"] = 0.0  #: velocity [m/s]
        input_dict["Phi"] = math.radians(45.0)  #: angle [rad]
        input_dict["dPhi_dT"] = math.radians(0.0)  #: angle velocity [rad/s]

        i_dict1 = input_dict.copy()
        i_dict2 = input_dict.copy()

        output_dict1 = {
            'a': 0.0  #: acceleration [m/s²]
        }
        output_dict2 = {
            'a': 0.0  #: acceleration [m/s²]
        }

        pyfuzzy_system_expected.fuzzify(i_dict1)
        pyfuzzy_system_expected.inference()
        pyfuzzy_system_expected.defuzzify(output_dict1)

        new_pyfuzzy_system.fuzzify(i_dict2)
        new_pyfuzzy_system.inference()
        new_pyfuzzy_system.defuzzify(output_dict2)

        pyfuzzy_system_expected.calculate(i_dict1, output_dict1)
        new_pyfuzzy_system.calculate(i_dict2, output_dict2)

        self.assertEquals(output_dict1['a'], output_dict2['a'])
        self._test_rules_adj_in_out_adjs(new_pyfuzzy_system)
Beispiel #6
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
Beispiel #7
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
Beispiel #8
0
    def test_set_from_pyfuzzy_for_polygon_type(self):
        " shoud return the correct corresponding SetModel for the Polygon pyfuzzy object "

        points = [(0.,0.),(30.,1.),(60.,0.)]
        pyfuzzy_set = Polygon(points=points)


        new_set = SetModel.from_pyfuzzy(pyfuzzy_set)

        pyfuzzy_set_full_namespace = pyfuzzy_set.__module__ + "." + pyfuzzy_set.__class__.__name__

        # are from the same class
        self.assertEquals(pyfuzzy_set_full_namespace, new_set.set)

        # have the same args
        self.assertEquals(1,new_set.parameters.all().count())
        points_param = new_set.parameters.all()[0]
        self.assertEquals("points",points_param.name)
        self.assertEquals(str(points),points_param.get_value())
Beispiel #9
0
    def test_set_only(self):
        " should return the correct outout when only changing the set to a SetModel in th System "

        from fuzzy_modeling.models import AdjectiveModel

        pyfuzzy_system_expected = self._createSystem()

        new_pyfuzzy_system = self._createSystem()

        SystemModel.from_pyfuzzy(pyfuzzy_system_expected).get_pyfuzzy()

        from fuzzy.norm.AlgebraicSum import AlgebraicSum
        from fuzzy.Adjective import Adjective
        from fuzzy.set.Polygon import Polygon

        COM = AlgebraicSum()
        a_stop = Adjective(Polygon([(-10., 0.), (0., 1.), (10., 0.)]), COM=COM)
        a_stop.name = 'stop'

        new_a_stop = AdjectiveModel.from_pyfuzzy(a_stop).get_pyfuzzy()
        new_pyfuzzy_system.variables['a'].adjectives['stop'] = new_a_stop

        self._test_new_vs_expected_fuzzy_sysem(new_pyfuzzy_system,
                                               pyfuzzy_system_expected)
Beispiel #10
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
Beispiel #11
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
Beispiel #12
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
Beispiel #13
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
Beispiel #14
0
def _createSystem():
    import fuzzy.System
    system = fuzzy.System.System(
        description=
        """This fuzzy system is to control the inverted pendulum into an upright position as well as
at the position X=0.

It also is used to demonstrate some features of pyfuzzy.
This is the reason, it uses different fuzzy norm in normally
symmetrical rules.""")

    from fuzzy.norm.AlgebraicProduct import AlgebraicProduct
    from fuzzy.norm.AlgebraicSum import AlgebraicSum
    from fuzzy.fuzzify.Plain import Plain
    from fuzzy.defuzzify.COG import COG
    # set defuzzification method and default norms
    INF = AlgebraicProduct()
    ACC = AlgebraicSum()
    COM = AlgebraicSum()
    CER = AlgebraicProduct()
    COG = COG(INF=INF, ACC=ACC, failsafe=0., segment_size=0.5)

    from fuzzy.InputVariable import InputVariable
    from fuzzy.OutputVariable import OutputVariable
    from fuzzy.Adjective import Adjective
    from fuzzy.set.Polygon import Polygon

    angle = InputVariable(fuzzify=Plain(),
                          description='angle',
                          min=0.,
                          max=360.,
                          unit='degrees')
    system.variables['Phi'] = angle
    angle.adjectives['up_more_right'] = Adjective(
        Polygon([(0., 0.), (30., 1.), (60., 0.)]))
    angle.adjectives['up_right'] = Adjective(
        Polygon([(30., 0.), (60., 1.), (90., 0.)]))
    angle.adjectives['up'] = Adjective(
        Polygon([(60., 0.), (90., 1.), (120., 0.)]))
    angle.adjectives['up_left'] = Adjective(
        Polygon([(90., 0.), (120., 1.), (150., 0.)]))
    angle.adjectives['up_more_left'] = Adjective(
        Polygon([(120., 0.), (150., 1.), (180., 0.)]))
    angle.adjectives['down_more_left'] = Adjective(
        Polygon([(180., 0.), (210., 1.), (240., 0.)]))
    angle.adjectives['down_left'] = Adjective(
        Polygon([(210., 0.), (240., 1.), (270., 0.)]))
    angle.adjectives['down'] = Adjective(
        Polygon([(240., 0.), (270., 1.), (300., 0.)]))
    angle.adjectives['down_right'] = Adjective(
        Polygon([(270., 0.), (300., 1.), (330., 0.)]))
    angle.adjectives['down_more_right'] = Adjective(
        Polygon([(300., 0.), (330., 1.), (360., 0.)]))

    angle_velocity = InputVariable(fuzzify=Plain(),
                                   description='angle velocity',
                                   min=-600.,
                                   max=600.,
                                   unit='degrees per second')
    system.variables['dPhi_dT'] = angle_velocity
    angle_velocity.adjectives['cw_fast'] = Adjective(
        Polygon([(-600., 1.), (-300., 0.)]))
    angle_velocity.adjectives['cw_slow'] = Adjective(
        Polygon([(-600., 0.), (-300., 1.), (0., 0.)]))
    angle_velocity.adjectives['stop'] = Adjective(
        Polygon([(-300., 0.), (0., 1.), (300., 0.)]))
    angle_velocity.adjectives['ccw_slow'] = Adjective(
        Polygon([(0., 0.), (300., 1.), (600., 0.)]))
    angle_velocity.adjectives['ccw_fast'] = Adjective(
        Polygon([(300., 0.), (600., 1.)]))

    position = InputVariable(fuzzify=Plain(),
                             description='position',
                             min=-20.,
                             max=20.,
                             unit='meter')
    system.variables['X'] = position
    position.adjectives['left_far'] = Adjective(
        Polygon([(-20., 1.), (-10., 0.)]))
    position.adjectives['left_near'] = Adjective(
        Polygon([(-20., 0.), (-5., 1.), (0., 0.)]))
    position.adjectives['stop'] = Adjective(
        Polygon([(-5., 0.), (0., 1.), (5., 0.)]))
    position.adjectives['right_near'] = Adjective(
        Polygon([(0., 0.), (5., 1.), (20., 0.)]))
    position.adjectives['right_far'] = Adjective(
        Polygon([(10., 0.), (20., 1.)]))

    velocity = InputVariable(fuzzify=Plain(),
                             description='velocity',
                             min=-10.,
                             max=10.,
                             unit='meter per second')
    system.variables['dX_dT'] = velocity
    velocity.adjectives['left_fast'] = Adjective(
        Polygon([(-10., 1.), (-5., 0.)]))
    velocity.adjectives['left_slow'] = Adjective(
        Polygon([(-10., 0.), (-2., 1.), (0., 0.)]))
    velocity.adjectives['stop'] = Adjective(
        Polygon([(-2., 0.), (0., 1.), (2., 0.)]))
    velocity.adjectives['right_slow'] = Adjective(
        Polygon([(0., 0.), (2., 1.), (10., 0.)]))
    velocity.adjectives['right_fast'] = Adjective(
        Polygon([(5., 0.), (10., 1.)]))

    acceleration = OutputVariable(defuzzify=COG,
                                  description='acceleration',
                                  min=-50.,
                                  max=50.,
                                  unit='meter per second^2')
    system.variables['a'] = acceleration
    acceleration.adjectives['left_fast'] = a_left_fast = Adjective(Polygon([
        (-50., 0.), (-20., 1.), (-10., 0.)
    ]),
                                                                   COM=COM)
    acceleration.adjectives['left_slow'] = a_left_slow = Adjective(Polygon([
        (-20., 0.), (-10., 1.), (0., 0.)
    ]),
                                                                   COM=COM)
    acceleration.adjectives['stop'] = a_stop = Adjective(Polygon([(-10., 0.),
                                                                  (0., 1.),
                                                                  (10., 0.)]),
                                                         COM=COM)
    acceleration.adjectives['right_slow'] = a_right_slow = Adjective(Polygon([
        (0., 0.), (10., 1.), (20., 0.)
    ]),
                                                                     COM=COM)
    acceleration.adjectives['right_fast'] = a_right_fast = Adjective(Polygon([
        (10., 0.), (20., 1.), (50., 0.)
    ]),
                                                                     COM=COM)

    from fuzzy.Rule import Rule
    from fuzzy.norm.Max import Max
    #from fuzzy.norm.Min import Min
    #from fuzzy.norm.BoundedDifference import BoundedDifference
    #from fuzzy.norm.DrasticSum import DrasticSum
    from fuzzy.norm.EinsteinSum import EinsteinSum
    from fuzzy.norm.DombiUnion import DombiUnion
    from fuzzy.operator.Compound import Compound
    from fuzzy.operator.Input import Input
    from fuzzy.operator.Not import Not

    system.rules['stop'] = Rule(
        adjective=a_stop,
        # it gets its value from here
        operator=Compound(
            Max(),
            Compound(AlgebraicProduct(),
                     Input(system.variables["Phi"].adjectives["up"]),
                     Input(system.variables["dPhi_dT"].adjectives["stop"])),
            Compound(AlgebraicProduct(),
                     Input(system.variables["Phi"].adjectives["up_right"]),
                     Input(
                         system.variables["dPhi_dT"].adjectives["ccw_slow"])),
            Compound(AlgebraicProduct(),
                     Input(system.variables["Phi"].adjectives["up_left"]),
                     Input(
                         system.variables["dPhi_dT"].adjectives["cw_slow"]))),
        CER=CER)

    system.rules['tilts right'] = Rule(
        adjective=a_right_slow,
        # it gets its value from here
        operator=Compound(
            AlgebraicProduct(),
            Not(
                Compound(
                    AlgebraicProduct(),
                    Compound(
                        AlgebraicSum(),
                        Input(system.variables["X"].adjectives["left_near"]),
                        Input(system.variables["X"].adjectives["left_far"])),
                    Compound(
                        EinsteinSum(),
                        Input(
                            system.variables["dX_dT"].adjectives["left_slow"]),
                        Input(system.variables["dX_dT"].adjectives["left_fast"]
                              ))), ),
            Input(system.variables["Phi"].adjectives["up_right"])),
        CER=CER)

    system.rules['tilts left'] = Rule(
        adjective=a_left_slow,
        # it gets its value from here
        operator=Compound(
            AlgebraicProduct(),
            Not(
                Compound(
                    AlgebraicProduct(),
                    Compound(
                        AlgebraicSum(),
                        Input(system.variables["X"].adjectives["right_near"]),
                        Input(system.variables["X"].adjectives["right_far"])),
                    Compound(
                        DombiUnion(0.25),
                        Input(system.variables["dX_dT"].
                              adjectives["right_slow"]),
                        Input(system.variables["dX_dT"].
                              adjectives["right_fast"]))), ),
            Input(system.variables["Phi"].adjectives["up_left"])),
        CER=CER)

    system.rules['far right'] = Rule(
        adjective=a_right_fast,
        # it gets its value from here
        operator=Input(system.variables["Phi"].adjectives["up_more_right"]),
        CER=CER)

    system.rules['far left'] = Rule(
        adjective=a_left_fast,
        # it gets its value from here
        operator=Input(system.variables["Phi"].adjectives["up_more_left"]),
        CER=CER)

    system.rules['accelerate cw if down'] = Rule(
        adjective=a_right_slow,
        # it gets its value from here
        operator=Compound(
            AlgebraicProduct(),
            Input(system.variables["Phi"].adjectives["down"]),
            Compound(
                AlgebraicProduct(),
                Input(system.variables["dPhi_dT"].adjectives["cw_slow"]),
                Input(system.variables["dPhi_dT"].adjectives["cw_slow"]),
            )),
        CER=CER)

    system.rules['accelerate ccw if down'] = Rule(
        adjective=a_left_slow,
        # it gets its value from here
        operator=Compound(
            AlgebraicProduct(),
            Input(system.variables["Phi"].adjectives["down"]),
            Compound(
                AlgebraicProduct(),
                Input(system.variables["dPhi_dT"].adjectives["ccw_slow"]),
                Input(system.variables["dPhi_dT"].adjectives["ccw_slow"]),
            )),
        CER=CER)

    return system
 def template(self, i, o, c):
     r = complement(c, Polygon(i))
     self.assertEqual(o, r.points)
Beispiel #16
0
 def template(self, s1, s2, z):
     r = [a for a in _merge_generator1(Polygon(s1), Polygon(s2))]
     self.assertEqual(z, r)
Beispiel #17
0
 def template(self, x1, x2, y, n):
     r = merge(n, Polygon(x1), Polygon(x2))
     self.assertEqual(y, r.points)
Beispiel #18
0
 def test2_(self):
     s1 = Polygon([(0.0, 0.25), (1.0, 0.75)])
     z = [(0.0, 0.25), (1.0, 0.75)]
     #r = list(_merge_generator1(s1,s2))
     r = [(x, y) for (x, y) in s1.getValuesXY()]
     self.assertEqual(z, r)
Beispiel #19
0
    def test_output_variable_only(self):
        " should return the correct outout when only changing the outputvar to a OutputVariableModel in th System "

        from fuzzy_modeling.models import OutputVariableModel

        from fuzzy.norm.AlgebraicSum import AlgebraicSum
        from fuzzy.norm.AlgebraicProduct import AlgebraicProduct
        from fuzzy.Adjective import Adjective
        from fuzzy.set.Polygon import Polygon
        from fuzzy.defuzzify.COG import COG
        from fuzzy.OutputVariable import OutputVariable

        pyfuzzy_system_expected = self._createSystem()

        new_pyfuzzy_system = self._createSystem()

        INF = AlgebraicProduct()
        ACC = AlgebraicSum()
        COM = AlgebraicSum()
        COG = COG(INF=INF, ACC=ACC, failsafe=0., segment_size=0.5)

        acceleration = OutputVariable(defuzzify=COG,
                                      description='acceleration',
                                      min=-50.,
                                      max=50.,
                                      unit='meter per second^2')

        acceleration.adjectives['left_fast'] = a_left_fast = Adjective(Polygon(
            [(-50., 0.), (-20., 1.), (-10., 0.)]),
                                                                       COM=COM)
        acceleration.adjectives['left_slow'] = a_left_slow = Adjective(Polygon(
            [(-20., 0.), (-10., 1.), (0., 0.)]),
                                                                       COM=COM)
        acceleration.adjectives['stop'] = a_stop = Adjective(Polygon([
            (-10., 0.), (0., 1.), (10., 0.)
        ]),
                                                             COM=COM)
        acceleration.adjectives['right_slow'] = a_right_slow = Adjective(
            Polygon([(0., 0.), (10., 1.), (20., 0.)]), COM=COM)
        acceleration.adjectives['right_fast'] = a_right_fast = Adjective(
            Polygon([(10., 0.), (20., 1.), (50., 0.)]), COM=COM)

        acceleration.name = 'a'
        new_acceleration = OutputVariableModel.from_pyfuzzy(
            acceleration).get_pyfuzzy()

        new_pyfuzzy_system.variables['a'] = acceleration

        import math
        input_dict = {}
        input_dict["X"] = 0.0  #: position [m]
        input_dict["dX_dT"] = 0.0  #: velocity [m/s]
        input_dict["Phi"] = math.radians(45.0)  #: angle [rad]
        input_dict["dPhi_dT"] = math.radians(0.0)  #: angle velocity [rad/s]

        i_dict1 = input_dict.copy()
        i_dict2 = input_dict.copy()

        output_dict1 = {
            'a': 0.0  #: acceleration [m/s²]
        }
        output_dict2 = {
            'a': 0.0  #: acceleration [m/s²]
        }

        pyfuzzy_system_expected.fuzzify(i_dict1)
        pyfuzzy_system_expected.inference()
        pyfuzzy_system_expected.defuzzify(output_dict1)

        new_pyfuzzy_system.fuzzify(i_dict2)
        new_pyfuzzy_system.inference()
        new_pyfuzzy_system.defuzzify(output_dict2)

        pyfuzzy_system_expected.calculate(i_dict1, output_dict1)
        new_pyfuzzy_system.calculate(i_dict2, output_dict2)

        self.assertNotEquals(output_dict1['a'], output_dict2['a'])
Beispiel #20
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]
Beispiel #21
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
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
Beispiel #23
0
 def template(self, i, o, n, p):
     r = norm(n, Polygon(i), p)
     self.assertEqual(o, r.points)
Beispiel #24
0
 def test2__(self):
     s1 = Polygon([(0.0, 0.25), (1.0, 0.75)])
     z = [0.0, 1.0]
     #r = list(_merge_generator1(s1,s2))
     r = [x for x in s1.getValuesX()]
     self.assertEqual(z, r)