예제 #1
0
        public void Run()
        {
            // Spawn our initial populaiton of individuals
            world.Spawn();

            Thread.Sleep(3000);

            // Set our generation count to zero and restart our timer
            int generation = 0;

            generationClock.Restart();

            while (window.IsOpen)
            {
                // Get the amount of time that has passed since we drew the last frame.
                float deltaT = clock.Restart().AsMicroseconds() / 1000000f;

                // Clear the previous frame
                window.Clear(Configuration.Background);

                // Process events
                window.DispatchEvents();

                // Camera movement is now separate from our other screen update code to minimise overhead.
                screenManager.UpdateCamera(deltaT);

                screenManager.Draw(deltaT);

                // Update the window
                window.Display();

                // If one second has passed, breed the next generation
                if (generationClock.ElapsedTime.AsSeconds() > generationTime &&
                    (doGeneration?.IsCompleted ?? true) &&
                    !world.HasConverged)
                {
                    doGeneration = Task.Run(() =>
                    {
                        // Do one generation of breeding & culling.
                        world.DoGeneration();

                        // Restart our generation timer
                        generationClock.Restart();

                        // Update the screen to show the new generation
                        pathScreen.GenerationString.StringText = $"Generation: {++generation}";

                        // Draw the paths of the current best individual
                        pathScreen.UpdateSequence(world.GetBestIndividual());

                        // Update all the screen components that are unrelated to our sequence.
                        screenManager.Update(deltaT);
                    });
                }

                // Our GA has either hit the max generations or has stopped improving, set the GA as complete
                // and copy the history of fitness values to the clipboard
                if (world.GenerationCount == GAConfig.MaxGenerations ||
                    world.NoImprovementCount == GAConfig.MaxNoImprovementCount)
                {
                    pathScreen.SetGACompleted();

                    // Copy the fitness to clipboard
                    Clipboard.SetText(string.Join(",", world.FitnessOverTime));
                }

                // Process any key presses that the user has made.
                this.ProcessUserInput();

                // Check to see if the user wants to stop the simulation.
                if (Keyboard.IsKeyPressed(Configuration.QuitKey) ||
                    Keyboard.IsKeyPressed(Keyboard.Key.Escape))
                {
                    return;
                }
            }
        }