home › Forums › # Technical Support › Create engine in Netbeans Java › Reply To: Create engine in Netbeans Java
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.