74-add-jFuzzyLogic #75

Merged
romanov73 merged 15 commits from 74-add-jFuzzyLogic into master 2022-11-28 01:00:07 +04:00
2 changed files with 28 additions and 21 deletions
Showing only changes of commit 81bcc89096 - Show all commits

View File

@ -11,12 +11,9 @@ import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
@ApiIgnore
class GlobalDefaultExceptionHandler {
@ -24,15 +21,15 @@ class GlobalDefaultExceptionHandler {
public static final String DEFAULT_ERROR_VIEW = "error";
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
LOG.warn(e.getMessage());
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName(DEFAULT_ERROR_VIEW);
return mav;
}
// @ExceptionHandler(value = Exception.class)
// public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
// LOG.warn(e.getMessage());
// ModelAndView mav = new ModelAndView();
// mav.addObject("exception", e);
// mav.addObject("url", req.getRequestURL());
// mav.setViewName(DEFAULT_ERROR_VIEW);
// return mav;
// }
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)

View File

@ -2,6 +2,9 @@ package ru.ulstu.extractor.rule.service;
import com.fuzzylite.Engine;
import com.fuzzylite.activation.Highest;
import com.fuzzylite.defuzzifier.Centroid;
import com.fuzzylite.norm.s.BoundedSum;
import com.fuzzylite.norm.s.Maximum;
import com.fuzzylite.norm.t.AlgebraicProduct;
import com.fuzzylite.rule.Rule;
import com.fuzzylite.rule.RuleBlock;
@ -52,10 +55,10 @@ public class FuzzyInferenceService {
input.setName(key);
input.setDescription("");
input.setEnabled(true);
input.setRange(0.000, antecedentValues.size() + 1);
input.setRange(-0.1, antecedentValues.size() + 1.1);
input.setLockValueInRange(false);
for (int i = 0; i < antecedentValues.size(); i++) {
input.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i, i + 2));
input.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i - 0.1, i + 2.1));
}
engine.addInputVariable(input);
});
@ -64,10 +67,13 @@ public class FuzzyInferenceService {
output.setName(OUTPUT_VARIABLE_NAME);
output.setDescription("");
output.setEnabled(true);
output.setRange(0.000, antecedentValues.size() + 1);
output.setRange(-0.1, antecedentValues.size() + 0.1);
output.setAggregation(new Maximum());
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, i + 2));
output.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i - 0.1, i + 2.1));
}
engine.addOutputVariable(output);
@ -75,8 +81,8 @@ public class FuzzyInferenceService {
mamdani.setName("mamdani");
mamdani.setDescription("");
mamdani.setEnabled(true);
mamdani.setConjunction(null);
mamdani.setDisjunction(null);
mamdani.setConjunction(new AlgebraicProduct());
mamdani.setDisjunction(new BoundedSum());
mamdani.setImplication(new AlgebraicProduct());
mamdani.setActivation(new Highest());
getRulesFromDb().forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
@ -94,8 +100,8 @@ public class FuzzyInferenceService {
Engine engine = getFuzzyEngine();
List<AntecedentValue> antecedentValues = antecedentValueService.getList();
Map<String, Double> variableValues = new HashMap<>();
variableValues.put("COMMITS", 10.0);
variableValues.put("AUTHORS", 10.0);
variableValues.put("COMMITS", 0.0);
variableValues.put("AUTHORS", 0.0);
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues));
return getConsequent(engine, variableValues);
}
@ -107,6 +113,10 @@ public class FuzzyInferenceService {
inputVariable.setValue(variableValue.getValue());
}
engine.process();
return outputVariable == null ? "" : outputVariable.fuzzyOutputValue();
if (outputVariable != null) {
outputVariable.defuzzify();
}
return outputVariable == null ? "" : outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
//return outputVariable == null ? "" : outputVariable.fuzzyOutputValue();
}
}