/// <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); } }
/// <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); } }