home Forums # Technical Support Create engine in Netbeans Java

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1646
    Unknown
    Member

    Hi,

    My name is Roxanne. I am a masters student in Kenya, doing a project on road traffic mgt using traffic lights that are controlled using a fuzzy logic controller. I want to create a FLC engine in Netbeans Java. I have already created it using qtFuzzylite. My issue is I am still new in Java, so i have no idea what i should do with the code import from qtfuzzylite.

    Please i would really appreciate it if you gave me the code i need to achieve this. Below is my code from qtfuzzylite version 4.0 that i want to use to create same engine in Netbeans Java.

    Engine engine = new Engine();
    engine.setName(“Traffic Light Controller”);

    InputVariable inputVariable1 = new InputVariable();
    inputVariable1.setEnabled(true);
    inputVariable1.setName(“TQ”);
    inputVariable1.setRange(0.000, 30.000);
    inputVariable1.addTerm(new Triangle(“Low”, 0.000, 7.500, 15.000));
    inputVariable1.addTerm(new Triangle(“Medium”, 7.500, 15.000, 22.500));
    inputVariable1.addTerm(new Triangle(“High”, 15.000, 22.500, 30.000));
    engine.addInputVariable(inputVariable1);

    InputVariable inputVariable2 = new InputVariable();
    inputVariable2.setEnabled(true);
    inputVariable2.setName(“WT”);
    inputVariable2.setRange(0.000, 6.000);
    inputVariable2.addTerm(new Triangle(“Low”, 0.000, 1.500, 3.000));
    inputVariable2.addTerm(new Triangle(“Medium”, 1.500, 3.000, 4.500));
    inputVariable2.addTerm(new Triangle(“High”, 3.000, 4.500, 6.000));
    engine.addInputVariable(inputVariable2);

    OutputVariable outputVariable = new OutputVariable();
    outputVariable.setEnabled(true);
    outputVariable.setName(“PD”);
    outputVariable.setRange(0.000, 10.000);
    outputVariable.fuzzyOutput().setAccumulation(new EinsteinSum());
    outputVariable.setDefuzzifier(new Centroid(200));
    outputVariable.setDefaultValue(Double.NaN);
    outputVariable.setLockValidOutput(false);
    outputVariable.setLockOutputRange(false);
    outputVariable.addTerm(new Triangle(“Low”, 0.000, 2.500, 5.000));
    outputVariable.addTerm(new Triangle(“Medium”, 2.500, 5.000, 7.500));
    outputVariable.addTerm(new Triangle(“High”, 5.000, 7.500, 10.000));
    engine.addOutputVariable(outputVariable);

    RuleBlock ruleBlock = new RuleBlock();
    ruleBlock.setEnabled(true);
    ruleBlock.setName(“”);
    ruleBlock.setConjunction(new EinsteinProduct());
    ruleBlock.setDisjunction(new EinsteinSum());
    ruleBlock.setActivation(new EinsteinProduct());
    ruleBlock.addRule(Rule.parse(“if TQ is Low and WT is Low then PD is Low”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Low and WT is Medium then PD is Low”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Low and WT is High then PD is Medium”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Medium and WT is Low then PD is Low”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Medium and WT is Medium then PD is Medium”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Medium and WT is High then PD is High”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is High and WT is Low then PD is Medium”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is High and WT is Medium then PD is High”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is High and WT is High then PD is High”, engine));
    engine.addRuleBlock(ruleBlock);

    Thanks in advance.

    Regards Roxanne

    #1647

    Hi Roxanne,

    thank you for your email.

    First, you will need to create a new Java Application project in NetBeans. In the project, you need to add the jfuzzylite.jar file that you can download from http://www.fuzzylite.com/downloads. Once you have done this, the next step is to create your class that will utilize the Fuzzy Engine you have exported from QtFuzzyLite. There are different ways you can achieve this, but an example of your class would be as follows:

    
    public class Roxanne{
        
        private Engine engine;
    
        public MyFuzzyClass(){
            //setup the engine, 
            engine = new Engine();
            engine.setName(“Traffic Light Controller”);
           //and the rest of parameters...
        }
    
       public Engine getEngine(){
          return this.engine;
       }
    
       public static void main(String[] args){
           Roxanne instance = new Roxanne();
           Engine engine = instance.getEngine();
           engine.getInputVariable("TQ").setInputValue(10.0); 
           engine.getInputVariable("WT").setInputValue(2.0);
           engine.process();
           double output = engine.getOutputVariable("PD").defuzzify();
           System.out.println("PD=" + output);
       }
    }
    

    I think this should give you a starting point to run a basic example. The next step is to loop setting different input values and processing the engine (revise example in http://fuzzylite.com/java). Another good advice is to study object oriented programming, which will help you design the classes to satisfy your needs.

    Let me know if this information helps you, or if you need something else.

    Cheers,

    Juan.

    #1648
    Unknown
    Member

    Hi,

    Thank you it has REALLY helped to shed some light. However, please could you explain the “main”code section especially from line three…i understand the first two lines (on object creation and instantiating). When i run the file i get errors; and the errors are on the following lines:

    engine.getInputVariable(“TQ”).setInputValue(10.0);
    engine.getInputVariable(“WT”).setInputValue(2.0);

    double output = engine.getOutputVariable(“PD”).defuzzify();
    System.out.println(“PD=” + output)

    Is it because i need to declare the variables TQ, WT and PD again? because the error that comes up is “cannot find variable…” Also the methods getOutputVariable and getInputVariable bring the same error. Please help. Any help would be greatly appreciated.

    Below is my code in netbeans:

    import com.fuzzylite.Engine;
    import com.fuzzylite.term.Triangle;
    import com.fuzzylite.variable.InputVariable;
    import com.fuzzylite.variable.OutputVariable;
    import com.fuzzylite.defuzzifier.Centroid;
    import com.fuzzylite.rule.RuleBlock;
    import com.fuzzylite.rule.Rule;
    import com.fuzzylite.norm.s.EinsteinSum;
    import com.fuzzylite.norm.t.EinsteinProduct;

    public class Roxanne{

    private Engine engine;

    public Roxanne(){
    //Engine engine = new Engine();
    engine.setName(“Traffic Light Controller”);

    InputVariable inputVariable1 = new InputVariable();
    inputVariable1.setEnabled(true);
    inputVariable1.setName(“TQ”);
    inputVariable1.setRange(0.000, 30.000);
    inputVariable1.addTerm(new Triangle(“Low”, 0.000, 7.500, 15.000));
    inputVariable1.addTerm(new Triangle(“Medium”, 7.500, 15.000, 22.500));
    inputVariable1.addTerm(new Triangle(“High”, 15.000, 22.500, 30.000));
    engine.addInputVariable(inputVariable1);

    InputVariable inputVariable2 = new InputVariable();
    inputVariable2.setEnabled(true);
    inputVariable2.setName(“WT”);
    inputVariable2.setRange(0.000, 6.000);
    inputVariable2.addTerm(new Triangle(“Low”, 0.000, 1.500, 3.000));
    inputVariable2.addTerm(new Triangle(“Medium”, 1.500, 3.000, 4.500));
    inputVariable2.addTerm(new Triangle(“High”, 3.000, 4.500, 6.000));
    engine.addInputVariable(inputVariable2);

    OutputVariable outputVariable = new OutputVariable();
    outputVariable.setEnabled(true);
    outputVariable.setName(“PD”);
    outputVariable.setRange(0.000, 10.000);
    outputVariable.fuzzyOutput().setAccumulation(new EinsteinSum());
    outputVariable.setDefuzzifier(new Centroid(200));
    outputVariable.setDefaultValue(Double.NaN);
    outputVariable.setLockValidOutput(false);
    outputVariable.setLockOutputRange(false);
    outputVariable.addTerm(new Triangle(“Low”, 0.000, 2.500, 5.000));
    outputVariable.addTerm(new Triangle(“Medium”, 2.500, 5.000, 7.500));
    outputVariable.addTerm(new Triangle(“High”, 5.000, 7.500, 10.000));
    engine.addOutputVariable(outputVariable);

    RuleBlock ruleBlock = new RuleBlock();
    ruleBlock.setEnabled(true);
    ruleBlock.setName(“”);
    ruleBlock.setConjunction(new EinsteinProduct());
    ruleBlock.setDisjunction(new EinsteinSum());
    ruleBlock.setActivation(new EinsteinProduct());
    ruleBlock.addRule(Rule.parse(“if TQ is Low and WT is Low then PD is Low”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Low and WT is Medium then PD is Low”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Low and WT is High then PD is Medium”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Medium and WT is Low then PD is Low”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Medium and WT is Medium then PD is Medium”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is Medium and WT is High then PD is High”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is High and WT is Low then PD is Medium”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is High and WT is Medium then PD is High”, engine));
    ruleBlock.addRule(Rule.parse(“if TQ is High and WT is High then PD is High”, engine));
    engine.addRuleBlock(ruleBlock);
    }

    public Engine getEngine(){
    return this.engine;
    }

    public static void main(String[] args){
    Roxanne instance = new Roxanne();
    Engine engine = instance.getEngine();
    engine.getInputVariable("TQ").setInputValue(10.0);
    engine.getInputVariable("WT").setInputValue(2.0);
    engine.process();
    double output = engine.getOutputVariable("PD").defuzzify();
    System.out.println("PD=" + output);

    }
    }

    Thanks in advance for the help.

    #1649

    Hi Roxanne,

    the problem you might be having is in the constructor. Please, make sure the constructor starts as follows.

    
    public Roxanne(){
    this.engine = new Engine();
    engine.setName(“Traffic Light Controller”);
    

    Cheers.

    #1656
    Unknown
    Member

    Hi,

    Thank You so much. I finally managed to set the engine on my netbeans. The problem was at the “main”code section…seems that when i pasted the code directly to my IDE, some things changed due to HTML coding. But i managed to correct the error.

    Concerning the For Loop. I have a few questions. Firstly, what is its function? My supervisor added the following code to my engine and it outputs several values. however i have no clue as to its purpose, because it just gives several outputs for the PD based on the inputs set. Here is the added loop code extract:

    public static void main(String[] args) {
    Roxanne instance = new Roxanne();
    Engine engine = instance.getEngine();
    engine.getInputVariable(“TQ”).setInputValue(13.0);
    engine.getInputVariable(“WT”).setInputValue(4.9);
    for (int j = 0; j < 6; j++) {
    double wt = engine.getInputVariable(“WT”).getMinimum() + j * (engine.getInputVariable(“WT”).range() / 20);
    for (int i = 0; i < 20; ++i) {
    double tq = engine.getInputVariable(“TQ”).getMinimum() + i * (engine.getInputVariable(“TQ”).range() / 20);

    engine.getInputVariable(“TQ”).setInputValue(tq);
    engine.getInputVariable(“WT”).setInputValue(wt);

    engine.process();

    double output = engine.getOutputVariable(“PD”).defuzzify();
    /* FuzzyLite.logger().info(String.format(
    “TQ.input = %s and WT.input=%s-> PD.output = %s”,
    Op.str(tq), Op.str(wt), Op.str(pd.defuzzify())));*/
    System.out.println(“WT=” + wt + “, TQ” + tq + “->PD=” + output);
    }
    }
    }

    Please kindly explain this, especially the FuzzyliteLogger() section and the Op.str section, seeing that our code still run despite us excluding those sections. We used your FOR Loop example to come up with it. There are no errors.

    Thank You in advance.

    #1669

    Hi,

    the for loops are setting the input values for your engine. Initially, the first loop sets the first input variable to different values from WT.minimum to WT.maximum incrementing by WT.range/20. The second loop sets the second variable to different values from TQ.minimum to TQ.maximum incrementing by TQ.range/20. So you have:
    WT = minimum+0*range/20; TQ = minimum+0*range/20; PD=output
    WT = minimum+0*range/20; TQ = minimum+1*range/20; PD=output
    WT = minimum+0*range/20; TQ = minimum+2*range/20; PD=output

    WT = minimum+1*range/20; TQ = minimum+0*range/20; PD=output
    WT = minimum+1*range/20; TQ = minimum+1*range/20; PD=output
    WT = minimum+1*range/20; TQ = minimum+2*range/20; PD=output

    and so on. It sets the inputs of the variables to different combinations so you know the outputs.

    The logger just shows information in console, much like System.out.println.

    #2774
    Unknown
    Member

    Hi,
    I am trying to implement a fuzzy controller using fuzzylite and i will like to print the output or write it to a database after deffuzification. Going through the above code i saw that the person passed the void method deffuzify() to a double and that should actually give an error. so can you help with a better method of collecting or storing the output variable after deffuzification.
    Thanks

    #2776

    Hi,

    thank you for your post and question.

    In previous versions of fuzzylite (4.0, I think), the defuzzification was computed when defuzzify() was called. This behaviour changed in (5.0, I think) so now defuzzify computes the defuzzification as stores it as a value. Hence, defuzzify() computes defuzzification, returns void, and you can access the defuzzified value using getValue().

    Hope this answers your question, else please feel free to post again.

    Cheers.

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