void PolynomialMutation(IEvolutionState state, IMersenneTwister random, FloatVectorSpecies species, int index) { double eta_m = species.GetMutationDistributionIndex(index); bool alternativePolynomialVersion = species.GetPolynomialIsAlternative(index); double rnd, delta1, delta2, mut_pow, deltaq; double y, yl, yu, val, xy; double y1; y1 = y = genome[index]; // ind[index]; yl = species.GetMinGene(index); // min_realvar[index]; yu = species.GetMaxGene(index); // max_realvar[index]; delta1 = (y - yl) / (yu - yl); delta2 = (yu - y) / (yu - yl); int totalTries = species.OutOfBoundsRetries; int tries = 0; for (tries = 0; tries < totalTries || totalTries == 0; tries++) // keep trying until totalTries is reached if it's not zero. If it's zero, go on forever. { rnd = random.NextDouble(); mut_pow = 1.0 / (eta_m + 1.0); if (rnd <= 0.5) { xy = 1.0 - delta1; val = 2.0 * rnd + (alternativePolynomialVersion ? (1.0 - 2.0 * rnd) * (Math.Pow(xy, (eta_m + 1.0))) : 0.0); deltaq = Math.Pow(val, mut_pow) - 1.0; } else { xy = 1.0 - delta2; val = 2.0 * (1.0 - rnd) + (alternativePolynomialVersion ? 2.0 * (rnd - 0.5) * (Math.Pow(xy, (eta_m + 1.0))) : 0.0); deltaq = 1.0 - (Math.Pow(val, mut_pow)); } y1 = y + deltaq * (yu - yl); if (!species.GetMutationIsBounded(index) || (y1 >= yl && y1 <= yu)) { break; // yay, found one } } // at this point, if tries is totalTries, we failed if (totalTries != 0 && tries == totalTries) { // just randomize y1 = (double)(species.GetMinGene(index) + random.NextDouble(true, true) * (species.GetMaxGene(index) - species.GetMinGene(index))); //(double)(min_realvar[index] + random.NextDouble() * (max_realvar[index] - min_realvar[index])); species.OutOfRangeRetryLimitReached(state); // it better get inlined } genome[index] = y1; // ind[index] = y1; }
void GaussianMutation(IEvolutionState state, IMersenneTwister random, FloatVectorSpecies species, int index) { double val; double min = species.GetMinGene(index); double max = species.GetMaxGene(index); double stdev = species.GetGaussMutationStdev(index); int outOfBoundsLeftOverTries = species.OutOfBoundsRetries; bool givingUpAllowed = species.OutOfBoundsRetries != 0; do { val = random.NextGaussian() * stdev + genome[index]; outOfBoundsLeftOverTries--; if (species.GetMutationIsBounded(index) && (val > max || val < min)) { if (givingUpAllowed && (outOfBoundsLeftOverTries == 0)) { val = min + random.NextFloat() * (max - min); species.OutOfRangeRetryLimitReached(state);// it better get inlined break; } } else { break; } }while (true); genome[index] = val; }
void FloatResetMutation(IMersenneTwister random, FloatVectorSpecies species, int index) { double minGene = species.GetMinGene(index); double maxGene = species.GetMaxGene(index); genome[index] = minGene + random.NextDouble(true, true) * (maxGene - minGene); }
void IntegerResetMutation(IMersenneTwister random, FloatVectorSpecies species, int index) { long minGene = (long)Math.Floor(species.GetMinGene(index)); long maxGene = (long)Math.Floor(species.GetMaxGene(index)); genome[index] = RandomValueFromClosedInterval(minGene, maxGene, random); //minGene + random.NextLong(maxGene - minGene + 1); }
void IntegerRandomWalkMutation(IMersenneTwister random, FloatVectorSpecies species, int index) { double min = species.GetMinGene(index); double max = species.GetMaxGene(index); if (!species.GetMutationIsBounded(index)) { // okay, technically these are still bounds, but we can't go beyond this without weird things happening max = MAXIMUM_INTEGER_IN_DOUBLE; min = -(max); } do { int n = (int)(random.NextBoolean() ? 1 : -1); double g = Math.Floor(genome[index]); if ((n == 1 && g < max) || (n == -1 && g > min)) { genome[index] = g + n; } else if ((n == -1 && g < max) || (n == 1 && g > min)) { genome[index] = g - n; } }while (random.NextBoolean(species.GetRandomWalkProbability(index))); }
protected String Map(IEvolutionState state, double[] genome, FloatVectorSpecies species, int index) { if (index < 0 || index >= Domain.Length) { state.Output.Fatal("No domain provided for meta parameter number " + index + "."); } Object d = Domain[index]; double min = species.GetMinGene(index); double max = species.GetMaxGene(index); double gene = genome[index]; if (d is bool[]) { if (gene < min || gene > max) { state.Output.Fatal("Gene index " + index + " has a value (" + gene + ") outside the min-max range (from " + min + " to " + max + " inclusive). Did you forget to bound the mutation?"); } else if (gene < (min + max) / 2.0) { return("false"); } else { return("true"); } } else if (d is int[]) { return("" + (int)(Math.Floor(gene))); } else if (d is double[]) { return("" + gene); } else if (d is String[]) { var dom = (String[])d; if (!min.Equals(0)) { state.Output.Fatal("Invalid min-gene value (" + min + ") for a string type in MetaProblem. Gene index was " + index + ". Should have been 0."); } else if (!max.Equals(dom.Length - 1)) { state.Output.Fatal("Invalid max-gene value (" + max + ") for a string type in MetaProblem. Gene index was " + index + ". Should have been " + (dom.Length - 1) + ", that is, the number of vals - 1."); } else if (gene < min || gene > max) { state.Output.Fatal("Gene index " + index + " has a value (" + gene + ") outside the min-max range (from " + min + " to " + max + " inclusive). Did you forget to bound the mutation?"); } else { return(dom[(int)(Math.Floor(gene))]); } } else { state.Output.Fatal("INTERNAL ERROR. Invalid mapping for domain of meta parameter number " + index + " in MetaProblem."); } return(null); // never happens }