private void BinaryCrossover(BinaryGene gene1, BinaryGene gene2, out Gene child1, out Gene child2) { int crossPoint = _randomSeed.Next(gene1.GeneValue.Length); bool[] a = new bool[gene1.GeneValue.Length]; bool[] b = new bool[gene1.GeneValue.Length]; do { for (int i = 0; i < crossPoint; i++) { a[i] = gene1.GeneValue[i]; b[i] = gene2.GeneValue[i]; } for (int i = crossPoint; i < gene1.GeneValue.Length; i++) { a[i] = gene2.GeneValue[i]; b[i] = gene1.GeneValue[i]; } } while (!GeneConstraint.ConstraintFunction(a) || !GeneConstraint.ConstraintFunction(b)); child1 = new BinaryGene(a, _randomSeed, gene1.GeneConstraint); child2 = new BinaryGene(b, _randomSeed, gene2.GeneConstraint); }
private void SimulatedBinaryCrossover(RealCodedGene g1, RealCodedGene g2, out Gene child1, out Gene child2) { //n_c is a parameter that controls the crossover process. A high value of the parameter will create near-parent solution const double n_c = .05; double a, b; do { double random = _randomSeed.NextDouble(); double beta = (random < 0.5) ? Math.Pow((2 * random), (1 / (n_c + 1))) : Math.Pow(1 / (2 * (1 - random)), (1 / (n_c + 1))); var x_i1 = g1.GeneValue; var x_i2 = g2.GeneValue; a = 0.5 * ((1 + beta) * x_i1 + (1 - beta) * x_i2); b = 0.5 * ((1 - beta) * x_i1 + (1 + beta) * x_i2); } while (!GeneConstraint.ConstraintFunction(a) || !GeneConstraint.ConstraintFunction(b)); child1 = new RealCodedGene(g1.GeneValue, _randomSeed, g1.GeneConstraint); child2 = new RealCodedGene(g1.GeneValue, _randomSeed, g2.GeneConstraint); }
public abstract void Crossover(Gene gene2, out Gene child1, out Gene child2);
public override void Crossover(Gene gene2, out Gene child1, out Gene child2) { SimulatedBinaryCrossover(this, (RealCodedGene)gene2, out child1, out child2); }
public override void Crossover(Gene gene2, out Gene child1, out Gene child2) { BinaryCrossover(this, (BinaryGene)gene2, out child1, out child2); }