00001 using System; 00002 using System.Collections.Generic; 00003 using System.IO; 00004 using System.Linq; 00005 00006 namespace SpaceFlight.Simulation 00007 { 00008 public class Evaluator 00009 { 00010 private List<TestCase> testCases; 00011 00012 public Evaluator(string path) 00013 { 00014 testCases = new List<TestCase>(); 00015 foreach (string filename in Directory.EnumerateFiles(path)) 00016 { 00017 try 00018 { 00019 testCases.Add(TestCase.Load(filename)); 00020 } 00021 catch 00022 { 00023 throw new Exception("Invalid test case file: " + filename); 00024 } 00025 } 00026 00027 if (testCases.Count == 0) 00028 { 00029 throw new Exception("No test cases loaded!"); 00030 } 00031 } 00032 00033 public double Evaluate(AiShipController controller) 00034 { 00035 const int MaxSteps = 2500; 00036 00037 var fitnesses = new List<double>(); 00038 foreach (TestCase testCase in testCases) 00039 { 00040 testCase.Reset(); 00041 testCase.Simulate(controller, MaxSteps); 00042 00043 double fitness = (double)testCase.TargetWaypoint / testCase.Waypoints.Count; 00044 if (testCase.TargetWaypoint == testCase.Waypoints.Count) 00045 { 00046 fitness += 1 - (double)testCase.TimeSteps / MaxSteps; 00047 } 00048 fitnesses.Add(fitness); 00049 } 00050 00051 double minFitness = fitnesses.Min(); 00052 return fitnesses.Average(f => Math.Min(f, minFitness + 250)); 00053 } 00054 } 00055 }