home Forums # Technical Support Confusing output

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1934
    Unknown
    Member

    Is there a degree of error in calculating the output of a fuzzy logic in jfuzzy lite?
    I have the below code from matlab

    [System]
    Name=’congestion’
    Type=’mamdani’
    Version=2.0
    NumInputs=2
    NumOutputs=1
    NumRules=5
    AndMethod=’min’
    OrMethod=’max’
    ImpMethod=’min’
    AggMethod=’max’
    DefuzzMethod=’centroid’

    [Input1]
    Name=’VehicleDensity’
    Range=[0 10]
    NumMFs=5
    MF1=’VeryLow’:’gaussmf’,[1.062 -2.776e-17]
    MF2=’Low’:’gaussmf’,[1.062 2.5]
    MF3=’Average’:’gaussmf’,[1.062 5]
    MF4=’High’:’gaussmf’,[1.062 7.5]
    MF5=’VeryHigh’:’gaussmf’,[1.062 10]

    [Input2]
    Name=’ModalSpeed’
    Range=[0 100]
    NumMFs=5
    MF1=’VeryLow’:’gaussmf’,[10.62 -2.22e-16]
    MF2=’Low’:’gaussmf’,[10.62 25]
    MF3=’Average’:’gaussmf’,[10.62 50]
    MF4=’High’:’gaussmf’,[10.62 75]
    MF5=’VeryHigh’:’gaussmf’,[10.62 99.7354497354497]

    [Output1]
    Name=’LevelOfService’
    Range=[0 60]
    NumMFs=5
    MF1=’VeryHigh’:’gaussmf’,[6.37 -2.22e-16]
    MF2=’High’:’gaussmf’,[6.37 15]
    MF3=’Average’:’gaussmf’,[6.37 30]
    MF4=’Low’:’gaussmf’,[6.37 45]
    MF5=’VeryLow’:’gaussmf’,[6.37 60]

    [Rules]
    1 5, 1 (1) : 1
    2 4, 2 (1) : 1
    3 3, 3 (1) : 1
    4 2, 4 (1) : 1
    5 1, 5 (1) : 1

    the above code was used to create the below java code

    public void defuzzifytraffic()
        {
            Engine engine = new Engine();
            engine.setName("CongestionDefuzzify");
            InputVariable inputVariable1 = new InputVariable();
            inputVariable1.setEnabled(true);
            inputVariable1.setName("VehicleDensityInput");
            inputVariable1.setRange(0,10);
            inputVariable1.addTerm(new Gaussian("VeryLow",1.062,-2.77e-17));
            inputVariable1.addTerm(new Gaussian("Low",1.062,2.5));
            inputVariable1.addTerm(new Gaussian("Average",1.062,5));
            inputVariable1.addTerm(new Gaussian("High",1.062,7.5));
            inputVariable1.addTerm(new Gaussian("VeryHigh",1.062,10));
            inputVariable1.setInputValue(this.getVehicleDensity());
            engine.addInputVariable(inputVariable1);
            
            InputVariable inputVariable2 = new InputVariable();
            inputVariable2.setEnabled(true);
            inputVariable2.setName("ModalSpeedInput");
            inputVariable2.setRange(0,100);
            inputVariable2.addTerm(new Gaussian("VeryLow",10.62,-2.22e-16));
            inputVariable2.addTerm(new Gaussian("Low",10.62,25));
            inputVariable2.addTerm(new Gaussian("Average",10.62,50));
            inputVariable2.addTerm(new Gaussian("High",10.62,75));
            inputVariable2.addTerm(new Gaussian("VeryHigh",10.62,100));
            inputVariable2.setInputValue(this.getModalSpeed());
            engine.addInputVariable(inputVariable2);
            
            OutputVariable OutputVariable = new OutputVariable();
            OutputVariable.setEnabled(true);
            OutputVariable.setName("LevelOfCongestionResult");
            OutputVariable.setRange(0,60);
            OutputVariable.fuzzyOutput().setAccumulation(new Maximum());
            OutputVariable.setDefaultValue(Double.NaN);
            OutputVariable.addTerm(new Gaussian("VeryHigh",6.37, -2.22e-16));
            OutputVariable.addTerm(new Gaussian("High",6.37, 15));
            OutputVariable.addTerm(new Gaussian("Average",6.37, 30));
            OutputVariable.addTerm(new Gaussian("Low",6.37, 45));
            OutputVariable.addTerm(new Gaussian("VeryLow",6.37, 60));
            
            engine.addOutputVariable(OutputVariable);
            RuleBlock ruleblock = new RuleBlock(); 
            ruleblock.setEnabled(true);
            ruleblock.setConjunction(new Minimum());
            ruleblock.setDisjunction(new Maximum());
            ruleblock.setActivation(new Minimum());
            ruleblock.addRule(Rule.parse("if VehicleDensityInput is VeryLow and ModalSpeedInput is VeryHigh then LevelOfCongestionResult is VeryHigh", engine));
            ruleblock.addRule(Rule.parse("if VehicleDensityInput is Low and ModalSpeedInput is High then LevelOfCongestionResult is High", engine));
            ruleblock.addRule(Rule.parse("if VehicleDensityInput is Average and ModalSpeedInput is Average then LevelOfCongestionResult is Average", engine));
            ruleblock.addRule(Rule.parse("if VehicleDensityInput is High and ModalSpeedInput is Low then LevelOfCongestionResult is Low", engine));
            ruleblock.addRule(Rule.parse("if VehicleDensityInput is VeryHigh and ModalSpeedInput is VeryLow then LevelOfCongestionResult is VeryLow", engine));
            engine.addRuleBlock(ruleblock);
            engine.configure("Minimum", "Maximum", "Minimum", "Maximum", "Centroid");
            StringBuilder status = new StringBuilder();
            if(!engine.isReady(status))
            {
                throw new RuntimeException("Engine not ready, the following errors we met:\n" + status.toString());
            }
            engine.setInputValue("VehicleDensityInput", this.getVehicleDensity());
            engine.setInputValue("ModalSpeedInput", this.getModalSpeed());
            engine.process();
            //this.setCongestionCoefficient(OutputVariable.getOutputValue()); 
            FuzzyLite.logger().info(String.format("result= %s", Op.str(OutputVariable.getOutputValue())));
        }
    

    I get confused because i get different result having used to same input.
    For instance, in matlab,

    evalfis([5,95],b)

    gives me

    16.1383

    while

    engine.setInputValue(“VehicleDensityInput”, 5); engine.setInputValue(“ModalSpeedInput”, 95);

    gives

    28.521

    using QTfuzzylite i get results similar to those i get from matlab. this leads me to think the problem is with my java code.

    #1949

    Hi,

    I am sorry for the late response (I was on holidays).

    The difference in the result is too big for it to be considered a margin of error.

    Could you please try using values without scientific notation (i.e., convert -2.776e-17 to 0.0, -2.22e-16 to 0.0, and 99.7354497354497 to 100)? I just want to make sure the problem is not when parsing such numbers. Please compare the results with Matlab and jfuzzylite, and let me know.

    Cheers.

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