Example #1
0
 public void SanityCheck()
 {
     // Check that the lander will crash with the default settings
     LanderSimulator.Model.Environment environment = new LanderSimulator.Model.Environment();
     LanderSimulator.Model.Lander lander = new LanderSimulator.Model.Lander(environment, 100, 0, 100);
     bool crashed = false;
     for (int i = 0; i < 100; i++)
     {
         if (lander.Update() == LanderStatus.Crashed)
         {
             crashed = true;
             break;
         }
     }
     Assert.IsTrue(crashed);
 }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            this.environment = new LanderSimulator.Model.Environment();
            this.lander = new LanderSimulator.Model.Lander(this.environment, 100, 0, 100);
            timer = new Timer(10);
            timer.Elapsed += new ElapsedEventHandler(UpdateLanderPosition);
            timer.AutoReset = true;
            this.LanderPositionX = this.lander.PositionX;
            this.LanderPositionY = this.lander.PositionY;

            this.MinFitnessValues = new ObservableCollection<Tuple<int, double>>();
            this.MaxFitnessValues = new ObservableCollection<Tuple<int, double>>();
            this.AvgFitnessValues = new ObservableCollection<Tuple<int, double>>();

            this.ExecuteTrainCommand();
        }
Example #3
0
        private static double Train(bool showOutput)
        {
            LanderIndividualSettings landerIndividualSettings = new LanderIndividualSettings();
            LanderIndividualFactory landerFactory = new LanderIndividualFactory();
            GeneticAlgorithm ga = new GeneticAlgorithm(landerFactory);
            LanderIndividual best = null;
            int currentIteration = 0;
            double bestfitness = 0;

            // Set up the ga
            LanderSimulator.Model.Environment environment = new LanderSimulator.Model.Environment();
            environment.Gravity = 2.0;
            environment.WindSpeed = 0.1;
            landerIndividualSettings.StartingFuel = 100;
            landerIndividualSettings.StartingHeight = 100;
            landerIndividualSettings.StartingHorizontal = 0;
            landerIndividualSettings.LanderEnvironment = environment;
            landerIndividualSettings.CrossoverAlgorithm = LanderIndividualSettings.CrossoverType.Uniform;
            ga.SelectionType = GeneticAlgorithm.SelectionTypes.Tournament;
            ga.TournamentSize = 5;
            ga.CrossoverProbability = 0.98;
            ga.MutationProbability = 1;
            ga.CalculationLimit = 60000;
            ga.ElitistCount = 10;
            ga.PopulationSize = 500;
            landerFactory.IndividualSettings = landerIndividualSettings;

            // This lambda function handles the iteration events from the ga.
            EventHandler<IterationEventArgs> handler = (sender, args) =>
            {
                if (showOutput)
                {
                    Console.CursorLeft = 0;
                    Console.Write("{0,4:g}{1,12:f3}{2,12:f3}{3,12:f3}{4,12:f3}{5,12:f3}{6,12:f3}",
                        currentIteration,
                        args.MinFitness, args.AverageFitness, args.MaxFitness,
                        args.MinDifference, args.AverageDifference, args.MaxDifference);
                }

                currentIteration++;
                bestfitness = args.MinFitness;
            };

            ga.IterationEvent += handler;

            if (showOutput)
            {
                Console.WriteLine("{0,4}{1,12}{2,12}{3,12}{4,12}{5,12}{6,12}",
                "", "Min Fit", "Avg Fit", "Max Fit",
                "Min Dif", "Avg Dif", "Max Dif");
            }

            // Run the ga
            best = (LanderIndividual)ga.Run(landerIndividualSettings);
            Console.WriteLine();

            neuralNetwork.SetAllWeights(best.CurrentNeuralNetwork.GetAllWeights());

            ga.IterationEvent -= handler;

            return bestfitness;
        }
Example #4
0
        /// <summary>
        /// Runs the simulation with given starting variables.
        /// </summary>
        /// <returns></returns>
        private static LanderStatus RunOneSimulation(bool showOutput, double positionx, double positiony,
            double fuel, double windspeed, double gravity)
        {
            // First query the neural net to see how it reacts to the current conditions.
            // Then update the conditions.
            bool stop = false;

            if (showOutput)
            {
                Console.WriteLine("(X,Y,Vx,Vy,Fuel,Wind,Gravity) => (Burn,Thrust)");
            }

            LanderSimulator.Model.Environment env = new LanderSimulator.Model.Environment();
            env.Gravity = gravity;
            env.WindSpeed = windspeed;
            LanderSimulator.Model.Lander lander = new LanderSimulator.Model.Lander(env, fuel, positionx, positiony);

            while (stop == false)
            {
                // height, xPosition, Yvelocity, Xvelocity, wind, acceleration, and fuel.
                List<double> inputs = new List<double>();
                inputs.Add(lander.PositionX);
                inputs.Add(lander.PositionY);
                inputs.Add(lander.VelocityX);
                inputs.Add(lander.VelocityY);
                inputs.Add(env.WindSpeed);
                inputs.Add(env.Gravity);
                inputs.Add(lander.Fuel);

                IList<double> output = neuralNetwork.Run(inputs);

                lander.Burn = output[0];
                lander.Thrust = output[1];

                lander.Update();

                if (lander.Status != LanderSimulator.Model.LanderStatus.Flying)
                {
                    stop = true;
                }

                if (showOutput)
                {
                    Console.CursorLeft = 0;
                    switch (lander.Status)
                    {
                        case LanderSimulator.Model.LanderStatus.Flying:
                            Console.BackgroundColor = ConsoleColor.Black;
                            Console.Write("Flying:  ");
                            Console.BackgroundColor = ConsoleColor.Black;
                            break;
                        case LanderSimulator.Model.LanderStatus.Landed:
                            Console.BackgroundColor = ConsoleColor.Green;
                            Console.ForegroundColor = ConsoleColor.Black;
                            Console.Write("Landed:  ");
                            Console.BackgroundColor = ConsoleColor.Black;
                            Console.ForegroundColor = ConsoleColor.Gray;
                            break;
                        case LanderSimulator.Model.LanderStatus.Crashed:
                            Console.BackgroundColor = ConsoleColor.Red;
                            Console.Write("Crashed: ");
                            Console.BackgroundColor = ConsoleColor.Black;
                            break;
                    }

                    Console.Write("({0,6:f2},{1,6:f2},{2,6:f2},{3,6:f2},{4,6:f2},{5,6:f2},{6,6:f2}) => ({7,6:f2},{8,6:f2})",
                        lander.PositionX, lander.PositionY, lander.VelocityX, lander.VelocityY, lander.Fuel,
                        env.WindSpeed, env.Gravity, output[0], output[1]);
                    Thread.Sleep(250);
                }

                env.Update();
            }

            if (showOutput)
            {
                Console.WriteLine();
                Console.WriteLine("Fitness: {0:f3}", lander.CalculateFitness());
            }

            return lander.Status;
        }