/// <summary> /// Initialize a new <see cref="LanderIndividual"/> instance with Random values. /// </summary> public LanderIndividual(Random random) { // NOTE: network topology defined here this.neuralNet = new NeuralNetwork(); this.neuralNet.InputCount = 7; this.neuralNet.OutputCount = 2; this.neuralNet.AddHiddenLayer(5); this.RandomGenerator = random; // Initialize the weights with random values between -1.0 and 1.0 this.weights = this.neuralNet.GetAllWeights(); for (int i = 0; i < this.weights.Count; i++) { this.weights[i] = this.RandomGenerator.NextDouble() * (weightMax - weightMin) - (weightMax - weightMin) / 2; } this.neuralNet.SetAllWeights(this.weights); }
/// <summary> /// Run the genetic algorithm to train the neural network /// </summary> private void ExecuteTrainCommand() { this.ExecuteStopCommand(); // The background worker allows the GA to be run in a different thread BackgroundWorker backgroundWorker = new BackgroundWorker(); // Setup of the genetic algorithm LanderIndividualSettings landerIndividualSettings = new LanderIndividualSettings(); LanderIndividualFactory landerFactory = new LanderIndividualFactory(); GeneticAlgorithm.GeneticAlgorithm ga = new GeneticAlgorithm.GeneticAlgorithm(landerFactory); LanderIndividual best = null; int currentIteration = 0; // Set up the ga this.environment.Gravity = 2.0; this.environment.WindSpeed = 0.1; landerIndividualSettings.StartingFuel = 100; landerIndividualSettings.StartingHeight = 100; landerIndividualSettings.StartingHorizontal = 0; landerIndividualSettings.LanderEnvironment = this.environment; landerIndividualSettings.CrossoverAlgorithm = LanderIndividualSettings.CrossoverType.OnePoint; ga.SelectionType = GeneticAlgorithm.GeneticAlgorithm.SelectionTypes.Tournament; ga.TournamentSize = 4; ga.CrossoverProbability = 0; ga.MutationProbability = 1; ga.CalculationLimit = 60000; ga.ElitistCount = 4; ga.PopulationSize = 100; landerFactory.IndividualSettings = landerIndividualSettings; // This lambda function handles the iteration events from the ga. EventHandler<IterationEventArgs> handler = (sender, args) => { backgroundWorker.ReportProgress(0, args); }; // Set the work lambda function. backgroundWorker.DoWork += (sender, e) => { // Add the iteration event handler to report progress back to the main thread ga.IterationEvent += handler; // Run the ga best = (LanderIndividual)ga.Run(landerIndividualSettings); ga.IterationEvent -= handler; }; // Update the graph to show progress backgroundWorker.ProgressChanged += (sender, e) => { this.MinFitnessValues.Add(new Tuple<int, double>(currentIteration, ((IterationEventArgs)e.UserState).MinFitness)); this.MaxFitnessValues.Add(new Tuple<int, double>(currentIteration, ((IterationEventArgs)e.UserState).MaxFitness)); this.AvgFitnessValues.Add(new Tuple<int, double>(currentIteration, ((IterationEventArgs)e.UserState).AverageFitness)); currentIteration++; if (this.MinFitnessValues.Count > 600) { this.MinFitnessValues.Clear(); this.MaxFitnessValues.Clear(); this.AvgFitnessValues.Clear(); } }; backgroundWorker.RunWorkerCompleted += (sender, e) => { this.IsTraining = false; // Save the resulting neural network this.neuralNetwork = best.CurrentNeuralNetwork; Debug.WriteLine(this.neuralNetwork.ToString()); }; // Clear out previous chart data and run the background worker this.MinFitnessValues.Clear(); this.MaxFitnessValues.Clear(); this.AvgFitnessValues.Clear(); backgroundWorker.WorkerReportsProgress = true; this.IsTraining = true; backgroundWorker.RunWorkerAsync(); }
public void Net1() { NeuralNetwork net = new NeuralNetwork(); net.InputCount = 2; net.OutputCount = 2; }
static void Main(string[] args) { bool quit = false; string input; // NOTE: network topology defined here neuralNetwork = new NeuralNetwork(); neuralNetwork.InputCount = 7; neuralNetwork.OutputCount = 2; neuralNetwork.AddHiddenLayer(5); PrintHelp(); while (quit == false) { Console.Write("> "); input = Console.ReadLine(); switch (input) { case "q": case "quit": quit = true; break; case "t": case "train": Train(true); break; case "tl": case "trainlong": TrainLong(); break; case "r": case "run": RunSimulation(); break; case "ra": case "runall": RunSimulationVarying(true); break; case "h": case "help": PrintHelp(); break; case "p": case "print": PrintNetwork(); break; case "rs": case "reset": ResetNetwork(); break; case "rg": case "rungeneralize": RunSimulationGeneralize(); break; default: Console.WriteLine("Unrecognized command. Try 'help'"); break; } } }