public object Clone() { Individuum <T> Cloned = new Individuum <T>(DeepCopyCreator, Data); Cloned.Data = DeepCopyCreator(Data); Cloned.Fitness = Fitness; return(Cloned); }
protected override void Mutation(Individuum <Decision> dna) { for (int j = 0; j < dna.Data.Length; j++) { if (rand.NextDouble() <= mutationChance) { dna.Data[j].Init(); //re-initzialize a choice } } }
/// <summary> /// Creates a new individuum with crossed decision data from parent1 and parent2 /// </summary> /// <param name="parent1"></param> /// <param name="parent2"></param> /// <returns>retruns a new individuum with crossed decision data from parent1 and parent2</returns> protected override Individuum <Decision> Crossover(Individuum <Decision> parent1, Individuum <Decision> parent2) { Individuum <Decision> child = (Individuum <Decision>)parent1.Clone(); int crossoverIndex = rand.Next(child.Data.Length); for (int i = 0; i < crossoverIndex; i++) { child.Data[i] = parent2.Data[i]; } return(child); }
protected virtual void Mutation(Individuum <T> dna) { for (int j = 0; j < dna.Data.Length; j++) { if (rand.NextDouble() <= mutationChance) { int swapIndex = rand.Next(dna.Data.Length); T swapValue = dna.Data[j]; dna.Data[j] = dna.Data[swapIndex]; dna.Data[swapIndex] = swapValue; } } }
/// <summary> /// Create a individuum and Setup decision data /// </summary> /// <returns></returns> protected override Individuum <Decision> CreateGen() { Decision[] decisions = new Decision[6]; decisions[0] = new ChoiceDecision(primary.Length, rand); //primary weapon decisions[1] = new ChoiceDecision(primary[decisions[0].Value].SuspressAble ? 2 : 1, rand); // primary suspressor decisions[2] = new ChoiceDecision(secondary.Length, rand); //secondary weapon decisions[3] = new ChoiceDecision(secondary[decisions[2].Value].SuspressAble ? 2 : 1, rand); //secondary suspressor decisions[4] = new ChoiceDecision(entries.Length, rand); //entry decisions[5] = new ChoiceDecision(entries.Length, rand); //exit Individuum <Decision> dna = new Individuum <Decision>(DeepCopy, decisions); return(dna); }
public int CompareTo(Individuum <T> other) { if (Fitness > other.Fitness) { return(1); } else if (Fitness < other.Fitness) { return(-1); } else { return(0); } }
protected virtual Individuum <T>[] GetElitist() { Individuum <T>[] newDna = new Individuum <T> [populationSize]; int count = (int)Math.Ceiling(populationSize * elitistChance); //number of elitiest that survives for (int i = 0; i < count; i++) { newDna[i] = (Individuum <T>)dna[i].Clone(); } if (newDna[0] == null) { newDna[0] = (Individuum <T>)dna[0].Clone(); } return(newDna); }
/// <summary> /// Starts calculations /// </summary> /// <returns>Returns best indviduum</returns> public virtual Individuum <T> Do() { for (int i = 0; i < iterations; i++) { IterationStarted?.Invoke(this, EventArgs.Empty); double SummedFitness = CalculateSummedFitness(); Individuum <T>[] NewPopulation = GetElitist(); for (int j = (int)Math.Ceiling(populationSize * elitistChance); j < populationSize; j++) { bool born = false; if (rand.NextDouble() <= crossoverChance) { Individuum <T> parent1 = null; Individuum <T> parent2 = null; ChooseParents(SummedFitness, ref parent1, ref parent2); Individuum <T> child = Crossover(parent1, parent2); child.Fitness = CalculateFitness(child); if (child.Fitness != parent1.Fitness && child.Fitness != parent2.Fitness) { NewPopulation[j] = child; born = true; } } if (!born) { if (surviveChance >= rand.NextDouble()) { Mutation(dna[j]); NewPopulation[j] = dna[j]; } else { NewPopulation[j] = CreateGen(); } } } dna = NewPopulation; IterationCompleted?.Invoke(this, new IterationArgs <T>(Best, i)); } return(Best); }
protected virtual void Initzialize() { Individuum <T>[] newDna = new Individuum <T> [populationSize]; if (dna != null) { ChooseSurvivors(ref newDna); } dna = newDna; for (int i = 0; i < dna.Length; i++) { if (dna[i] == null) { dna[i] = CreateGen(); } } }
protected virtual void ChooseParents(double summedFitness, ref Individuum <T> parent1, ref Individuum <T> parent2) { do { for (int j = 0; j < populationSize; j++) { if (parent1 != null && parent2 != null) { return; } if (summedFitness <= 0) { int value = rand.Next(populationSize); parent1 = dna[value]; if (value + 1 < populationSize) { parent2 = dna[value + 1]; } else if (value - 1 > 0) { parent2 = dna[value - 1]; } else { parent2 = parent1; } } else { if (parent1 == null && parent2 != dna[j] && 1 - (dna[j].Fitness / summedFitness) >= rand.NextDouble()) { parent1 = dna[j]; } if (parent2 == null && parent1 != dna[j] && 1 - (dna[j].Fitness / summedFitness) >= rand.NextDouble()) { parent2 = dna[j]; } } } } while (parent1 == null || parent2 == null); }
protected virtual double CalculateSummedFitness() { double SumFitness = 0; for (int i = 0; i < populationSize; i++) { dna[i].Fitness = CalculateFitness(dna[i]); } Array.Sort(dna); for (int i = 0; i < populationSize; i++) { if (best == null || dna[i].Fitness > best.Fitness) { best = dna[i]; } SumFitness += dna[i].Fitness; } return(SumFitness); }
protected abstract Individuum <T> Crossover(Individuum <T> dna1, Individuum <T> dna2);
protected abstract int CalculateFitness(Individuum <T> dna);
/// <summary> /// Runs a threaded game session (Fitness = health) /// </summary> /// <param name="dna"></param> /// <returns>Returns fitness</returns> protected override int CalculateFitness(Individuum <Decision> dna) { return(scene.DoTraining(dna.Data)); }
public IterationArgs(Individuum <T> best, int iteration) { Best = best; Iteration = iteration; }