#74 -- complete fis

This commit is contained in:
Anton Romanov 2022-10-18 15:02:33 +04:00
parent 00119b896c
commit 374415e154

View File

@ -1,6 +1,6 @@
package ru.ulstu.extractor.rule.service; 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.membership.MembershipFunctionTriangular;
import net.sourceforge.jFuzzyLogic.rule.FuzzyRule; import net.sourceforge.jFuzzyLogic.rule.FuzzyRule;
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleExpression; import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleExpression;
@ -12,18 +12,21 @@ import net.sourceforge.jFuzzyLogic.ruleConnection.RuleConnectionMethodAndMin;
import net.sourceforge.jFuzzyLogic.ruleImplication.RuleImplicationMethodMin; import net.sourceforge.jFuzzyLogic.ruleImplication.RuleImplicationMethodMin;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList; import java.util.LinkedList;
public class FuzzyInference { public class FuzzyInference {
public void run() { 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("Погода"); Variable weather = new Variable("Погода");
weather.getLinguisticTerms().put("солнечно", weather.getLinguisticTerms().put("солнечно",
new LinguisticTerm("солнечно", new MembershipFunctionTriangular(10, 20, 30))); new LinguisticTerm("солнечно", new MembershipFunctionTriangular(0, 20, 30)));
weather.getLinguisticTerms().put("мороз", weather.getLinguisticTerms().put("мороз",
new LinguisticTerm("мороз", new MembershipFunctionTriangular(-50, 0, 10))); new LinguisticTerm("мороз", new MembershipFunctionTriangular(-50, -10, 10)));
weather.setDefuzzifier(new DefuzzifierCenterOfArea(weather)); weather.setDefuzzifier(new DefuzzifierCenterOfGravity(weather));
Variable suit = new Variable("Одежда"); Variable suit = new Variable("Одежда");
@ -31,40 +34,66 @@ public class FuzzyInference {
new LinguisticTerm("легко одет", new MembershipFunctionTriangular(0, 5, 10))); new LinguisticTerm("легко одет", new MembershipFunctionTriangular(0, 5, 10)));
suit.getLinguisticTerms().put("тепло одет", suit.getLinguisticTerms().put("тепло одет",
new LinguisticTerm("тепло одет", new MembershipFunctionTriangular(5, 10, 20))); new LinguisticTerm("тепло одет", new MembershipFunctionTriangular(5, 10, 20)));
suit.setDefuzzifier(new DefuzzifierCenterOfArea(suit)); suit.setDefuzzifier(new DefuzzifierCenterOfGravity(suit));
Variable feel = new Variable("Ощущение"); Variable feel = new Variable("Ощущение");
feel.getLinguisticTerms().put("Холодно", feel.getLinguisticTerms().put("Холодно",
new LinguisticTerm("Холодно", new MembershipFunctionTriangular(0, 5, 10))); new LinguisticTerm("Холодно", new MembershipFunctionTriangular(0, 5, 10)));
feel.getLinguisticTerms().put("Жарко", feel.getLinguisticTerms().put("Жарко",
new LinguisticTerm("Жарко", new MembershipFunctionTriangular(5, 10, 20))); new LinguisticTerm("Жарко", new MembershipFunctionTriangular(5, 10, 20)));
feel.setDefuzzifier(new DefuzzifierCenterOfArea(feel)); feel.setDefuzzifier(new DefuzzifierCenterOfGravity(feel));
FuzzyRuleTerm weatherTerm1 = new FuzzyRuleTerm(weather, "солнечно", false); FuzzyRuleTerm weatherTerm1 = new FuzzyRuleTerm(weather, "солнечно", false);
//FuzzyRuleTerm weatherTerm2 = new FuzzyRuleTerm(weather, "мороз", false); FuzzyRuleTerm weatherTerm2 = new FuzzyRuleTerm(weather, "мороз", false);
//FuzzyRuleTerm weatherTerm3 = new FuzzyRuleTerm(weather, "дождливо", false);
//FuzzyRuleTerm suitTerm1 = new FuzzyRuleTerm(suit, "легко одет", false); FuzzyRuleTerm suitTerm1 = new FuzzyRuleTerm(suit, "легко одет", false);
FuzzyRuleTerm suitTerm2 = 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 feelWarm = new FuzzyRuleTerm(feel, "Жарко", false);
//FuzzyRuleTerm feelBad = new FuzzyRuleTerm(feel, "Сыро", false);
FuzzyRuleExpression expression1 = new FuzzyRuleExpression(weatherTerm1, suitTerm2, new RuleConnectionMethodAndMin()); FuzzyRuleExpression expression1 = new FuzzyRuleExpression(weatherTerm1, suitTerm2, new RuleConnectionMethodAndMin());
//FuzzyRuleExpression expression2 = new FuzzyRuleExpression(weatherTerm2, suitTerm1, new RuleConnectionMethodAndMin()); fuzzyRule1.setAntecedents(expression1);
fuzzyRule.setAntecedents(expression1); fuzzyRule1.setConsequents(new LinkedList<>(Collections.singleton(feelWarm)));
fuzzyRule.setConsequents(new LinkedList<>(Collections.singleton(feelWarm)));
fuzzyRule.evaluate(new RuleImplicationMethodMin()); FuzzyRuleExpression expression2 = new FuzzyRuleExpression(weatherTerm2, suitTerm1, new RuleConnectionMethodAndMin());
fuzzyRule2.setAntecedents(expression2);
fuzzyRule2.setConsequents(new LinkedList<>(Collections.singleton(feelCold)));
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(); FuzzyRuleSet set = new FuzzyRuleSet();
set.add(fuzzyRule); set.add(fuzzyRule1);
set.add(fuzzyRule2);
set.add(fuzzyRule3);
set.evaluate(); set.evaluate();
System.out.println(fuzzyRule.toString()); set.setVariable("Погода", 25);
set.setVariable("Одежда", 7);
// Evaluate fuzzy set
set.evaluate();
// 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);
} }
} }