Exemplo n.º 1
0
 def test_defuzzifier(self) -> None:
     self.assertEqual(fl.FllExporter().defuzzifier(None), "none")
     defuzzifier = fl.Centroid()
     self.assertEqual(fl.FllExporter().to_string(defuzzifier),
                      fl.FllExporter().defuzzifier(defuzzifier))
     self.assertEqual(fl.FllExporter().defuzzifier(defuzzifier),
                      "Centroid 100")
Exemplo n.º 2
0
    def test_centroid(self) -> None:
        DefuzzifierAssert(self, fl.Centroid()) \
            .exports_fll("Centroid 100") \
            .has_parameters("100") \
            .configured_as("200") \
            .exports_fll("Centroid 200")

        DefuzzifierAssert(self, fl.Centroid()) \
            .defuzzifies(
            {
                fl.Triangle("", -1, 0): -0.5,
                fl.Triangle("", -1, 1): 0.0,
                fl.Triangle("", 0, 1): 0.5,
                fl.Aggregated("", 0, 1, fl.Maximum(), [
                    fl.Activated(fl.Triangle("Medium", 0.25, 0.5, 0.75), 0.2, fl.Minimum()),
                    fl.Activated(fl.Triangle("High", 0.5, 0.75, 1.0), 0.8, fl.Minimum())
                ]): 0.6900552486187845
            }, -1, 1)
Exemplo n.º 3
0
    def test_defuzzifier(self) -> None:
        self.assertEqual(fl.PythonExporter().defuzzifier(None), "None")

        defuzzifier: fl.Defuzzifier = fl.Centroid()
        self.assertEqual(fl.PythonExporter().to_string(defuzzifier),
                         fl.PythonExporter().defuzzifier(defuzzifier))
        self.assertEqual(fl.PythonExporter().defuzzifier(defuzzifier),
                         "fl.Centroid(100)")

        defuzzifier = fl.WeightedAverage()
        self.assertEqual(fl.PythonExporter().to_string(defuzzifier),
                         fl.PythonExporter().defuzzifier(defuzzifier))
        self.assertEqual(fl.PythonExporter().defuzzifier(defuzzifier),
                         "fl.WeightedAverage(\"Automatic\")")
Exemplo n.º 4
0
        terms=[
            fl.Trapezoid("rancid", 0.000, 0.000, 2.500, 7.500),
            fl.Trapezoid("delicious", 2.500, 7.500, 10.000, 10.000)
        ]
    )
]
engine.output_variables = [
    fl.OutputVariable(
        name="mTip",
        description="tip based on Mamdani inference",
        enabled=True,
        minimum=0.000,
        maximum=30.000,
        lock_range=False,
        aggregation=fl.Maximum(),
        defuzzifier=fl.Centroid(100),
        lock_previous=False,
        terms=[
            fl.Triangle("cheap", 0.000, 5.000, 10.000),
            fl.Triangle("average", 10.000, 15.000, 20.000),
            fl.Triangle("generous", 20.000, 25.000, 30.000)
        ]
    ),
    fl.OutputVariable(
        name="tsTip",
        description="tip based on Takagi-Sugeno inference",
        enabled=True,
        minimum=0.000,
        maximum=30.000,
        lock_range=False,
        aggregation=None,
                     maximum=10.000,
                     lock_range=False,
                     terms=[
                         fl.Trapezoid("Bad", 0.000, 1.000, 3.000, 7.000),
                         fl.Trapezoid("Good", 3.000, 7.000, 10.000, 11.000)
                     ])
]
engine.output_variables = [
    fl.OutputVariable(name="Tip",
                      description="",
                      enabled=True,
                      minimum=0.000,
                      maximum=30.000,
                      lock_range=False,
                      aggregation=fl.AlgebraicSum(),
                      defuzzifier=fl.Centroid(200),
                      lock_previous=False,
                      terms=[
                          fl.Gaussian("AboutTenPercent", 10.000, 2.000),
                          fl.Gaussian("AboutFifteenPercent", 15.000, 2.000),
                          fl.Gaussian("AboutTwentyPercent", 20.000, 2.000)
                      ]),
    fl.OutputVariable(name="CheckPlusTip",
                      description="",
                      enabled=True,
                      minimum=1.000,
                      maximum=1.300,
                      lock_range=False,
                      aggregation=fl.AlgebraicSum(),
                      defuzzifier=fl.Centroid(200),
                      lock_previous=False,
            fl.Triangle('medium', .5, 2.5, 3.75),
            fl.Ramp('far', 2.5, 5)
        ]
    ),                        
]

avoidance_engine.output_variables = [
    fl.OutputVariable(
        name='Rotation',
        description='Rotational/angular velocity',
        enabled=True,
        minimum=-1.5,
        maximum=1.5,
        lock_range=True,
        aggregation=fl.Maximum(),
        defuzzifier=fl.Centroid(),
        terms=[
            # Angular velocity (used be to degree of rotation mapped from -90 to 90 but /cmd_vel expects angular velocity)
            fl.Ramp('very_right', -.75, -1.5),
            fl.Triangle('right', -1.5, -.75, 0),
            fl.Triangle('straight_ahead', -.1, 0, .1),
            fl.Triangle('left', 0, .75, 1.5),
            fl.Ramp('very_left', .75, 1.5),
        ]
    ),
    fl.OutputVariable(
        name='Velocity',
        description='Translational velocity',
        enabled=True,
        minimum=-.5,
        maximum=.6,
Exemplo n.º 7
0
    def test_engine(self) -> None:
        engine = fl.Engine()
        engine.name = "tipper"
        engine.description = "(service and food) -> (tip)"

        service = fl.InputVariable()
        service.name = "service"
        service.description = "quality of service"
        service.enabled = True
        service.range = (0.000, 10.000)
        service.lock_range = True
        service.terms.append(fl.Trapezoid("poor", 0.000, 0.000, 2.500, 5.000))
        service.terms.append(fl.Triangle("good", 2.500, 5.000, 7.500))
        service.terms.append(fl.Trapezoid("excellent", 5.000, 7.500, 10.000, 10.000))
        engine.input_variables.append(service)

        food = fl.InputVariable()
        food.name = "food"
        food.description = "quality of food"
        food.enabled = True
        food.range = (0.000, 10.000)
        food.lock_range = True
        food.terms.append(fl.Trapezoid("rancid", 0.000, 0.000, 2.500, 7.500))
        food.terms.append(fl.Trapezoid("delicious", 2.500, 7.500, 10.000, 10.000))
        engine.input_variables.append(food)

        mTip = fl.OutputVariable()  # noqa N806 should be lowercase
        mTip.name = "mTip"
        mTip.description = "tip based on Mamdani inference"
        mTip.enabled = True
        mTip.range = (0.000, 30.000)
        mTip.lock_range = False
        mTip.aggregation = fl.Maximum()
        mTip.defuzzifier = fl.Centroid(100)
        mTip.default_value = fl.nan
        mTip.lock_previous = False
        mTip.terms.append(fl.Triangle("cheap", 0.000, 5.000, 10.000))
        mTip.terms.append(fl.Triangle("average", 10.000, 15.000, 20.000))
        mTip.terms.append(fl.Triangle("generous", 20.000, 25.000, 30.000))
        engine.output_variables.append(mTip)

        tsTip = fl.OutputVariable()  # noqa N806 should be lowercase
        tsTip.name = "tsTip"
        tsTip.description = "tip based on Takagi-Sugeno inference"
        tsTip.enabled = True
        tsTip.range = (0.000, 30.000)
        tsTip.lock_range = False
        tsTip.aggregation = None
        tsTip.defuzzifier = fl.WeightedAverage("TakagiSugeno")
        tsTip.default_value = fl.nan
        tsTip.lock_previous = False
        tsTip.terms.append(fl.Constant("cheap", 5.000))
        tsTip.terms.append(fl.Constant("average", 15.000))
        tsTip.terms.append(fl.Constant("generous", 25.000))
        engine.output_variables.append(tsTip)

        mamdani = fl.RuleBlock()
        mamdani.name = "mamdani"
        mamdani.description = "Mamdani inference"
        mamdani.enabled = True
        mamdani.conjunction = fl.AlgebraicProduct()
        mamdani.disjunction = fl.AlgebraicSum()
        mamdani.implication = fl.Minimum()
        mamdani.activation = fl.General()
        mamdani.rules.append(
            fl.Rule.create("if service is poor or food is rancid then mTip is cheap", engine))
        mamdani.rules.append(fl.Rule.create("if service is good then mTip is average", engine))
        mamdani.rules.append(fl.Rule.create(
            "if service is excellent or food is delicious then mTip is generous with 0.5", engine))
        mamdani.rules.append(fl.Rule.create(
            "if service is excellent and food is delicious then mTip is generous with 1.0", engine))
        engine.rule_blocks.append(mamdani)

        takagiSugeno = fl.RuleBlock()  # noqa N806 should be lowercase
        takagiSugeno.name = "takagiSugeno"
        takagiSugeno.description = "Takagi-Sugeno inference"
        takagiSugeno.enabled = True
        takagiSugeno.conjunction = fl.AlgebraicProduct()
        takagiSugeno.disjunction = fl.AlgebraicSum()
        takagiSugeno.implication = None
        takagiSugeno.activation = fl.General()
        takagiSugeno.rules.append(fl.Rule.create(
            "if service is poor or food is rancid then tsTip is cheap", engine))
        takagiSugeno.rules.append(fl.Rule.create(
            "if service is good then tsTip is average", engine))
        takagiSugeno.rules.append(fl.Rule.create(
            "if service is excellent or food is delicious then tsTip is generous with 0.5", engine))
        takagiSugeno.rules.append(fl.Rule.create(
            "if service is excellent and food is delicious then tsTip is generous with 1.0",
            engine))
        engine.rule_blocks.append(takagiSugeno)

        EngineAssert(self, engine) \
            .has_name("tipper") \
            .has_description("(service and food) -> (tip)") \
            .has_n_inputs(2).has_inputs(["service", "food"]) \
            .has_n_outputs(2).has_outputs(["mTip", "tsTip"]) \
            .has_n_blocks(2).has_blocks(["mamdani", "takagiSugeno"]) \
            .evaluate_fld(
            """\
#service food mTip tsTip
0.0000000000000000 0.0000000000000000 4.9989502099580099 5.0000000000000000
0.0000000000000000 3.3333333333333335 7.7561896551724301 6.5384615384615392
0.0000000000000000 6.6666666666666670 12.9489036144578247 10.8823529411764728
0.0000000000000000 10.0000000000000000 13.5707062050051448 11.6666666666666661
3.3333333333333335 0.0000000000000000 8.5688247396168276 7.5000000000000000
3.3333333333333335 3.3333333333333335 10.1101355034654858 8.6734693877551035
3.3333333333333335 6.6666666666666670 13.7695060342408198 12.9245283018867916
3.3333333333333335 10.0000000000000000 14.3676481312670976 13.8888888888888911
6.6666666666666670 0.0000000000000000 12.8954528230390419 11.0000000000000000
6.6666666666666670 3.3333333333333335 13.2040624705105234 12.7966101694915260
6.6666666666666670 6.6666666666666670 17.9862390284958273 20.6363636363636367
6.6666666666666670 10.0000000000000000 21.1557340720221632 22.7777777777777821
10.0000000000000000 0.0000000000000000 13.5707062050051448 11.6666666666666661
10.0000000000000000 3.3333333333333335 13.7092196934510024 13.8888888888888875
10.0000000000000000 6.6666666666666670 20.2157800031293959 22.7777777777777821
10.0000000000000000 10.0000000000000000 25.0010497900419928 25.0000000000000000
""", decimals=16)
Exemplo n.º 8
0
 def test_defuzzifier(self) -> None:
     self.assertEqual(fl.PythonExporter().defuzzifier(None), "None")
     defuzzifier = fl.Centroid()
     self.assertEqual(fl.PythonExporter().to_string(defuzzifier),
                      fl.PythonExporter().defuzzifier(defuzzifier))
     self.assertEqual(fl.PythonExporter().defuzzifier(defuzzifier), "fl.Centroid(100)")