def test_algebraic_sum(self) -> None: NormAssert(self, fl.AlgebraicSum()) \ .is_s_norm() \ .evaluates( { (0.00, 0.00): 0.00, (0.00, 0.25): 0.25, (0.00, 0.50): 0.50, (0.00, 0.75): 0.75, (0.00, 1.00): 1.00, (0.50, 0.25): 0.625, (0.50, 0.50): 0.75, (0.50, 0.75): 0.875, (1.00, 0.00): 1.00, (1.00, 0.25): 1.00, (1.00, 0.50): 1.00, (1.00, 0.75): 1.00, (1.00, 1.00): 1.00, })
defuzzifier=fl.WeightedAverage("TakagiSugeno"), lock_previous=False, terms=[ fl.Constant("cheap", 5.000), fl.Constant("average", 15.000), fl.Constant("generous", 25.000) ] ) ] engine.rule_blocks = [ fl.RuleBlock( name="mamdani", description="Mamdani inference", enabled=True, conjunction=fl.AlgebraicProduct(), disjunction=fl.AlgebraicSum(), implication=fl.Minimum(), activation=fl.General(), rules=[ fl.Rule.create("if service is poor or food is rancid then mTip is cheap", engine), fl.Rule.create("if service is good then mTip is average", engine), fl.Rule.create("if service is excellent or food is delicious then mTip is generous with 0.500", engine), fl.Rule.create("if service is excellent and food is delicious then mTip is generous", engine) ] ), fl.RuleBlock( name="takagiSugeno", description="Takagi-Sugeno inference", enabled=True, conjunction=fl.AlgebraicProduct(), disjunction=fl.AlgebraicSum(),
minimum=1.000, 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),
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)