/// <summary> /// return the power of differences between problem feature and solution feature /// note that now only support numberic feature type /// </summary> /// <param name="problem"></param> /// <param name="solution"></param> /// <returns></returns> public virtual double Compute(Feature problem, Feature solution) { if (problem.GetFeatureType() != solution.GetFeatureType()) { //throw exception } int featureType = problem.GetFeatureType(); double diff = 0; if (featureType == FeatureType.TYPE_FEATURE_BOOL) { diff = TypeHandle.DoType((System.Boolean)Convert.ToBoolean(problem.GetFeatureValue()), (System.Boolean)Convert.ToBoolean(solution.GetFeatureValue())); } else if (featureType == FeatureType.TYPE_FEATURE_FLOAT) { diff = TypeHandle.DoType((System.Double)Convert.ToDouble(problem.GetFeatureValue().ToString()), (System.Double)Convert.ToDouble(solution.GetFeatureValue().ToString())); } else if (featureType == FeatureType.TYPE_FEATURE_IMAGE) { } else if (featureType == FeatureType.TYPE_FEATURE_INT) { diff = TypeHandle.DoType( (System.Int32)Convert.ToInt32(problem.GetFeatureValue().ToString()), (System.Int32)Convert.ToInt32(solution.GetFeatureValue().ToString())); } else if (featureType == FeatureType.TYPE_FEATURE_CATEGORICAL) { diff = TypeHandle.DoType( problem.GetFeatureValue().ToString(), solution.GetFeatureValue().ToString()); } else if (featureType == FeatureType.TYPE_FEATURE_Ordinal) { diff = TypeHandle.DoType((System.Double)Convert.ToDouble(problem.GetFeatureValue().ToString()), (System.Double)Convert.ToDouble(solution.GetFeatureValue().ToString())); int y = Convert.ToInt32(problem.GetFeatureUnit()) - 1; diff = Math.Pow(diff, 2) / y; return(diff); } else if (featureType == FeatureType.TYPE_FEATURE_MSTRING) { } else if (featureType == FeatureType.TYPE_FEATURE_STRING) { string a = problem.GetFeatureValue().ToString().Trim(); string b = solution.GetFeatureValue().ToString().Trim(); if (String.IsNullOrEmpty(a) || String.IsNullOrEmpty(b)) { return(0); } char[] separators = { ' ' }; string[] a_array, b_array; a_array = a.Split(separators); b_array = b.Split(separators); if (a_array[0].ToUpper().Trim() == b_array[0].ToUpper().Trim()) { diff = 0; } else { diff = 1; } return(diff); /*double percent_similar_words=0; * * string a = problem.GetFeatureValue().ToString().Trim(); * string b = solution.GetFeatureValue().ToString().Trim(); * if (String.IsNullOrEmpty(a) || String.IsNullOrEmpty(b)) return 0; * char[] separators ={' '}; * string[] a_array, b_array; * a_array = a.Split(separators); * b_array = b.Split(separators); * for (int i = 0; i < a_array.Length; i++) * for (int j = 0; j < b_array.Length; j++) * if (a_array[i].ToUpper().Trim() == b_array[j].ToUpper().Trim()) * { percent_similar_words++; break; } * if (percent_similar_words == 0) percent_similar_words = 1; * else * if (a_array.Length > b_array.Length) * percent_similar_words = percent_similar_words/a_array.Length; * else * percent_similar_words = percent_similar_words / b_array.Length; * diff = percent_similar_words; * return diff; * * /* * string a = problem.GetFeatureValue().ToString().Trim(); * string b = solution.GetFeatureValue().ToString().Trim(); * if (String.IsNullOrEmpty(a) || String.IsNullOrEmpty(b)) return 0; * int lengthA = a.Length; * int lengthB = b.Length; * var distances = new int[lengthA + 1, lengthB + 1]; * for (int i = 0; i <= lengthA; distances[i, 0] = i++) ; * for (int j = 0; j <= lengthB; distances[0, j] = j++) ; * * for (int i = 1; i <= lengthA; i++) * for (int j = 1; j <= lengthB; j++) * { * int cost = b[j - 1] == a[i - 1] ? 0 : 1; * distances[i, j] = Math.Min * ( * Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1), * distances[i - 1, j - 1] + cost * ); * } * diff= distances[lengthA, lengthB]; * return diff;*/ } else if (featureType == FeatureType.TYPE_FEATURE_UNDEFINED) { } else { //throw exception } return(Math.Pow(diff, 2)); }