home Forums # Technical Support JAVA API

Viewing 7 posts - 21 through 27 (of 27 total)
  • Author
    Posts
  • #1543

    Hi Fernando,

    The fuzzy set is within the Accumulated term in the OutputVariable: engine.getOutputVariable("Risk").fuzzyOutput().toString() You can iterate over each term in the fuzzyOutput().getTerms() and there is your output fuzzy set.

    Cheers.

    #1546
    Unknown
    Member

    And why activatedTerm.getName() doesn’t work in my example?

    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.Accumulated;
    import com.fuzzylite.term.Term;
    import com.fuzzylite.term.Thresholded;
    import com.fuzzylite.term.Trapezoid;
    import com.fuzzylite.term.Triangle;
    import com.fuzzylite.variable.InputVariable;
    import com.fuzzylite.variable.OutputVariable;
    import java.util.List;
    
    /**
     *
     * @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 velocidad_auto = new InputVariable();
            velocidad_auto.setEnabled(true);
            velocidad_auto.setName("velocidad_auto");
            velocidad_auto.setRange(0.000, 60.000);
            velocidad_auto.addTerm(new Trapezoid("LENTO", 0.000, 0.000, 10.000, 30.000));
            velocidad_auto.addTerm(new Trapezoid("NORMAL", 10.000, 30.000, 40.000, 60.000));
            velocidad_auto.addTerm(new Triangle("RAPIDO", 40.000, 60.000, 60.000));
            engine.addInputVariable(velocidad_auto);
            
            InputVariable distancia_auto = new InputVariable();
            distancia_auto.setEnabled(true);
            distancia_auto.setName("distancia_auto");
            distancia_auto.setRange(0.000, 100.000);
            distancia_auto.addTerm(new Triangle("CERCA", 0.000, 0.000, 30.000));
            distancia_auto.addTerm(new Triangle("LEJOS", 20.000, 50.000, 80.000));
            distancia_auto.addTerm(new Triangle("MUYLEJOS", 70.000, 100.000, 100.000));
            engine.addInputVariable(distancia_auto);
            
            OutputVariable velocida_peaton = new OutputVariable();
            velocida_peaton.setEnabled(true);
            velocida_peaton.setName("velocida_peaton");
            velocida_peaton.setRange(0.000, 6.000);
            velocida_peaton.fuzzyOutput().setAccumulation(new Maximum());
            velocida_peaton.setDefuzzifier(new Centroid());
            velocida_peaton.setDefaultValue(Double.NaN);
            velocida_peaton.setLockValidOutput(false);
            velocida_peaton.setLockOutputRange(false);
            velocida_peaton.addTerm(new Trapezoid("MUYLENTO", 0.000, 0.000, 1.000, 2.000 ));
            velocida_peaton.addTerm(new Triangle("LENTO", 1.000, 2.000, 3.000));
            velocida_peaton.addTerm(new Triangle("CAMINAR", 2.000, 3.000, 4.000));
            velocida_peaton.addTerm(new Triangle("TROTAR", 3.000, 4.000, 5.000));
            velocida_peaton.addTerm(new Triangle("CORRER", 4.000, 5.000, 6.000));
            velocida_peaton.addTerm(new Triangle("CORRERMUCHO", 5.000, 6.000, 6.000));
            engine.addOutputVariable(velocida_peaton);
            
            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 velocidad_auto is LENTO and distancia_auto is CERCA then velocida_peaton is CAMINAR",engine));
            ruleBlock.addRule(Rule.parse("if velocidad_auto is LENTO and distancia_auto is LEJOS then velocida_peaton is LENTO", engine));
            ruleBlock.addRule(Rule.parse("if velocidad_auto is LENTO and distancia_auto is MUYLEJOS then velocida_peaton is MUYLENTO",engine));
            ruleBlock.addRule(Rule.parse("if velocidad_auto is NORMAL and distancia_auto is CERCA then velocida_peaton is CORRER", engine));
            ruleBlock.addRule(Rule.parse("if velocidad_auto is NORMAL and distancia_auto is LEJOS then velocida_peaton is TROTAR",engine));
            ruleBlock.addRule(Rule.parse("if velocidad_auto is NORMAL and distancia_auto is MUYLEJOS then velocida_peaton is CAMINAR", engine));
            ruleBlock.addRule(Rule.parse("if velocidad_auto is RAPIDO and distancia_auto is CERCA then velocida_peaton is CORRERMUCHO",engine));
            ruleBlock.addRule(Rule.parse("if velocidad_auto is RAPIDO and distancia_auto is LEJOS then velocida_peaton is CORRER", engine));
            ruleBlock.addRule(Rule.parse("if velocidad_auto is RAPIDO and distancia_auto is MUYLEJOS then velocida_peaton is TROTAR",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("velocidad_auto", 25.000);
            engine.setInputValue("distancia_auto", 22.000);
            engine.process();
            
            RuleBlock ruleBlock2 = engine.getRuleBlock(0);
            List<Rule> listRules = ruleBlock2.getRules();
            for (Rule r : listRules) {
                System.out.println( r.activationDegree(ruleBlock.getConjunction(), ruleBlock.getDisjunction()) + " -> Rule: " + r.toString());
            }
            
            Accumulated fuzzyOutput = velocida_peaton.fuzzyOutput();
            List<Term> aux = fuzzyOutput.getTerms();
            for (Term aux1 : aux) {
                Thresholded activatedTerm = (Thresholded) aux1;
                System.out.println("Term: " + activatedTerm.getName() + ": " + activatedTerm.getThreshold());
            }
            //System.out.println(velocida_peaton.fuzzify(engine.getOutputValue("velocida_peaton")));
    
            //valor salida difusa
            System.out.println(engine.getOutputVariable("velocida_peaton").fuzzyOutput().toString());
            //System.out.println(engine.getOutputVariable("velocida_peaton").fuzzyOutput().parameters());
            System.out.println(engine.getOutputValue("velocida_peaton") );
                    
        }
        
    }

    output:

    0.25 -> Rule: if velocidad_auto is LENTO and distancia_auto is CERCA  then velocida_peaton is CAMINAR
    0.06666666666666667 -> Rule: if velocidad_auto is LENTO and distancia_auto is LEJOS  then velocida_peaton is LENTO
    0.0 -> Rule: if velocidad_auto is LENTO and distancia_auto is MUYLEJOS  then velocida_peaton is MUYLENTO
    0.26666666666666666 -> Rule: if velocidad_auto is NORMAL and distancia_auto is CERCA  then velocida_peaton is CORRER
    0.06666666666666667 -> Rule: if velocidad_auto is NORMAL and distancia_auto is LEJOS  then velocida_peaton is TROTAR
    0.0 -> Rule: if velocidad_auto is NORMAL and distancia_auto is MUYLEJOS  then velocida_peaton is CAMINAR
    0.0 -> Rule: if velocidad_auto is RAPIDO and distancia_auto is CERCA  then velocida_peaton is CORRERMUCHO
    0.0 -> Rule: if velocidad_auto is RAPIDO and distancia_auto is LEJOS  then velocida_peaton is CORRER
    0.0 -> Rule: if velocidad_auto is RAPIDO and distancia_auto is MUYLEJOS  then velocida_peaton is TROTAR
    Term: : 0.25
    Term: : 0.06666666666666667
    Term: : 0.26666666666666666
    Term: : 0.06666666666666667
    term: fuzzyOutput Accumulated 0,000 6,000 Maximum term:  Thresholded 0,250 Minimum term: CAMINAR Triangle 2,000 3,000 4,000 term:  Thresholded 0,067 Minimum term: LENTO Triangle 1,000 2,000 3,000 term:  Thresholded 0,267 Minimum term: CORRER Triangle 4,000 5,000 6,000 term:  Thresholded 0,067 Minimum term: TROTAR Triangle 3,000 4,000 5,000
    3.8559826946848017
    #1547
    Unknown
    Member

    The answer should be: CAMINAR

    Regards

    #1558

    Hi Allison,

    I am not sure why the answer you expect is Caminar. The fuzzy output is showing the following fuzzy set:

    
    0,250  term: CAMINAR 
    0,067  term: LENTO 
    0,267  term: CORRER 
    0,067  term: TROTAR

    If you take the term with the highest degree to which a term has been activated, the answer would be CORRER, not CAMINAR.

    #1563
    Unknown
    Member

    Hello

    Sorry, I think that the answer should be TROTAR because the centroid is 3.8559826946848017, a value close to the center of the fuzzy set TROTAR. And if I fuzzify 3.8559826946848017,

    velocida_peaton.fuzzify(engine.getOutputValue("velocida_peaton"))

    output:

    0,000/MUYLENTO + 0,000/LENTO + 0,144/CAMINAR + 0,856/TROTAR + 0,000/CORRER + 0,000/CORRERMUCHO

    (The example is here: http://vimeo.com/11262053 )

    And why activatedTerm.getName() doesn’t work in my example?

    Regards

    #1564

    Hi Allison,

    what you have done is correct. You probably do not get anything from activatedTerm because it is a wrapper of another term. Please, do instead activatedTerm.getTerm().getName().

    #1565
    Unknown
    Member

    Thanks, now it works.

    But I still don’t know how to get the output fuzzy set (In this case: TROTAR).

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