/// <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; }
/// <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; }
/** * Defuzzify a set using the Sugeno style aproximation function * if it fails it returns -1 (which may be a valid answer) */ /// <summary> /// Defuzzify a set using the Sugeno style aproximation function // if it fails it returns -1 (which may be a valid answer) /// </summary> /// <param name="fs"></param> /// <returns></returns> public static double DeFuzzifySFI(FuzzySet fs) { if (fs == null) { return -1; } if (fs.Invalid()) { fs.Err = 10; return -1; } double sumTop = 0; double sumBottom = 0; double w, s; int i; for (i = 0; i < fs.GetNumPoints(); i++) { w = fs.WorldValue[i]; s = fs.SetValue[i]; sumTop = sumTop + s * w; sumBottom = sumBottom + s; } if (Operations.AEqual(0, sumBottom)) { fs.Err = 15; return -1; } return sumTop / sumBottom; }
/// <summary> /// Performs a SCALE on a Fuzzy Set /// </summary> /// <param name="fs">The Fuzzy Set to Scale</param> /// <param name="val">The scalar amount to Scale the Set</param> /// <returns></returns> public static FuzzySet ScaleFS(FuzzySet fs, double val) { FuzzySet retSet = new FuzzySet(); if (fs == null) { retSet.SetErr(10); return retSet; } retSet = new FuzzySet(fs); if (retSet.Invalid()) { retSet.SetErr(10); return retSet; } int i; for (i = 0; i < retSet.GetNumPoints(); i++) { double w = retSet.GetWorldValue(i); double s = retSet.GetSetValue(i) * val; if (s > 1) s = 1; if (s < 0) s = 0; retSet.AdjustPoint(i, w, s); } return retSet; }
/** * Defuzzify a set using the Mean of Maximums aproximation function * if it fails it returns -1 (which may be a valid answer) */ /// <summary> /// Defizzify a FuzzySet and get its MEAN OF MAXIMUMS /// </summary> /// <param name="fs"></param> /// <returns>Mean of Maximums</returns> public static double DeFuzzifyMOM(FuzzySet fs) { if (fs == null) { return -1; } if (fs.Invalid()) { fs.Err = 10; return -1; } double m = fs.GetMaxSetValue(); double sumTop = 0; double sumBottom = 0; double w; int i; for (i = 0; i < fs.GetNumPoints(); i++) { if (AEqual(fs.SetValue[i], m)) { w = fs.WorldValue[i]; sumTop = sumTop + w; sumBottom = sumBottom + 1; } } if (AEqual(0, sumBottom)) { fs.Err = 15; return -1; } return sumTop / sumBottom; }