public StandardGA(int GeneSize, int GenerationSize, int GenerationCarryover, FittnessDelegate fittness) { this.generationSize = GenerationSize; this.generationCarryover = GenerationCarryover; this._fittness = fittness; //create random generation 0 Random rand = new Random(); for (int i = 0; i < generationSize; i++) { chromosome c = new chromosome(); c.gene = Enumerable.Range(0, GeneSize) .Select(j => new Tuple <int, int>(rand.Next(GeneSize), j)) .OrderBy(k => k.Item1) .Select(l => l.Item2).ToList(); this.CurrentGeneration.Add(c); } this.generation = 0; this.HighScore = Int64.MaxValue; this.HighScoreGeneration = -1; }
// create the function that performs the mutation on each chromosome static ArrayList Mutate(double Mutation_Probability, ArrayList XPrime) { Random random = new Random(); ArrayList tempXPrime = new ArrayList(); for (int temp = 0; temp < XPrime.Count; temp++) { chromosome C1 = (chromosome)XPrime[temp]; for (int temp2 = 0; temp2 < C1.Bits.Length; temp2++) { if (random.NextDouble() < Mutation_Probability) { if (C1.Bits[temp2] == 1) { C1.Bits[temp2] = 0; } if (C1.Bits[temp2] == 0) { C1.Bits[temp2] = 1; } } } tempXPrime.Add(C1); } XPrime.Clear(); XPrime = tempXPrime; return(XPrime); }
//Create the function that selects certain percentage of members based // on removing large numbers static void changechromosome(chromosome currentchromosome, int[] X, double PercentageSupressed) { int ElementsSupressed = System.Convert.ToInt32(X.Length * PercentageSupressed); int TempElementNumber = 0; int TempElement = 0; Random random = new Random(); //Set the chromosome Bit Values to 1 for (int temp = 0; temp < currentchromosome.Bits.Length; temp++) { currentchromosome.Bits[temp] = 1; } TempElement = X[0]; for (int temp2 = 0; temp2 < ElementsSupressed; temp2++) { //find the largets element in X for (int temp3 = 0; temp3 < X.Length; temp3++) { if (currentchromosome.Bits[temp3] == 1) { if (TempElement < X[temp3]) { TempElement = X[temp3]; TempElementNumber = temp3; } } } if (random.NextDouble() < 0.5) { currentchromosome.Bits[TempElementNumber] = 0; TempElement = X[0]; } System.Threading.Thread.Sleep(1); } }
public ParallelGA(int GeneSize, int GenerationSize, int GenerationCarryover, int NumThreads, FittnessDelegate fittness) { this.generationSize = GenerationSize; this._fittness = fittness; this.generationCarryover = GenerationCarryover; this._numThreads = NumThreads; this.CompletedTesting = new List <chromosome>(); this.NextGeneration = new List <chromosome>(); //genrate rangom 0th generation Random rand = new Random(); for (int i = 0; i < generationSize; i++) { chromosome c = new chromosome(); c.gene = Enumerable.Range(0, GeneSize) .Select(j => new Tuple <int, int>(rand.Next(GeneSize), j)) .OrderBy(k => k.Item1) .Select(l => l.Item2).ToList(); this.CurrentGeneration.Add(c); } this.generation = 0; this.HighScore = Int64.MaxValue; this.HighScoreGeneration = -1; }
/// <summary> /// 初始化; /// </summary> private static void Init() { chromosomes.Clear(); // 染色体数量; int length = 10; int totalFit = 0; for (int i = 0; i < length; i++) { chromosome chromosome = new chromosome(); for (int j = 0; j < chromosome.bits.Length; j++) { // 随机出0或者1; //int seed = (i + j) * 100 / 3;//种子; int bitValue = random.Next(0, 2); chromosome.bits[j] = bitValue; } //获得十进制的值; int x = DeCode(chromosome.bits); int y = GetFitValue(x); chromosome.fitValue = y; chromosomes.Add(chromosome); //算出total fit; if (chromosome.fitValue <= 0) { totalFit += 0; } else { totalFit += chromosome.fitValue; } } }
//Create the function that swaps the values between two chromosomes static void Swap(chromosome C1, chromosome C2) { for (int temp = 0; temp < C1.Bits.Length; temp++) { C2.Bits[temp] = C1.Bits[temp]; } C2.NumberOfBits = C1.NumberOfBits; C2.Fitness = C1.Fitness; }
protected override void Train() { //test current generation double generationHighScore = double.MaxValue; foreach (chromosome c in this.CurrentGeneration) { c.score = this._fittness(c); if (c.score < generationHighScore) { generationHighScore = c.score; } if (c.score < this.HighScore) { this.HighScore = c.score; this.HighScoreGeneration = this.generation; this.BestChromosome = c.gene; } } //select top List <chromosome> top = this.CurrentGeneration.OrderBy(c => c.score).ToList().GetRange(0, generationCarryover); List <chromosome> nextGeneration = new List <chromosome>(); //crossover for (int i = 0; i < generationSize - generationCarryover; i++) { chromosome ParentA = this.RouletteWheelSelection(top); chromosome ParentB = this.RouletteWheelSelection(top); //crossover nextGeneration.Add(this.Crossover(ParentA, ParentB)); } //mutate foreach (chromosome c in nextGeneration) { this.Mutate(c); } //Add top performers from current generation to next generation nextGeneration.AddRange(new List <chromosome>(top)); this.CurrentGeneration = nextGeneration; if (this.generation % 1 == 0) { using (StreamWriter file = File.AppendText(LogFilePath)) { file.WriteLineAsync(DateTime.Now.ToString() + ", " + generation + ", " + this.ElapsedMilliseconds.ToString() + ", " + generationHighScore + ", " + this.HighScore + ", " + this.HighScoreGeneration); } } this.generation++; }
/// <summary> /// 克隆 /// </summary> /// <returns></returns> public chromosome Clone() { chromosome c = new chromosome(); for (int i = 0; i < bits.Length; i++) { c.bits[i] = bits[i]; } c.fitValue = fitValue; c.fitValuePercent = fitValuePercent; c.probability = probability; return(c); }
protected void Mutate(chromosome Child) { Random rand = new Random(); for (int i = 0; i < Child.gene.Count() - 1; i++) { if (rand.NextDouble() < this.MutationProbability) { Int32 temp = Child.gene[i]; Child.gene[i] = Child.gene[i + 1]; Child.gene[i + 1] = temp; } } }
/// <summary> /// 初始化; /// </summary> static void Init() { chromosomes.Clear(); // 染色体数量; int length = 4; int totalFit = 0; for (int i = 0; i < length; i++) { chromosome chromosome = new chromosome(); for (int j = 0; j < chromosome.bits.Length; j++) { // 随机出0或者1; int seed = (i + j) * 100 / 3;//种子; Random random = new Random(seed); int bitValue = random.Next(0, 2); chromosome.bits[j] = bitValue; } //获得十进制的值; int x = DeCode(chromosome.bits); int y = GetFitValue(x); chromosome.fitValue = y; chromosomes.Add(chromosome); //算出total fit; if (chromosome.fitValue <= 0) { totalFit += 0; } else { totalFit += chromosome.fitValue; } } for (int i = 0; i < chromosomes.Count; i++) { //计算适应值得百分比,该参数是在用轮盘赌选择法时需要用到的; if (chromosomes[i].fitValue <= 0) { chromosomes[i].fitValuePercent = 0; } else { chromosomes[i].fitValuePercent = chromosomes[i].fitValue / totalFit; } // chromosomes[i].probability = 0; } }
// Create the function that executes the cross over of two Chromosomes static ArrayList CrossOver(double CrossOver_Probability, ref chromosome[] X, ref ArrayList XPrime) { ArrayList tempXprime = new ArrayList(); chromosome C1 = X[0]; chromosome C2 = X[0]; chromosome[] TempChromosomes = new chromosome[X.Length]; Random Crossing = new Random(); int CountofXPrime = X.Length; for (int temp2 = 0; temp2 < X.Length; temp2++) { TempChromosomes[temp2] = new chromosome(X[0].Bits.Length); Swap(X[temp2], TempChromosomes[temp2]); XPrime.Add(TempChromosomes[temp2]); } for (int temp = 0; temp < CountofXPrime / 2; temp++) { //System.Threading.Thread.Sleep(1); int CrossingPoint = Crossing.Next(C1.Bits.Length); C1 = (chromosome)XPrime[Crossing.Next(XPrime.Count)]; XPrime.Remove(C1); //System.Threading.Thread.Sleep(1); C2 = (chromosome)XPrime[Crossing.Next(XPrime.Count)]; XPrime.Remove(C2); int tempvalue = 0; //if (GetRandomConfirmation(CrossOver_Probability)) if (Crossing.NextDouble() < CrossOver_Probability) { for (int temp2 = CrossingPoint; temp2 < C1.Bits.Length; temp2++) { tempvalue = C1.Bits[temp2]; C1.Bits[temp2] = C2.Bits[temp2]; C2.Bits[temp2] = tempvalue; } } tempXprime.Add(C1); tempXprime.Add(C2); } XPrime.Clear(); XPrime = tempXprime; return(XPrime); }
// Create function that selects the fittest from a chromosome Object array // and a new genration ArrayList based on some percentage passed, the rest are selected randomly static void SelectFittestImproved(chromosome[] X, ref ArrayList XPrime, int Populationsize, double PercentageFittest) { ArrayList AllObjects = new ArrayList(); ArrayList FittestObjects = new ArrayList(); AllObjects = XPrime; chromosome temp_chromosome = (chromosome)AllObjects[0]; Random newItem = new Random(); for (int temp = 0; temp < X.Length; temp++) { AllObjects.Add(X[temp]); } double Min_Fitness = ((chromosome)AllObjects[0]).Fitness; Populationsize = System.Convert.ToInt32(AllObjects.Count / 2 * PercentageFittest); int OriginalSize = AllObjects.Count / 2; for (int temp = 0; temp < Populationsize; temp++) { for (int temp2 = 0; temp2 < AllObjects.Count; temp2++) { if (Min_Fitness > ((chromosome)AllObjects[temp2]).Fitness) { temp_chromosome = (chromosome)AllObjects[temp2]; Min_Fitness = ((chromosome)AllObjects[temp2]).Fitness; } } FittestObjects.Add(temp_chromosome); AllObjects.Remove(temp_chromosome); Min_Fitness = ((chromosome)AllObjects[0]).Fitness; } for (int temp = 0; temp < (OriginalSize - Populationsize); temp++) { temp_chromosome = (chromosome)AllObjects[newItem.Next(AllObjects.Count)]; FittestObjects.Add(temp_chromosome); AllObjects.Remove(temp_chromosome); } for (int temp = 0; temp < X.Length; temp++) { (chromosome)X[temp] = (chromosome)FittestObjects[temp]; } }
// Create the function that calculates the fitness static double GetFitness(chromosome ChromoUnderTest, ref int[] XArray) { int c = 1000; int S = 0; int X = 0; double YPrime = 0; double ReturnValue = 0; //Calculate YPrime for this Chromosome for (int temp = 0; temp < XArray.Length; temp++) { if (ChromoUnderTest.Bits[temp] == 1) { X++; YPrime = YPrime + XArray[temp]; } } S = XArray.Length - X; X = XArray.Length; if (X != S) { YPrime = YPrime / (X - S); } if (S == X) { return(c * Math.Abs(S)); } //Calculate the rest of the return value for (int temp = 0; temp < XArray.Length; temp++) { if (ChromoUnderTest.Bits[temp] == 1) { ReturnValue = ReturnValue + Math.Pow(XArray[temp] - YPrime, 2); } } ReturnValue = ReturnValue + c * Math.Abs(S); return(ReturnValue); }
protected chromosome Crossover(chromosome ParentA, chromosome ParentB) { //inputs - Parent A, Parent B //output - Child chromosome Child = new chromosome(); //generate corssover index int crossoverIdx = (new Random()).Next(ParentA.gene.Count()); //copy up to crossover index of Parent A to Child Child.gene = ParentA.gene.GetRange(0, crossoverIdx); //copy from Parent B to end into Child leaving indexes of genes already in Child blank for (int i = crossoverIdx; i < ParentB.gene.Count(); i++) { int gene = ParentB.gene[i]; if (!Child.gene.Contains(gene)) { Child.gene.Add(gene); } else { Child.gene.Add(Int32.MinValue); } } //iterate through Parent B adding missing genes into next blank in Child if (Child.gene.Contains(Int32.MinValue)) { for (int i = 0; i < ParentB.gene.Count(); i++) { int gene = ParentB.gene[i]; if (!Child.gene.Contains(gene)) { int idx = Child.gene.IndexOf(Int32.MinValue); Child.gene[idx] = gene; } } } return(Child); }
private void GenerationThread(EventWaitHandle handle, List <chromosome> top, int numToGenerate) { for (int i = 0; i < numToGenerate; i++) { chromosome ParentA = this.RouletteWheelSelection(top); chromosome ParentB = this.RouletteWheelSelection(top); //crossover chromosome Child = this.Crossover(ParentA, ParentB); //mutate this.Mutate(Child); this.NextGeneration.Add(Child); } handle.Set(); }
/// <summary> /// 初始化 /// </summary> static void Init() { chromosomes.Clear(); // 染色体数量 int length = 4; int totalFit = 0; for (int i = 0; i < length; i++) { chromosome chromosome = new chromosome(); for (int j = 0; j < chromosome.bits.Length; j++) { //随机出0或者1 Random random = new Random(GetSeed()); int bitValue = random.Next(0, 2); chromosome.bits[j] = bitValue; } //获得十进制的值 int x = DeCode(chromosome.bits); int y = GetFitValue(x); chromosome.fitValue = y; chromosomes.Add(chromosome); //算出total fit { totalFit += chromosome.fitValue; } } for (int i = 0; i < chromosomes.Count; i++) { //计算适应值得百分比,该参数是在用轮盘选择法时需要用到的 chromosomes[i].fitValuePercent = chromosomes[i].fitValue / (totalFit * 1.0f); } chromosomes[0].probability = chromosomes[0].fitValuePercent; for (int i = 1; i < chromosomes.Count; i++) { //上一个的累积概率加上自己的概率,得到自己的累积概率 chromosomes[i].probability = chromosomes[i - 1].probability + chromosomes[i].fitValuePercent; } }
// Create function that selects the fittest from a chromosome Object array // and a new genration ArrayList static void SelectFittest(chromosome[] X, ref ArrayList XPrime, int Populationsize) { ArrayList AllObjects = new ArrayList(); ArrayList FittestObjects = new ArrayList(); AllObjects = XPrime; chromosome temp_chromosome = (chromosome)AllObjects[0]; for (int temp = 0; temp < X.Length; temp++) { AllObjects.Add(X[temp]); } double Min_Fitness = ((chromosome)AllObjects[0]).Fitness; Populationsize = AllObjects.Count / 2; for (int temp = 0; temp < Populationsize; temp++) { for (int temp2 = 0; temp2 < AllObjects.Count; temp2++) { if (Min_Fitness > ((chromosome)AllObjects[temp2]).Fitness) { temp_chromosome = (chromosome)AllObjects[temp2]; Min_Fitness = ((chromosome)AllObjects[temp2]).Fitness; } } FittestObjects.Add(temp_chromosome); AllObjects.Remove(temp_chromosome); Min_Fitness = ((chromosome)AllObjects[0]).Fitness; } for (int temp = 0; temp < X.Length; temp++) { (chromosome)X[temp] = (chromosome)FittestObjects[temp]; } }
static void Main(string[] args) { //Create basic parameters bool Enable_Mutation = true; bool Enable_CrossOver = true; double Mutation_Probability = 0.07; double CrossOver_Probability = 0.10; int Population_Size = 1000; int Iterations = 50; int SizeOfArray = 500; ArrayList Xprime = new ArrayList(); double PercentageFitness = 0.8; double PercentageSupressed = 0.9; int TotalRuns = 50; TextWriter writehandle = new StreamWriter("Output.txt"); //Prepare the output file writehandle.Write("Run Number"); writehandle.Write(","); writehandle.Write("Iteration"); writehandle.Write(","); writehandle.Write("Fitness"); writehandle.Write(","); writehandle.WriteLine("Members"); //Create the original population int[] X; X = new int[40] { 64, 92, 39, 97, 52, 85, 93, 30, 1, 53, 74, 87, 2, 24, 42, 89, 60, 95, 12, 79, 35, 27, 9, 18, 19, 32, 31, 94, 63, 36, 28, 41, 34, 72, 48, 46, 40, 25, 26, 84 }; int ElementsChanged = System.Convert.ToInt32(X.Length * PercentageSupressed); // Begin the total Runs for (int CurrentRun = 0; CurrentRun < TotalRuns; CurrentRun++) { double OldValue = 99999999999; int lastiteration = 0; chromosome tempchromosome = new chromosome(X.Length); //Create the chromosemes Console.WriteLine("Generating the Population"); chromosome[] chromosomes = new chromosome[Population_Size]; for (int temp = 0; temp < Population_Size; temp++) { chromosomes[temp] = new chromosome(X.Length); } // Change the values for certain percentages of the initial chromosomes to supress // High values of X /*Console.WriteLine("Bias Certain number of chromosomes"); * for (int tempChange = 0; tempChange < ElementsChanged; tempChange++) * { * changechromosome(chromosomes[tempChange], X, PercentageSupressed); * }*/ //Begin the main loop for certain numner of iterations for (int CurrentIteration = 0; CurrentIteration < Iterations; CurrentIteration++) { //Calculate the fitness for every member in chromosomes Console.WriteLine("Calculating the Fitness for each chromosome"); for (int temp = 0; temp < Population_Size; temp++) { chromosomes[temp].Fitness = GetFitness(chromosomes[temp], ref X); } //Perform the Crossover Console.WriteLine("Performing CrossOver"); Xprime = CrossOver(CrossOver_Probability, ref chromosomes, ref Xprime); //Perform the mutation Console.WriteLine("Performing Mutation"); Xprime = Mutate(Mutation_Probability, Xprime); // Calcultae Fitness for new Arraylist for (int temp = 0; temp < Population_Size; temp++) { ((chromosome)Xprime[temp]).Fitness = GetFitness(((chromosome)Xprime[temp]), ref X); //Xprime.Add(chromosomes[temp]); } //Select Fittest and put them in X Console.WriteLine("Selecting the fittest"); SelectFittest(chromosomes, ref Xprime, Population_Size); //SelectFittestImproved(chromosomes, ref Xprime, Population_Size, PercentageFitness); //Clear the ArrayList Xprime.Clear(); double tempvalue = chromosomes[0].Fitness; chromosome C1 = chromosomes[0]; for (int temp = 0; temp < chromosomes.Length; temp++) { if (tempvalue > chromosomes[temp].Fitness) { tempvalue = chromosomes[temp].Fitness; C1 = chromosomes[temp]; } } Console.WriteLine(tempvalue); for (int temp = 0; temp < C1.Bits.Length; temp++) { Console.Write(C1.Bits[temp]); } Console.WriteLine(""); if (OldValue > C1.Fitness) { lastiteration = CurrentIteration; tempchromosome = C1; } OldValue = C1.Fitness; if (CurrentIteration == Iterations - 1) { writehandle.Write(CurrentRun); writehandle.Write(","); writehandle.Write(lastiteration); writehandle.Write(","); writehandle.Write(tempchromosome.Fitness); writehandle.Write(",,"); for (int temp = 0; temp < tempchromosome.Bits.Length; temp++) { if (tempchromosome.Bits[temp] == 1) { writehandle.Write(X[temp]); writehandle.Write(","); } } writehandle.WriteLine(""); } } } writehandle.Close(); }
public chromosome(chromosome c) { this.gene = c.gene; this.score = c.score; }