JFuzzylite - 连词+建议

时间:2017-02-28 22:37:34

标签: java android logic fuzzy

我对实现模糊逻辑比较陌生。

使用jfuzzylite和来自android传感器的输入(Z轴加速度和从手机gps计算的速度),我想输出驱动程序的行为。

java代码如下;

public void fuzzyMethod(double carspeed, double acceleration){
    Engine engine = new Engine();
    engine.setName("DriverClassifier");

    /*Speed input value
      Speed range set between 0 and 95km/hr
     Three terms - slow, normal, fast
     */
    InputVariable speed = new InputVariable();
    speed.setEnabled(true);
    speed.setName("SPEED");
    speed.setRange(0.000,95.000);
    speed.addTerm(new Triangle("SLOW",0.000,25.000,50.000 ));
    speed.addTerm(new Triangle("NORMAL",40.000,52.500,65.000));
    speed.addTerm(new Triangle("FAST", 55.000, 75.000,95.000));
    engine.addInputVariable(speed);

    /*
    Accelerometer values in z-axis direction - left to right
     */
    InputVariable zaxis = new InputVariable();
    zaxis.setEnabled(true);
    zaxis.setName("ZAXISACCELERATION");
    zaxis.setRange(-4.500,4.500);

    //Deceleration
    zaxis.addTerm(new Triangle("HARDDECELERATION",1.500,3.00,4.500 ));
    zaxis.addTerm(new Triangle("DECELERATION",0.500,1.5,2.5));

    //Calm/Neutral
    zaxis.addTerm(new Triangle("NEUTRAL",-0.5,0,0.5));

    //Acceleration
    zaxis.addTerm(new Triangle("ACCELERATION",-0.500,-1.250,-2.000));
    zaxis.addTerm(new Triangle("HARDACCELERATION",-1.500,-3.000,-4.500));
    engine.addInputVariable(zaxis);


    OutputVariable drivingStyle = new OutputVariable();
    drivingStyle.setName("DRIVINGSTYLE");
        //drivingStyle.setDefuzzifier(new Centroid(200));
    drivingStyle.addTerm(new Triangle("CALM",0,0.5,1));
    drivingStyle.addTerm(new Triangle("NORMAL",1,1.5,2));
    drivingStyle.addTerm(new Triangle("AGGRESSIVE",2, 2.5, 3));
        engine.addOutputVariable(drivingStyle);



    //Set Rules
    RuleBlock ruleBlock = new RuleBlock();
    //SLOW SPEED
    ruleBlock.addRule(Rule.parse("if (SPEED is SLOW) and (ZAXISACCELERATION is HARDDECELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is SLOW) and (ZAXISACCELERATION is DECELERATION) then DRIVINGSTYLE is NORMAL", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is SLOW) and (ZAXISACCELERATION is NEUTRAL) then DRIVINGSTYLE is NORMAL", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is SLOW) and (ZAXISACCELERATION is ACCELERATION) then DRIVINGSTYLE is NORMAL", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is SLOW) and (ZAXISACCELERATION is HARDACCELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));


    //NORMAL SPEED
    ruleBlock.addRule(Rule.parse("if (SPEED is NORMAL) and (ZAXISACCELERATION is HARDDECELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is NORMAL) and (ZAXISACCELERATION is DECELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is NORMAL) and (ZAXISACCELERATION is NEUTRAL) then DRIVINGSTYLE is NORMAL", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is NORMAL) and (ZAXISACCELERATION is ACCELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is NORMAL) and (ZAXISACCELERATION is HARDACCELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));


    //TOO FAST
    ruleBlock.addRule(Rule.parse("if (SPEED is FAST) and (ZAXISACCELERATION is HARDDECELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is FAST) and (ZAXISACCELERATION is DECELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is FAST) and (ZAXISACCELERATION is NEUTRAL) then DRIVINGSTYLE is AGGRESSIVE", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is FAST) and (ZAXISACCELERATION is ACCELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));
    ruleBlock.addRule(Rule.parse("if (SPEED is FAST) and (ZAXISACCELERATION is HARDACCELERATION) then DRIVINGSTYLE is AGGRESSIVE", engine));
    engine.addRuleBlock(ruleBlock);

        speed.setInputValue(carspeed);
        zaxis.setInputValue(acceleration);
        //engine.configure("",  "Minimum","", "Maximum", "Centroid");
        engine.configure("Minimum","Maximum","","","Centroid");
        //ruleBlock.setConjunction(new Minimum());
        //ruleBlock.setDisjunction(new Maximum());
        //ruleBlock.setActivation(new Minimum());
        engine.process();
        Log.d("fuzzy",drivingStyle.getOutputValue()+ " ");
        //FuzzyLite.logger().info(Op.str(drivingStyle.getOutputValue()));
    }

有人可以指导我为实现所需的输出需要做些什么吗?目前,我的输出是D /模糊:NaN。

输入变量基于我在驾驶时获得的值 - 因此定义的范围。

  1. 我是否使用了相关应用程序的正确条款?
  2. 连接,分离和激活值是否正确?
  3. 以下是FLL制作;

    Engine: Driver Classification
    InputVariable: Speed
      enabled: true
      range: 0.000 95.000
      term: Slow Triangle 0.000 25.000 50.000
      term: Normal Triangle 40.000 52.500 65.000
      term: Fast Triangle 55.000 75.500 95.000
    InputVariable: Acceleration
      enabled: true
      range: -4.500 4.500
      term: HardDeceleration Triangle 1.500 3.000 4.500
      term: Deceleration Triangle 0.500 1.500 2.500
      term: Neutral Triangle -0.500 0.000 0.500
      term: Acceleration Triangle -2.000 -1.250 -0.500
      term: HardDeceleration Triangle -4.500 -3.000 -1.500
    OutputVariable: DrivingStyle
      enabled: true
      range: 0.000 1.000
      accumulation: none
      defuzzifier: WeightedSum
      default: nan
      lock-valid: false
      lock-range: false
      term: Calm Triangle 0.000 0.250 0.500
      term: Normal Triangle 0.250 0.500 0.750
      term: Aggressive Triangle 0.500 0.750 1.000
    RuleBlock: 
      enabled: true
      conjunction: Minimum
      disjunction: Maximum
      activation: Minimum
      rule: if Speed is Slow and Acceleration is HardDeceleration then DrivingStyle is Aggressive
      rule: if Speed is Slow and Acceleration is Deceleration then DrivingStyle is Calm
      rule: if Speed is Slow and Acceleration is Neutral then DrivingStyle is Calm
      rule: if Speed is Slow and Acceleration is Acceleration then DrivingStyle is Calm
      rule: if Speed is Slow and Acceleration is HardDeceleration then DrivingStyle is Aggressive
      rule: if Speed is Normal and Acceleration is HardDeceleration then DrivingStyle is Aggressive
      rule: if Speed is Normal and Acceleration is Deceleration then DrivingStyle is Normal
      rule: if Speed is Normal and Acceleration is Neutral then DrivingStyle is Calm
      rule: if Speed is Normal and Acceleration is Acceleration then DrivingStyle is Normal
      rule: if Speed is Normal and Acceleration is HardDeceleration then DrivingStyle is Aggressive
      rule: if Speed is Fast and Acceleration is HardDeceleration then DrivingStyle is Aggressive
      rule: if Speed is Fast and Acceleration is Deceleration then DrivingStyle is Aggressive
      rule: if Speed is Fast and Acceleration is Neutral then DrivingStyle is Aggressive
      rule: if Speed is Fast and Acceleration is Acceleration then DrivingStyle is Aggressive
      rule: if Speed is Fast and Acceleration is HardDeceleration then DrivingStyle is Aggressive
    

    提前致谢!

    此致 史蒂文法鲁吉亚

0 个答案:

没有答案