home Forums # Technical Support JAVA API Reply To: JAVA API

#1186
Unknown
Member

Hello juan

I want to thank you first for your valuable job.

I’m trying to use fuzzylite for android application but unfortunately i get nan answer with all the inputs.

I’ve tried all your previous recommendations and I also compiled and executed my code but the same result again (nan).

I also kept my code structure with samples in package but it worked fine. I am really confused now.
Any help would be appreciated. Here is my code.



import com.fuzzylite.*;
import com.fuzzylite.defuzzifier.*;
import com.fuzzylite.factory.*;
import com.fuzzylite.hedge.*;
import com.fuzzylite.imex.*;
import com.fuzzylite.norm.*;
import com.fuzzylite.norm.s.*;
import com.fuzzylite.norm.t.*;
import com.fuzzylite.rule.*;
import com.fuzzylite.term.*;
import com.fuzzylite.variable.*;

		public class test {
			
			public static void main(String[] args) {
				
			Engine engine = new Engine();
			engine.setName("Untitled8");
			
			InputVariable inputVariable1 = new InputVariable();
			inputVariable1.setEnabled(true);
			inputVariable1.setName("Gender");
			inputVariable1.setRange(0.000, 1.000);
			inputVariable1.addTerm(new Triangle("Male", 0.000, 0.000, 0.000));
			inputVariable1.addTerm(new Triangle("Female", 1.000, 1.000, 1.000));
			engine.addInputVariable(inputVariable1);
		
			InputVariable inputVariable2 = new InputVariable();
			inputVariable2.setEnabled(true);
			inputVariable2.setName("Age");
			inputVariable2.setRange(2.000, 20.000);
			inputVariable2.addTerm(new Trapezoid("25", 2.000, 2.000, 5.000, 5.000));
			inputVariable2.addTerm(new Trapezoid("58", 5.000, 5.000, 8.000, 8.000));
			inputVariable2.addTerm(new Trapezoid("89", 8.000, 8.000, 9.000, 9.000));
			inputVariable2.addTerm(new Trapezoid("910", 9.000, 9.000, 10.000, 10.000));
			inputVariable2.addTerm(new Trapezoid("1011", 10.000, 10.000, 11.000, 11.000));
			inputVariable2.addTerm(new Trapezoid("1112", 11.000, 11.000, 12.000, 12.000));
			inputVariable2.addTerm(new Trapezoid("1213", 12.000, 12.000, 13.000, 13.000));
			inputVariable2.addTerm(new Trapezoid("1314", 13.000, 13.000, 14.000, 14.000));
			inputVariable2.addTerm(new Trapezoid("1415", 14.000, 14.000, 15.000, 15.000));
			inputVariable2.addTerm(new Trapezoid("1520", 15.000, 15.000, 20.000, 20.000));
			engine.addInputVariable(inputVariable2);
		
			InputVariable inputVariable3 = new InputVariable();
			inputVariable3.setEnabled(true);
			inputVariable3.setName("BMI");
			inputVariable3.setRange(0.000, 33.000);
			inputVariable3.addTerm(new Trapezoid("013", 0.000, 0.000, 13.000, 13.000));
			inputVariable3.addTerm(new Trapezoid("1314", 13.000, 13.000, 14.000, 14.000));
			inputVariable3.addTerm(new Trapezoid("1415", 14.000, 14.000, 15.000, 15.000));
			inputVariable3.addTerm(new Trapezoid("1516", 15.000, 15.000, 16.000, 16.000));
			inputVariable3.addTerm(new Trapezoid("1617", 16.000, 16.000, 17.000, 17.000));
			inputVariable3.addTerm(new Trapezoid("1718", 17.000, 17.000, 18.000, 18.000));
			inputVariable3.addTerm(new Trapezoid("1819", 18.000, 18.000, 19.000, 19.000));
			inputVariable3.addTerm(new Trapezoid("1920", 19.000, 19.000, 20.000, 20.000));
			inputVariable3.addTerm(new Trapezoid("2021", 20.000, 20.000, 21.000, 21.000));
			inputVariable3.addTerm(new Trapezoid("2122", 21.000, 21.000, 22.000, 22.000));
			inputVariable3.addTerm(new Trapezoid("2223", 22.000, 22.000, 23.000, 23.000));
			inputVariable3.addTerm(new Trapezoid("2324", 23.000, 23.000, 24.000, 24.000));
			inputVariable3.addTerm(new Trapezoid("2425", 24.000, 24.000, 25.000, 25.000));
			inputVariable3.addTerm(new Trapezoid("2526", 25.000, 25.000, 26.000, 26.000));
			inputVariable3.addTerm(new Trapezoid("2627", 26.000, 26.000, 27.000, 27.000));
			inputVariable3.addTerm(new Trapezoid("2728", 27.000, 27.000, 28.000, 28.000));
			inputVariable3.addTerm(new Trapezoid("2829", 28.000, 28.000, 29.000, 29.000));
			inputVariable3.addTerm(new Trapezoid("2940", 29.000, 29.000, 40.000, 40.000));
			engine.addInputVariable(inputVariable3);
		
			OutputVariable outputVariable = new OutputVariable();
			outputVariable.setEnabled(true);
			outputVariable.setName("Interpretation");
			outputVariable.setRange(0.000, 100.000);
			outputVariable.fuzzyOutput().setAccumulation(new Maximum());
			outputVariable.setDefuzzifier(new Centroid(200));
			outputVariable.setDefaultValue(Double.NaN);
			outputVariable.setLockValidOutput(false);
			outputVariable.setLockOutputRange(false);
			outputVariable.addTerm(new Trapezoid("Risk_for_Underweight", 0.000, 0.000, 2.500, 3.500));
			outputVariable.addTerm(new Trapezoid("Obese", 99.000, 99.900, 100.000, 100.000));
			outputVariable.addTerm(new Trapezoid("Healthy", 2.500, 3.500, 84.000, 86.000));
			outputVariable.addTerm(new Trapezoid("Risk_for_Overweight", 84.000, 85.000, 96.000, 98.000));
			outputVariable.addTerm(new Trapezoid("Overweight", 96.500, 97.000, 99.500, 99.900));
			engine.addOutputVariable(outputVariable);
		
			RuleBlock ruleBlock = new RuleBlock();
			ruleBlock.setEnabled(true);
			ruleBlock.setName("");
			ruleBlock.setConjunction(new Minimum());
			ruleBlock.setDisjunction(new Maximum());
			ruleBlock.setActivation(new Minimum());
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 25 and BMI is 013 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 25 and BMI is 1314 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 25 and BMI is 1415 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 013 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 1314 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 1415 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 1516 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 1617 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 1718 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 1819 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 1920 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2021 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2122 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2223 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2324 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2425 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2526 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2526 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2627 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2728 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2829 then Interpretation is Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Female and Age is 1520 and BMI is 2940 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 013 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 1314 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 1415 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 1516 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 1617 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 1718 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 1819 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 1920 then Interpretation is Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2021 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2122 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2223 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2324 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2425 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2526 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2627 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2728 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2829 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 25 and BMI is 2940 then Interpretation is Obese", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 013 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 1314 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 1415 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 1516 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 1617 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 1718 then Interpretation is Risk_for_Underweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 1819 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 1920 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2021 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2122 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2223 then Interpretation is Healthy", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2324 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2425 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2526 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2627 then Interpretation is Risk_for_Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2728 then Interpretation is Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2829 then Interpretation is Overweight", engine));
			ruleBlock.addRule(Rule.parse("if Gender is Male and Age is 1520 and BMI is 2940 then Interpretation is Obese", engine));
			engine.addRuleBlock(ruleBlock);
			//No Conjunction or Disjunction is needed
			 engine.configure("AlgebraicProduct", "AlgebraicSum", "AlgebraicProduct", "AlgebraicSum", "Centroid");
			StringBuilder status = new StringBuilder();
			 if (!engine.isReady(status))
			      throw new RuntimeException("Engine not ready. " +
			            "The following errors were encountered:\n" + status.toString());
			 for (int i = 0; i < 50; ++i){
			     double light = inputVariable1.getMinimum() + i * (inputVariable1.range() / 50);
			     inputVariable1.setInputValue(light);
			     double light1 = inputVariable2.getMinimum() + i * (inputVariable2.range() / 50);
			     inputVariable2.setInputValue(light1);
			     double light2 = inputVariable3.getMinimum() + i * (inputVariable3.range() / 50);
			     inputVariable3.setInputValue(light2);
			     engine.process();
			engine.process();
			FuzzyLite.logger().info(String.format(
		            "input1 = %s, input2 %s, input3 %s -> output = %s", 
		            Op.str(light),Op.str(light1), Op.str(light2), Op.str(outputVariable.defuzzify())));
			 }
			}
}