Merge remote-tracking branch 'origin/74-add-jFuzzyLogic' into 74-add-jFuzzyLogic
# Conflicts: # src/main/java/ru/ulstu/extractor/GitExtractorApplication.java # src/main/java/ru/ulstu/extractor/rule/service/FuzzyInferenceService.java
This commit is contained in:
commit
f043e45b49
@ -20,69 +20,6 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
|||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
public class GitExtractorApplication {
|
public class GitExtractorApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Engine engine = new Engine();
|
|
||||||
engine.setName("Git rules");
|
|
||||||
engine.setDescription("");
|
|
||||||
|
|
||||||
InputVariable obstacle = new InputVariable();
|
|
||||||
obstacle.setName("obstacle");
|
|
||||||
obstacle.setDescription("");
|
|
||||||
obstacle.setEnabled(true);
|
|
||||||
obstacle.setRange(0.000, 1.000);
|
|
||||||
obstacle.setLockValueInRange(false);
|
|
||||||
obstacle.addTerm(new Trapezoid("left", 0.000, 0.700));
|
|
||||||
obstacle.addTerm(new Trapezoid("right", 0.400, 1.000));
|
|
||||||
engine.addInputVariable(obstacle);
|
|
||||||
|
|
||||||
OutputVariable mSteer = new OutputVariable();
|
|
||||||
mSteer.setName("mSteer");
|
|
||||||
mSteer.setDescription("");
|
|
||||||
mSteer.setEnabled(true);
|
|
||||||
mSteer.setRange(0.000, 1.000);
|
|
||||||
mSteer.setLockValueInRange(false);
|
|
||||||
mSteer.setAggregation(new Maximum());
|
|
||||||
mSteer.setDefuzzifier(new Centroid(100));
|
|
||||||
mSteer.setDefaultValue(Double.NaN);
|
|
||||||
mSteer.setLockPreviousValue(false);
|
|
||||||
mSteer.addTerm(new Trapezoid("left", 0.000, 0.700));
|
|
||||||
mSteer.addTerm(new Trapezoid("right", 0.400, 1.000));
|
|
||||||
engine.addOutputVariable(mSteer);
|
|
||||||
|
|
||||||
RuleBlock mamdani = new RuleBlock();
|
|
||||||
mamdani.setName("mamdani");
|
|
||||||
mamdani.setDescription("");
|
|
||||||
mamdani.setEnabled(true);
|
|
||||||
mamdani.setConjunction(null);
|
|
||||||
mamdani.setDisjunction(null);
|
|
||||||
mamdani.setImplication(new AlgebraicProduct());
|
|
||||||
mamdani.setActivation(new Highest());
|
|
||||||
mamdani.addRule(Rule.parse("if obstacle is left then mSteer is right", engine));
|
|
||||||
mamdani.addRule(Rule.parse("if obstacle is right then mSteer is left", engine));
|
|
||||||
engine.addRuleBlock(mamdani);
|
|
||||||
|
|
||||||
|
|
||||||
StringBuilder status = new StringBuilder();
|
|
||||||
if (!engine.isReady(status))
|
|
||||||
throw new RuntimeException("[engine error] engine is not ready:n" + status);
|
|
||||||
|
|
||||||
InputVariable obstacle1 = engine.getInputVariable("obstacle");
|
|
||||||
OutputVariable steer = engine.getOutputVariable("mSteer");
|
|
||||||
|
|
||||||
for (int i = 10; i <= 50; ++i) {
|
|
||||||
double location = obstacle1.getMinimum() + i * (obstacle1.range() / 50);
|
|
||||||
obstacle1.setValue(location);
|
|
||||||
engine.process();
|
|
||||||
if (Double.isNaN(steer.getValue())) {
|
|
||||||
System.out.println("no decision");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
FuzzyLite.logger().info(String.format(
|
|
||||||
"obstacle.input = %s -> steer.output = %s",
|
|
||||||
Op.str(location), Op.str(steer.getValue())));
|
|
||||||
FuzzyLite.logger().info(String.format(
|
|
||||||
"obstacle.input = %s -> steer.output = %s",
|
|
||||||
Op.str(location), steer.highestMembership(steer.getValue()).getSecond().getName()));
|
|
||||||
}
|
|
||||||
SpringApplication.run(GitExtractorApplication.class, args);
|
SpringApplication.run(GitExtractorApplication.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,27 @@
|
|||||||
package ru.ulstu.extractor.rule.service;
|
package ru.ulstu.extractor.rule.service;
|
||||||
|
|
||||||
import com.fuzzylite.Engine;
|
import net.sourceforge.jFuzzyLogic.defuzzifier.DefuzzifierCenterOfGravity;
|
||||||
import com.fuzzylite.activation.Highest;
|
import net.sourceforge.jFuzzyLogic.membership.MembershipFunctionTriangular;
|
||||||
import com.fuzzylite.norm.t.AlgebraicProduct;
|
import net.sourceforge.jFuzzyLogic.rule.FuzzyRule;
|
||||||
import com.fuzzylite.rule.Rule;
|
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleExpression;
|
||||||
import com.fuzzylite.rule.RuleBlock;
|
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleSet;
|
||||||
import com.fuzzylite.term.Triangle;
|
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleTerm;
|
||||||
import com.fuzzylite.variable.InputVariable;
|
import net.sourceforge.jFuzzyLogic.rule.LinguisticTerm;
|
||||||
import com.fuzzylite.variable.OutputVariable;
|
import net.sourceforge.jFuzzyLogic.rule.Variable;
|
||||||
|
import net.sourceforge.jFuzzyLogic.ruleConnection.RuleConnectionMethodAndMin;
|
||||||
|
import net.sourceforge.jFuzzyLogic.ruleImplication.RuleImplicationMethodMin;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.ulstu.extractor.rule.model.AntecedentValue;
|
import ru.ulstu.extractor.rule.model.AntecedentValue;
|
||||||
import ru.ulstu.extractor.rule.model.DbRule;
|
import ru.ulstu.extractor.rule.model.Rule;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class FuzzyInferenceService {
|
public class FuzzyInferenceService {
|
||||||
private final static String RULE_TEMPLATE = "if %s is %s AND %s is %s then state is %s";
|
|
||||||
private final RuleService ruleService;
|
private final RuleService ruleService;
|
||||||
private final AntecedentValueService antecedentValueService;
|
private final AntecedentValueService antecedentValueService;
|
||||||
|
|
||||||
@ -29,69 +31,118 @@ public class FuzzyInferenceService {
|
|||||||
this.antecedentValueService = antecedentValueService;
|
this.antecedentValueService = antecedentValueService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getRulesFromDb() {
|
private List<FuzzyRule> getFuzzyRulesFromDb() {
|
||||||
List<DbRule> dbDbRules = ruleService.getList();
|
List<FuzzyRule> fuzzyRules = new ArrayList<>();
|
||||||
return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
|
//List<Variable> variables = getFuzzyVariables();
|
||||||
|
for (Rule dbRule : ruleService.getList()) {
|
||||||
|
FuzzyRule fuzzyRule = new FuzzyRule(String.format("Fuzzy rule %s", dbRule.getId()));
|
||||||
|
// fuzzyRule.setAntecedents(expression);
|
||||||
|
// fuzzyRule.setConsequents(new LinkedList<>(Collections.singleton(new FuzzyRuleTerm(dbRule.getConsequent(), false))));
|
||||||
|
fuzzyRules.add(fuzzyRule);
|
||||||
|
}
|
||||||
|
return fuzzyRules;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFuzzyRule(DbRule dbRule) {
|
private List<Variable> getFuzzyVariablesWithoutMembership() {
|
||||||
return String.format(RULE_TEMPLATE,
|
List<Variable> variables = new ArrayList<>();
|
||||||
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) {
|
|
||||||
variableValues.forEach((key, value) -> {
|
|
||||||
InputVariable input = new InputVariable();
|
|
||||||
input.setName(key);
|
|
||||||
input.setDescription("");
|
|
||||||
input.setEnabled(true);
|
|
||||||
input.setRange(0.000, 1.000);
|
|
||||||
input.setLockValueInRange(false);
|
|
||||||
for (int i = 0; i < antecedentValues.size(); i++) {
|
|
||||||
input.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i, i + 2));
|
|
||||||
}
|
|
||||||
engine.addInputVariable(input);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
RuleBlock mamdani = new RuleBlock();
|
|
||||||
mamdani.setName("mamdani");
|
|
||||||
mamdani.setDescription("");
|
|
||||||
mamdani.setEnabled(true);
|
|
||||||
mamdani.setConjunction(null);
|
|
||||||
mamdani.setDisjunction(null);
|
|
||||||
mamdani.setImplication(new AlgebraicProduct());
|
|
||||||
mamdani.setActivation(new Highest());
|
|
||||||
getRulesFromDb().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 run() {
|
|
||||||
Engine engine = getFuzzyEngine();
|
|
||||||
List<AntecedentValue> antecedentValues = antecedentValueService.getList();
|
List<AntecedentValue> antecedentValues = antecedentValueService.getList();
|
||||||
Map<String, Double> variableValues = new HashMap<>();
|
for (AntecedentValue antecedentValue : antecedentValues) {
|
||||||
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues));
|
variables.add(new Variable(antecedentValue.getAntecedentValue()));
|
||||||
return getConsequent(engine, variableValues);
|
}
|
||||||
|
return variables;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getConsequent(Engine engine, Map<String, Double> variableValues) {
|
// private FuzzyRuleExpression getFuzzyRulesAntecedents(TimeSeriesType timeSeriesType1, TimeSeriesType timeSeriesType2) {
|
||||||
OutputVariable outputVariable = engine.getOutputVariable("state");
|
// return new FuzzyRuleExpression(getFuzzyRuleTerm(), getFuzzyRuleTerm(), new RuleConnectionMethodAndMin());
|
||||||
for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
|
// }
|
||||||
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
|
|
||||||
inputVariable.setValue(variableValue.getValue());
|
private FuzzyRuleExpression getFuzzyRuleExpression(FuzzyRuleTerm term1, FuzzyRuleTerm term2) {
|
||||||
|
return new FuzzyRuleExpression(term1, term2, new RuleConnectionMethodAndMin());
|
||||||
}
|
}
|
||||||
engine.process();
|
|
||||||
return outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
|
private FuzzyRuleTerm getFuzzyRuleTerm(Variable variable, String term) {
|
||||||
|
return new FuzzyRuleTerm(variable, term, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
FuzzyRule fuzzyRule1 = new FuzzyRule("rule 1");
|
||||||
|
FuzzyRule fuzzyRule2 = new FuzzyRule("rule 2");
|
||||||
|
FuzzyRule fuzzyRule3 = new FuzzyRule("rule 3");
|
||||||
|
|
||||||
|
Variable weather = new Variable("Погода");
|
||||||
|
weather.getLinguisticTerms().put("солнечно",
|
||||||
|
new LinguisticTerm("солнечно", new MembershipFunctionTriangular(0, 20, 30)));
|
||||||
|
weather.getLinguisticTerms().put("мороз",
|
||||||
|
new LinguisticTerm("мороз", new MembershipFunctionTriangular(-50, -10, 10)));
|
||||||
|
weather.setDefuzzifier(new DefuzzifierCenterOfGravity(weather));
|
||||||
|
|
||||||
|
|
||||||
|
Variable suit = new Variable("Одежда");
|
||||||
|
suit.getLinguisticTerms().put("легко одет",
|
||||||
|
new LinguisticTerm("легко одет", new MembershipFunctionTriangular(0, 5, 10)));
|
||||||
|
suit.getLinguisticTerms().put("тепло одет",
|
||||||
|
new LinguisticTerm("тепло одет", new MembershipFunctionTriangular(5, 10, 20)));
|
||||||
|
suit.setDefuzzifier(new DefuzzifierCenterOfGravity(suit));
|
||||||
|
|
||||||
|
Variable feel = new Variable("Ощущение");
|
||||||
|
feel.getLinguisticTerms().put("Холодно",
|
||||||
|
new LinguisticTerm("Холодно", new MembershipFunctionTriangular(0, 5, 10)));
|
||||||
|
feel.getLinguisticTerms().put("Жарко",
|
||||||
|
new LinguisticTerm("Жарко", new MembershipFunctionTriangular(5, 10, 20)));
|
||||||
|
feel.setDefuzzifier(new DefuzzifierCenterOfGravity(feel));
|
||||||
|
|
||||||
|
FuzzyRuleTerm weatherTerm1 = new FuzzyRuleTerm(weather, "солнечно", false);
|
||||||
|
FuzzyRuleTerm weatherTerm2 = new FuzzyRuleTerm(weather, "мороз", false);
|
||||||
|
|
||||||
|
FuzzyRuleTerm suitTerm1 = new FuzzyRuleTerm(suit, "легко одет", false);
|
||||||
|
FuzzyRuleTerm suitTerm2 = new FuzzyRuleTerm(suit, "тепло одет", false);
|
||||||
|
|
||||||
|
FuzzyRuleTerm feelCold = new FuzzyRuleTerm(feel, "Холодно", false);
|
||||||
|
FuzzyRuleTerm feelWarm = new FuzzyRuleTerm(feel, "Жарко", false);
|
||||||
|
|
||||||
|
FuzzyRuleExpression expression1 = new FuzzyRuleExpression(weatherTerm1, suitTerm2, new RuleConnectionMethodAndMin());
|
||||||
|
fuzzyRule1.setAntecedents(expression1);
|
||||||
|
fuzzyRule1.setConsequents(new LinkedList<>(Collections.singleton(feelWarm)));
|
||||||
|
|
||||||
|
FuzzyRuleExpression expression2 = new FuzzyRuleExpression(weatherTerm2, suitTerm1, new RuleConnectionMethodAndMin());
|
||||||
|
fuzzyRule2.setAntecedents(expression2);
|
||||||
|
fuzzyRule2.setConsequents(new LinkedList<>(Collections.singleton(feelCold)));
|
||||||
|
|
||||||
|
FuzzyRuleExpression expression3 = new FuzzyRuleExpression(weatherTerm1, suitTerm1, new RuleConnectionMethodAndMin());
|
||||||
|
fuzzyRule3.setAntecedents(expression3);
|
||||||
|
fuzzyRule3.setConsequents(new LinkedList<>(Collections.singleton(feelCold)));
|
||||||
|
|
||||||
|
fuzzyRule1.evaluate(new RuleImplicationMethodMin());
|
||||||
|
fuzzyRule2.evaluate(new RuleImplicationMethodMin());
|
||||||
|
//fuzzyRule3.evaluate(new RuleImplicationMethodMin());
|
||||||
|
|
||||||
|
FuzzyRuleSet set = new FuzzyRuleSet();
|
||||||
|
set.add(fuzzyRule1);
|
||||||
|
set.add(fuzzyRule2);
|
||||||
|
set.add(fuzzyRule3);
|
||||||
|
set.evaluate();
|
||||||
|
|
||||||
|
set.setVariable("Погода", 25);
|
||||||
|
set.setVariable("Одежда", 7);
|
||||||
|
// Evaluate fuzzy set
|
||||||
|
set.evaluate();
|
||||||
|
|
||||||
|
// Show output variable's chart
|
||||||
|
//set.getVariable("Ощущение").chartDefuzzifier(true);
|
||||||
|
System.out.println(set.getVariable("Ощущение").getLatestDefuzzifiedValue());
|
||||||
|
System.out.println(set);
|
||||||
|
System.out.println(set.getVariable("Ощущение"));
|
||||||
|
System.out.println(
|
||||||
|
feel.getLinguisticTerms()
|
||||||
|
.entrySet()
|
||||||
|
.stream()
|
||||||
|
.max(Comparator.comparing(e -> e.getValue()
|
||||||
|
.getMembershipFunction()
|
||||||
|
.membership(set.getVariable("Ощущение").getLatestDefuzzifiedValue())))
|
||||||
|
.get()
|
||||||
|
.getValue().getTermName()
|
||||||
|
);
|
||||||
|
set.getVariable("Ощущение").chartDefuzzifier(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user