private static Chromosome OnePointBinary(Chromosome mother, Chromosome father) { // implemented for binary encoding Chromosome child = new Chromosome(mother); // for array allocation for (int i = 0; i < child.NoGenes; i++) { int len = child.BinaryGenes[i].Length; int pivot = (int)(ExtendedRandom.NextUniform() * len); if (ExtendedRandom.NextUniform() < _info.Probability) { child.BinaryGenes[i] = mother.BinaryGenes[i].Substring(0, pivot) + father.BinaryGenes[i].Substring(pivot); } else // no crossover, copy one parent { if (ExtendedRandom.NextUniform() < 0.5) { child.BinaryGenes[i] = mother.BinaryGenes[i]; } else { child.BinaryGenes[i] = father.BinaryGenes[i]; } } } return(child); }
private static void ExchangePermutation(Chromosome child) { // implemented for permutation encoding for (int i = 0; i < child.NoGenes; i++) { int len = child.PermutationGenes[i].Length; if (ExtendedRandom.NextUniform() < _info.Probability) { // generate exchange points int p1 = (int)(ExtendedRandom.NextUniform() * len); int p2 = p1; while (p2 == p1) { p2 = (int)(ExtendedRandom.NextUniform() * len); } int auxi = child.PermutationGenes[i][p1]; child.PermutationGenes[i][p1] = child.PermutationGenes[i][p2]; child.PermutationGenes[i][p2] = auxi; } } }
private static void ResetBinary(Chromosome child) { // implemented for binary encoding for (int i = 0; i < child.NoGenes; i++) { string newGene = ""; for (int j = 0; j < child.BinaryGenes[i].Length; j++) { if (ExtendedRandom.NextUniform() < _info.Probability) { if (child.BinaryGenes[i][j] == '0') { newGene += "1"; } else { newGene += "0"; } } else { newGene += child.BinaryGenes[i][j]; } } child.BinaryGenes[i] = newGene; } }
private static Chromosome OnePointPermutation(Chromosome mother, Chromosome father) { // implemented for permutation encoding Chromosome child = new Chromosome(mother); // for array allocation for (int i = 0; i < child.NoGenes; i++) { int len = child.PermutationGenes[i].Length; int pivot = (int)(ExtendedRandom.NextUniform() * len); if (ExtendedRandom.NextUniform() < _info.Probability) { List <int> values = new List <int>(); int j = 0; for (; j < pivot; j++) { // child.PermutationGenes[i][j] = mother.PermutationGenes[i][j]; values.Add(mother.PermutationGenes[i][j]); } // add all the values in the father, in order, except those already added from the mother int k = 0; while (j < len) { if (values.Contains(father.PermutationGenes[i][k])) { k++; } else { child.PermutationGenes[i][j] = father.PermutationGenes[i][k]; j++; k++; } } } else // no crossover, copy one parent { if (ExtendedRandom.NextUniform() < 0.5) { for (int j = 0; j < len; j++) { child.PermutationGenes[i][j] = mother.PermutationGenes[i][j]; } } else { for (int j = 0; j < len; j++) { child.PermutationGenes[i][j] = father.PermutationGenes[i][j]; } } } } return(child); }
private static void ResetReal(Chromosome child, Parameters parameters) { // implemented for real valued encoding for (int i = 0; i < child.NoGenes; i++) { if (ExtendedRandom.NextUniform() < _info.Probability) { child.RealGenes[i] = parameters.EncodingInfo.MinValues[i] + ExtendedRandom.NextUniform() * (parameters.EncodingInfo.MaxValues[i] - parameters.EncodingInfo.MinValues[i]); } } }
private static Chromosome Arithmetic(Chromosome mother, Chromosome father) { Chromosome child = new Chromosome(mother); if (ExtendedRandom.NextUniform() < _info.Probability) { for (int i = 0; i < child.NoGenes; i++) { double point = ExtendedRandom.NextUniform(); child.RealGenes[i] = point * mother.RealGenes[i] + (1 - point) * father.RealGenes[i]; } } return(child); }
private static Chromosome ArithmeticInteger(Chromosome mother, Chromosome father) { Chromosome child = new Chromosome(mother); if (ExtendedRandom.NextUniform() < _info.Probability) { for (int i = 0; i < child.NoGenes; i++) { if (ExtendedRandom.NextUniform() < 0.5) { child.RealGenes[i] = father.RealGenes[i]; } } } return(child); }
private static void GaussianReal(Chromosome child, Parameters parameters) { // implemented for real valued encoding for (int i = 0; i < child.NoGenes; i++) { if (ExtendedRandom.NextUniform() < _info.Probability) { child.RealGenes[i] = ExtendedRandom.NextNormal(child.RealGenes[i], _info.StandardDeviation); if (child.RealGenes[i] < parameters.EncodingInfo.MinValues[i]) { child.RealGenes[i] = parameters.EncodingInfo.MinValues[i]; } if (child.RealGenes[i] > parameters.EncodingInfo.MaxValues[i]) { child.RealGenes[i] = parameters.EncodingInfo.MaxValues[i]; } } } }
private static Chromosome MultiplePoint(Chromosome mother, Chromosome father) { // implemented for binary encoding Chromosome child = new Chromosome(mother); // for array allocation for (int i = 0; i < child.NoGenes; i++) { int len = child.BinaryGenes[i].Length; if (ExtendedRandom.NextUniform() < _info.Probability) { // generate crossover points bool[] selected = new bool[len]; for (int j = 0; j < len; j++) { selected[j] = false; } for (int j = 0; j < _info.NoPoints; j++) { bool ok = false; while (!ok) { int crtSel = (int)(ExtendedRandom.NextUniform() * len); if (!selected[crtSel]) { selected[crtSel] = true; ok = true; } } } bool selMother = true; // switch between mother genes and father genes string newGene = ""; for (int j = 0; j < len; j++) { if (selected[j]) { selMother = !selMother; } if (selMother) { newGene += mother.BinaryGenes[i][j]; } else { newGene += father.BinaryGenes[i][j]; } } child.BinaryGenes[i] = newGene; } else // no crossover, copy one parent { if (ExtendedRandom.NextUniform() < 0.5) { child.BinaryGenes[i] = mother.BinaryGenes[i]; } else { child.BinaryGenes[i] = father.BinaryGenes[i]; } } } return(child); }
public override void Solve(Problem problem) { if (_initialValue == null) { throw new Exception("You must provide a starting point for the search"); } _problem = problem; _population = new Chromosome[_noNeighbors]; bool searchOver = false; int generations = 0; Chromosome best = new Chromosome(_initialValue); while (!searchOver) { _population[0] = best; _population[0].ComputeFitness(_problem); // generating population by mutation for (int i = 1; i < _noNeighbors; i++) { _population[i] = new Chromosome(best); double r = _rand.NextDouble(); if (r < Settings.pmg) { Mutation.PerformMutation(_population[i], _problem.Parameters); // mutatie gaussiana } else if (r < Settings.pmg + Settings.pmr) // mutatie prin resetare { for (int j = 0; j < _population[i].NoGenes; j++) { if (_rand.NextDouble() < Settings.pm) { _population[i].RealGenes[j] = _problem.Parameters.EncodingInfo.MinValues[j] + ExtendedRandom.NextUniform() * (_problem.Parameters.EncodingInfo.MaxValues[j] - _problem.Parameters.EncodingInfo.MinValues[j]); } } } else { int i1 = _rand.Next(_population[i].RealGenes.Length); int i2 = _rand.Next(_population[i].RealGenes.Length); if (_population[i].RealGenes[i1] >= 2 && _population[i].RealGenes[i2] >= 2) { _population[i].RealGenes[i1] -= 1; _population[i].RealGenes[i2] += 1; } } _population[i].ComputeFitness(_problem); } best = Selection.GetBest(_population); generations++; if (generations > _noIterations) { _solution = best; searchOver = true; } } }