/// <summary> /// Based On 2opt Algo /// </summary> /// <param name="chromose"></param> private void ChromoseCompraror(GAChromosome chromosome) { double [,] NighborMatrix = new double[Points.Count, Points.Count]; for (int i = 0; i < Points.Count; i++) { NighborMatrix[i, i] = -1; //distance diagonal } //start filling the Connection Matrix with cities and distances between each other for (int i = 0; i < Points.Count - 1; i++) { int City1Index = int.Parse(((GAGene)chromosome[i]).Value); for (int j = i + 1; j < Points.Count; j++) { int City2Index = int.Parse(((GAGene)chromosome[j]).Value); double distance = Distance((Point)Points[City1Index], (Point)Points[City2Index]); NighborMatrix[City1Index, City2Index] = distance; NighborMatrix[City2Index, City1Index] = distance; } } GAChromosome newChromosome = new GAChromosome(); //Start By Random Selection int iCurrentSel = RndObj.Next(0, Points.Count); GAGene Gene = (GAGene)chromosome[iCurrentSel]; newChromosome.AddGene(new GAGene(Gene.Value)); //left cities for visiting int iLeftPoints = Points.Count - 1; int iCurrentCitySel = int.Parse(Gene.Value); do { int iNearstNeighbor = GetNearstNeighbor(NighborMatrix, iCurrentCitySel); newChromosome.AddGene(new GAGene(iNearstNeighbor.ToString())); iLeftPoints--; iCurrentCitySel = iNearstNeighbor; }while(iLeftPoints > 0); chromosome.CopyChromosome(newChromosome); }
/// <summary> /// Mutation is done By Swapping elements from Chromosome /// </summary> /// <param name="chromose"></param> private void Mutator(GAChromosome chromose) { System.Random rnd = new Random(); int ChromoLen = chromose.Capacity; int iSelection1 = rnd.Next(0, ChromoLen); int iSelection2 = iSelection1; while (iSelection2 == iSelection1) { iSelection2 = rnd.Next(0, ChromoLen); } GAGene Gene1 = (GAGene)chromose[iSelection1]; GAGene Gene2 = (GAGene)chromose[iSelection2]; chromose.RemoveAt(iSelection1); chromose.Insert(iSelection1, Gene2); chromose.RemoveAt(iSelection2); chromose.Insert(iSelection1, Gene1); }
/// <summary> /// // /// </summary> /// <param name="Dad"></param> /// <param name="Mum"></param> /// <param name="child1"></param> private void GreedyCrossOver ( GAChromosome Dad , GAChromosome Mum , ref GAChromosome child ) { int length = Dad.GeneLength; int MumIndex = -1; int DadIndex = RndObj.Next(0, length); GAGene DadGene = (GAGene)Dad[DadIndex]; MumIndex = Mum.HasThisGene(DadGene); if (MumIndex < 0) { throw new Exception("Gene not found in mum"); } child.Add(new GAGene(DadGene.Value)); bool bDadAdded = true; bool bMumAdded = true; do { //As long as I can add from dad GAGene obMumGene = null; GAGene obDadGene = null; if (bDadAdded) { if (DadIndex > 0) { DadIndex = DadIndex - 1; } else { DadIndex = length - 1; } obDadGene = (GAGene)Dad[DadIndex]; } else { bDadAdded = false; } //As long as I can add from mum if (bMumAdded) { if (MumIndex < length - 1) { MumIndex = MumIndex + 1; } else { MumIndex = 0; } obMumGene = (GAGene)Mum[MumIndex]; } else { bMumAdded = false; } if (bDadAdded && child.HasThisGene(obDadGene) < 0) { //Add to head Dad gene child.Insert(0, obDadGene); } else { bDadAdded = false; } if (bMumAdded && child.HasThisGene(obMumGene) < 0) { //Add to Tail Mum gene child.AddGene(obMumGene); } else { bMumAdded = false; } }while(bDadAdded || bMumAdded); // Add rest of genes by Random Selection while (child.GeneLength < length) { bool bDone = false; do { int iRandom = this.RndObj.Next(0, length); if (child.HasThisGene(new GAGene(iRandom.ToString())) < 0) { child.Add(new GAGene(iRandom.ToString())); bDone = true; } }while(!bDone); } }