From 00119b896cf8422135cc350d26fb392539f0e865 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 18 Oct 2022 00:19:07 +0400 Subject: [PATCH] #74 -- add some fuzzy rules --- build.gradle | 4 ++ .../extractor/GitExtractorApplication.java | 3 + .../rule/service/FuzzyInference.java | 70 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/main/java/ru/ulstu/extractor/rule/service/FuzzyInference.java diff --git a/build.gradle b/build.gradle index a94560f..e8eaefa 100644 --- a/build.gradle +++ b/build.gradle @@ -35,6 +35,9 @@ compileJava { repositories { mavenLocal() mavenCentral() + maven { + url="https://repository.ow2.org/nexus/content/repositories/public" + } } configurations { @@ -65,6 +68,7 @@ dependencies { implementation group: 'com.ibm.icu', name: 'icu4j', version: '63.1' implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.9.0.202009080501-r' + implementation group: 'net.sourceforge.jFuzzyLogic', name: 'jFuzzyLogic', version: '1.2.1' implementation group: 'org.webjars', name: 'jquery', version: '3.6.0' implementation group: 'org.webjars', name: 'bootstrap', version: '4.6.0' diff --git a/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java b/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java index f9a272d..85d9a30 100644 --- a/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java +++ b/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java @@ -3,11 +3,14 @@ package ru.ulstu.extractor; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; +import ru.ulstu.extractor.rule.service.FuzzyInference; @SpringBootApplication @EnableScheduling public class GitExtractorApplication { public static void main(String[] args) { + + new FuzzyInference().run(); SpringApplication.run(GitExtractorApplication.class, args); } } diff --git a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInference.java b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInference.java new file mode 100644 index 0000000..54f99f4 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInference.java @@ -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()); + } +}