home › Forums › # Technical Support › How to get single string output
- This topic has 2 replies, 2 voices, and was last updated 5 years, 8 months ago by
Unknown.
-
AuthorPosts
-
October 3, 2017 at 05:33 #2611
Unknown
Member/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testfuzzy;import com.fuzzylite.Engine;
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;/**
*
* @author chandana
*/
public class Fuzzy {public static void main(String[] args) {
fuzzyCheck(3.3, 1.3, 1.3);
}private static String fuzzyCheck(double eye_color, double wound_color, double tongue_color) {
Engine engine = new Engine();
engine.setName(“snake-detector”);InputVariable red = new InputVariable();
red.setName(“Eye_Color”);
red.setRange(0.5, 3.5);
red.addTerm(new Triangle(“one”, 2, 2.2, 2.4));
red.addTerm(new Triangle(“two”, 0.9, 1.1, 1.3));
red.addTerm(new Triangle(“three”, 0.7, 0.9, 1.1));
red.addTerm(new Triangle(“four”, 1.1, 1.3, 1.5));
red.addTerm(new Triangle(“five”, 0.5, 0.7, 0.9));
red.addTerm(new Triangle(“six”, 3.1, 3.3, 3.5));
engine.addInputVariable(red);InputVariable green = new InputVariable();
green.setName(“Toung_Color”);
green.setRange(0.5, 3.5);
green.addTerm(new Triangle(“one”, 2, 2.2, 2.4));
green.addTerm(new Triangle(“two”, 0.9, 1.1, 1.3));
green.addTerm(new Triangle(“three”, 0.7, 0.9, 1.1));
green.addTerm(new Triangle(“four”, 1.1, 1.3, 1.5));
green.addTerm(new Triangle(“five”, 0.5, 0.7, 0.9));
green.addTerm(new Triangle(“six”, 3.1, 3.3, 3.5));
engine.addInputVariable(green);InputVariable blue = new InputVariable();
blue.setName(“Wound_Color”);
blue.setRange(0.5, 3.5);
blue.addTerm(new Triangle(“one”, 2, 2.2, 2.4));
blue.addTerm(new Triangle(“two”, 0.9, 1.1, 1.3));
blue.addTerm(new Triangle(“three”, 0.7, 0.9, 1.1));
blue.addTerm(new Triangle(“four”, 1.1, 1.3, 1.5));
blue.addTerm(new Triangle(“five”, 0.5, 0.7, 0.9));
blue.addTerm(new Triangle(“six”, 3.1, 3.3, 3.5));
engine.addInputVariable(blue);OutputVariable snake = new OutputVariable();
snake.setName(“snake”);
snake.setRange(0, 12);
snake.setDefaultValue(Double.NaN);
snake.addTerm(new Triangle(“Cobra”, 0, 1, 2));
snake.addTerm(new Triangle(“Russell’s_viper”, 2, 3, 4));
snake.addTerm(new Triangle(“Krait”, 4, 5, 6));
snake.addTerm(new Triangle(“Sea_snake”, 6, 7, 8));
snake.addTerm(new Triangle(“Hump_nosed_viper”, 8, 9, 10));
snake.addTerm(new Triangle(“Saw_scaled_viper”, 10, 11, 12));
engine.addOutputVariable(snake);RuleBlock ruleBlock = new RuleBlock();
ruleBlock.addRule(Rule.parse(“if Eye_Color is one and Toung_Color is two and Wound_Color is one then snake is Cobra”, engine));
ruleBlock.addRule(Rule.parse(“if Eye_Color is two and Toung_Color is three and Wound_Color is one then snake is Russell’s_viper”, engine));
ruleBlock.addRule(Rule.parse(“if Eye_Color is one and Toung_Color is four and Wound_Color is one then snake is Krait”, engine));
ruleBlock.addRule(Rule.parse(“if Eye_Color is four and Toung_Color is one and Wound_Color is four then snake is Sea_snake”, engine));
ruleBlock.addRule(Rule.parse(“if Eye_Color is five and Toung_Color is one and Wound_Color is six then snake is Hump_nosed_viper”, engine));
ruleBlock.addRule(Rule.parse(“if Eye_Color is six and Toung_Color is four and Wound_Color is four then snake is Saw_scaled_viper”, engine));
engine.addRuleBlock(ruleBlock);engine.configure(“Minimum”, “Maximum”, “Minimum”, “Maximum”, “Centroid”, null);
engine.setInputValue(“Eye_Color”, eye_color);
engine.setInputValue(“Wound_Color”, wound_color);
engine.setInputValue(“Toung_Color”, tongue_color);
engine.process();
//System.out.print(new JavaExporter().toString(engine));
System.out.print(engine.getOutputVariable(“snake”).fuzzyOutput());
return String.format(“”, engine.getOutputVariable(“snake”).fuzzyOutput());
}
}out put:snake: Aggregated Maximum[Minimum(1.000,Saw_scaled_viper)]
I need get single value inside bracketSaw_scaled_viper
October 3, 2017 at 11:13 #2612Juan Rada-Vilela (admin)
KeymasterHi,
thanks for your post.
Maybe something like this could work:
Aggregated fuzzyOutput = engine.getOutputVariable(“snake”).fuzzyOutput(); for (Activated activated : fuzzyOutput.getTerms()){ System.out.println(String.format("%f %s", activated.getDegree(), activated.getTerm().getName())); }
If not, please check the source code of Aggregated and Activated terms under com.fuzzylite.term.
Cheers.
October 3, 2017 at 16:19 #2613Unknown
MemberThank you for great help. its work well.
-
AuthorPosts
- You must be logged in to reply to this topic.