The class represents the output of a Fuzzy Inference System.

The class keeps set of rule's output - pairs with the output fuzzy label and the rule's firing strength.

Sample usage:

// linguistic labels (fuzzy sets) that compose the distances FuzzySet fsNear = new FuzzySet( "Near", new TrapezoidalFunction( 15, 50, TrapezoidalFunction.EdgeType.Right ) ); FuzzySet fsMedium = new FuzzySet( "Medium", new TrapezoidalFunction( 15, 50, 60, 100 ) ); FuzzySet fsFar = new FuzzySet( "Far", new TrapezoidalFunction( 60, 100, TrapezoidalFunction.EdgeType.Left ) ); // front distance (input) LinguisticVariable lvFront = new LinguisticVariable( "FrontalDistance", 0, 120 ); lvFront.AddLabel( fsNear ); lvFront.AddLabel( fsMedium ); lvFront.AddLabel( fsFar ); // linguistic labels (fuzzy sets) that compose the angle FuzzySet fsZero = new FuzzySet( "Zero", new TrapezoidalFunction( -10, 5, 5, 10 ) ); FuzzySet fsLP = new FuzzySet( "LittlePositive", new TrapezoidalFunction( 5, 10, 20, 25 ) ); FuzzySet fsP = new FuzzySet( "Positive", new TrapezoidalFunction( 20, 25, 35, 40 ) ); FuzzySet fsVP = new FuzzySet( "VeryPositive", new TrapezoidalFunction( 35, 40, TrapezoidalFunction.EdgeType.Left ) ); // angle LinguisticVariable lvAngle = new LinguisticVariable( "Angle", -10, 50 ); lvAngle.AddLabel( fsZero ); lvAngle.AddLabel( fsLP ); lvAngle.AddLabel( fsP ); lvAngle.AddLabel( fsVP ); // the database Database fuzzyDB = new Database( ); fuzzyDB.AddVariable( lvFront ); fuzzyDB.AddVariable( lvAngle ); // creating the inference system InferenceSystem IS = new InferenceSystem( fuzzyDB, new CentroidDefuzzifier( 1000 ) ); // going straight IS.NewRule( "Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero" ); // turning left IS.NewRule( "Rule 2", "IF FrontalDistance IS Near THEN Angle IS Positive" ); ... // inference section // setting inputs IS.SetInput( "FrontalDistance", 20 ); // getting outputs try { FuzzyOutput fuzzyOutput = IS.ExecuteInference ( "Angle" ); // showing the fuzzy output foreach ( FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList ) { Console.WriteLine( oc.Label + " - " + oc.FiringStrength.ToString( ) ); } } catch ( Exception ) { ... }
Exemplo n.º 1
0
        /// <summary>
        /// Executes the fuzzy inference, obtaining the <see cref="FuzzyOutput"/> of the system for the required
        /// <see cref="LinguisticVariable"/>.
        /// </summary>
        ///
        /// <param name="variableName">Name of the <see cref="LinguisticVariable"/> to evaluate.</param>
        ///
        /// <returns>A <see cref="FuzzyOutput"/> containing the fuzzy output of the system for the
        /// <see cref="LinguisticVariable"/> specified in <paramref name="variableName"/>.</returns>
        ///
        /// <exception cref="KeyNotFoundException">The variable indicated was not found in the database.</exception>
        ///
        public FuzzyOutput ExecuteInference(string variableName)
        {
            // gets the variable
            LinguisticVariable lingVar = database.GetVariable(variableName);

            // object to store the fuzzy output
            FuzzyOutput fuzzyOutput = new FuzzyOutput(lingVar);

            // select only rules with the variable as output
            Rule[] rules = rulebase.GetRules( );
            foreach (Rule r in rules)
            {
                if (r.Output.Variable.Name == variableName)
                {
                    string labelName      = r.Output.Label.Name;
                    float  firingStrength = r.EvaluateFiringStrength( );
                    if (firingStrength > 0)
                    {
                        fuzzyOutput.AddOutput(labelName, firingStrength);
                    }
                }
            }

            // returns the fuzzy output obtained
            return(fuzzyOutput);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Centroid method to obtain the numerical representation of a fuzzy output. The numerical
        /// value will be the center of the shape formed by the several fuzzy labels with their
        /// constraints.
        /// </summary>
        /// 
        /// <param name="fuzzyOutput">A <see cref="FuzzyOutput"/> containing the output of several
        /// rules of a Fuzzy Inference System.</param>
        /// <param name="normOperator">A <see cref="INorm"/> operator to be used when constraining
        /// the label to the firing strength.</param>
        /// 
        /// <returns>The numerical representation of the fuzzy output.</returns>
        /// 
        /// <exception cref="Exception">The numerical output is unavaliable. All memberships are zero.</exception>
        /// 
        public float Defuzzify(FuzzyOutput fuzzyOutput, INorm normOperator)
        {
            // results and accumulators
            float weightSum = 0, membershipSum = 0;

            // speech universe
            float start = fuzzyOutput.OutputVariable.Start;
            float end = fuzzyOutput.OutputVariable.End;

            // increment
            float increment = (end - start) / this.intervals;

            // running through the speech universe and evaluating the labels at each point
            for (float x = start; x < end; x += increment)
            {
                // we must evaluate x membership to each one of the output labels
                foreach (FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList)
                {
                    // getting the membership for X and constraining it with the firing strength
                    float membership = fuzzyOutput.OutputVariable.GetLabelMembership(oc.Label, x);
                    float constrainedMembership = normOperator.Evaluate(membership, oc.FiringStrength);

                    weightSum += x * constrainedMembership;
                    membershipSum += constrainedMembership;
                }
            }

            // if no membership was found, then the membershipSum is zero and the numerical output is unknown.
            if (membershipSum == 0)
                throw new Exception("The numerical output in unavailable. All memberships are zero.");

            return weightSum / membershipSum;
        }
        /// <summary>
        /// Centroid method to obtain the numerical representation of a fuzzy output. The numerical
        /// value will be the center of the shape formed by the several fuzzy labels with their
        /// constraints.
        /// </summary>
        ///
        /// <param name="fuzzyOutput">A <see cref="FuzzyOutput"/> containing the output of several
        /// rules of a Fuzzy Inference System.</param>
        /// <param name="normOperator">A <see cref="INorm"/> operator to be used when constraining
        /// the label to the firing strength.</param>
        ///
        /// <returns>The numerical representation of the fuzzy output.</returns>
        ///
        /// <exception cref="Exception">The numerical output is unavaliable. All memberships are zero.</exception>
        ///
        public float Defuzzify(FuzzyOutput fuzzyOutput, INorm normOperator)
        {
            // results and accumulators
            float weightSum = 0, membershipSum = 0;

            // speech universe
            float start = fuzzyOutput.OutputVariable.Start;
            float end   = fuzzyOutput.OutputVariable.End;

            // increment
            float increment = (end - start) / this.intervals;

            // running through the speech universe and evaluating the labels at each point
            for (float x = start; x < end; x += increment)
            {
                // we must evaluate x membership to each one of the output labels
                foreach (FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList)
                {
                    // getting the membership for X and constraining it with the firing strength
                    float membership            = fuzzyOutput.OutputVariable.GetLabelMembership(oc.Label, x);
                    float constrainedMembership = normOperator.Evaluate(membership, oc.FiringStrength);

                    weightSum     += x * constrainedMembership;
                    membershipSum += constrainedMembership;
                }
            }

            // if no membership was found, then the membershipSum is zero and the numerical output is unknown.
            if (membershipSum == 0)
            {
                throw new Exception("The numerical output in unavailable. All memberships are zero.");
            }

            return(weightSum / membershipSum);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Executes the fuzzy inference, obtaining a numerical output for a choosen output
        /// linguistic variable.
        /// </summary>
        ///
        /// <param name="variableName">Name of the <see cref="LinguisticVariable"/> to evaluate.</param>
        ///
        /// <returns>The numerical output of the Fuzzy Inference System for the choosen variable.</returns>
        ///
        /// <exception cref="KeyNotFoundException">The variable indicated was not found in the database.</exception>
        ///
        public float Evaluate(string variableName)
        {
            // call the defuzzification on fuzzy output
            FuzzyOutput fuzzyOutput = ExecuteInference(variableName);
            float       res         = defuzzifier.Defuzzify(fuzzyOutput, normOperator);

            return(res);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Executes the fuzzy inference, obtaining the <see cref="FuzzyOutput"/> of the system for the required
        /// <see cref="LinguisticVariable"/>. 
        /// </summary>
        /// 
        /// <param name="variableName">Name of the <see cref="LinguisticVariable"/> to evaluate.</param>
        /// 
        /// <returns>A <see cref="FuzzyOutput"/> containing the fuzzy output of the system for the
        /// <see cref="LinguisticVariable"/> specified in <paramref name="variableName"/>.</returns>
        /// 
        /// <exception cref="KeyNotFoundException">The variable indicated was not found in the database.</exception>
        /// 
        public FuzzyOutput ExecuteInference(string variableName)
        {
            // gets the variable
            LinguisticVariable lingVar = database.GetVariable(variableName);

            // object to store the fuzzy output
            FuzzyOutput fuzzyOutput = new FuzzyOutput(lingVar);

            // select only rules with the variable as output
            Rule[] rules = rulebase.GetRules();
            foreach (Rule r in rules)
            {
                if (r.Output.Variable.Name == variableName)
                {
                    string labelName = r.Output.Label.Name;
                    float firingStrength = r.EvaluateFiringStrength();
                    if (firingStrength > 0)
                        fuzzyOutput.AddOutput(labelName, firingStrength);
                }
            }

            // returns the fuzzy output obtained
            return fuzzyOutput;
        }