Merge pull request '74-add-jFuzzyLogic' (#75) from 74-add-jFuzzyLogic into master
Reviewed-on: #75pull/87/head
commit
bf5220acd0
@ -0,0 +1,29 @@
|
|||||||
|
package ru.ulstu.extractor.http;
|
||||||
|
|
||||||
|
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||||
|
|
||||||
|
public class SmoothingTimeSeries {
|
||||||
|
private JsonTimeSeries originalTimeSeries;
|
||||||
|
private String methodClassName;
|
||||||
|
|
||||||
|
public SmoothingTimeSeries(TimeSeries timeSeries) {
|
||||||
|
originalTimeSeries = new JsonTimeSeries(timeSeries);
|
||||||
|
this.methodClassName = "FTransform";
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonTimeSeries getOriginalTimeSeries() {
|
||||||
|
return originalTimeSeries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOriginalTimeSeries(JsonTimeSeries originalTimeSeries) {
|
||||||
|
this.originalTimeSeries = originalTimeSeries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMethodClassName() {
|
||||||
|
return methodClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMethodClassName(String methodClassName) {
|
||||||
|
this.methodClassName = methodClassName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package ru.ulstu.extractor.recommendation.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import ru.ulstu.extractor.branch.service.BranchService;
|
||||||
|
import ru.ulstu.extractor.recommendation.model.FilterBranchForm;
|
||||||
|
import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
|
||||||
|
import springfox.documentation.annotations.ApiIgnore;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static ru.ulstu.extractor.core.Route.RECOMMENDATIONS;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@ApiIgnore
|
||||||
|
public class RecommendationController {
|
||||||
|
private final FuzzyInferenceService fuzzyInferenceService;
|
||||||
|
private final BranchService branchService;
|
||||||
|
|
||||||
|
public RecommendationController(FuzzyInferenceService fuzzyInferenceService,
|
||||||
|
BranchService branchService) {
|
||||||
|
this.fuzzyInferenceService = fuzzyInferenceService;
|
||||||
|
this.branchService = branchService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(RECOMMENDATIONS)
|
||||||
|
public String getRecommendations(Model model, @RequestParam Optional<Integer> branchId) {
|
||||||
|
model.addAttribute("branches", branchService.findAll());
|
||||||
|
if (branchId.isPresent()) {
|
||||||
|
model.addAttribute("recommendations", fuzzyInferenceService.getRecommendations(branchId.get()));
|
||||||
|
model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get()));
|
||||||
|
} else {
|
||||||
|
model.addAttribute("filterBranchForm", new FilterBranchForm());
|
||||||
|
}
|
||||||
|
return RECOMMENDATIONS;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package ru.ulstu.extractor.recommendation.model;
|
||||||
|
|
||||||
|
public class FilterBranchForm {
|
||||||
|
private Integer branchId;
|
||||||
|
|
||||||
|
public FilterBranchForm() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilterBranchForm(Integer branchId) {
|
||||||
|
this.branchId = branchId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getBranchId() {
|
||||||
|
return branchId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBranchId(Integer branchId) {
|
||||||
|
this.branchId = branchId;
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package ru.ulstu.extractor.rule.repository;
|
package ru.ulstu.extractor.rule.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import ru.ulstu.extractor.rule.model.Rule;
|
import ru.ulstu.extractor.rule.model.DbRule;
|
||||||
|
|
||||||
public interface RuleRepository extends JpaRepository<Rule, Integer> {
|
public interface RuleRepository extends JpaRepository<DbRule, Integer> {
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,150 @@
|
|||||||
|
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;
|
||||||
|
import com.fuzzylite.term.Triangle;
|
||||||
|
import com.fuzzylite.variable.InputVariable;
|
||||||
|
import com.fuzzylite.variable.OutputVariable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
|
||||||
|
import ru.ulstu.extractor.rule.model.AntecedentValue;
|
||||||
|
import ru.ulstu.extractor.rule.model.DbRule;
|
||||||
|
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||||
|
import ru.ulstu.extractor.ts.service.TimeSeriesService;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FuzzyInferenceService {
|
||||||
|
private final static String OUTPUT_VARIABLE_NAME = "state";
|
||||||
|
private final static String RULE_TEMPLATE = "if %s is %s and %s is %s then "
|
||||||
|
+ OUTPUT_VARIABLE_NAME
|
||||||
|
+ " is %s";
|
||||||
|
private final DbRuleService ruleService;
|
||||||
|
private final AntecedentValueService antecedentValueService;
|
||||||
|
private final GitRepositoryService gitRepositoryService;
|
||||||
|
private final TimeSeriesService timeSeriesService;
|
||||||
|
|
||||||
|
public FuzzyInferenceService(DbRuleService ruleService,
|
||||||
|
AntecedentValueService antecedentValueService,
|
||||||
|
GitRepositoryService gitRepositoryService,
|
||||||
|
TimeSeriesService timeSeriesService) {
|
||||||
|
this.ruleService = ruleService;
|
||||||
|
this.antecedentValueService = antecedentValueService;
|
||||||
|
this.gitRepositoryService = gitRepositoryService;
|
||||||
|
this.timeSeriesService = timeSeriesService;
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFuzzyRule(DbRule dbRule) {
|
||||||
|
return String.format(RULE_TEMPLATE,
|
||||||
|
dbRule.getFirstAntecedent().name(),
|
||||||
|
dbRule.getFirstAntecedentValue().getAntecedentValue(),
|
||||||
|
dbRule.getSecondAntecedent().name(),
|
||||||
|
dbRule.getSecondAntecedentValue().getAntecedentValue(),
|
||||||
|
dbRule.getConsequent());
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
input.setDescription("");
|
||||||
|
input.setEnabled(true);
|
||||||
|
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 - 0.1, i + 2.1));
|
||||||
|
}
|
||||||
|
engine.addInputVariable(input);
|
||||||
|
});
|
||||||
|
|
||||||
|
OutputVariable output = new OutputVariable();
|
||||||
|
output.setName(OUTPUT_VARIABLE_NAME);
|
||||||
|
output.setDescription("");
|
||||||
|
output.setEnabled(true);
|
||||||
|
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 < consequentValues.size(); i++) {
|
||||||
|
output.addTerm(new Triangle(consequentValues.get(i), i - 0.1, i + 2.1));
|
||||||
|
}
|
||||||
|
engine.addOutputVariable(output);
|
||||||
|
|
||||||
|
RuleBlock mamdani = new RuleBlock();
|
||||||
|
mamdani.setName("mamdani");
|
||||||
|
mamdani.setDescription("");
|
||||||
|
mamdani.setEnabled(true);
|
||||||
|
mamdani.setConjunction(new AlgebraicProduct());
|
||||||
|
mamdani.setDisjunction(new BoundedSum());
|
||||||
|
mamdani.setImplication(new AlgebraicProduct());
|
||||||
|
mamdani.setActivation(new Highest());
|
||||||
|
getRulesFromDb(variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
||||||
|
return mamdani;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Engine getFuzzyEngine() {
|
||||||
|
Engine engine = new Engine();
|
||||||
|
engine.setName("Git rules");
|
||||||
|
engine.setDescription("");
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRecommendations(Integer branchId) {
|
||||||
|
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, 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()) {
|
||||||
|
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
|
||||||
|
inputVariable.setValue(variableValue.getValue());
|
||||||
|
}
|
||||||
|
engine.process();
|
||||||
|
if (outputVariable != null) {
|
||||||
|
outputVariable.defuzzify();
|
||||||
|
}
|
||||||
|
return (outputVariable == null || Double.isNaN(outputVariable.getValue()))
|
||||||
|
? "Нет рекомендаций"
|
||||||
|
: outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue