/// <summary> /// Calculate the Chebyshev measure for the given set of elements. /// </summary> /// <param name="elements"></param> /// <returns></returns> public float Calculate(ICollection <Utility> elements) { var wsum = 0.0f; int count = elements.Count; if (count == 0) { return(0.0f); } foreach (var el in elements) { wsum += el.Weight; } if (CrMath.AeqZero(wsum)) { return(0.0f); } var vlist = new List <float>(count); foreach (var el in elements) { vlist.Add(el.Value * (el.Weight / wsum)); } var ret = vlist.Max <float>(); return(ret); }
/// <summary> /// Chebyshev norm for the given utility list. /// </summary> /// <param name="this">Utility list.</param> public static float Chebyshev(this ICollection <Utility> @this) { if (@this.Count == 0) { return(0.0f); } var wsum = @this.SumWeights(); if (CrMath.AeqZero(wsum)) { return(0.0f); } var vlist = new List <float>(@this.Count); foreach (var util in @this) { var v = util.Value * (util.Weight / wsum); vlist.Add(v); } var ret = vlist.Max <float>(); return(ret); }
/// <summary> /// Calculate the l-p weighted metrics measure for the given set of elements. /// </summary> /// <param name="elements"></param> /// <returns></returns> public float Calculate(ICollection <Utility> elements) { var count = elements.Count; if (count == 0) { return(0.0f); } var wsum = 0.0f; foreach (var el in elements) { wsum += el.Weight; } if (CrMath.AeqZero(wsum)) { return(0.0f); } var vlist = new List <float>(count); foreach (var el in elements) { var v = el.Weight / wsum * (float)Math.Pow(el.Value, _p); vlist.Add(v); } var sum = vlist.Sum(); var res = (float)Math.Pow(sum, _oneOverP); return(res); }
/// <summary> /// Rescales the given value to the interval [0,1]. If the value is smaller than min, it will be /// clamped to min and if the value is larger than max it will be clamped to max. /// WARNING: Never use this extension like this: 1.0.Normalize(min, max), since if the value is negative /// the sign (if there is one) will not be passed to the extension method leading to unexpected results. /// </summary> public static double Normalize01(this double @this, double min, double max) { @this = @this.Clamp <double>(min, max); @this = @this - min; var df = max - min; if (CrMath.AeqZero(df)) { throw new MinEqualMaxException(); } return(@this / df); }
/// <summary> /// Rescales the given value to the interval [0,1]. If the value is smaller than min, it will be /// clamped to min and if the value is larger than max it will be clamped to max. /// WARNING: Never use this extension like this: 1.0f.Normalize01(min, max), since if the value is negative /// the sign (if there is one) will not be passed to the extension method leading to unexpected results. /// </summary> public static float Normalize01(this float @this, float min, float max) { @this = @this.Clamp <float>(min, max); @this = @this - min; var df = max - min; if (CrMath.AeqZero(df)) { throw new MinEqualMaxException(); } return(@this / df); }