Exemplo n.º 1
0
    def get_pyfuzzy(self):
        """
        Return the Pyfuzzy class of this model
        """
        FuzzifyClass = get_class_by_python_path(self.fuzzify)

        ivar = InputVariable(fuzzify=FuzzifyClass(),
                             description=self.description,
                             min=self.min,
                             max=self.max,
                             unit=self.unit)
        adjs = self.adjectivemodel_set.all()
        for adj in adjs:
            ivar.adjectives[adj.name] = adj.get_pyfuzzy()

        return ivar
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
0
def create_controller():
    controller = PendulumController()

    # Create the membership functions to variable O (inclination of the pendulum).
    # Ovbn = Very Big Negative
    # Obn  = Big Negative
    # Osn  = Small Negative
    # Oz   = Near Zero
    # Osp  = Small Positive
    # Obp  = Big Positive
    # Ovbp = Very Big Positive
    O = InputVariable(fuzzify=Plain())
    controller.variables['O'] = O
    O_names = ['Ovbn', 'Obn', 'Osn', 'Oz', 'Osp', 'Obp', 'Ovbp']
    O.adjectives = {name: Adjective(mf) for name, mf in zip(O_names, flat_saw((-3 * pi / 8, 3 * pi / 8), 7))}

    # Create the membership functions to variable w (angular speed).
    # wbn = Big Negative
    # wsn = Small Negative
    # wz  = Near Zero
    # wsp = Small Positive
    # wbp = Big Positive
    w = InputVariable(fuzzify=Plain())
    controller.variables['w'] = w
    w_names = ['wbn', 'wsn', 'wz', 'wsp', 'wbp']
    w.adjectives = {name: Adjective(mf) for name, mf in zip(w_names, flat_saw((-3 * pi, 3 * pi), 5))}

    # Create the membership functions to variable F (Force applied to chart).
    # Fvvbn = Very Very Big Negative
    # Fvbn  = Very Big Negative
    # Fbn   = Big Negative
    # Fsn   = Small Negative
    # Fz    = Near Zero
    # Fsp   = Small Positive
    # Fbp   = Big Positive
    # Fvbp  = Very Big Positive
    # Fvvbp = Very Very Big Positive
    F = OutputVariable(defuzzify=controller.defuzzy())
    controller.variables['F'] = F
    F_names = ['Fvvbn', 'Fvbn', 'Fbn', 'Fsn', 'Fz', 'Fsp', 'Fbp', 'Fvbp', 'Fvvbp']
    F.adjectives = {name: Adjective(mf) for name, mf in zip(F_names, flat_saw((-100., 100.), 9))}

    # Create the controller and insert into it the decision rules. The decision
    # rules are inserted with the use of the add_table method. In this table, each
    # line represents a linguistic value of the O variable; each column represents
    # a linguistic value of the variable w. Each element of the table is the given
    # answer linguistic value of the variable F.
    table_names = [
        ['Fvvbn', 'Fvvbn', 'Fvbn', 'Fbn', 'Fsn'],
        ['Fvvbn', 'Fvbn', 'Fbn', 'Fsn', 'Fz'],
        ['Fvbn', 'Fbn', 'Fsn', 'Fz', 'Fsp'],
        ['Fbn', 'Fsn', 'Fz', 'Fsp', 'Fbp'],
        ['Fsn', 'Fz', 'Fsp', 'Fbp', 'Fvbp'],
        ['Fz', 'Fsp', 'Fbp', 'Fvbp', 'Fvvbp'],
        ['Fsp', 'Fbp', 'Fvbp', 'Fvvbp', 'Fvvbp']
    ]
    controller.add_table([controller.variables['O'].adjectives[name] for name in O_names],
                     [controller.variables['w'].adjectives[name] for name in w_names],
                     [[controller.variables['F'].adjectives[name] for name in row] for row in table_names])

    # Create the membership functions to variable x (cart position).
    x = InputVariable(fuzzify=Plain())
    controller.variables['x'] = x
    x_names = ['xn', 'xz', 'xp']
    x.adjectives = {name: Adjective(mf) for name, mf in zip(x_names, flat_saw((-10., 10.), 3))}

    # Create the membership functions to variable v (cart speed).
    v = InputVariable(fuzzify=Plain())
    controller.variables['v'] = v
    v_names = ['vn', 'vz', 'vp']
    v.adjectives = {name: Adjective(mf) for name, mf in zip(v_names, flat_saw((-6., 6.), 3))}

    # Decision rules for position and speed of the cart. While this worked, the
    # pendulum ended up very unstable.
    table_names = [
        ['Fbp', 'Fbp', 'Fz'],
        ['Fbp', 'Fz', 'Fbn'],
        ['Fz', 'Fbn', 'Fbn']
    ]
    # controller.add_table([controller.variables['x'].adjectives[name] for name in x_names],
    #                  [controller.variables['v'].adjectives[name] for name in v_names],
    #                  [[controller.variables['F'].adjectives[name] for name in row] for row in table_names])

    return controller
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
Exemplo n.º 7
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