public int run(GeneticRunParameters parameters) { if (!(parameters is RandomRestartRunParameters)) throw new WrongParameterTypeException("Random Restart Genetics can only be run using Random Restart Parameters"); RandomRestartRunParameters rrParameters = (RandomRestartRunParameters)parameters; int bestFitness = 0; List<GeneStringFitnessPair> bestResults = new List<GeneStringFitnessPair>(); do { subGenetics = rrParameters.algorithm.newInstance(initial, rrParameters.algorithmInstanceParameters); int maxfitness = runUntilRepeating(rrParameters.algorithmRunParameters, rrParameters.numRepeatsBeforeRestart, rrParameters.verbose); GeneStringFitnessPair bestOfGeneration = subGenetics.getBestPair(); bestResults.Add(bestOfGeneration); if (maxfitness > bestFitness) bestFitness = maxfitness; if (rrParameters.verbose) { Console.WriteLine(); Console.Write("Local Max at " + maxfitness); } goalValue = (1000 - rrParameters.promilleGoalDecreasePerRound) * goalValue / 1000; if (rrParameters.verbose) Console.WriteLine(", restarting with goal at " + goalValue + " best = " + bestFitness); } while (bestFitness < goalValue); return runChampionRound(rrParameters, bestResults); }
public RandomRestartRunParameters(GeneticAlgorithm algorithm, GeneticInstanceParameters algorithmInstanceParameters, GeneticRunParameters algorithmRunParameters, int promilleGoalDecreasePerRound, int numRepeatsBeforeRestart, bool verbose) { this.algorithm = algorithm; this.algorithmInstanceParameters = algorithmInstanceParameters; this.algorithmRunParameters = algorithmRunParameters; this.promilleGoalDecreasePerRound = promilleGoalDecreasePerRound; this.numRepeatsBeforeRestart = numRepeatsBeforeRestart; this.verbose = verbose; }
public FuzzyGeneticsRunParameters(GeneticAlgorithm algorithm, GeneticInstanceParameters algorithmInstanceParameters, GeneticRunParameters algorithmRunParameters, int numberToAverageScoreOver, int waitSinceLastImprovement, bool verbose) { this.algorithm = algorithm; this.algorithmInstanceParameters = algorithmInstanceParameters; this.algorithmRunParameters = algorithmRunParameters; this.numberToAverageScoreOver = numberToAverageScoreOver; this.waitSinceLastImprovement = waitSinceLastImprovement; this.verbose = verbose; }
public int run(GeneticRunParameters parameters) { if (!(parameters is FuzzyGeneticsRunParameters)) throw new WrongParameterTypeException("Fuzzy Genetics only accepts Fuzzy Genetics Run Parameters"); FuzzyGeneticsRunParameters fgParameters = (FuzzyGeneticsRunParameters)parameters; subGenetics = fgParameters.algorithm.newInstance(initial, fgParameters.algorithmInstanceParameters); int bestFitness = 0; int periodSinceLastImprovement = 0; LinkedList<int> lastFitnesses = new LinkedList<int>(); for (int n = 0; n < fgParameters.numberToAverageScoreOver; n++) { lastFitnesses.AddFirst(0); } while (periodSinceLastImprovement < fgParameters.waitSinceLastImprovement) { int fitness = subGenetics.run(fgParameters.algorithmRunParameters); lastFitnesses.RemoveFirst(); lastFitnesses.AddLast(fitness); int summedFitness = lastFitnesses.Sum(); periodSinceLastImprovement++; if (bestFitness < summedFitness) { bestFitness = summedFitness; periodSinceLastImprovement = 0; result = subGenetics.getAll(); resultPairs = subGenetics.getAllPairs(); } if (fgParameters.verbose) Console.WriteLine("best of round: " + fitness + ". average = " + summedFitness / fgParameters.numberToAverageScoreOver + " (" + periodSinceLastImprovement + ")"); } return lastFitnesses.Last.Value; }
// remainder of 1 - mutation - crossover = copy chance // returns max fitness this round public int run(GeneticRunParameters parameters) { if (!(parameters is BasicGeneticRunParameters)) throw new WrongParameterTypeException("Basic Genetics only take Basic Genetics Run Parameters"); BasicGeneticRunParameters bParameters = (BasicGeneticRunParameters)parameters; if (bParameters.mutationChance + bParameters.crossoverChance > 1.0d) throw new OutOfBoundsException("mutation chance + crossover chance is more than 100%"); double crossoverChance = bParameters.crossoverChance + bParameters.mutationChance; // < mutation = mutation, else < crossover = crossover, else copy GeneString[] newGeneration = new GeneString[population.Count]; for (int n = 0; n < bParameters.keepPerRound; n++) { newGeneration[n] = population[n].genestring.copy(); } for (int n = bParameters.keepPerRound; n < population.Count; n++) { Double rnd = SharedRandom.NextDouble(); if (rnd < bParameters.mutationChance) newGeneration[n] = mutateFrom(bParameters.keepPerRound, bParameters.mutationSeverity); else if (rnd < crossoverChance) newGeneration[n] = crossoverFrom(bParameters.keepPerRound, bParameters.keepLength); else newGeneration[n] = copyFrom(bParameters.keepPerRound); } population = evaluateAll(newGeneration); population.Sort(); return population[0].fitness; }
private int runUntilRepeating(GeneticRunParameters parameters, int numRepeatsBeforeRestart, Boolean verbose) { int numAtSameMax = 0; int currentMax = 0; int maxFitness = 0; while (numAtSameMax != numRepeatsBeforeRestart) { maxFitness = subGenetics.run(parameters); if (maxFitness == currentMax) numAtSameMax++; else { numAtSameMax = 0; if (verbose) Console.Write("."); } currentMax = maxFitness; } return maxFitness; }