private Rabbit() { _fitness = -1; _dna = new Dna { Gene = (List <int>)Enumerable.Empty <int>() }; }
/// <summary> /// /// </summary> /// <param name="dna"></param> /// <returns></returns> public override double GetFitness(IDna dna) { var fitness = dna.Gene.Sum() / (double)dna.Gene.Count; if (Math.Abs(fitness - SolutionValue) > 0.0) { return(dna.Gene.Sum() / (double)dna.Gene.Count); } Stop?.Invoke(dna); return(1); }
/// <summary> /// Tworzy nową generację z określoną ilością obiektów oraz kontrolerem ewolucyjnym. /// </summary> /// <param name="evolutionController">Kontroler ewolucyjny.</param> /// <param name="size">Ilość obiektów.</param> public Generation(IEvolutionController evolutionController, uint size) { Objects = new IDna[size]; this.evolutionController = evolutionController ?? throw new ArgumentNullException("evolutionController nie może być null."); for (int i = 0; i < size; i++) { Objects[i] = evolutionController.GetRandomDna(); Objects[i].Generation = this; Objects[i].Id = i; } }
public override double GetFitness(IDna dna) { var s = dna.Gene.Select((gen, index) => gen * index).Sum(); var result = (int)(s - SolutionValue); if (result > 0.0) { return(Math.Abs(1 / (s - SolutionValue))); } Stop?.Invoke(dna); return(1); }
public IDna <Vector2> Crossover(IDna <Vector2> partner) { var child = new VectorDna(Genes.Length); var midpoint = Convert.ToInt32(Random.value * Genes.Length); for (var i = 0; i < Genes.Length; i++) { if (i < midpoint) { child.Genes[i] = Genes[i]; } else { child.Genes[i] = partner.Genes[i]; } } return(child); }
public IDna <char> Crossover(IDna <char> partner) { var midpoint = Random.Next(Genes.Length); var child = new PhraseDna(Genes.Length); for (var i = 0; i < Genes.Length; i++) { if (i < midpoint) { child.Genes[i] = partner.Genes[i]; } else { child.Genes[i] = Genes[i]; } } return(child); }
/// <summary> /// Tworzy nową generację krzyżując obiekty z obecnej generacji. /// </summary> /// <returns>Zwraca nową generację.</returns> public Generation CreateNewGeneration() { if (!CurrentGeneration.WasEvaluated) { CurrentGeneration.Evaluate(); } IDna[] objects = new IDna[GenerationsSize]; for (uint i = 0; i < GenerationsSize; i++) { IDna parentA = CurrentGeneration.GetDnaBasedOnFitness(); IDna parentB = CurrentGeneration.GetDnaBasedOnFitness(); objects[i] = evolutionController.Crossover(parentA, parentB); IDna db = objects[i]; Console.WriteLine(new string(db.GetGenes() as char[]) + " ||| " + db.Fitness); } CurrentGeneration = new Generation(evolutionController, objects); CurrentGeneration.Mutate(MutationChance > 100 ? (byte)100 : MutationChance); return(CurrentGeneration); }
static void TestGeneticAlgorithm() { string target = "Be or not to be?"; IEvolutionController evolutionController = new EvolutionController(target); Population population = new Population(evolutionController, 300); population.MutationChance = 10; population.CurrentGeneration.Evaluate(); while (true) { ReadKey(); population.CurrentGeneration.Evaluate(); IDna db = population.CurrentGeneration.Best; WriteLine(new string(db.GetGenes() as char[]) + " ||| " + db.Fitness); population.CreateNewGeneration(); } }
// Zaimplementowane z IEvolutionController public IDna Crossover(IDna parentA, IDna parentB) { IDna child = new Dna(target, target.Length); char[] childGenes = child.GetGenes() as char[]; char[] aGenes = parentA.GetGenes() as char[]; char[] bGenes = parentB.GetGenes() as char[]; int midpoint = childGenes.Length / 2; for (int i = 0; i < childGenes.Length; i++) { if (i < midpoint) { childGenes[i] = aGenes[i]; } else { childGenes[i] = bGenes[i]; } } return(child); }
public abstract double GetFitness(IDna dna);
private static void StopAlgorithm(IDna dna) { Console.WriteLine("The genome satisfy fitness criteria: ({0}), dna: {1}", FitnessSolution, string.Join(" ", dna.Gene)); }
public Rabbit(IDna dna, double fitness) { _dna = dna; _fitness = fitness; }
private static void StopAlgorithm(IDna dna) { var gene = dna.Gene; Console.WriteLine("FIND SOLUTION:: {0} + 2*{1} + 3*{2} + 4*{3}", gene[0], gene[1], gene[2], gene[3]); }