コード例 #1
0
        /// <summary>
        /// BASIC CALCULATION NECESSARY FOR FITNESS
        /// </summary>
        /// <param name="r"></param>
        /// <param name="c"></param>
        public override void GenerationRan(ref GADataSet.SolutionsRow r, ref IChromosome chromosome)
        {
            GADataSet.DataRow d = r.DataAxuliar.NewDataRow();
            r.DataAxuliar.AddDataRow(d);
            try
            {
restart:

                Gene[]   genes = chromosome.GetGenes();
                object[] fitnessMatrixCounts = FitnessRawEvaluator(r.IsChromosomeNull(), ref chromosome);
                //matrix aqui

                d.ExternalDataObject = fitnessMatrixCounts[1];
                //fitness aqui
                double fitness = (double)fitnessMatrixCounts[0];
                r.Fitness = fitness;
                if (fitnessMatrixCounts[2] != null)
                {
                    SystemException ex = fitnessMatrixCounts[2] as SystemException;
                    r.Okays = ex.Message;
                }
                else
                {
                    int  rotNew     = (int)genes[0].Value;
                    int  rotOld     = rotNew;
                    bool changeGene = false;


                    if (this.GARow != null)
                    {
                        if (!this.GARow.IsFitnessNull())
                        {
                            double comparableFitness = this.GARow.Fitness;

                            if (fitness <= comparableFitness)
                            {
                                StagnationCounter++;
                            }

                            else
                            {
                                StagnationCounter = 0;
                            }

                            if (StagnationCounter > 15)
                            {
                            }
                            else if (StagnationCounter > 10)
                            {
                                if (rotNew + 180 <= 180)
                                {
                                    rotNew += 180;
                                }
                                else if (rotNew - 180 >= -180)
                                {
                                    rotNew -= 180;
                                }
                                changeGene = true;
                            }
                            else if (StagnationCounter > 5)
                            {
                                if (rotNew + 90 <= 180)
                                {
                                    rotNew += 90;
                                }
                                else if (rotNew - 90 >= -180)
                                {
                                    rotNew -= 90;
                                }
                                changeGene = true;
                            }

                            if (changeGene)
                            {
                                chromosome.ReplaceGene(0, new Gene(rotNew));
                                StagnationCounter = 0;
                                goto restart;
                            }


                            decimal[] counts = fitnessMatrixCounts[3] as decimal[];
                            if (counts != null)
                            {
                                r.Okays = string.Concat(counts.SelectMany(o => Decimal.Round(o, 2).ToString() + ","));
                            }

                            if (repairCounter == 10)
                            {
                                //   this.util.MaxTX = Convert.ToInt32(this.util.MaxTX * 0.95);
                                repairCounter = 0;
                            }
                            if (repairCounter == 10)
                            {
                                //  this.util.MaxTY = Convert.ToInt32(this.util.MaxTY * 0.95);
                                repairCounter = 0;
                            }

                            this.util.readjustSearchSpace(ref genes, fitness, comparableFitness);
                        }
                    }

                    if (GA.GenerationsNumber == 20 && specificAdam != null)
                    {
                        // GA.Population.CurrentGeneration.Chromosomes.RemoveAt(GA.Population.CurrentGeneration.Chromosomes.Count() - 1);
                        //
                        GA.Population.CurrentGeneration.Chromosomes.Add(specificAdam);

                        // GA.Population.CreateNewGeneration(GA.Population.CurrentGeneration.Chromosomes);

                        //   GA.Crossover.Cross(GA.Population.CurrentGeneration.Chromosomes);
                        //  GA.BestChromosome.Fitness = specificAdam.Fitness;
                        //  GA.Population.CurrentGeneration.BestChromosome.ReplaceGenes(0,specificAdam.GetGenes());

                        //
                        //   GA.Population.CurrentGeneration.Chromosomes.RemoveAt(GA.Population.CurrentGeneration.Chromosomes.Count() - 1);
                        //Add(specificAdam);
                        //    GA.Population.CurrentGeneration.Chromosomes.Add(specificAdam);
                        //  chromosome.ReplaceGenes(0, specificAdam.GetGenes());
                        //     chromosome.Fitness = specificAdam.Fitness;
                        //   chromosome = specificAdam;
                    }
                }
                r.Genotype = Aid.SetStrings(r.GenesAsInts, " | ", 0, "0");
            }
            catch (Exception ex)
            {
                r.Okays = ex.Message;// + ex.InnerException?.Message;
            }
        }