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); }
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); }
public override bool Mutate(double mRate) { if (_randomSeed.NextDouble() <= mRate) { double tempValue = double.NaN; while (!GeneConstraint.ConstraintFunction(tempValue)) { tempValue = GeneValue + (((_randomSeed.Next(10) % 2 == 0) ? -1 : 1) * _randomSeed.NextDouble() / 10.0) * GeneValue; } GeneValue = tempValue; return(true); } return(false); }
public override bool WithinConstraints() { return(GeneConstraint.ConstraintFunction(GeneValue)); }
public RealCodedGene(double value, Random r, GeneConstraint g) : base(r) { GeneValue = value; GeneConstraint = g; }
public BinaryGene(bool[] value, Random r, GeneConstraint c) : base(r) { GeneValue = value; GeneConstraint = c; }