/// <summary> /// sort by bi-sort algorithm /// </summary> /// <param name="stats"></param> /// <param name="s"></param> public void SortSimilarity(ArrayList stats, IStat stat) { int len = stats.Count; int low = 0; int high = len - 1; int mid = 0; double similarity = stat.GetCaseSimilarity(); #region only for test // if (Version.DEBUG) // { // System.Console.WriteLine(" init stats:"); // for(int i = 0; i < stats.Count; i++) // { // IStat s = (IStat)stats[i]; // System.Console.WriteLine(s.GetCaseSimilarity()); // } // } #endregion while (low <= high) { mid = (low + high) / 2; if (Version.DEBUG) { System.Console.WriteLine("low:" + low + " mid:"+ mid + " high:"+ high); } IStat s = (IStat)stats[mid]; if (s.GetCaseSimilarity() > similarity) { low = mid + 1; } else { high = mid - 1; } } stats.Insert(high + 1, stat); #region only for test if (Version.DEBUG) { System.Console.WriteLine("result stats:"); for (int i = 0; i < stats.Count; i++) { IStat s = (IStat)stats[i]; System.Console.WriteLine(s.GetCaseSimilarity()); } } #endregion }
public object ComputeMeanValue(ArrayList stats, string featureName) { double sum = 0; double totalSimilarity = 0; for (int i = 0; i < stats.Count; i++) { IStat s = (IStat)stats[i]; Case c = s.GetCBRCase(); if (c != null) { Feature f = c.GetFeature(featureName); if (f != null) { if (f.GetFeatureType() == FeatureType.TYPE_FEATURE_FLOAT) { sum += (double)f.GetFeatureValue() * s.GetCaseSimilarity(); totalSimilarity += s.GetCaseSimilarity(); } else if (f.GetFeatureType() == FeatureType.TYPE_FEATURE_INT) { sum += (int)f.GetFeatureValue() * s.GetCaseSimilarity(); totalSimilarity += s.GetCaseSimilarity(); } else { throw new Exception("ComputeMeanValue method exception:unsupported type"); } } } } if (totalSimilarity == 0) { throw new Exception("ComputeMeanValue method exception"); } return(sum / totalSimilarity); }
[Test] public void testSortSimilarity3() { m.SortSimilarity(list, stat3); Assert.AreEqual(stat3.GetCaseSimilarity(), ((IStat)list[0]).GetCaseSimilarity()); }