private void stopProcessing(object sender, EventArgs e) { CommandChannel.SetCommand(1); stopButton.Enabled = false; saveResultButton.Enabled = true; timer1.Stop(); }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); CommandChannel.SetCommand(1); }
public void Run(object obj_iterations) { int nrOfIterations = (int)obj_iterations; double referenceNegativeIndex = double.MaxValue; double referenceComeback = NrOfPolygons; bool nrOfMutationsTryChanged = false; int limit = 0; int shakeTheImageCounter = 0; double currentNegativeFitness = workingGene.NegativeFitness; for (int i = 0; i < nrOfIterations; i++) { if (CommandChannel.GetCommand() == 1) { break; } List <int> mutationsList = retrieveMutationsList(); for (int mi = 0; mi < mutationsList.Count; mi++) { int mutationStep = mutationsList[mi]; switch (mutationStep) { case 1: performAddPolygon(workingGene); break; case 2: performMutateColor(workingGene); break; case 3: performMutatePolygon(workingGene); break; case 4: performMutatePolygonAndColor(workingGene); break; case 5: performMutateColorParam(workingGene); break; case 6: performMutatePolygonPoint(workingGene); break; } } #region check if the gene had evolved in the last 250 iterations; comment this region if you don't one to have momentum in your app if (currentNegativeFitness != workingGene.NegativeFitness) { currentNegativeFitness = workingGene.NegativeFitness; shakeTheImageCounter = 0; } else { //that means the in the last 250 iterations nothing changed in the gene fitness. if (shakeTheImageCounter > 250) { int nrOfPolysToBeRemoved = workingGene.NrOfPolygons / 10; for (int ix = 0; ix < nrOfPolysToBeRemoved; ix++) { randomlyRemovePolygons(workingGene); } referenceComeback = workingGene.Polygons.Count + 1; } shakeTheImageCounter++; } #endregion //if gene is more fit, we added to the container; Console.WriteLine("Negative fitness: {0} , {1}", workingGene.NegativeFitness, workingGene.Polygons.Count); if (workingGene.NegativeFitness < referenceNegativeIndex) { GeneContainer.Instance.AddGene(workingGene.Clone()); referenceNegativeIndex = workingGene.NegativeFitness; } #region every 100 iterations we increase the number of polygons, till we reach 50 if (workingGene.Polygons.Count < 50 && this.NrOfPolygons < 50) { if (i % 100 == 0 && i != 0) { this.NrOfPolygons++; workingGene.NrOfPolygons++; } } #endregion //if every time the number of polygons in the gene reaches the max number of polygons, then we increase the nr of polygons and //we remove the less fit individuals if (workingGene.Polygons.Count < 50) { if (workingGene.Polygons.Count == referenceComeback) { limit = workingGene.Polygons.Count / 10; for (int ix = 0; ix < limit; ix++) { removeLessFitPolys(workingGene); } Console.WriteLine("-------- [ReferenceComeback] :" + referenceComeback); referenceComeback++; } } // when 50 poly are reached, we double the nr of mutations tries; // and we remove a larger number of less fit individuals; else if (workingGene.Polygons.Count >= 50) { if (nrOfMutationsTryChanged == false) { NrOfMutationsTries = NrOfMutationsTries * 2; nrOfMutationsTryChanged = true; } limit = workingGene.Polygons.Count / 8; for (int ix = 0; ix < limit; ix++) { removeLessFitPolys(workingGene); } } } CommandChannel.SetCommand(2); //tell timer to stop }