public override void ApplyMutationOpr(Chromosome.Chromosome chromosome) { double minGeneVal, maxGeneVal; //minimum ans maximum values for a gene double geneVal; //for storing gene value of the chromosome double delta; double randomNo; double value, deltaq; double tempGene; int polynomialOrder = gaParameters.MutationPolynomialOrder; for (int i = 0; i < Template.DesignVariables.Count; i++) { geneVal = chromosome[i]; // Flip a biased coin with mutation probability if (GARandom.Flip(gaParameters.MutationProbability)) { minGeneVal = Template.DesignVariables[i].LowerBound; maxGeneVal = Template.DesignVariables[i].UpperBound; if ((geneVal - minGeneVal) < (maxGeneVal - geneVal)) //if gene value is closer to the minimum gene value, then computing slope with gene value and minimum gene value { delta = (geneVal - minGeneVal) / (maxGeneVal - minGeneVal); } else //if gene value is closer to the maximum gene value (or in middle), then computing the slope with gene value and maximum gene value { delta = (maxGeneVal - geneVal) / (maxGeneVal - minGeneVal); } randomNo = GARandom.random01(); if (randomNo <= 0.5) { value = 2.0 * randomNo + (1.0 - 2.0 * randomNo) * Math.Pow(1.0 - delta, (double)(polynomialOrder + 1)); deltaq = Math.Pow(value, 1.0 / ((double)(polynomialOrder + 1))) - 1.0; } else { value = 2.0 * (1.0 - randomNo) + 2.0 * (randomNo - 0.5) * Math.Pow(1.0 - delta, (double)(polynomialOrder + 1)); deltaq = 1.0 - Math.Pow(value, 1.0 / ((double)(polynomialOrder + 1))); } tempGene = geneVal + (maxGeneVal - minGeneVal) * deltaq; if (Template.DesignVariables[i].Type == DesignVariableType.Integer) { tempGene = Math.Round(tempGene); } if (tempGene < minGeneVal) { tempGene = minGeneVal; } else if (tempGene > maxGeneVal) { tempGene = maxGeneVal; } chromosome[i] = tempGene; } } }
//shuffles an array containing indices (will be used by both tournament selection operators, i.e. one without replacement and other with replacement) // protected void shuffleArray(int[] index) // { // int temp; //used for swapping // for (int i = 0; i < index.Length - 2; i++) // { // int randomIndex = GARandom.BoundedRandomInteger(i, index.Length); // //swapping // if (i != randomIndex) // { // temp = index[i]; // index[i] = index[randomIndex]; // index[randomIndex] = temp; // } // } // } //same implementation as in C++ to get same results protected void shuffleArray(int[] index) { int i, temp, randomIndex, D1, D2; for (i = 1, D1 = 1, D2 = (index.Length - i + D1) / D1; D2 > 0; D2--, i += D1) { randomIndex = (int)((index.Length - i) * GARandom.random01() + i); temp = index[randomIndex - 1]; index[randomIndex - 1] = index[i - 1]; index[i - 1] = temp; } }
public override void ApplyCrossoverOpr(Chromosome.Chromosome chromosome1, Chromosome.Chromosome chromosome2) { double minGene, maxGene; double gene1, gene2; double beta, betaq; double alpha; double tempGene; //***********************************need to delete, just for comparison*********************************** var c1 = new Chromosome.MultiObjectiveChromosome(Template, gaParameters, false); var c2 = new Chromosome.MultiObjectiveChromosome(Template, gaParameters, false); //***********************************need to delete, just for comparison*********************************** if (GARandom.Flip(gaParameters.CrossoverProbability)) { for (int i = 0; i < Template.DesignVariables.Count; i++) { if (GARandom.Flip(genewiseSwapProb) && Math.Abs(chromosome1[i] - chromosome2[i]) >= 1.0E-10) { minGene = Template.DesignVariables[i].LowerBound; maxGene = Template.DesignVariables[i].UpperBound; if (chromosome2[i] > chromosome1[i]) { gene1 = chromosome1[i]; gene2 = chromosome2[i]; } else { gene1 = chromosome2[i]; gene2 = chromosome1[i]; } if ((gene1 - minGene) > (maxGene - gene2)) { beta = (gene2 - gene1) / (2.0 * maxGene - gene2 - gene1); } else { beta = (gene2 - gene1) / (gene2 + gene1 - 2.0 * minGene); } alpha = GARandom.random01() * (2.0 - Math.Pow(beta, (polynomialOrder + 1))); if (alpha <= 1.0) { betaq = Math.Pow(alpha, 1.0 / ((double)(polynomialOrder + 1))); } else { betaq = Math.Pow(1.0 / (2.0 - alpha), 1.0 / ((double)(polynomialOrder + 1))); } tempGene = 0.5 * ((gene1 + gene2) - betaq * (gene2 - gene1)); if (Template.DesignVariables[i].Type == DesignVariableType.Integer) { tempGene = (int)(tempGene + 0.5); } chromosome1[i] = tempGene; tempGene = 0.5 * ((gene1 + gene2) + betaq * (gene2 - gene1)); if (Template.DesignVariables[i].Type == DesignVariableType.Integer) { tempGene = (int)(tempGene + 0.5); } chromosome2[i] = tempGene; } } } }