Пример #1
0
        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;
                }
            }
        }
        public override void ApplyCrossoverOpr(Chromosome.Chromosome chromosome1, Chromosome.Chromosome chromosome2)
        {
            int crossoverPoint = GARandom.BoundedRandomInteger(0, Template.DesignVariables.Count - 1);

            for (int i = 0; i <= crossoverPoint; i++)
            {
                double tempGene = chromosome1[i];
                chromosome1[i] = chromosome2[i];
                chromosome2[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;
            }
        }
Пример #4
0
        public override void ApplyMutationOpr(Chromosome.Chromosome chromosome)
        {
            int mutationPosition = GARandom.BoundedRandomInteger(0, Template.DesignVariables.Count);

            double gene = 0;

            if (Template.DesignVariables[mutationPosition].Type == DesignVariableType.Integer)
            {
                gene = GARandom.BoundedRandomInteger((int)Template.DesignVariables[mutationPosition].LowerBound, (int)Template.DesignVariables[mutationPosition].UpperBound);
            }
            else if (Template.DesignVariables[mutationPosition].Type == DesignVariableType.Double)
            {
                gene = GARandom.BoundedRandomDouble(Template.DesignVariables[mutationPosition].LowerBound, Template.DesignVariables[mutationPosition].UpperBound);
            }
            else
            {
                //System.out.println("Invalid Design Variable Type");
                //System.exit(1);
            }

            chromosome[mutationPosition] = gene;
        }
Пример #5
0
        public Chromosome(OptimisationTemplate Template, GAParameters gaParameters, bool empty)
        {
            this.Template     = Template;
            this.gaParameters = gaParameters;


            int noOfDesignVariables = Template.DesignVariables.Count;

            genes = new double[noOfDesignVariables];
            for (int i = 0; i < noOfDesignVariables; i++)
            {
                if (Template.DesignVariables[i].Type == DesignVariableType.Integer)
                {
                    if (!empty)
                    {
                        genes[i] = GARandom.BoundedRandomInteger((int)Template.DesignVariables[i].LowerBound, (int)Template.DesignVariables[i].UpperBound);
                    }
                    else
                    {
                        genes[i] = 0.0;
                    }
                }
                if (Template.DesignVariables[i].Type == DesignVariableType.Double)
                {
                    if (!empty)
                    {
                        genes[i] = GARandom.BoundedRandomDouble(Template.DesignVariables[i].LowerBound, Template.DesignVariables[i].UpperBound);
                    }
                    else
                    {
                        genes[i] = 0.0;
                    }
                }
            }
            constraintValues          = new double[Template.Constraints.Count];
            constraintViolationValues = new double[Template.Constraints.Count];
        }
        public override bool ApplyOn()
        {
            //initialising selection operator
            if (GAParameters.SelectionOprMethod == SelectionOprMethods.TournamentSelectionWithoutReplacement)
            {
                selectionOpr = new TournamentSelectionOprWithoutReplacement(OptimisationTemplate, GAParameters);
            }
            else if (GAParameters.SelectionOprMethod == SelectionOprMethods.TournamentSelectionWithReplacement)
            {
            }

            //initialising crossover operator
            if (GAParameters.CrossoverOprMethod == CrossoverOprMethods.OnePointCrossover)
            {
                crossoverOpr = new OnePointCrossoverOpr(OptimisationTemplate, GAParameters);
            }
            else if (GAParameters.CrossoverOprMethod == CrossoverOprMethods.TwoPointCrossover)
            {
            }
            else if (GAParameters.CrossoverOprMethod == CrossoverOprMethods.UniformCrossover)
            {
            }
            else if (GAParameters.CrossoverOprMethod == CrossoverOprMethods.SimulatedBinaryCrossover)
            {
                crossoverOpr = new SimulatedBinaryCrossoverOpr(OptimisationTemplate, GAParameters);
            }

            //initialising mutation operator
            if (GAParameters.MutationOprMethod == MutationOprMethods.SelectiveMutation)
            {
                mutationOpr = new SelectiveMutationOpr(OptimisationTemplate, GAParameters);
            }
            else if (GAParameters.MutationOprMethod == MutationOprMethods.GenewiseMutation)
            {
            }
            else if (GAParameters.MutationOprMethod == MutationOprMethods.PolynomialMutation)
            {
                mutationOpr = new PolynomialMutationOpr(OptimisationTemplate, GAParameters);
            }

            //initialising elitism operator
            if (GAParameters.ElitismOprMethod == ElitismOprMethods.ProportionalElitism)
            {
                //this.elitismOpr = new ProportionalElitismOpr(Template, gaParameters);
            }
            else if (GAParameters.ElitismOprMethod == ElitismOprMethods.UnproportionalElitism)
            {
                //this.elitismOpr = new UnproportionalElitismOpr(Template, gaParameters);
            }



            var    bestChromosome = new SingleObjectiveChromosome(OptimisationTemplate, GAParameters, false);
            string result         = "";

            //for (int j = 0; j < this.BestChromosome.Genes.Length; j++)
            //result += this.BestChromosome.Genes[j] + "\t";
            //result += this.BestChromosome.ObjectiveValue + "\t";
            //for (int j = 0; j < this.BestChromosome.ConstraintViolationValues.Length; j++)
            //result += this.BestChromosome.ConstraintViolationValues[j] + "\t";
            System.Console.WriteLine(result);



            parentPopulation = new SingleObjectivePopulation(OptimisationTemplate, GAParameters, false);
            childPopulation  = new SingleObjectivePopulation(OptimisationTemplate, GAParameters, false);



            TextWriter solutionsFileWriter = new StreamWriter(GAParameters.EvaluatedSolutionsFile);


            int generationID = 0;

            parentPopulation.Evaluate();

            /*
             * System.Console.WriteLine("after evaluating population");
             * System.Console.WriteLine(this.parentPopulation);
             * System.Console.WriteLine(this.childPopulation);
             */



            //writing to console and text file
            System.Console.WriteLine("Generation: " + generationID);
            double[] x;
            double   y;

            double[] c;
            result = "";


            for (int i = 0; i < parentPopulation.Size; i++)
            {
                x       = ((SingleObjectiveChromosome)(parentPopulation[i])).Genes;
                y       = ((SingleObjectiveChromosome)(parentPopulation[i])).ObjectiveValue;
                c       = ((SingleObjectiveChromosome)(parentPopulation[i])).ConstraintViolationValues;
                result  = "";
                result += y;
                System.Console.WriteLine(result);
                result = "";
                for (int j = 0; j < x.Length; j++)
                {
                    result += x[j] + "\t";
                }
                result += y + "\t";
                for (int j = 0; j < c.Length; j++)
                {
                    result += c[j] + "\t";
                }
                solutionsFileWriter.WriteLine(result);
                System.Console.WriteLine(result);
            }
            System.Console.WriteLine("");

            while (generationID < GAParameters.NoOfGenerations)
            {
                //selection
                int[] matingPoolIndex = selectionOpr.ApplySelectionOpr(parentPopulation);

                /*
                 * for (int i = 0; i < matingPoolIndex.Length; i++)
                 *  System.Console.Write(matingPoolIndex[i] + "***");
                 */
                for (int i = 0; i < parentPopulation.Size; i++)
                {
                    childPopulation[i] = (SingleObjectiveChromosome)parentPopulation[matingPoolIndex[i]];
                }

                /*
                 * System.Console.WriteLine("after selection");
                 * System.Console.WriteLine(this.parentPopulation);
                 * System.Console.WriteLine(this.childPopulation);
                 */

                //crossover
                for (int i = 0; i < parentPopulation.Size; i += 2)
                {
                    if (GARandom.Flip(GAParameters.CrossoverProbability))
                    {
                        crossoverOpr.ApplyCrossoverOpr(childPopulation[i], childPopulation[i + 1]);
                    }
                }

                /*
                 * System.Console.WriteLine("after crossover");
                 * System.Console.WriteLine(this.parentPopulation);
                 * System.Console.WriteLine(this.childPopulation);
                 */

                //mutation
                for (int i = 0; i < parentPopulation.Size; i++)
                {
                    //if (GARandom.coinFlip(gaSet.getMutationProbability())) {
                    mutationOpr.ApplyMutationOpr(childPopulation[i]);
                    //}
                }

                /*
                 * System.Console.WriteLine("after mutation");
                 * System.Console.WriteLine(this.parentPopulation);
                 * System.Console.WriteLine(this.childPopulation);
                 */

                childPopulation.Evaluate();

                /*
                 * System.Console.WriteLine("after evaluating childPopulation");
                 * System.Console.WriteLine(this.parentPopulation);
                 * System.Console.WriteLine(this.childPopulation);
                 */

                //this.newPopulation.computeObjectiveStatistics();
                //this.newPopulation.computeFitnessStatistics();


                for (int i = 0; i < childPopulation.Size; i++)
                {
                    x      = ((SingleObjectiveChromosome)(childPopulation[i])).Genes;
                    y      = ((SingleObjectiveChromosome)(childPopulation[i])).ObjectiveValue;
                    c      = ((SingleObjectiveChromosome)(childPopulation[i])).ConstraintViolationValues;
                    result = "";
                    for (int j = 0; j < x.Length; j++)
                    {
                        result += x[j] + "\t";
                    }
                    result += y + "\t";
                    for (int j = 0; j < c.Length; j++)
                    {
                        result += c[j] + "\t";
                    }
                    solutionsFileWriter.WriteLine(result);
                }



                bool useElitism = true;
                if (useElitism)
                {
                    var elitismOpr = new ProportionalElitismOpr(OptimisationTemplate, GAParameters, parentPopulation, childPopulation);
                    elitismOpr.ApplyElitismOpr();
                }
                else
                {
                }

                /*
                 * System.Console.WriteLine("after elitism operator");
                 * System.Console.WriteLine(this.parentPopulation);
                 * System.Console.WriteLine(this.childPopulation);
                 */

                parentPopulation.Evaluate();

                /*
                 * System.Console.WriteLine("after elitism operator and then evaluating");
                 * System.Console.WriteLine(this.parentPopulation);
                 * System.Console.WriteLine(this.childPopulation);
                 */

                //this.parentPopulation.ComputeObjectiveStatistics();
                //this.parentPopulation.ComputeFitnessStatistics();

                /*
                 * generationID++;
                 * System.Console.WriteLine("Generation: " + generationID);
                 * fileWriter.WriteLine("Generation: " + generationID);
                 * System.Console.WriteLine("Best Solution:");
                 * fileWriter.WriteLine("Best Solution:");
                 * parentPopulation.ComputeBestChromosome();
                 * System.Console.WriteLine(parentPopulation.BestChromosome);
                 * fileWriter.WriteLine(parentPopulation.BestChromosome);
                 */
                generationID++;
                //writing to console
                System.Console.WriteLine("Generation: " + generationID);

                // Best chromosome
                System.Console.WriteLine("Best Solution:");
                parentPopulation.ComputeBestChromosome();
                parentPopulation.GetBestChromosome().EvaluateObjective();
                parentPopulation.GetBestChromosome().EvaluateConstraints();
                x      = parentPopulation.GetBestChromosome().Genes;
                y      = parentPopulation.GetBestChromosome().ObjectiveValue;
                c      = parentPopulation.GetBestChromosome().ConstraintViolationValues;
                result = "";
                for (int j = 0; j < x.Length; j++)
                {
                    result += x[j] + "\t";
                }
                result += y + "\t";
                for (int j = 0; j < c.Length; j++)
                {
                    result += c[j] + "\t";
                }
                System.Console.WriteLine(result);
                System.Console.WriteLine("");
            }

            solutionsFileWriter.Close();
            return(true);
        }
Пример #7
0
        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;
                    }
                }
            }
        }
 static RandomFactory()
 {
     Rand = new GARandom();
 }