home Forums # Technical Support FuzzyLite Use in Android Reply To: FuzzyLite Use in Android

#2614
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 mytest;

import com.fuzzylite.Engine;
import com.fuzzylite.rule.Rule;
import com.fuzzylite.rule.RuleBlock;
import com.fuzzylite.term.Activated;
import com.fuzzylite.term.Aggregated;
import com.fuzzylite.term.Triangle;
import com.fuzzylite.variable.InputVariable;
import com.fuzzylite.variable.OutputVariable;

/**
*
* @author pc
*/
public class MyTest {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
////2.2,1.1,2.2
//1.1,0.9,2.2
//2.2,1.3,2.2
//1.3,2.2,1.3
//0.7,2.2,3.3
//3.3,1.3,1.3

fuzzy(1.3,2.2,1.3);
}

public static String fuzzy(double eye_color,double tongue_color,double wound_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();
String snakeName = “”;
Aggregated fuzzyOutput = engine.getOutputVariable(“snake”).fuzzyOutput();
for (Activated activated : fuzzyOutput.getTerms()) {
System.out.println(String.format(“%f %s”, activated.getDegree(), activated.getTerm().getName()));
snakeName = String.format(“%s”, activated.getTerm().getName());
}
System.out.println(snakeName);
return snakeName;
}
}