#74 -- add some fuzzy rules
parent
f59dc9b523
commit
00119b896c
@ -0,0 +1,70 @@
|
|||||||
|
package ru.ulstu.extractor.rule.service;
|
||||||
|
|
||||||
|
import net.sourceforge.jFuzzyLogic.defuzzifier.DefuzzifierCenterOfArea;
|
||||||
|
import net.sourceforge.jFuzzyLogic.membership.MembershipFunctionTriangular;
|
||||||
|
import net.sourceforge.jFuzzyLogic.rule.FuzzyRule;
|
||||||
|
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleExpression;
|
||||||
|
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleSet;
|
||||||
|
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleTerm;
|
||||||
|
import net.sourceforge.jFuzzyLogic.rule.LinguisticTerm;
|
||||||
|
import net.sourceforge.jFuzzyLogic.rule.Variable;
|
||||||
|
import net.sourceforge.jFuzzyLogic.ruleConnection.RuleConnectionMethodAndMin;
|
||||||
|
import net.sourceforge.jFuzzyLogic.ruleImplication.RuleImplicationMethodMin;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class FuzzyInference {
|
||||||
|
public void run() {
|
||||||
|
FuzzyRule fuzzyRule = new FuzzyRule("rule 1");
|
||||||
|
|
||||||
|
Variable weather = new Variable("Погода");
|
||||||
|
weather.getLinguisticTerms().put("солнечно",
|
||||||
|
new LinguisticTerm("солнечно", new MembershipFunctionTriangular(10, 20, 30)));
|
||||||
|
weather.getLinguisticTerms().put("мороз",
|
||||||
|
new LinguisticTerm("мороз", new MembershipFunctionTriangular(-50, 0, 10)));
|
||||||
|
weather.setDefuzzifier(new DefuzzifierCenterOfArea(weather));
|
||||||
|
|
||||||
|
|
||||||
|
Variable suit = new Variable("Одежда");
|
||||||
|
suit.getLinguisticTerms().put("легко одет",
|
||||||
|
new LinguisticTerm("легко одет", new MembershipFunctionTriangular(0, 5, 10)));
|
||||||
|
suit.getLinguisticTerms().put("тепло одет",
|
||||||
|
new LinguisticTerm("тепло одет", new MembershipFunctionTriangular(5, 10, 20)));
|
||||||
|
suit.setDefuzzifier(new DefuzzifierCenterOfArea(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));
|
||||||
|
|
||||||
|
FuzzyRuleTerm weatherTerm1 = new FuzzyRuleTerm(weather, "солнечно", false);
|
||||||
|
//FuzzyRuleTerm weatherTerm2 = new FuzzyRuleTerm(weather, "мороз", false);
|
||||||
|
//FuzzyRuleTerm weatherTerm3 = new FuzzyRuleTerm(weather, "дождливо", 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 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)));
|
||||||
|
|
||||||
|
fuzzyRule.evaluate(new RuleImplicationMethodMin());
|
||||||
|
|
||||||
|
FuzzyRuleSet set = new FuzzyRuleSet();
|
||||||
|
set.add(fuzzyRule);
|
||||||
|
set.evaluate();
|
||||||
|
|
||||||
|
System.out.println(fuzzyRule.toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue