#2 -- Fix inference
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 2m15s

This commit is contained in:
Anton Romanov 2025-03-03 22:16:44 +04:00
parent 76ece94028
commit 67dc9d275d
2 changed files with 22 additions and 11 deletions

View File

@ -1,10 +1,12 @@
package ru.ulstu.fc.rule.model; package ru.ulstu.fc.rule.model;
public class OutputValue { public class OutputValue {
private String variable;
private String fuzzyTerm; private String fuzzyTerm;
private Double degree; private Double degree;
public OutputValue(String fuzzyTerm, Double degree) { public OutputValue(String variable, String fuzzyTerm, Double degree) {
this.variable = variable;
this.fuzzyTerm = fuzzyTerm; this.fuzzyTerm = fuzzyTerm;
this.degree = degree; this.degree = degree;
} }
@ -24,4 +26,12 @@ public class OutputValue {
public void setDegree(Double degree) { public void setDegree(Double degree) {
this.degree = degree; this.degree = degree;
} }
public String getVariable() {
return variable;
}
public void setVariable(String variable) {
this.variable = variable;
}
} }

View File

@ -24,6 +24,7 @@ import ru.ulstu.fc.rule.model.ProjectInferenceData;
import ru.ulstu.fc.rule.model.Variable; import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.dto.VariableValueDto; import ru.ulstu.fc.rule.model.dto.VariableValueDto;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -62,6 +63,7 @@ public class FuzzyInferenceService {
final InputVariable input = new InputVariable(); final InputVariable input = new InputVariable();
input.setName(variable.getName()); input.setName(variable.getName());
input.setDescription(""); input.setDescription("");
variable.getFuzzyTerms().sort(Comparator.comparing(FuzzyTerm::getCrispValue));
input.setRange(0, variable.getFuzzyTerms().get(variable.getFuzzyTerms().size() - 1).getCrispValue()); input.setRange(0, variable.getFuzzyTerms().get(variable.getFuzzyTerms().size() - 1).getCrispValue());
input.setEnabled(true); input.setEnabled(true);
input.setLockValueInRange(false); input.setLockValueInRange(false);
@ -120,22 +122,21 @@ public class FuzzyInferenceService {
} }
private List<OutputValue> getConsequent(Engine engine, Map<String, Double> variableValues) { private List<OutputValue> getConsequent(Engine engine, Map<String, Double> variableValues) {
OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME); List<OutputVariable> outputVariables = engine.getOutputVariables();
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());
inputVariable.setValue(variableValue.getValue()); inputVariable.setValue(variableValue.getValue());
} }
engine.process(); engine.process();
if (outputVariable != null) {
LOG.info("Output: {}", outputVariable.getValue()); return outputVariables
}
return Double.isNaN(outputVariable.getValue())
? List.of(new OutputValue(NO_RESULT, 0.0))
: outputVariable.fuzzyOutput()
.getTerms()
.stream() .stream()
.map(t -> new OutputValue(t.getTerm().getName(), t.getDegree())) .filter(v -> !Double.isNaN(v.getValue()))
.collect(Collectors.toList()); .map(OutputVariable::fuzzyOutput)
.map(a -> new OutputValue(a.getName(),
a.getTerms().getFirst().getTerm().getName(),
a.getTerms().getFirst().getDegree()))
.toList();
} }
public List<OutputValue> getFuzzyInference(Map<String, Double> vals) { public List<OutputValue> getFuzzyInference(Map<String, Double> vals) {