static void RunFinalTests(CandidateSolution <double, StateData> candidate, out double finalScore, out string testDetails) { var sampleData = new Dataset(); StringBuilder sb = new StringBuilder(); sb.AppendLine("Test results at " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString()); // run through our test data and see how close the answer the genetic program // comes up with is to the training data. Lower fitness scores are better than bigger scores double totalDifference = 0; while (true) { var row = sampleData.GetRowOfTestingData(); if (row == null) { break; } // populate vars with values from row of the training data for (int i = 0; i < Dataset.NumColumns; i++) { if (i != Dataset.LabelColumnIndex) { candidate.SetVariableValue("v" + i, row[i]); } } var result = candidate.Evaluate(); // now figure the difference between the calculated value and the training data var actualAnswer = row[Dataset.LabelColumnIndex]; var diff = result - actualAnswer; totalDifference += diff * diff; Console.WriteLine("Ans: " + actualAnswer.ToString(" 0") + " AI: " + result.ToString("0.00")); sb.AppendLine(actualAnswer.ToString("0.0") + ", " + result.ToString("0.000")); } totalDifference = Math.Sqrt(totalDifference); finalScore = (float)totalDifference; sb.AppendLine("\"Final score\", " + finalScore.ToString("0.000")); testDetails = sb.ToString(); }
static float EvaluateCandidate(CandidateSolution <double, StateData> candidate) { var sampleData = new Dataset(); // run through our test data and see how close the answer the genetic program // comes up with is to the training data. Lower fitness scores are better than bigger scores double totalDifference = 0; while (true) { var row = sampleData.GetRowOfTrainingData(); if (row == null) { break; } // populate vars with values from row of the training data, and then get the calculated value for (int i = 0; i < Dataset.NumColumns; i++) { if (i != Dataset.LabelColumnIndex) { candidate.SetVariableValue("v" + i, row[i]); } } var result = candidate.Evaluate(); // now figure the difference between the calculated value and the training data var actualAnswer = row[Dataset.LabelColumnIndex]; var diff = result - actualAnswer; totalDifference += diff * diff; } // sqrt of the summed squared diffences totalDifference = Math.Sqrt(totalDifference); // fitness function returns a float return((float)totalDifference); }