Exemple #1
0
    // Applies a set of rules to a given output
    private void ApplyRuleSet(Rule[] rules)
    {
        IFuzzy ruleResults = Fuzzy.Zero();

        Debug.Assert(rules.Length > 0);
        LinguisticVariable output = rules[0].output;

        for (int i = 0; i < rules.Length; ++i)
        {
            Rule r = rules[i];
            // Make sure everything in this rule exists
            if (!r.IsValid())
            {
                Debug.LogWarning("Bad fuzzy rule: " + r.ToString());
                continue;
            }
            // Make sure all of the rules have the same properties, we don't want to mix up our outputs
            Debug.Assert(r.output == output, r.output + "!=" + output);

            // Input to the fuzzy membership is the input's referenced field
            float input = r.input.value;
            // Apply the rule
            IFuzzy ruleStrength = r.ApplyRule(input);
            // Combine this input with the other inputs to the rule
            ruleResults = Fuzzy.Union(ruleResults, ruleStrength);
            // TODO maybe make this so I don't have ~n! copies of rules in memory
        }

        // Defuzzify them
        float crisp = Fuzzy.Defuzzify(ruleResults);

        if (ConfidenceLerp)
        {
            FuzzyOutput confidence = ruleResults.Membership(crisp);
            output.value = Mathf.Lerp(output.value, crisp, confidence);
        }
        else
        {
            output.value = crisp;
        }
    }