#91 -- Fix fuzzy inference

This commit is contained in:
Anton Romanov 2023-04-21 15:47:27 +04:00
parent 6823f34997
commit 1dc44f7988
4 changed files with 16 additions and 67 deletions

View File

@ -30,7 +30,6 @@ public class AssessmentController {
model.addAttribute("branches", branchService.findAllValid()); model.addAttribute("branches", branchService.findAllValid());
if (branchId.isPresent()) { if (branchId.isPresent()) {
model.addAttribute("assessments", assessmentService.getAssessments(branchId.get())); model.addAttribute("assessments", assessmentService.getAssessments(branchId.get()));
model.addAttribute("singleAssessment", assessmentService.getSingleAssessment(branchId.get()));
model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get())); model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get()));
} else { } else {
model.addAttribute("filterBranchForm", new FilterBranchForm()); model.addAttribute("filterBranchForm", new FilterBranchForm());

View File

@ -2,7 +2,6 @@ package ru.ulstu.extractor.assessment.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.assessment.model.Assessment; 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.model.DbRule;
import ru.ulstu.extractor.rule.service.AntecedentValueService; import ru.ulstu.extractor.rule.service.AntecedentValueService;
import ru.ulstu.extractor.rule.service.DbRuleService; 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.model.TimeSeries;
import ru.ulstu.extractor.ts.service.TimeSeriesService; import ru.ulstu.extractor.ts.service.TimeSeriesService;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
@Service @Service
public class AssessmentService { public class AssessmentService {
@ -37,44 +33,10 @@ public class AssessmentService {
public List<Assessment> getAssessments(Integer branchId) { public List<Assessment> getAssessments(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId); List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
List<DbRule> dbRules = ruleService.getList(); List<DbRule> dbRules = ruleService.getList();
try {
return getAssessments(dbRules, timeSeries);
} catch (AssessmentException ex) {
ex.printStackTrace();
return new ArrayList<>();
}
}
public List<Assessment> getSingleAssessment(Integer branchId) throws AssessmentException {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
List<DbRule> dbRules = ruleService.getList();
return getSingleAssessment(dbRules, timeSeries);
}
private List<Assessment> getSingleAssessment(List<DbRule> dbRules, List<TimeSeries> timeSeries) throws AssessmentException {
Map<String, Double> variableValues = new HashMap<>(); Map<String, Double> variableValues = new HashMap<>();
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts))); timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts)));
return fuzzyInferenceService.getFuzzyInference(dbRules, return fuzzyInferenceService.getFuzzyInference(dbRules,
antecedentValueService.getList(), antecedentValueService.getList(),
variableValues); variableValues);
} }
private List<Assessment> getAssessments(List<DbRule> dbRules, List<TimeSeries> timeSeries) {
return dbRules
.stream()
.flatMap(dbRule -> {
Map<String, Double> 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());
}
} }

View File

@ -8,6 +8,7 @@ import com.fuzzylite.norm.t.AlgebraicProduct;
import com.fuzzylite.norm.t.Minimum; import com.fuzzylite.norm.t.Minimum;
import com.fuzzylite.rule.Rule; import com.fuzzylite.rule.Rule;
import com.fuzzylite.rule.RuleBlock; import com.fuzzylite.rule.RuleBlock;
import com.fuzzylite.term.Activated;
import com.fuzzylite.term.Triangle; import com.fuzzylite.term.Triangle;
import com.fuzzylite.variable.InputVariable; import com.fuzzylite.variable.InputVariable;
import com.fuzzylite.variable.OutputVariable; import com.fuzzylite.variable.OutputVariable;
@ -109,15 +110,19 @@ public class FuzzyInferenceService {
Engine engine = getFuzzyEngine(); Engine engine = getFuzzyEngine();
List<Integer> consequentValues = dbRules.stream().map(DbRule::getId).collect(Collectors.toList()); List<Integer> consequentValues = dbRules.stream().map(DbRule::getId).collect(Collectors.toList());
engine.addRuleBlock(getRuleBlock(engine, dbRules, variableValues, antecedentValues, consequentValues)); engine.addRuleBlock(getRuleBlock(engine, dbRules, variableValues, antecedentValues, consequentValues));
Map.Entry<String, Double> consequent = getConsequent(engine, variableValues); Map<String, Double> consequents = getConsequent(engine, variableValues);
if (consequent.getKey().equals(NO_RESULT)) { if (consequents.containsKey(NO_RESULT)) {
return new ArrayList<>(); return new ArrayList<>();
} }
return dbRules List<Assessment> assessments = new ArrayList<>();
.stream() for (Map.Entry<String, Double> consequent : consequents.entrySet()) {
.filter(r -> r.getId().equals(Integer.valueOf(consequent.getKey()))) for (DbRule dbRule : dbRules) {
.map(r -> new Assessment(r, consequent.getValue())) if (dbRule.getId().equals(Integer.valueOf(consequent.getKey()))) {
.collect(Collectors.toList()); assessments.add(new Assessment(dbRule, consequent.getValue()));
}
}
}
return assessments;
} }
@ -134,7 +139,7 @@ public class FuzzyInferenceService {
} }
} }
private Map.Entry<String, Double> getConsequent(Engine engine, Map<String, Double> variableValues) { private Map<String, Double> getConsequent(Engine engine, Map<String, Double> variableValues) {
OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME); OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME);
for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) { for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey()); InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
@ -144,8 +149,8 @@ public class FuzzyInferenceService {
if (outputVariable != null) { if (outputVariable != null) {
LOG.info("Output: {}", outputVariable.getValue()); LOG.info("Output: {}", outputVariable.getValue());
} }
return (outputVariable == null || Double.isNaN(outputVariable.getValue())) return Double.isNaN(outputVariable.getValue())
? Map.entry(NO_RESULT, 0.0) ? Map.of(NO_RESULT, 0.0)
: Map.entry(outputVariable.highestMembershipTerm(outputVariable.getValue()).getName(), outputVariable.getValue()); : outputVariable.fuzzyOutput().getTerms().stream().collect(Collectors.toMap(t -> t.getTerm().getName(), Activated::getDegree));
} }
} }

View File

@ -46,22 +46,5 @@
<div th:if="${assessments != null && #lists.size(assessments) == 0}"> <div th:if="${assessments != null && #lists.size(assessments) == 0}">
<h5>Нет результатов</h5> <h5>Нет результатов</h5>
</div> </div>
<hr/>
<div th:if="${singleAssessment != null && #lists.size(assessments) > 0}">
<h5>Состояние репозитория по лидирующему правилу описывается следующими выражениями:</h5>
<div th:each="assessment: ${singleAssessment}">
<span th:text="${assessment.consequent}"></span>
вследствие тенденции '<span th:text="${assessment.firstAntecedentTendency}"></span>' показателя '<span
th:text="${assessment.firstAntecedent.description}"></span>'
и тенденции '<span th:text="${assessment.secondAntecedentTendency}"></span>' показателя '<span
th:text="${assessment.secondAntecedent.description}"></span>';
<span class="badge badge-warning" th:text="${assessment.degree}"></span>
</div>
</div>
<div th:if="${singleAssessment != null && #lists.size(singleAssessment) == 0}">
<h5>Нет результатов</h5>
</div>
</div> </div>
</html> </html>