#31 -- Add horizon for inference
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 2m13s

This commit is contained in:
Anton Romanov 2025-03-20 14:47:30 +04:00
parent 784bc57aa5
commit de63be3014
2 changed files with 42 additions and 13 deletions

View File

@ -3,6 +3,7 @@ package ru.ulstu.fc.rule.model.dto;
public class FuzzyRuleDataDto {
private String[] fuzzyTerms;
private int window = 3;
private int horizon = 1;
public String[] getFuzzyTerms() {
return fuzzyTerms;
@ -19,4 +20,12 @@ public class FuzzyRuleDataDto {
public void setWindow(int window) {
this.window = window;
}
public int getHorizon() {
return horizon;
}
public void setHorizon(int horizon) {
this.horizon = horizon;
}
}

View File

@ -24,6 +24,7 @@ import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.dto.FuzzyRuleDataDto;
import ru.ulstu.fc.rule.model.dto.VariableValueDto;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
@ -216,19 +217,38 @@ public class FuzzyInferenceService {
ProjectDto projectDto = projectService.save(new ProjectDto(new Date().toString()));
fuzzyRuleParseService.generateRules(projectDto.getId(), fuzzyRuleDataDto);
Map<String, Double> variableValues = new HashMap<>();
List<String> labels = new LinkedList<>(Arrays.asList(fuzzyRuleDataDto.getFuzzyTerms()));
while (labels.size() > fuzzyRuleDataDto.getWindow()) {
labels.removeFirst();
List<OutputValue> result = new ArrayList<>();
for (int h = 0; h < fuzzyRuleDataDto.getHorizon(); h++) {
List<String> labels = new LinkedList<>(Arrays.asList(fuzzyRuleDataDto.getFuzzyTerms()));
labels.addAll(result.stream().map(OutputValue::getFuzzyTerm).toList());
while (labels.size() > fuzzyRuleDataDto.getWindow()) {
labels.removeFirst();
}
for (int i = 0; i < labels.size(); i++) {
String label = labels.get(i);
variableValues.put("fuzzyLevel" + i,
variableService.getByProjectAndName(projectDto.getId(), "fuzzyLevel" + i)
.getFuzzyTerms()
.stream()
.filter(ft -> ft.getDescription().equals(label))
.mapToDouble(FuzzyTerm::getCrispValue).findAny().getAsDouble());
}
List<OutputValue> onePointResult = getProjectFuzzyInference(projectDto.getId(), variableValues);
if (onePointResult.isEmpty()) {
onePointResult = new ArrayList<>();
if (result.isEmpty()) {
onePointResult.add(
new OutputValue("someVar",
fuzzyRuleDataDto.getFuzzyTerms()[fuzzyRuleDataDto.getFuzzyTerms().length - 1],
1.0));
} else {
onePointResult.add(result.getLast());
}
}
result.add(onePointResult.getFirst());
}
for (int i = 0; i < labels.size(); i++) {
String label = labels.get(i);
variableValues.put("fuzzyLevel" + i,
variableService.getByProjectAndName(projectDto.getId(), "fuzzyLevel" + i)
.getFuzzyTerms()
.stream()
.filter(ft -> ft.getDescription().equals(label))
.mapToDouble(FuzzyTerm::getCrispValue).findAny().getAsDouble());
}
return getProjectFuzzyInference(projectDto.getId(), variableValues);
return result;
}
}