static void Main(string[] args) { // Initialize the case base org.opencbr.core.engine.DefaultEngine _engine = new org.opencbr.core.engine.DefaultEngine(); string _env = @"D:\projects\opencbr\sample\workspace\config.xml"; _engine.SetEnvironmentVariable(_env); // Initialize the problem Case _problem = new Case(1, "cooling", "cooling problem"); _problem.AddFeature("speed", FeatureType.TYPE_FEATURE_FLOAT, 19.0, 0.3, false, false); _problem.AddFeature("temp", FeatureType.TYPE_FEATURE_FLOAT, 44.0, 0.7, false, false); ////initialize to 51 or 44 to demo retrieval of closest cases (3 versus 4) from Database _problem.AddFeature("quality", FeatureType.TYPE_FEATURE_FLOAT, 232.0, 1.0, false, false); _engine.SetProblem(_problem); // Run the reasoning engine _engine.Startup(); _engine.Run(); // Print the result of reasoning org.opencbr.core.context.ICBRContext ctx = _engine.GetCBRContext(); org.opencbr.core.express.Case c = ctx.GetSolutionCase(); Console.WriteLine(c); }
/// <summary> /// compute the similarity between problem and solution /// throw exception NoSupportTypeException /// </summary> /// <param name="problem"></param> /// <param name="solution"></param> /// <returns>the value of similarity</returns> public double Compare(org.opencbr.core.express.Case problem, org.opencbr.core.express.Case solution) { if (problem == null || solution == null) { return(0); } double totalSimilarity = 0; ArrayList problemFeatures = problem.GetFeatures(); ArrayList solutionFeatures = solution.GetFeatures(); if (problemFeatures != null && solutionFeatures != null && problemFeatures.Count > 0 && solutionFeatures.Count > 0 && problemFeatures.Count == solutionFeatures.Count) { int length = problemFeatures.Count; for (int i = 0; i < length; i++) { Feature problemf = (Feature)problemFeatures[i]; string problemFeatureName = problemf.GetFeatureName(); if (problemFeatureName == null || problemFeatureName.Length <= 0) { //throw exception } for (int j = 0; j < length; j++) { Feature solutionf = (Feature)solutionFeatures[j]; string solutionFeatureName = solutionf.GetFeatureName(); if (solutionFeatureName == null || solutionFeatureName.Length <= 0) { //throw exception } //compute the similarity if same feature name and same weight //and not key if (problemFeatureName.Equals(solutionFeatureName) && problemf.GetWeight() == solutionf.GetWeight() && problemf.GetIsKey() == false && solutionf.GetIsKey() == false && problemf.GetFeatureType() == solutionf.GetFeatureType() ) { double weight = problemf.GetWeight(); double diff = Math.Pow(weight, 2) * Compute(problemf, solutionf); if (Version.DEBUG) { System.Console.WriteLine(problemf.GetFeatureName() + "'s weight: " + weight + "\n\tdata: " + problemf.GetFeatureValue() + "\t" + solutionf.GetFeatureValue()); } totalSimilarity += diff; } } } } else { //throw exception } double distance = Math.Sqrt(totalSimilarity); double result = 1 / (1 + _alpha * distance); if (Version.DEBUG) { System.Console.WriteLine("similarity is " + result); //System.Console.WriteLine("alpha is " + _alpha); } return(result); }