Ejemplo n.º 1
0
        /// <summary>
        /// FUZZY 'AND' RULE
        /// 
        /// "IF 
        ///     val1 is a member of fs1, 
        /// AND
        ///     val2 is a member of fs2
        /// THEN 
        ///     fs3 IS fs4
        /// </summary>
        /// <param name="val1">Antecedent 1</param> 
        /// <param name="fs1">Antecedent Set 1</param>
        /// <param name="val2">Antecedent 2</param>
        /// <param name="fs2">Antecedent Set 2</param>
        /// <param name="fs3">Consequent Output</param>
        /// <param name="fs4">Consequent</param>
        /// <param name="ftemp">Rule Applicability Output Set</param>
        public static FuzzySet AND(double val1, FuzzySet fs1, double val2, FuzzySet fs2, ref FuzzySet fs3, FuzzySet fs4,
                               FuzzySet ruleSet)
        {
            double lowRange = fs4.GetLowRange();
            double highRange = fs4.GetHighRange();

            //... and of the Consequent Output...
            string consName = fs3.Id;
            Brush consColour = fs3.LineColour;

            FuzzySet ftemp = new FuzzySet(ruleSet);
            ftemp.Clear();
            ftemp.SetRangeWithPoints(lowRange, highRange);

            double fval1 = fs1.Fuzzify(val1);
            double fval2 = fs2.Fuzzify(val2);

            double membership = Math.Min(fval1, fval2); //MIN for AND

            if (membership > 0)
            {
                FuzzySet oldRule = new FuzzySet(ftemp);

                ftemp = new FuzzySet(Operations.ScaleFS(fs4, membership));
                ftemp.LineColour = ruleSet.LineColour;
                ftemp.Id = ruleSet.Id;

                if (ftemp.GetNumPoints() < 2)
                    ftemp = oldRule;

                fs3 = new FuzzySet(Operations.UnionFS(fs3, ftemp, consColour));
                fs3.Id = consName;

            }

            return ftemp;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// FUZZY 'IS' RULE
        /// 
        /// "IF 
        ///     val1 is a member of fs1, 
        /// THEN 
        ///     fs3 IS fs4
        /// </summary>
        /// <param name="val1">Antecedent 1</param> 
        /// <param name="fs1">Antecedent Set 1</param>
        /// <param name="fs3">Consequent Output</param>
        /// <param name="fs4">Consequent</param>
        /// <param name="ftemp">Rule Applicability Output Set</param>
        public static FuzzySet IS(double val1, FuzzySet fs1, ref FuzzySet fs3, FuzzySet fs4,
                                FuzzySet ruleSet)
        {
            double lowRange = fs4.GetLowRange();
            double highRange = fs4.GetHighRange();

            string consName = fs3.Id;
            Brush consColour = fs3.LineColour;

            FuzzySet ftemp = new FuzzySet(ruleSet);
            ftemp.Clear();
            ftemp.SetRangeWithPoints(lowRange, highRange);

            double membership = fs1.Fuzzify(val1);

            if (membership > 0)
            {
                ftemp = Operations.ScaleFS(fs4, membership);
                ftemp.LineColour = ruleSet.LineColour;
                ftemp.Id = ruleSet.Id;

                fs3 = Operations.UnionFS(fs3, ftemp, ruleSet.LineColour);
                fs3.Id = consName;
                fs3.LineColour = consColour;
            }

            return ftemp;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Defizzify a FuzzySet and get its CENTER OF GRAVITY
        /// </summary>
        /// <param name="fs">Input Set</param>
        /// <returns>Center of Gravity</returns>
        public static double DeFuzzifyCOG(FuzzySet fs)
        {
            if (fs == null) { return -1; }

            int points = (int)(((double)fs.GetNumPoints()) * 1.9 + 1);

            if (points < 21) points = 21;

            if (points < 4 || points > 1000) { return -1; }
            if (fs.Invalid()) { fs.Err = 10; return -1; }
            double sumTop = 0;
            double sumBottom = 0;
            double w, s, r, step;
            r = fs.HighRange - fs.LowRange;
            step = r / (points - 1);
            int i;
            for (i = 0; i < points; i++)
            {
                w = fs.LowRange + step * i;
                s = fs.Fuzzify(w);
                sumTop = sumTop + s * w;
                sumBottom = sumBottom + s;
            }
            if (AEqual(0, sumBottom)) { fs.Err = 15; return -1; }

            return sumTop / sumBottom;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Performs a UNION of two Fuzzy Sets
        /// </summary>
        /// <param name="fs1">Fuzzy Set 1</param>
        /// <param name="fs2">Fuzzy Set 2</param>
        /// <param name="colour">The colour to render the result</param>
        /// <returns>Result Set</returns>
        public static FuzzySet UnionFS(FuzzySet fs1, FuzzySet fs2, Brush colour)
        {
            FuzzySet result = new FuzzySet(); // just for error checking and returning

            //verify...
            if (fs1 == null || fs2 == null || fs1.Invalid() || fs2.Invalid())
            {
                result.SetErr(10);
                return result;
            }

            if (!RangeEqual(fs1, fs2))
            {
                result.SetErr(11); return result;
            }

            result = new FuzzySet(fs1);

            result.Clear();

            //lets get to work
            result.SetRange(fs1.GetLowRange(), fs1.GetHighRange());

            double x = 0, y = 0;
            double fs1x1, fs1x2, fs1y1, fs1y2;
            double fs2x3, fs2x4, fs2y3, fs2y4;
            double fs1s, fs1w, fs2s, fs2w;
            int i, k, rc;

            // traverse fs1 add all its points above fs2
            for (i = 0; i < fs1.GetNumPoints(); i++)
            {
                fs1w = fs1.GetWorldValue(i);
                fs1s = fs1.GetSetValue(i);
                fs2s = fs2.Fuzzify(fs1w);
                if (fs1s > fs2s || Operations.AEqual(fs1s, fs2s))
                {
                    result.AddPoint(fs1w, fs1s, false, false);
                }
            }

            // traverse fs2 add all its points above fs1
            for (i = 0; i < fs2.GetNumPoints(); i++)
            {
                fs2w = fs2.GetWorldValue(i);
                fs2s = fs2.GetSetValue(i);
                fs1s = fs1.Fuzzify(fs2w);
                if (fs2s > fs1s)
                {
                    result.AddPoint(fs2w, fs2s, false, false);
                }
            }

            // now traverse all line segments in fs1 testing for intersection with fs2
            for (i = 1; i < fs1.GetNumPoints(); i++)
            {
                fs1x1 = fs1.GetWorldValue(i - 1);
                fs1y1 = fs1.GetSetValue(i - 1);
                fs1x2 = fs1.GetWorldValue(i);
                fs1y2 = fs1.GetSetValue(i);
                for (k = 1; k < fs2.GetNumPoints(); k++)
                {
                    fs2x3 = fs2.GetWorldValue(k - 1);
                    fs2y3 = fs2.GetSetValue(k - 1);
                    fs2x4 = fs2.GetWorldValue(k);
                    fs2y4 = fs2.GetSetValue(k);

                    rc = Intersect(ref x, ref y,
                       fs1x1, fs1y1, fs1x2, fs1y2,
                       fs2x3, fs2y3, fs2x4, fs2y4);

                    if (rc == 0)
                    {
                        result.AddPoint(x, y, false, false);
                    }
                }
            }
            result.ColapseSet();

            result.LineColour = colour;

            return result;
        }