diff --git a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInference.java b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInference.java index 54f99f4..0bd0ef2 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInference.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInference.java @@ -1,6 +1,6 @@ package ru.ulstu.extractor.rule.service; -import net.sourceforge.jFuzzyLogic.defuzzifier.DefuzzifierCenterOfArea; +import net.sourceforge.jFuzzyLogic.defuzzifier.DefuzzifierCenterOfGravity; import net.sourceforge.jFuzzyLogic.membership.MembershipFunctionTriangular; import net.sourceforge.jFuzzyLogic.rule.FuzzyRule; import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleExpression; @@ -12,18 +12,21 @@ import net.sourceforge.jFuzzyLogic.ruleConnection.RuleConnectionMethodAndMin; import net.sourceforge.jFuzzyLogic.ruleImplication.RuleImplicationMethodMin; import java.util.Collections; +import java.util.Comparator; import java.util.LinkedList; public class FuzzyInference { public void run() { - FuzzyRule fuzzyRule = new FuzzyRule("rule 1"); + FuzzyRule fuzzyRule1 = new FuzzyRule("rule 1"); + FuzzyRule fuzzyRule2 = new FuzzyRule("rule 2"); + FuzzyRule fuzzyRule3 = new FuzzyRule("rule 3"); Variable weather = new Variable("Погода"); weather.getLinguisticTerms().put("солнечно", - new LinguisticTerm("солнечно", new MembershipFunctionTriangular(10, 20, 30))); + new LinguisticTerm("солнечно", new MembershipFunctionTriangular(0, 20, 30))); weather.getLinguisticTerms().put("мороз", - new LinguisticTerm("мороз", new MembershipFunctionTriangular(-50, 0, 10))); - weather.setDefuzzifier(new DefuzzifierCenterOfArea(weather)); + new LinguisticTerm("мороз", new MembershipFunctionTriangular(-50, -10, 10))); + weather.setDefuzzifier(new DefuzzifierCenterOfGravity(weather)); Variable suit = new Variable("Одежда"); @@ -31,40 +34,66 @@ public class FuzzyInference { new LinguisticTerm("легко одет", new MembershipFunctionTriangular(0, 5, 10))); suit.getLinguisticTerms().put("тепло одет", new LinguisticTerm("тепло одет", new MembershipFunctionTriangular(5, 10, 20))); - suit.setDefuzzifier(new DefuzzifierCenterOfArea(suit)); - + suit.setDefuzzifier(new DefuzzifierCenterOfGravity(suit)); Variable feel = new Variable("Ощущение"); feel.getLinguisticTerms().put("Холодно", new LinguisticTerm("Холодно", new MembershipFunctionTriangular(0, 5, 10))); feel.getLinguisticTerms().put("Жарко", new LinguisticTerm("Жарко", new MembershipFunctionTriangular(5, 10, 20))); - feel.setDefuzzifier(new DefuzzifierCenterOfArea(feel)); + feel.setDefuzzifier(new DefuzzifierCenterOfGravity(feel)); FuzzyRuleTerm weatherTerm1 = new FuzzyRuleTerm(weather, "солнечно", false); - //FuzzyRuleTerm weatherTerm2 = new FuzzyRuleTerm(weather, "мороз", false); - //FuzzyRuleTerm weatherTerm3 = new FuzzyRuleTerm(weather, "дождливо", false); + FuzzyRuleTerm weatherTerm2 = new FuzzyRuleTerm(weather, "мороз", false); - //FuzzyRuleTerm suitTerm1 = new FuzzyRuleTerm(suit, "легко одет", false); + FuzzyRuleTerm suitTerm1 = new FuzzyRuleTerm(suit, "легко одет", false); FuzzyRuleTerm suitTerm2 = new FuzzyRuleTerm(suit, "тепло одет", false); - //FuzzyRuleTerm suitTerm3 = new FuzzyRuleTerm(suit, "промокающая", false); - //FuzzyRuleTerm feelCold = new FuzzyRuleTerm(feel, "Холодно", false); + FuzzyRuleTerm feelCold = new FuzzyRuleTerm(feel, "Холодно", false); FuzzyRuleTerm feelWarm = new FuzzyRuleTerm(feel, "Жарко", false); - //FuzzyRuleTerm feelBad = new FuzzyRuleTerm(feel, "Сыро", false); - FuzzyRuleExpression expression1 = new FuzzyRuleExpression(weatherTerm1, suitTerm2, new RuleConnectionMethodAndMin()); - //FuzzyRuleExpression expression2 = new FuzzyRuleExpression(weatherTerm2, suitTerm1, new RuleConnectionMethodAndMin()); - fuzzyRule.setAntecedents(expression1); - fuzzyRule.setConsequents(new LinkedList<>(Collections.singleton(feelWarm))); + fuzzyRule1.setAntecedents(expression1); + fuzzyRule1.setConsequents(new LinkedList<>(Collections.singleton(feelWarm))); + + FuzzyRuleExpression expression2 = new FuzzyRuleExpression(weatherTerm2, suitTerm1, new RuleConnectionMethodAndMin()); + fuzzyRule2.setAntecedents(expression2); + fuzzyRule2.setConsequents(new LinkedList<>(Collections.singleton(feelCold))); - fuzzyRule.evaluate(new RuleImplicationMethodMin()); + FuzzyRuleExpression expression3 = new FuzzyRuleExpression(weatherTerm1, suitTerm1, new RuleConnectionMethodAndMin()); + fuzzyRule3.setAntecedents(expression3); + fuzzyRule3.setConsequents(new LinkedList<>(Collections.singleton(feelCold))); + + fuzzyRule1.evaluate(new RuleImplicationMethodMin()); + fuzzyRule2.evaluate(new RuleImplicationMethodMin()); + //fuzzyRule3.evaluate(new RuleImplicationMethodMin()); FuzzyRuleSet set = new FuzzyRuleSet(); - set.add(fuzzyRule); + set.add(fuzzyRule1); + set.add(fuzzyRule2); + set.add(fuzzyRule3); + set.evaluate(); + + set.setVariable("Погода", 25); + set.setVariable("Одежда", 7); + // Evaluate fuzzy set set.evaluate(); - System.out.println(fuzzyRule.toString()); + // Show output variable's chart + //set.getVariable("Ощущение").chartDefuzzifier(true); + System.out.println(set.getVariable("Ощущение").getLatestDefuzzifiedValue()); + System.out.println(set); + System.out.println(set.getVariable("Ощущение")); + System.out.println( + feel.getLinguisticTerms() + .entrySet() + .stream() + .max(Comparator.comparing(e -> e.getValue() + .getMembershipFunction() + .membership(set.getVariable("Ощущение").getLatestDefuzzifiedValue()))) + .get() + .getValue().getTermName() + ); + set.getVariable("Ощущение").chartDefuzzifier(true); } }