Package net.sourceforge.jFuzzyLogic

Examples of net.sourceforge.jFuzzyLogic.FunctionBlock


    System.out.println("Begin TestTipperJava");

    FIS fis = new FIS();

    // FUNCTION_BLOCK tipper
    FunctionBlock functionBlock = new FunctionBlock(fis);
    fis.addFunctionBlock("tipper", functionBlock);

    //    VAR_INPUT             
    //       service : REAL;
    //       food : REAL
    //    END_VAR

    Variable service = new Variable("service");
    Variable food = new Variable("food");
    functionBlock.setVariable(service.getName(), service);
    functionBlock.setVariable(food.getName(), food);

    //    VAR_OUTPUT
    //       tip : REAL;
    //    END_VAR

    Variable tip = new Variable("tip");
    functionBlock.setVariable(tip.getName(), tip);

    //    FUZZIFY service
    //       TERM poor := (0, 1) (4, 0) ;
    //       TERM good := (1, 0) (4,1) (6,1) (9,0);
    //       TERM excellent := (6, 0) (9, 1);
    //    END_FUZZIFY

    Value poorX[] = { new Value(0), new Value(4) };
    Value poorY[] = { new Value(1), new Value(0) };
    MembershipFunction poor = new MembershipFunctionPieceWiseLinear(poorX, poorY);

    MembershipFunction good = new MembershipFunctionTrapetzoidal(new Value(1), new Value(4), new Value(6), new Value(9));

    Value exX[] = { new Value(6), new Value(9), new Value(10) };
    Value exY[] = { new Value(0), new Value(1), new Value(1) };
    MembershipFunction excellent = new MembershipFunctionPieceWiseLinear(exX, exY);

    LinguisticTerm ltPoor = new LinguisticTerm("poor", poor);
    LinguisticTerm ltGood = new LinguisticTerm("good", good);
    LinguisticTerm ltExcellent = new LinguisticTerm("excellent", excellent);

    service.add(ltPoor);
    service.add(ltGood);
    service.add(ltExcellent);

    //    FUZZIFY food
    //       TERM rancid := (0, 1) (1, 1) (3,0) ;
    //       TERM delicious := (7,0) (9,1) (10,1);
    //    END_FUZZIFY

    Value ranX[] = { new Value(0), new Value(1), new Value(3) };
    Value ranY[] = { new Value(1), new Value(1), new Value(0) };
    MembershipFunction rancid = new MembershipFunctionPieceWiseLinear(ranX, ranY);

    Value delX[] = { new Value(7), new Value(9), new Value(10) };
    Value delY[] = { new Value(0), new Value(1), new Value(1) };
    MembershipFunction delicious = new MembershipFunctionPieceWiseLinear(delX, delY);

    LinguisticTerm ltRancid = new LinguisticTerm("rancid", rancid);
    LinguisticTerm ltDelicious = new LinguisticTerm("delicious", delicious);

    food.add(ltRancid);
    food.add(ltDelicious);

    //    DEFUZZIFY tip
    //       TERM cheap := (0,0) (5,1) (10,0);
    //       TERM average := (10,0) (15,1) (20,0);
    //       TERM generous := (20,0) (25,1) (30,0);
    //       METHOD : COG;
    //       DEFAULT := 0;
    //    END_DEFUZZIFY

    MembershipFunction cheap = new MembershipFunctionTriangular(new Value(0), new Value(5), new Value(10));
    MembershipFunction average = new MembershipFunctionTriangular(new Value(10), new Value(15), new Value(20));
    MembershipFunction generous = new MembershipFunctionTriangular(new Value(20), new Value(25), new Value(30));

    LinguisticTerm ltCheap = new LinguisticTerm("cheap", cheap);
    LinguisticTerm ltAverage = new LinguisticTerm("average", average);
    LinguisticTerm ltGenerous = new LinguisticTerm("generous", generous);

    tip.add(ltCheap);
    tip.add(ltAverage);
    tip.add(ltGenerous);

    tip.setDefuzzifier(new DefuzzifierCenterOfGravity(tip));

    //    RULEBLOCK No1
    //       ACCU : MAX;
    //       AND : MIN;
    //       ACT : MIN;
    RuleBlock ruleBlock = new RuleBlock(functionBlock);
    ruleBlock.setName("No1");
    ruleBlock.setRuleAccumulationMethod(new RuleAccumulationMethodMax());
    ruleBlock.setRuleActivationMethod(new RuleActivationMethodMin());

    //       RULE 1 : IF service IS poor OR food is rancid THEN tip IS cheap;
    Rule rule1 = new Rule("Rule1", ruleBlock);
    rule1.addAntecedent(service, "poor", false);
    rule1.addAntecedent(food, "rancid", false);
    rule1.addConsequent(tip, "cheap", false);
    ruleBlock.add(rule1);

    //       RULE 2 : IF service IS good THEN tip IS average;
    Rule rule2 = new Rule("Rule2", ruleBlock);
    rule2.addAntecedent(service, "good", false);
    rule2.addConsequent(tip, "average", false);
    ruleBlock.add(rule2);

    //       RULE 3 : IF ((service IS good) OR (service IS excellent)) AND food IS delicious THEN tip is generous;
    Rule rule3 = new Rule("Rule3", ruleBlock);
    RuleTerm term1 = new RuleTerm(service, "good", false); // Create 'terms'
    RuleTerm term2 = new RuleTerm(service, "excellent", false);
    RuleTerm term3 = new RuleTerm(food, "delicious", false);

    RuleExpression antecedentOr = new RuleExpression(term1, term2, new RuleConnectionMethodOrMax()); // Combine terms using connection methods: OR, AND
    RuleExpression antecedentAnd = new RuleExpression(antecedentOr, term3, new RuleConnectionMethodAndMin());
    rule3.setAntecedents(antecedentAnd); // Set antecedent

    rule3.addConsequent(tip, "generous", false);
    ruleBlock.add(rule3);

    //    END_RULEBLOCK
    //
    //    END_FUNCTION_BLOCK
    HashMap<String, RuleBlock> ruleBlocksMap = new HashMap<String, RuleBlock>();
    ruleBlocksMap.put(ruleBlock.getName(), ruleBlock);
    functionBlock.setRuleBlocks(ruleBlocksMap);

    //---
    // Show generated FIS (FCL) and show animation
    //---
    System.out.println(fis);
View Full Code Here


        "END_RULEBLOCK\n" + //
        "\n" + //
        "END_FUNCTION_BLOCK\n";

    FIS fis = FIS.createFromString(fcl, true);
    FunctionBlock functionBlock = fis.getFunctionBlock("tipper");
    functionBlock.chart();

    // Set inputs
    functionBlock.setVariable("service", 3);
    functionBlock.setVariable("food", 7);
   
    // Evaluate fuzzy set
    functionBlock.evaluate();

    // Show output variable's chart
    functionBlock.getVariable("tip").chartDefuzzifier(true);

    // Print ruleSet
    System.out.println(functionBlock);
   
    System.out.println("End TestTipperString");
View Full Code Here

      System.err.println("Can't load file: '" + fileName + "'");
      return;
    }

    // Show ruleset
    FunctionBlock functionBlock = fis.getFunctionBlock(null);
    functionBlock.chart();

    // Set inputs
    functionBlock.setVariable("service", 3);
    functionBlock.setVariable("food", 7);

    // Evaluate
    functionBlock.evaluate();

    // Show output variable's chart
    functionBlock.getVariable("tip").chartDefuzzifier(true);

    // Print ruleSet
    System.out.println(functionBlock);
  }
View Full Code Here

  @Override
  protected void init() {
    if( fis == null ) { return; }
    functionBlock = fis.getFunctionBlock(null);
    variables.clear();
    FunctionBlock block = fis.getFunctionBlock(null);

    for( Variable x : block.variables() )
      variables.add(x);
    ((GenericModel) model).setVariables(variables);
  }
View Full Code Here

  @Override
  public double evaluate(RuleBlock ruleBlock) {
    double error = 0;

    FunctionBlock functionBlock = ruleBlock.getFunctionBlock();

    // Good parameters combination
    ruleBlock.setVariable("city", 1); // Pick one city
    ruleBlock.setVariable("occupation_type", 10); // Good ocupation type
    ruleBlock.setVariable("scoring_partner", -10); // No partner

    // Bad parameters combination
    // fuzzyRuleSet.setVariable("city", 2000); // Other city
    // fuzzyRuleSet.setVariable("occupation_type", 1); // Bad ocupation type

    // Caculate for each table's element (score and income)
    for( int scoreInd = 0; scoreInd < scoreXL.length; scoreInd++ ) {
      double score = scoreXL[scoreInd];
      ruleBlock.setVariable("scoring", score);
      for( int incomeInd = 0; incomeInd < incomeXL.length; incomeInd++ ) {
        double income = incomeXL[incomeInd];
        ruleBlock.setVariable("sel", income);

        // Evaluate fuzzy system
        functionBlock.evaluate();

        // get output
        double credLimMul = ruleBlock.getVariable("credLimMul").getLatestDefuzzifiedValue();
        if( debug ) {
          for( Variable var : ruleBlock.getFunctionBlock().variables() )
View Full Code Here

    //---
    // Load FIS (Fuzzy Inference System)
    //---
    FIS fis = FIS.load("fcl/qualify.fcl");
    FunctionBlock functionBlock = fis.getFunctionBlock(null);
    // functionBlock.chart();
    RuleBlock ruleBlock = functionBlock.getFuzzyRuleBlock(null);

    //---
    // Create a list of parameter to optimize
    //---
    ArrayList<Parameter> parameterList = new ArrayList<Parameter>();
    // Add variables.
    // Note: Fuzzy sets' parameters for these (scoring and credLimMul) variables will be optimized
    Parameter.parameterListAddVariable(parameterList, ruleBlock.getVariable("scoring"));
    Parameter.parameterListAddVariable(parameterList, ruleBlock.getVariable("credLimMul"));

    // Add every rule's weight
    for( Rule rule : ruleBlock.getRules() ) {
      Parameter.parameterListAddRule(parameterList, rule);
    }

    //---
    // Create an error function to be optimized (i.e. minimized)
    //---
    ErrorFunctionQualifyDemo errorFunction = new ErrorFunctionQualifyDemo();

    //---
    // Optimize (using 'Delta jump optimization')
    //---
    OptimizationDeltaJump optimizationDeltaJump = new OptimizationDeltaJump(ruleBlock, errorFunction, parameterList);
    optimizationDeltaJump.setMaxIterations(5); // Number optimization of iterations
    optimizationDeltaJump.optimize(true);

    //---
    // Save optimized fuzzyRuleSet to file
    //---
    System.out.println(functionBlock);
    Gpr.toFile("fcl/qualify_optimized.fcl", functionBlock.toString());

    System.out.println("ParameterOptimizationDemo: End");
  }
View Full Code Here

  @Override
  public void paintComponent(Graphics g) {

    if( fis != null ) {
      // How many variables do we need to plot?
      FunctionBlock fb = fis.getFunctionBlock(null);
      if( fb != null ) {
        // Initialize plot sizes and positions
        posx = posy = 0;
        int w = getWidth(), h = getHeight();
        int vars = fb.getVariables().size();
        int nx = (int) Math.sqrt(vars);
        int ny = vars / nx;
        if( (nx * ny - vars) < 0 ) ny++;
        int plotSizex = w / nx;
        int plotSizey = h / ny;
        Rectangle2D rect = new Rectangle2D.Double();

        // Plot each variable
        for( Variable var : fb.variablesSorted() ) {
          // Rectangle2D rect = new Rectangle2D.Double(posx, posy, plotSizex, plotSizey); // Define where this variable should be plotted
          rect.setFrame(posx, posy, plotSizex, plotSizey);

          JFreeChart chart = var.chart(false);
          chart.draw((Graphics2D) g, rect);
View Full Code Here

    if( fis == null ) { // Error while loading?
      System.err.println("Can't load file: '" + fileName + "'");
      return null;
    }

    FunctionBlock functionBlock = fis.getFunctionBlock(null);

    // Time recording
    List<Double> timeRecords = new ArrayList<Double>();
    long startTime;
    int curStep = 0;

    // Test
    for( int i = 0; i <= steps; i++, curStep += stepSize ) {
      startTime = System.currentTimeMillis();

      for( int j = 0; j <= curStep; j++ ) {
        // Set inputs
        for( int k = 0; k < inputVariables.length; k++ )
          functionBlock.setVariable(inputVariables[k], Math.random() * 5);

        // Evaluate fuzzy set
        functionBlock.evaluate();
      }
      timeRecords.add(new Double(System.currentTimeMillis() - startTime));
      if( debug ) Gpr.debug("Evaluate " + fileName + "\ti:" + i + "\tcurStep: " + curStep);
    }
    return timeRecords;
View Full Code Here

TOP

Related Classes of net.sourceforge.jFuzzyLogic.FunctionBlock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.