public virtual void Competition(GeneticGeneration generation) { int length = generation.Chromosome.Length; double[,] matrix = new double[length, length]; for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { if (i == j) { continue; } matrix[i, j] = DoTask(generation[i].Gen, generation[j].Gen); } } for (int i = 0; i < length; i++) { generation.Chromosome[i].FitnessResult = 0; for (int j = 0; j < length; j++) { if (matrix[i, j] >= matrix[j, i]) { generation.Chromosome[i].FitnessResult++; } } } generation.Sort(Comparator); }
public void Divide(Chromosome[] chromosomes) { for (int i = 0; i < CurrentGeneration.Length; i++) { CurrentGeneration[i] = new GeneticGeneration(10, 64); for (int j = 0; j < 10; j++) { CurrentGeneration[i].Chromosome[j] = chromosomes[i * 10 + j]; } } }
public List <Chromosome> Survive(GeneticGeneration parents, List <Chromosome> childs) { List <Chromosome> fullGenerations = new List <Chromosome>(childs); fullGenerations.AddRange(parents.Chromosome); fullGenerations.Sort(comparator); if (cfg.KillSame == true) { fullGenerations = fullGenerations.Distinct(childs[0] as IEqualityComparer <Chromosome>).ToList(); } return(fullGenerations.GetRange(0, cfg.ChromosomeCount)); }
public void ResultCachePushTest() { GeneticGeneration gen = new GeneticGeneration(2, 2); gen[0][0] = 1; GeneticGeneration gen2 = new GeneticGeneration(2, 2); cache.Push(gen); List <GeneticGeneration> both = cache.Data; Assert.AreEqual(gen, both[0]); Assert.AreNotEqual(gen2, both[0]); }
public void CheckCancellationTest() { GeneticAlgorithm alg = Factory.DefaultGeneticAlgorithmFactory.Create(2, 2); CancellationConditions cond = alg.Cancellation; GeneticGeneration temp1 = new GeneticGeneration(2, 2); alg.Result.Push(temp1); alg.Result.Data[0][0].FitnessResult = 1; GeneticGeneration temp2 = new GeneticGeneration(2, 2); alg.Result.Push(temp2); alg.Result.Data[1][0].FitnessResult = 0.5; Assert.IsFalse(cond.CheckCancellation(alg)); alg.Result.Data[1][0].FitnessResult = alg.Result.Data[0][0].FitnessResult - cond.ErrorStep; Assert.IsTrue(cond.CheckCancellation(alg)); cond.ErrorStep = -1; alg.CurrentGeneration = temp1; cond.MoreOrEquals = 1; Assert.IsTrue(cond.CheckCancellation(alg)); cond.MoreOrEquals = double.MaxValue; cond.LessOrEquals = 1; Assert.IsTrue(cond.CheckCancellation(alg)); cond.LessOrEquals = double.MinValue; Assert.IsFalse(cond.CheckCancellation(alg)); Assert.IsFalse(cond.CheckCancellation(alg)); }