public void Resume(GAFunction fitnessFunc, OnGeneration onGenerationFunc) { FitnessFunction = fitnessFunc; OnGenerationFunction = onGenerationFunc; Go(true); }
public StringFunction(Func <IFitness> getFitnessMethod, GAStatus bestStatus, GAFunction fitnessFunc, OnGeneration generationFunc, double crossoverRate, double mutationRate, int genomeSize, TargetParams targetParams) { _getFitnessFunc = getFitnessMethod; _bestStatus = bestStatus; _crossoverRate = crossoverRate; _mutationRate = mutationRate; _genomeSize = genomeSize; _fitnessFunc = fitnessFunc; _generationFunc = generationFunc; _targetParams = targetParams; }
public StringFunction(Func<IFitness> getFitnessMethod, GAStatus bestStatus, GAFunction fitnessFunc, OnGeneration generationFunc, double crossoverRate, double mutationRate, int genomeSize, TargetParams targetParams) { _getFitnessFunc = getFitnessMethod; _bestStatus = bestStatus; _crossoverRate = crossoverRate; _mutationRate = mutationRate; _genomeSize = genomeSize; _fitnessFunc = fitnessFunc; _generationFunc = generationFunc; _targetParams = targetParams; }
public StringFunctionChunk(Func <IFitness> getFitnessMethod, Action <string, object> onStepComplete, GAStatus bestStatus, GAFunction fitnessFunc, OnGeneration generationFunc, double crossoverRate, double mutationRate, int genomeSize, TargetParams targetParams, int chunkSize = 4) { _getFitnessFunc = getFitnessMethod; _onStepComplete = onStepComplete; _bestStatus = bestStatus; _crossoverRate = crossoverRate; _mutationRate = mutationRate; _genomeSize = genomeSize; _fitnessFunc = fitnessFunc; _generationFunc = generationFunc; _targetParams = targetParams; _chunkSize = chunkSize; }
public void InitializeGA(NetworkInitializer initializer, Func <double[], double> fitnessFunction) { var input = (GeneticInitializer)initializer; this.crossover = input.Crossover; this.iterations = input.Iterations; this.mutationRate = input.MutationRate; this.populationSize = input.PopulationSize; InitializeWeights(initializer); ga = new GA(crossover, mutationRate, populationSize, iterations, weights); ga.Elitism = true; GAFunction func = new GAFunction(fitnessFunction); ga.FitnessFunction = func; }
/// <summary> /// Setup the genetic algorithm and run it. /// </summary> /// <returns>Best brain's output source code</returns> public static string Run(IGeneticAlgorithm iga, GAFunction fitnessFunc, OnGeneration generationFunc, Action setupFunc = null, bool resume = false) { GA ga = (GA)iga; if (!resume) { if (setupFunc != null) { // Perform any additional setup for this fitness. setupFunc(); } try { // Delete any existing dat file. File.Delete(Directory.GetCurrentDirectory() + "\\my-genetic-algorithm.dat"); } catch (Exception excep) { Console.WriteLine("Unable to delete " + Directory.GetCurrentDirectory() + "\\my-genetic-algorithm.dat\n" + excep.Message); } // Start a new genetic algorithm. ga.GAParams.Elitism = true; ga.GAParams.HistoryPath = Directory.GetCurrentDirectory() + "\\history.txt"; ga.FitnessFunction = new GAFunction(fitnessFunc); ga.OnGenerationFunction = new OnGeneration(generationFunc); ga.Go(); } else { // Load a saved genetic algorithm. ga.Load("my-genetic-algorithm.dat"); ga.Resume(fitnessFunc, generationFunc); } // Results. double[] weights; double fitness; ga.GetBest(out weights, out fitness); Console.WriteLine("***** DONE! *****"); return(CommonManager.ConvertDoubleArrayToBF(weights)); }
/// <summary> /// Setup the genetic algorithm and run it. /// </summary> /// <returns>Best brain's output source code</returns> public static string Run(IGeneticAlgorithm iga, GAFunction fitnessFunc, OnGeneration generationFunc, Action setupFunc = null, bool resume = false) { GA ga = (GA)iga; if (!resume) { if (setupFunc != null) { // Perform any additional setup for this fitness. setupFunc(); } // Delete any existing dat file. File.Delete(Directory.GetCurrentDirectory() + "\\my-genetic-algorithm.dat"); // Start a new genetic algorithm. ga.GAParams.Elitism = true; ga.GAParams.HistoryPath = Directory.GetCurrentDirectory() + "\\history.txt"; ga.FitnessFunction = new GAFunction(fitnessFunc); ga.OnGenerationFunction = new OnGeneration(generationFunc); ga.Go(); } else { // Load a saved genetic algorithm. ga.Load("my-genetic-algorithm.dat"); ga.Resume(fitnessFunc, generationFunc); } // Results. double[] weights; double fitness; ga.GetBest(out weights, out fitness); Console.WriteLine("***** DONE! *****"); return CommonManager.ConvertDoubleArrayToBF(weights); }