public override void SetPreprocess() { util = new PDIGeneticUtil(); specificAdam = null; StagnationCounter = 0; if (imagen != null) //imagen = new Img(); { imagen.Dispose(); } imagen = new Img(); repairCounter = 0; GADataSet.DataRow[] datas = (GADataSet.DataRow[])ProblemData; GADataSet.DataRow data = datas.FirstOrDefault(o => o.ProblemID == this.PROBLEMID); imagen.path = data.Label1; int scale = 6; imagen.GetImg(data.Label2, scale); imagen.GetImgToCompare(data.Label3, scale); // CvInvoke.DetailEnhance(imagen.UIOne.Mat, imagen.UIOne.Mat); // CvInvoke.DetailEnhance(imagen.UITwo.Mat, imagen.UITwo.Mat); imagen.UITwo.CountNonzero(); Image <Rgba, byte> uno = imagen.UIOne; Image <Rgba, byte> dos = imagen.UITwo; CvInvoke.Imshow("ONE", uno); CvInvoke.Imshow("TWO", dos); double cm = imagen.OneMoments[0].GetCentralMoment(1, 1); double sm = imagen.OneMoments[0].GetSpatialMoment(1, 1); Img.DisposeArrayOfImages(ref all); all = new List <object>(); // horizontalCounter = 1; }
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(); }
/// <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; } }