home Forums # Technical Support JAVA API

Viewing 10 posts - 11 through 20 (of 27 total)
  • Author
    Posts
  • #1411
    Unknown
    Member

    hello i’m trying to use the java api but always gets NaN as a result can anyone help me?

    there is the setup of the engine

    
    engine = new Engine("simple-dimmer");
    InputVariable personas = new InputVariable();
    personas.setName("PERSONAS");
    personas.setRange(0, 30);
    personas.addTerm(new Trapezoid("POCAS", 0, 0, 5, 10));
    personas.addTerm(new Trapezoid("LEVE", 5,10,15,20));
    personas.addTerm(new Trapezoid("MUCHAS", 15,20,30,30));
    engine.addInputVariable(personas);
    InputVariable temp=new InputVariable();
    temp.setName("TEMPERATURA");
    temp.setRange(12,28);
    temp.addTerm(new Trapezoid("BAJA",12,12,18,20));
    temp.addTerm(new Trapezoid("MEDIA", 18,20,24,26));
    temp.addTerm(new Trapezoid("ALTA", 24,26,28,29));
    engine.addInputVariable(temp);
    
    OutputVariable outputVariable = new OutputVariable();
    outputVariable.setEnabled(true);
    outputVariable.setName("RPM");
    outputVariable.setRange(0, 5000);
    outputVariable.fuzzyOutput().setAccumulation(new Maximum());
    outputVariable.setDefuzzifier(new Centroid(200));
    outputVariable.setDefaultValue(Double.NaN);
    outputVariable.addTerm(new Trapezoid("LENTO",0, 0,1500,2000));
    outputVariable.addTerm(new Trapezoid("INTERMEDIO",1000,2500,3000,4500));
    outputVariable.addTerm(new Trapezoid("RAPIDO", 3500,4000,5000,5000));
    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 PERSONAS is POCAS and TEMPERATURA is BAJA then RPM is LENTO",engine));
    ruleBlock.addRule(Rule.parse("if PERSONAS is POCAS and TEMPERATURA is MEDIA then RPM is INTERMEDIO", engine));
    ruleBlock.addRule(Rule.parse("if PERSONAS is POCAS and TEMPERATURA is ALTA then RPM is RAPIDO",engine));
    ruleBlock.addRule(Rule.parse("if PERSONAS is LEVE and TEMPERATURA is BAJA then RPM is INTERMEDIO",engine));
    ruleBlock.addRule(Rule.parse("if PERSONAS is LEVE and TEMPERATURA is MEDIA then RPM is INTERMEDIO", engine));
    ruleBlock.addRule(Rule.parse("if PERSONAS is LEVE and TEMPERATURA is ALTA then RPM is RAPIDO",engine));
    ruleBlock.addRule(Rule.parse("if PERSONAS is MUCHAS and TEMPERATURA is BAJA then RPM is INTERMEDIO",engine));
    ruleBlock.addRule(Rule.parse("if PERSONAS is MUCHAS and TEMPERATURA is MEDIA then RPM is RAPIDO", engine));
    ruleBlock.addRule(Rule.parse("if PERSONAS is MUCHAS and TEMPERATURA is ALTA then RPM is RAPIDO",engine));
    engine.addRuleBlock(ruleBlock);
    
    StringBuilder status = new StringBuilder();
    if (!engine.isReady(status))
    throw new RuntimeException("no se pdo arrancar el motor difuso se encontraron las siguientes fallas \n"+ status.toString());
    

    and there is where i try to use it in another part of my code

    
    engine.setInputValue("PERSONAS", sldPERSONAS.getValue());
    engine.setInputValue("TEMPERATURA",sldTEMP.getValue());
    engine.process();
    System.out.println(engine.getOutputVariable("RPM").getLastValidOutput());
    
    #1415

    Hi raac,

    The problem is that you need to do:

    System.out.println(engine.getOutputVariable("RPM").defuzzify());

    This method will change in the next version of jfuzzylite just like fuzzylite 5.0.

    Cheers.

    #1417
    Unknown
    Member

    thanks for your response i’ll try it latter

    #1448
    Unknown
    Member

    Hi, Mr Vilela

    I have a question about the outcome of my code. I have a linguistic output variable that can return me three options: sell, hold or buy. The result I get is only one value, eg 0.62999. I wonder which of the possible outputs this value makes reference, sell, hold or buy, is to know?

    Follows the code below:

    Engine engine = new Engine();
    engine.setName("acao");
    
    InputVariable inputVariable1 = new InputVariable();
    inputVariable1.setEnabled(true);
    inputVariable1.setName("MACD");
    inputVariable1.setRange(-2.000, 2.000);
    inputVariable1.addTerm(new Triangle("baixo", -2.000, -0.780, 0.340));
    inputVariable1.addTerm(new Triangle("alto", -0.380, 0.720, 2.000));
    engine.addInputVariable(inputVariable1);
    
    InputVariable inputVariable2 = new InputVariable();
    inputVariable2.setEnabled(true);
    inputVariable2.setName("BETA");
    inputVariable2.setRange(-2.000, 2.000);
    inputVariable2.addTerm(new Triangle("baixo", -2.000, -1.260, -0.500));
    inputVariable2.addTerm(new Triangle("neutro", -1.000, 0.000, 1.000));
    inputVariable2.addTerm(new Triangle("alto", 0.520, 1.260, 2.000));
    engine.addInputVariable(inputVariable2);
    
    InputVariable inputVariable3 = new InputVariable();
    inputVariable3.setEnabled(true);
    inputVariable3.setName("RSI");
    inputVariable3.setRange(0.000, 100.000);
    inputVariable3.addTerm(new Triangle("sobrevendido", 0.000, 25.000, 60.000));
    inputVariable3.addTerm(new Triangle("sobrecomprado", 40.000, 65.000, 100.000));
    engine.addInputVariable(inputVariable3);
    
    OutputVariable outputVariable = new OutputVariable();
    outputVariable.setEnabled(true);
    outputVariable.setName("DECISAO");
    outputVariable.setRange(-3.000, 3.000);
    outputVariable.fuzzyOutput().setAccumulation(new AlgebraicSum());
    outputVariable.setDefuzzifier(new Centroid(200));
    outputVariable.setDefaultValue(Double.NaN);
    outputVariable.setLockValidOutput(false);
    outputVariable.setLockOutputRange(false);
    outputVariable.addTerm(new Triangle("sell", -2.980, -1.750, -0.620));
    outputVariable.addTerm(new Triangle("old", -1.140, -0.050, 1.220));
    outputVariable.addTerm(new Triangle("buy", 0.650, 1.590, 3.040));
    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 MACD is baixo and BETA is baixo and RSI is sobrevendido then DECISAO is sell", engine));
    ruleBlock.addRule(Rule.parse("if MACD is baixo and BETA is baixo and RSI is sobrecomprado then DECISAO is sell", engine));
    ruleBlock.addRule(Rule.parse("if MACD is baixo and BETA is neutro and RSI is sobrevendido then DECISAO is old", engine));
    ruleBlock.addRule(Rule.parse("if MACD is baixo and BETA is neutro and RSI is sobrecomprado then DECISAO is old", engine));
    ruleBlock.addRule(Rule.parse("if MACD is baixo and BETA is alto and RSI is sobrevendido then DECISAO is old", engine));
    ruleBlock.addRule(Rule.parse("if MACD is baixo and BETA is alto and RSI is sobrecomprado then DECISAO is old", engine));
    ruleBlock.addRule(Rule.parse("if MACD is alto and BETA is baixo and RSI is sobrevendido then DECISAO is old", engine));
    ruleBlock.addRule(Rule.parse("if MACD is alto and BETA is baixo and RSI is sobrecomprado then DECISAO is old", engine));
    ruleBlock.addRule(Rule.parse("if MACD is alto and BETA is neutro and RSI is sobrevendido then DECISAO is old", engine));
    ruleBlock.addRule(Rule.parse("if MACD is alto and BETA is neutro and RSI is sobrecomprado then DECISAO is old", engine));
    ruleBlock.addRule(Rule.parse("if MACD is alto and BETA is alto and RSI is sobrevendido then DECISAO is buy", engine));
    ruleBlock.addRule(Rule.parse("if MACD is alto and BETA is alto and RSI is sobrecomprado then DECISAO is buy", engine));
    engine.addRuleBlock(ruleBlock);
    
    engine.setInputValue("MACD", MACD);
    engine.setInputValue("BETA", BETA);
    engine.setInputValue("RSI", RSI);
    engine.process();
    
    System.out.println(engine.getOutputValue("DECISAO"));
    

    Thank’s

    #1450

    Hi allissonjorge,

    The value you are obtaining is a defuzzified output value. If you want to see the fuzzy output value, the correct way would be:

    
    Accumulated fuzzyOutput = outputVariable.fuzzyOutput();
    for (Term term : fuzzyOutput){
       Thresholded activatedTerm = (Thresholded)term;
       System.out.println("Term: " + activatedTerm.getName() + ": " + activatedTerm.getThreshold());
    }
    

    However, you could also do: outputVariable.fuzzify(0.62999), which will return you a string corresponding to the fuzzification of such a value.

    Have in mind that there is a difference between the two methods. The first method is the correct way to obtain the true fuzzy output value. The second will obtain a fuzzification from an already defuzzified output value, for which information will not be accurate. More specifically, the first method will return the fuzzy output of the variable, but the second method will defuzzify the output variable (yielding 0.629999) and then it is trying to obtain the original fuzzy output value from just 0.629999.

    In QtFuzzyLite 5, you can see the difference between both values by clicking the output value label, which swaps the value $\tilde{y}$ to $\mu(y)$. The former is the fuzzy output value, the latter is the membership function of the defuzzified output value.

    #1451
    Unknown
    Member

    Hi, Dr Juan Vilela

    Thank you for the clarification! I am using your API to get the results for my dissertation. I look forward to your help!

    Thank you so much!

    #1512
    Unknown
    Member

    hi

    how I can get the value of the output fuzzy set and the value of the rules that were activated?

    regards

    package ejemfuzzy1;
    
    import com.fuzzylite.Engine;
    import com.fuzzylite.defuzzifier.Centroid;
    import com.fuzzylite.norm.s.Maximum;
    import com.fuzzylite.norm.t.Minimum;
    import com.fuzzylite.rule.Rule;
    import com.fuzzylite.rule.RuleBlock;
    import com.fuzzylite.term.Trapezoid;
    import com.fuzzylite.term.Triangle;
    import com.fuzzylite.variable.InputVariable;
    import com.fuzzylite.variable.OutputVariable;
    
    /**
     *
     * @author Fernandinho
     */
    public class EjemFuzzy1 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            Engine engine = new Engine();
            engine.setName("ejemplo");
            
            InputVariable project_funding = new InputVariable();
            project_funding.setEnabled(true);
            project_funding.setName("ProjectFunding");
            project_funding.setRange(0.000, 100.000);
            project_funding.addTerm(new Trapezoid("INADEQUATE", 0.000, 0.000, 20.000, 50.000));
            project_funding.addTerm(new Triangle("MARGINAL", 31.250, 50.000, 68.750));
            project_funding.addTerm(new Trapezoid("ADEQUATE", 50.000, 80.000, 100.000, 100.000));
            engine.addInputVariable(project_funding);
            
            InputVariable project_staffing = new InputVariable();
            project_staffing.setEnabled(true);
            project_staffing.setName("ProjectStaffing");
            project_staffing.setRange(0.000, 100.000);
            project_staffing.addTerm(new Trapezoid("SMALL", 0.000, 0.000, 30.000, 63.333));
            project_staffing.addTerm(new Trapezoid("LARGE", 40.000, 68.57, 100.000, 100.000));
            engine.addInputVariable(project_staffing);
            
            OutputVariable risk = new OutputVariable();
            risk.setEnabled(true);
            risk.setName("Risk");
            risk.setRange(0.000, 100.000);
            risk.fuzzyOutput().setAccumulation(new Maximum());
            risk.setDefuzzifier(new Centroid());
            risk.setDefaultValue(Double.NaN);
            risk.setLockValidOutput(false);
            risk.setLockOutputRange(false);
            risk.addTerm(new Trapezoid("LOW", 0.000, 0.000, 20.000, 40.000 ));
            risk.addTerm(new Triangle("NORMAL", 20.000, 50.000, 80.000));
            risk.addTerm(new Trapezoid("HIGH", 60.000, 80.000, 100.000, 100.000));
            engine.addOutputVariable(risk);
            
            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 ProjectFunding is ADEQUATE or ProjectStaffing is SMALL then Risk is LOW",engine));
            ruleBlock.addRule(Rule.parse("if ProjectFunding is MARGINAL and ProjectStaffing is LARGE then Risk is NORMAL", engine));
            ruleBlock.addRule(Rule.parse("if ProjectFunding is INADEQUATE then Risk is HIGH",engine));
            engine.addRuleBlock(ruleBlock);
            
            StringBuilder status = new StringBuilder();
            if (!engine.isReady(status)) 
                throw new RuntimeException("No fue posible generar el archivo. "
                + "Los siguientes errores fueron encontrados:\n"
                + status.toString());
    
            engine.setInputValue("ProjectFunding", 35.000);
            engine.setInputValue("ProjectStaffing", 60.000);
            engine.process();
    
            System.out.println(engine.getOutputValue("Risk"));
                    
        }
        
    }
    #1519

    Hi,

    thank you for your post.

    The fuzzy output value can be found as engine.getOutputVariable("Risk").fuzzyOutput().toString().
    The activation degrees of each rule can be found by iterating over the rules as follows:

    
    RuleBlock ruleBlock = engine.getRuleBlock(0);
    for (Rule r : ruleBlock){
       System.out.println( r.activationDegree(ruleBlock.getConjunction(), ruleBlock.getDisjunction()) + " -> Rule: " + r.toString());
    }

    An activation degree of 0.0 means the rule was not activated.

    Let me know if this answers your question.

    #1531
    Unknown
    Member

    Yes!!! Thank you very much.

    #1532
    Unknown
    Member

    And how I can know the fuzzy output set?

Viewing 10 posts - 11 through 20 (of 27 total)
  • You must be logged in to reply to this topic.