예제 #1
0
        public override void GACompleted <T>(ref GADataSet.SolutionsRow r, ref T s)
        {
            try
            {
                GADataSet.StringsRow str = s as GADataSet.StringsRow;

                // r.Chromosome //= s.Chromosome = System.IO.File.ReadAllBytes(scriptFile + ".gif");

                GADataSet.DataRow aux = r.DataAxuliar.FirstOrDefault();


                //  Bitmap b = mat.Bitmap;
                //  escaledUI = original.Resize(w / scale, h / scale, Emgu.CV.CvEnum.Inter.LinearExact, true);

                bool ok = r.IsChromosomeNull();
                ok = ok && aux.ExternalDataObject != null;
                if (ok)
                {
                    object o     = aux.ExternalDataObject;
                    string texto = r.Genotype + " F=" + Decimal.Round(Convert.ToDecimal(r.Fitness), 2).ToString();
                    r.Chromosome = Img.ExtractImage(ref o, texto);

                    all.Add(o);

                    //   im.Dispose();
                }

                //mat.ToImage<Rgba,byte>().ToJpegData();
            }
            catch (Exception ex)
            {
            }


            //   r.Chromosome = imagen.expandedEscaledUIToCompare.Resize(mat.Width / 5, mat.Height / 5, Emgu.CV.CvEnum.Inter.LinearExact).ToJpegData();
        }
예제 #2
0
파일: Form.cs 프로젝트: fulviofarina/GAlgo
        private void dgvDoubleMouseclick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            DataGridView dgv = (sender as DataGridView);

            if (dgv.Rows.Count == 0)
            {
                return;
            }

            dynamic dgvr = dgv.Rows[e.RowIndex].DataBoundItem;

            if (dgvr == null)
            {
                return;
            }

            if (sender.Equals(this.problemsDataGridView))
            {
                GADataSet.ProblemsRow currentProblem = dgvr.Row as GADataSet.ProblemsRow;

                //   this.ConditionsBS.Filter = this.gADataSet.Conditions.ProblemIDColumn.ColumnName + "=" + currentProblem.ProblemID;

                //   this.DataBS.Filter = this.gADataSet.Data.ProblemIDColumn.ColumnName + "=" + currentProblem.ProblemID;

                this.gABS.SuspendBinding();

                this.SolBS.SuspendBinding();

                taControl1.FillProblemData(currentProblem.ProblemID);

                this.gABS.Filter = this.gADataSet.GA.ProblemIDColumn.ColumnName + "=" + currentProblem.ProblemID;
                this.gABS.Sort   = this.gADataSet.GA.IDColumn.ColumnName + " desc";

                this.SolBS.Filter = this.gADataSet.Solutions.ProblemIDColumn.ColumnName + "=" + currentProblem.ProblemID;

                this.SolBS.Sort = this.gADataSet.Solutions.ChromosomeLengthColumn.ColumnName + " desc";

                this.gABS.ResumeBinding();
                this.SolBS.ResumeBinding();

                pform.Find(currentProblem.ProblemID);
                pform.Show();
            }
            else if (sender.Equals(this.gADataGridView))
            {
                GADataSet.GARow currentGARow = null;
                currentGARow = dgvr.Row as GADataSet.GARow;

                this.SolBS.Filter = this.gADataSet.Solutions.GAIDColumn.ColumnName + "=" + currentGARow.ID;

                this.SolBS.Sort = this.gADataSet.Solutions.FitnessColumn.ColumnName + " desc";
            }
            else if (sender.Equals(this.SolutionsDataGridView))
            {
                GADataSet.SolutionsRow sol = dgvr.Row as GADataSet.SolutionsRow;
                if (sol.IsChromosomeNull())
                {
                    return;
                }
                System.IO.File.WriteAllBytes("current.gif", sol.Chromosome);
                picBox.ImageLocation = "current.gif";
                picBox.Refresh();
            }
        }
예제 #3
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;
            }
        }