private GenericGA CreateGeneticAlgorithm(List <float> seed = null)
        {
            var geneticAlgorithm = new GenericGA();

            var carConfiguration = new CarConfiguration();

            geneticAlgorithm.CreateIndividual = () =>
            {
                return(GAHelpers.SpawnCarController(carConfiguration, random, seed));
            };

            geneticAlgorithm.CrossoverIndividuals = (a, b) =>
            {
                return(GAHelpers.DoCrossover(a, b, random));
            };

            geneticAlgorithm.CrossoverEnabled = true;

            geneticAlgorithm.MutationEnabled = true;

            geneticAlgorithm.MutateParentsAsChildren = true;

            geneticAlgorithm.PopulationCount = 100;

            geneticAlgorithm.SpawnPopulation();

            return(geneticAlgorithm);
        }
        public SelfTrainingScreen(
            IApplicationService appService,
            bool enableVisualization = true)
        {
            random = new Random();


            List <float> seed     = null;
            var          seedFile = "Resources/CarAi/AI_2.mlpnn";

            if (File.Exists(seedFile))
            {
                var seedText = File.ReadAllText(seedFile);
                seed = seedText.Split(',').Select(s => float.Parse(s)).ToList();
            }

            // Create the GA
            genericAlgorithm = this.CreateGeneticAlgorithm(seed);

            // Load the tracks used for evaluation
            tracks = TrackHelper.LoadTrackFiles("Resources/Tracks");

            // Spawn the initial population
            genericAlgorithm.SpawnPopulation();

            // Load the simulation
            simulation = appService.Kernel.Get <RacingSimulationLogic>();
            simulation.SetTrack(currentTrack);

            // Initialize the simulation
            simulation.SetCars(genericAlgorithm.GetPopulation().Cast <ICarController>());
            simulation.ResetCars();

            // Add the metrics we will use to judge our cars fitness
            simulation.GetCars().ForEach(car => this.AddFitnessMetrics(car));

            // If the visualization is turned on, create it, set the track and add the cars.
            if (enableVisualization)
            {
                simulationVisualization = appService.Kernel.Get <RacingSimulationVisualization>();
                simulationVisualization.SetTrack(currentTrack);
                simulationVisualization.InitializeCars(simulation.GetCars());
            }
        }