|
|
|
@ -44,8 +44,9 @@ public class FuzzyInferenceService {
|
|
|
|
|
this.timeSeriesService = timeSeriesService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<String> getRulesFromDb() {
|
|
|
|
|
public List<String> getRulesFromDb(Map<String, Double> variableValues) {
|
|
|
|
|
List<DbRule> dbDbRules = ruleService.getList();
|
|
|
|
|
validateVariables(variableValues, dbDbRules);
|
|
|
|
|
return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -58,7 +59,10 @@ public class FuzzyInferenceService {
|
|
|
|
|
dbRule.getConsequent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RuleBlock getRuleBlock(Engine engine, Map<String, Double> variableValues, List<AntecedentValue> antecedentValues) {
|
|
|
|
|
private RuleBlock getRuleBlock(Engine engine,
|
|
|
|
|
Map<String, Double> variableValues,
|
|
|
|
|
List<AntecedentValue> antecedentValues,
|
|
|
|
|
List<String> consequentValues) {
|
|
|
|
|
variableValues.forEach((key, value) -> {
|
|
|
|
|
InputVariable input = new InputVariable();
|
|
|
|
|
input.setName(key);
|
|
|
|
@ -81,8 +85,8 @@ public class FuzzyInferenceService {
|
|
|
|
|
output.setDefuzzifier(new Centroid(100));
|
|
|
|
|
output.setDefaultValue(Double.NaN);
|
|
|
|
|
output.setLockValueInRange(false);
|
|
|
|
|
for (int i = 0; i < antecedentValues.size(); i++) {
|
|
|
|
|
output.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i - 0.1, i + 2.1));
|
|
|
|
|
for (int i = 0; i < consequentValues.size(); i++) {
|
|
|
|
|
output.addTerm(new Triangle(consequentValues.get(i), i - 0.1, i + 2.1));
|
|
|
|
|
}
|
|
|
|
|
engine.addOutputVariable(output);
|
|
|
|
|
|
|
|
|
@ -94,7 +98,7 @@ public class FuzzyInferenceService {
|
|
|
|
|
mamdani.setDisjunction(new BoundedSum());
|
|
|
|
|
mamdani.setImplication(new AlgebraicProduct());
|
|
|
|
|
mamdani.setActivation(new Highest());
|
|
|
|
|
getRulesFromDb().forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
|
|
|
|
getRulesFromDb(variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
|
|
|
|
return mamdani;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -109,12 +113,26 @@ public class FuzzyInferenceService {
|
|
|
|
|
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
|
|
|
|
|
Engine engine = getFuzzyEngine();
|
|
|
|
|
List<AntecedentValue> antecedentValues = antecedentValueService.getList();
|
|
|
|
|
List<String> consequentValues = ruleService.getConsequentList();
|
|
|
|
|
Map<String, Double> variableValues = new HashMap<>();
|
|
|
|
|
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts)));
|
|
|
|
|
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues));
|
|
|
|
|
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues));
|
|
|
|
|
return getConsequent(engine, variableValues);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateVariables(Map<String, Double> variableValues, List<DbRule> dbDbRules) {
|
|
|
|
|
for (DbRule dbRule : dbDbRules) {
|
|
|
|
|
if (!variableValues.containsKey(dbRule.getFirstAntecedent().name())) {
|
|
|
|
|
throw new RuntimeException(String.format("Переменной в правиле не задано значение (нет временного ряда): %s ",
|
|
|
|
|
dbRule.getFirstAntecedent().name()));
|
|
|
|
|
}
|
|
|
|
|
if (!variableValues.containsKey(dbRule.getSecondAntecedent().name())) {
|
|
|
|
|
throw new RuntimeException(String.format("Переменной в правиле не задано значение (нет временного ряда): %s ",
|
|
|
|
|
dbRule.getSecondAntecedent().name()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getConsequent(Engine engine, Map<String, Double> variableValues) {
|
|
|
|
|
OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME);
|
|
|
|
|
for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
|
|
|
|
|