Skip to main content
. 2018 Oct 12;20(10):781. doi: 10.3390/e20100781
  •  public class MutationOperator : IMutationOperator

  •  {

  •    private static MersenneTwister rand = new MersenneTwister();

  •    public double MutationProbability { get; set; }

  •    public MutationOperator(double probability = 0.0) {this.MutationProbability = probability;}

  •    public ISolution Execute(IPopulation population)

  •    {

  •       double ifMutate;

  •       Solution solToReturn = null;

  •       foreach(Solution sol in population){

  •          ifMutate = rand.NextDouble();

  •         if(ifMutate <= this.MutationProbability){

  •          int selectedChromosome = rand.Next(0, sol.Size - 1);

  •             int bitsInChromosome = (int)(Math.Log(sol.Size, 2.0) + 1);

  •             int selectedQbit = rand.Next(0, bitsInChromosome - 1);

  •             sol[selectedChromosome][selectedQbit].ExecuteNotGate();

  •             sol.toPermutation();

  •             solToReturn = new Solution(sol);

  •             break;

  •          }

  •        }

  •        return solToReturn;

  •     }

  • }

  • ....

  •  

  • public class RotationGateOperator : IEvolutionaryOperator

  • {

  • ...

  •    public void ExecuteOriginal(IPopulation population, Solution best, double alpha)

  •    {

  •       double theta;

  •       double alphaTimesBeta;

  •       double angle = 0.0;

  •       double sign = 0.0;

  •       this.solSize = best.Size;

  •       this.bitsInSol = (int)(Math.Log(this.solSize, 2.0) + 1);

  •       Solution prevSolution = null;

  •  

  •       foreach (Solution sol in population){

  •        if (sol.Goal >best.Goal){

  •          prevSolution  = new Solution(sol);

  •          for (int i = 0; i < this.solSize; i++){

  •             for (int j = 0; j < this.bitsInSol; j++){

  •                theta = 0.0;

  •                alphaTimesBeta = sol[i][j].Alpha * sol[i][j].Beta;

  •                angle = 0.0;

  •                sign = 0.0;

  •                if (sol[i][j].ObservedState == 1 &&  best[i][j].ObservedState == 0){

  •                   if (alphaTimesBeta > 0.0) sign = -1.0;

  •                   else if (alphaTimesBeta < 0.0) sign = 1.0;

  •                   else if (sol[i][j].Alpha == 0.0){

  •                      double d = rand.NextDouble();

  •                      if (d > 0.5) sign = 1;

  •                      else sign = -1.0;

  •                   }

  •                   angle = 0.5 * Math.PI;

  •                }

  •                else if (sol[i][j].ObservedState == 1 && best[i][j].ObservedState == 1){

  •                   if (alphaTimesBeta > 0.0) sign = 1.0;

  •                   else if (alphaTimesBeta < 0.0) sign = -1.0;

  •                   else if (sol[i][j].Beta == 0.0){

  •                      double d = rand.NextDouble();

  •                      if (d > 0.5) sign = 1.0;

  •                      else sign = -1.0;

  •                   }

  •                   angle = 0.2 * Math.PI;

  •                }

  •                theta = angle * sign;

  •                if (theta != 0.0) sol[i][j].ExecuteRotationGate(theta*alpha);

  •             }

  •          }

  •          sol.toPermutation();

  •          if (sol.Goal > prevSolution.Goal) sol.BestSolution = prevSolution;

  •       }

  •    }

  •  }

  • }