From 1dc44f79889eae29b0f0bbb36e5abb5f0965304e Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 21 Apr 2023 15:47:27 +0400 Subject: [PATCH] #91 -- Fix fuzzy inference --- .../controller/AssessmentController.java | 1 - .../assessment/service/AssessmentService.java | 38 ------------------- .../rule/service/FuzzyInferenceService.java | 27 +++++++------ src/main/resources/templates/assessments.html | 17 --------- 4 files changed, 16 insertions(+), 67 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java index 35beb28..9a7ffd8 100644 --- a/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java +++ b/src/main/java/ru/ulstu/extractor/assessment/controller/AssessmentController.java @@ -30,7 +30,6 @@ public class AssessmentController { model.addAttribute("branches", branchService.findAllValid()); if (branchId.isPresent()) { model.addAttribute("assessments", assessmentService.getAssessments(branchId.get())); - model.addAttribute("singleAssessment", assessmentService.getSingleAssessment(branchId.get())); model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get())); } else { model.addAttribute("filterBranchForm", new FilterBranchForm()); diff --git a/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java b/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java index 83e99ef..0bae62a 100644 --- a/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java +++ b/src/main/java/ru/ulstu/extractor/assessment/service/AssessmentService.java @@ -2,7 +2,6 @@ package ru.ulstu.extractor.assessment.service; import org.springframework.stereotype.Service; import ru.ulstu.extractor.assessment.model.Assessment; -import ru.ulstu.extractor.rule.model.AssessmentException; import ru.ulstu.extractor.rule.model.DbRule; import ru.ulstu.extractor.rule.service.AntecedentValueService; import ru.ulstu.extractor.rule.service.DbRuleService; @@ -10,12 +9,9 @@ import ru.ulstu.extractor.rule.service.FuzzyInferenceService; import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.service.TimeSeriesService; -import java.util.ArrayList; -import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; @Service public class AssessmentService { @@ -37,44 +33,10 @@ public class AssessmentService { public List getAssessments(Integer branchId) { List timeSeries = timeSeriesService.getByBranch(branchId); List dbRules = ruleService.getList(); - try { - return getAssessments(dbRules, timeSeries); - } catch (AssessmentException ex) { - ex.printStackTrace(); - return new ArrayList<>(); - } - } - - public List getSingleAssessment(Integer branchId) throws AssessmentException { - List timeSeries = timeSeriesService.getByBranch(branchId); - List dbRules = ruleService.getList(); - return getSingleAssessment(dbRules, timeSeries); - } - - private List getSingleAssessment(List dbRules, List timeSeries) throws AssessmentException { Map variableValues = new HashMap<>(); timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts))); return fuzzyInferenceService.getFuzzyInference(dbRules, antecedentValueService.getList(), variableValues); } - - private List getAssessments(List dbRules, List timeSeries) { - return dbRules - .stream() - .flatMap(dbRule -> { - Map variableValues = new HashMap<>(); - timeSeries - .stream() - .filter(ts -> ts.getTimeSeriesType() == dbRule.getFirstAntecedent() - || ts.getTimeSeriesType() == dbRule.getSecondAntecedent()) - .forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService - .getLastTimeSeriesTendency(ts))); - return fuzzyInferenceService.getFuzzyInference(List.of(dbRule), - antecedentValueService.getList(), - variableValues).stream(); - }) - .sorted(Comparator.comparing(Assessment::getDegree)) - .collect(Collectors.toList()); - } } diff --git a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java index a1a7f53..28838c6 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java @@ -8,6 +8,7 @@ import com.fuzzylite.norm.t.AlgebraicProduct; import com.fuzzylite.norm.t.Minimum; import com.fuzzylite.rule.Rule; import com.fuzzylite.rule.RuleBlock; +import com.fuzzylite.term.Activated; import com.fuzzylite.term.Triangle; import com.fuzzylite.variable.InputVariable; import com.fuzzylite.variable.OutputVariable; @@ -109,15 +110,19 @@ public class FuzzyInferenceService { Engine engine = getFuzzyEngine(); List consequentValues = dbRules.stream().map(DbRule::getId).collect(Collectors.toList()); engine.addRuleBlock(getRuleBlock(engine, dbRules, variableValues, antecedentValues, consequentValues)); - Map.Entry consequent = getConsequent(engine, variableValues); - if (consequent.getKey().equals(NO_RESULT)) { + Map consequents = getConsequent(engine, variableValues); + if (consequents.containsKey(NO_RESULT)) { return new ArrayList<>(); } - return dbRules - .stream() - .filter(r -> r.getId().equals(Integer.valueOf(consequent.getKey()))) - .map(r -> new Assessment(r, consequent.getValue())) - .collect(Collectors.toList()); + List assessments = new ArrayList<>(); + for (Map.Entry consequent : consequents.entrySet()) { + for (DbRule dbRule : dbRules) { + if (dbRule.getId().equals(Integer.valueOf(consequent.getKey()))) { + assessments.add(new Assessment(dbRule, consequent.getValue())); + } + } + } + return assessments; } @@ -134,7 +139,7 @@ public class FuzzyInferenceService { } } - private Map.Entry getConsequent(Engine engine, Map variableValues) { + private Map getConsequent(Engine engine, Map variableValues) { OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME); for (Map.Entry variableValue : variableValues.entrySet()) { InputVariable inputVariable = engine.getInputVariable(variableValue.getKey()); @@ -144,8 +149,8 @@ public class FuzzyInferenceService { if (outputVariable != null) { LOG.info("Output: {}", outputVariable.getValue()); } - return (outputVariable == null || Double.isNaN(outputVariable.getValue())) - ? Map.entry(NO_RESULT, 0.0) - : Map.entry(outputVariable.highestMembershipTerm(outputVariable.getValue()).getName(), outputVariable.getValue()); + return Double.isNaN(outputVariable.getValue()) + ? Map.of(NO_RESULT, 0.0) + : outputVariable.fuzzyOutput().getTerms().stream().collect(Collectors.toMap(t -> t.getTerm().getName(), Activated::getDegree)); } } diff --git a/src/main/resources/templates/assessments.html b/src/main/resources/templates/assessments.html index f213f81..d88b271 100644 --- a/src/main/resources/templates/assessments.html +++ b/src/main/resources/templates/assessments.html @@ -46,22 +46,5 @@
Нет результатов
- -
- -
-
Состояние репозитория по лидирующему правилу описывается следующими выражениями:
-
- - вследствие тенденции '' показателя '' - и тенденции '' показателя ''; - -
-
-
-
Нет результатов
-