Compare commits

..

2 Commits

Author SHA1 Message Date
1dc44f7988 #91 -- Fix fuzzy inference 2023-04-21 15:47:27 +04:00
6823f34997 #91 -- Fix fuzzy sets 2023-04-21 15:00:14 +04:00
4 changed files with 20 additions and 79 deletions

View File

@ -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());

View File

@ -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<Assessment> getAssessments(Integer branchId) {
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
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<>();
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts)));
return fuzzyInferenceService.getFuzzyInference(dbRules,
antecedentValueService.getList(),
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.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;
@ -57,20 +58,11 @@ public class FuzzyInferenceService {
input.setName(key);
input.setDescription("");
input.setEnabled(true);
double delta = antecedentValues.size() > 1
? 2.0 / (antecedentValues.size() - 1)
: 2.0;
input.setRange(-1, 1);
input.setLockValueInRange(false);
for (int i = 0; i < antecedentValues.size(); i++) {
input.addTerm(
new Triangle(
antecedentValues.get(i).getAntecedentValue(),
-1 + i * delta - 0.5 * delta,
-1 + i * delta + delta + 0.5 * delta
)
);
}
input.addTerm(new Triangle("спад", -1, 0));
input.addTerm(new Triangle("стабильно", -0.1, 0.1));
input.addTerm(new Triangle("рост", 0, 1));
engine.addInputVariable(input);
});
@ -114,18 +106,23 @@ public class FuzzyInferenceService {
List<AntecedentValue> antecedentValues,
Map<String, Double> variableValues) {
validateVariables(variableValues, dbRules);
variableValues.entrySet().forEach(e -> System.out.println(e.getKey() + " " + e.getValue()));
Engine engine = getFuzzyEngine();
List<Integer> consequentValues = dbRules.stream().map(DbRule::getId).collect(Collectors.toList());
engine.addRuleBlock(getRuleBlock(engine, dbRules, variableValues, antecedentValues, consequentValues));
Map.Entry<String, Double> consequent = getConsequent(engine, variableValues);
if (consequent.getKey().equals(NO_RESULT)) {
Map<String, Double> 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<Assessment> assessments = new ArrayList<>();
for (Map.Entry<String, Double> consequent : consequents.entrySet()) {
for (DbRule dbRule : dbRules) {
if (dbRule.getId().equals(Integer.valueOf(consequent.getKey()))) {
assessments.add(new Assessment(dbRule, consequent.getValue()));
}
}
}
return assessments;
}
@ -142,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);
for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
@ -152,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));
}
}

View File

@ -46,22 +46,5 @@
<div th:if="${assessments != null && #lists.size(assessments) == 0}">
<h5>Нет результатов</h5>
</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>
</html>