home Forums # Technical Support jfuzzylite – result doesn`t change

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1822
    Unknown
    Member

    Hi, I find this library very interesting.
    I am facing two problems, first is that my result doesn`t change even though I am changing the inputs. The second is that i recieve NaN if I change the values of the inputs.

    I will be very greatful for your help.
    I will attach the code.

    Best regards,
    Nadia

    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 DiagnosisManager {
    	public static void main(String[] args)  {
    		
    		Engine engine = new Engine();
    		engine.setName("Diagnosis");
    
    		InputVariable inputVariable1 = new InputVariable();
    		inputVariable1.setEnabled(true);
    		inputVariable1.setName("Chest");
    		inputVariable1.setRange(1.000, 5.000);
    		inputVariable1.addTerm(new Triangle("typ", 0.000, 1.000, 1.500));
    		inputVariable1.addTerm(new Triangle("atyp", 1.500, 2.000, 2.500));
    		inputVariable1.addTerm(new Triangle("non", 2.500, 3.000, 3.500));
    		inputVariable1.addTerm(new Triangle("asym", 3.500, 4.000, 4.500));
    		engine.addInputVariable(inputVariable1);
    		
    		InputVariable inputVariable2 = new InputVariable();
    		inputVariable2.setEnabled(true);
    		inputVariable2.setName("BloodP");
    		inputVariable2.setRange(100.000, 300.000);
    		inputVariable2.addTerm(new Trapezoid("low", 0.000, 0.000, 130.000, 140.000));
    		inputVariable2.addTerm(new Triangle("medium", 130.000, 140.000, 155.000));
    		inputVariable2.addTerm(new Triangle("high", 140.000, 155.000, 170.000));
    		inputVariable2.addTerm(new Trapezoid("vhigh", 155.000, 170.000, 300.000, 300.000));
    		engine.addInputVariable(inputVariable2);
    		
    		InputVariable inputVariable3 = new InputVariable();
    		inputVariable3.setEnabled(true);
    		inputVariable3.setName("Chol");
    		inputVariable3.setRange(150.000, 550.000);
    		inputVariable3.addTerm(new Trapezoid("low", 0.000, 0.000, 160.000, 200.000));
    		inputVariable3.addTerm(new Triangle("medium", 190.000, 220.000, 250.000));
    		inputVariable3.addTerm(new Triangle("high", 220.000, 270.000, 310.000));
    		inputVariable3.addTerm(new Trapezoid("vhigh", 270.000, 340.000, 550.000, 550.000));
    		engine.addInputVariable(inputVariable3);
    		
    		InputVariable inputVariable4 = new InputVariable();
    		inputVariable4.setEnabled(true);
    		inputVariable4.setName("BloodS");
    		inputVariable4.setRange(60.000, 160.000);
    		inputVariable4.addTerm(new Trapezoid("true", 105.000, 120.000, 160.000, 160.000));
    		engine.addInputVariable(inputVariable4);
    		
    		InputVariable inputVariable5 = new InputVariable();
    		inputVariable5.setEnabled(true);
    		inputVariable5.setName("ECG");
    		inputVariable5.setRange(-0.500, 2.500);
    		inputVariable5.addTerm(new Trapezoid("normal", -1.625, -1.625, 0.1, 0.5));
    		inputVariable5.addTerm(new Triangle("abn", 0.250, 1.000, 1.750));
    		inputVariable5.addTerm(new Trapezoid("hyper", 1.400, 1.950, 2.500, 2.500));
    		engine.addInputVariable(inputVariable5);
    		
    		InputVariable inputVariable6 = new InputVariable();
    		inputVariable6.setEnabled(true);
    		inputVariable6.setName("Heart");
    		inputVariable6.setRange(100.000, 500.000);
    		inputVariable6.addTerm(new Trapezoid("low", 0, 0, 120, 140));
    		inputVariable6.addTerm(new Triangle("medium", 120, 150, 200));
    		inputVariable6.addTerm(new Trapezoid("high", 150, 200, 500, 500));
    		engine.addInputVariable(inputVariable6);
    		
    		InputVariable inputVariable7 = new InputVariable();
    		inputVariable7.setEnabled(true);
    		inputVariable7.setName("Exercise");
    		inputVariable7.setRange(-2.000, 2.000);
    		inputVariable7.addTerm(new Trapezoid("true", 0.500, 1.000, 2.000, 2.000));
    		engine.addInputVariable(inputVariable7);
    		
    		InputVariable inputVariable8 = new InputVariable();
    		inputVariable8.setEnabled(true);
    		inputVariable8.setName("Peak");
    		inputVariable8.setRange(0.000, 6.000);
    		inputVariable8.addTerm(new Trapezoid("low", 0, 0, 0.900, 2.000));
    		inputVariable8.addTerm(new Triangle("risk", 1.500, 2.800, 4.100));
    		inputVariable8.addTerm(new Trapezoid("terrible", 2.500, 4.000, 6.000, 6.000));
    		engine.addInputVariable(inputVariable8);
    		
    		InputVariable inputVariable9 = new InputVariable();
    		inputVariable9.setEnabled(true);
    		inputVariable9.setName("Thal");
    		inputVariable9.setRange(2.000, 8.000);
    		inputVariable9.addTerm(new Triangle("normal", 2.000, 3.000, 4.000));
    		inputVariable9.addTerm(new Triangle("fixed", 4.500, 6.000, 6.500));
    		inputVariable9.addTerm(new Triangle("rever", 6.500, 7.000, 8.000));
    		engine.addInputVariable(inputVariable9); 
    		
    		InputVariable inputVariable10 = new InputVariable();
    		inputVariable10.setEnabled(true);
    		inputVariable10.setName("Sex");
    		inputVariable10.setRange(-1.000, 2.000);
    		inputVariable10.addTerm(new Triangle("male", -0.500, 0.000, 0.500));
    		inputVariable10.addTerm(new Triangle("female", 0.500, 1.000, 1.500));
    		engine.addInputVariable(inputVariable10);
    		
    		InputVariable inputVariable11 = new InputVariable();
    		inputVariable11.setEnabled(true);
    		inputVariable11.setName("Age");
    		inputVariable11.setRange(30.000, 75.000);
    		inputVariable11.addTerm(new Trapezoid("young", 0.000, 0.000, 33.000, 38.000));
    		inputVariable11.addTerm(new Triangle("mild", 33.000, 38.000, 45.000));
    		inputVariable11.addTerm(new Triangle("old", 40.000, 48.000, 58.000));
    		inputVariable11.addTerm(new Trapezoid("vold", 52.000, 60.000, 75.000, 75.000));
    		engine.addInputVariable(inputVariable11);
    		
    		OutputVariable outputVariable1 = new OutputVariable();
    		outputVariable1.setEnabled(true);
    		outputVariable1.setName("Diag");
    		outputVariable1.setRange(0.000, 5.000);
    		outputVariable1.fuzzyOutput().setAccumulation(new Maximum());
    		outputVariable1.setDefuzzifier(new MeanOfMaximum(500));
    		outputVariable1.setDefaultValue(Double.NaN);
    		outputVariable1.setLockPreviousOutputValue(false);
    		outputVariable1.setLockOutputValueInRange(false);
    		outputVariable1.addTerm(new Trapezoid("healthy", 0.000, 0.000, 0.400, 1.000));
    		outputVariable1.addTerm(new Triangle("s1", 0.000, 1.000, 2.000));
    		outputVariable1.addTerm(new Triangle("s2", 1.000, 2.000, 3.000));
    		outputVariable1.addTerm(new Triangle("s3", 2.000, 3.000, 4.000));
    		outputVariable1.addTerm(new Trapezoid("s4", 3.000, 3.600, 4.000, 4.000));
    		engine.addOutputVariable(outputVariable1);
    
    		RuleBlock ruleBlock = new RuleBlock();
    		ruleBlock.setEnabled(true);
    ruleBlock.addRule(Rule.parse(
    				"if Chest is typ then Diag is healthy", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chest is atyp then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chest is non then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chest is asym then Diag is s3", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chest is asym then Diag is s4", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Sex is female then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Sex is male then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if BloodP is low then Diag is healthy", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if BloodP is medium then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if BloodP is high then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if BloodP is high then Diag is s3", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if BloodP is vhigh then Diag is s4", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chol is low then Diag is healthy", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chol is medium then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chol is high then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chol is high then Diag is s3", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Chol is vhigh then Diag is s4", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if BloodS is true then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if ECG is normal then Diag is healthy", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if ECG is normal then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if ECG is abn then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if ECG is hyper then Diag is s3", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if ECG is hyper then Diag is s4", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Heart is low then Diag is healthy", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Heart is medium then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Heart is medium then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Heart is low then Diag is s3", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Heart is high then Diag is s4", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Exercise is true then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Peak is low then Diag is healthy", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Peak is low then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Peak is terrible then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Peak is terrible then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Peak is risk then Diag is s4", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Thal is normal then Diag is healthy", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Thal is normal then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Thal is fixed then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Thal is rever then Diag is s3", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Thal is rever then Diag is s4", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Age is young then Diag is healthy", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Age is mild then Diag is s1", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Age is old then Diag is s2", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Age is old then Diag is s3", engine));
    		ruleBlock.addRule(Rule.parse(
    				"if Age is vold then Diag is s4", engine));
    engine.addRuleBlock(ruleBlock);
    
    		engine.configure("", "", "Minimum", "Maximum", "Centroid");
    	//	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());
    		}
    
    		double age = 44.000;
    		inputVariable11.setInputValue(age);
    		double sex = 1.000;
    		inputVariable10.setInputValue(sex);
    		double chest = 4.000;
    		inputVariable1.setInputValue(chest);
    		double bp = 140.000;
    		inputVariable2.setInputValue(bp);
    		double chol = 260.000;
    		inputVariable3.setInputValue(chol);		
    		double bs = 110.000;
    		inputVariable4.setInputValue(bs);
    		double ecg = 1.000;
    		inputVariable5.setInputValue(ecg);
    		double heart = 112.000;
    		inputVariable6.setInputValue(heart);
    		double ex = 1.000;
    		inputVariable7.setInputValue(ex);
    		double peak = 3.000;
    		inputVariable8.setInputValue(peak);
    		double thal = 2.000;
    		inputVariable9.setInputValue(thal);
    		
    		engine.process();
    		
    		FuzzyLite.logger().info(
    				String.format("Diag.output = %s",
    						Op.str(outputVariable1.getOutputValue())));
    #1824

    Hi Nadia,

    thank you for your post.

    Could you please post the FLL code instead? You can get it using FuzzyLite.logger().info(new FllExporter().toString(engine));

    Also, once you obtain the FLL code, could you please experiment with QtFuzzyLite first? You can use QtFuzzyLite 5 (commercial) or the QtFuzzyLite 4 (free). If using the free version, you may have to manually change the FLL code of OutputVariable to have lock-previous instead of lock-valid.

    If you still have issues, please let me know and I will check into that.

    Cheers,

    Juan.

    #1849
    Unknown
    Member

    Hi, thank you for your kind answer, I am sorry for answering after more days. I tried QtFuzzyLite, and I found it really helpful! I changed easily everything I needed and now it works perfectly. The problem was in my definitions of the rules.

    Best regards,
    Nadia

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.