From 81bcc89096117cb0e48bb9d1ad06cea3f0623ed0 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 25 Oct 2022 16:26:54 +0400 Subject: [PATCH] #74 -- recommendations without time series data --- .../config/GlobalDefaultExceptionHandler.java | 21 ++++++-------- .../rule/service/FuzzyInferenceService.java | 28 +++++++++++++------ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java b/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java index a5af0ec..62f2b4d 100644 --- a/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java +++ b/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java @@ -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) diff --git a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java index b5c394c..1a4fe35 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java @@ -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 antecedentValues = antecedentValueService.getList(); Map 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(); } }