public Aprox(Aprox apx) { score = apx.score; Generation = apx.Generation; rectangles = new List <Rectangle>(); brushes = new List <SolidBrush>(); r = new Random(apx.r.Next()); this.parentID = apx.parentID; ShapeNum = apx.ShapeNum; ImageWidth = apx.ImageWidth; ImageHeight = apx.ImageHeight; madeImage = new Bitmap(apx.madeImage); // rectangles =apx.rectangles; rectangles.Clear(); foreach (Rectangle re in apx.rectangles) { rectangles.Add(new Rectangle(re.X, re.Y, re.Width, re.Height)); } // brushes = apx.brushes; brushes.Clear(); foreach (SolidBrush sb in apx.brushes) { brushes.Add(new SolidBrush(sb.Color)); } }
private void AddApxToSelection(int id, Aprox apx) { PictureBox picBox = new PictureBox(); picBox.SizeMode = PictureBoxSizeMode.Zoom; int SelectViewIconsSize = 222; picBox.Width = SelectViewIconsSize; picBox.Height = SelectViewIconsSize; picBox.Image = apx.madeImage; picBox.Click += new System.EventHandler(this.pictureBoxChoosen_Click); flowLayoutPanel1.Controls.Add(picBox); flowLayoutPanel1.Controls.SetChildIndex(picBox, id); // Scores.Items.Add(apx.score); }
private void Iterations_Click(object sender, EventArgs e) { int num = (int)numericUpDown2.Value; int num2 = num; int numCopy; Scores.SelectedIndexChanged -= Scores_SelectedIndexChanged; int podzialID = 1 + rnd.Next(Aproxes.Count - 2); AproxesCopy.Clear(); foreach (Aprox a in Aproxes) { AproxesCopy.Add(a);// new Aprox(a)); } SetLocalLowApxScrID(podzialID, 0); SetLocalLowApxScrID(podzialID, 1); int podzialIDCopy = podzialID; Image orginl = (Image)pictureBoxOrginal.Image.Clone(); Random rndCopy = new Random(); numCopy = num; int ShapeNumCopy = ShapeNum; int ShapeNumMinCopy = ShapeNumMin; Thread t1 = MakeThread(AproxesCopy, orginl, numCopy, ShapeNumCopy, ShapeNumMinCopy, rndCopy, podzialIDCopy); t1.Start(); num2--; for (int i = 0; i < num; i++, num2--)//, numericUpDown2.Value--) { if (num2 % 100 == 0) { label3.Text = num2.ToString(); label3.Update(); } Aprox apx; if (rnd.NextDouble() > 0.15) { int ID = podzialID + rnd.Next(Aproxes.Count - podzialID); apx = new Aprox(Aproxes[ID], (Image)pictureBoxOrginal.Image.Clone(), rnd.Next(), ID); bool ok = true; for (int idx = podzialID; idx < Aproxes.Count; idx++) { if (Aproxes[idx].score == apx.score) { ok = false; } } if (apx.score < Aproxes[ID].score && ok) { if (ShowCheckBox.Checked) { flowLayoutPanel1.SuspendLayout(); } if (ShowCheckBox.Checked) { flowLayoutPanel1.Controls.RemoveAt(LOCALlowestApxScoreID2); } // Scores.Items.RemoveAt(LOCALlowestApxScoreID2); Aproxes.RemoveAt(LOCALlowestApxScoreID2); Aproxes.Insert(LOCALlowestApxScoreID2, apx); if (ShowCheckBox.Checked) { AddApxToSelection(LOCALlowestApxScoreID2, apx); } SetLocalLowApxScrID(podzialID, 1); if (ShowCheckBox.Checked) { flowLayoutPanel1.Update(); } // Scores.Update(); if (ShowCheckBox.Checked) { flowLayoutPanel1.ResumeLayout(); } } } else { apx = new Aprox(rnd.Next(ShapeNum) + ShapeNumMin, (Image)pictureBoxOrginal.Image.Clone(), rnd.Next(), -1); bool ok = true; for (int idx = podzialID; idx < Aproxes.Count; idx++) { if (Aproxes[idx].score == apx.score) { ok = false; } } if (apx.score < Aproxes[LOCALlowestApxScoreID2].score && ok) { if (ShowCheckBox.Checked) { flowLayoutPanel1.SuspendLayout(); } if (ShowCheckBox.Checked) { flowLayoutPanel1.Controls.RemoveAt(LOCALlowestApxScoreID2); } // Scores.Items.RemoveAt(LOCALlowestApxScoreID2); Aproxes.RemoveAt(LOCALlowestApxScoreID2); Aproxes.Insert(LOCALlowestApxScoreID2, apx); if (ShowCheckBox.Checked) { AddApxToSelection(LOCALlowestApxScoreID2, apx); } SetLocalLowApxScrID(podzialID, 1); if (ShowCheckBox.Checked) { flowLayoutPanel1.Update(); } // Scores.Update(); if (ShowCheckBox.Checked) { flowLayoutPanel1.ResumeLayout(); } } } } t1.Join(); for (int i = 0; i < podzialID; i++) { Aproxes[i] = AproxesCopy[i]; flowLayoutPanel1.Controls.RemoveAt(i); // Scores.Items.RemoveAt(i); AddApxToSelection(i, Aproxes[i]); } if (!ShowCheckBox.Checked) { for (int i = podzialID; i < Aproxes.Count; i++) { flowLayoutPanel1.Controls.RemoveAt(i); // Scores.Items.RemoveAt(i); AddApxToSelection(i, Aproxes[i]); } } Scores.Items.Clear(); for (int i = 0; i < Aproxes.Count; i++) { Scores.Items.Add(Aproxes[i].score); } Scores.SelectedIndexChanged += Scores_SelectedIndexChanged; totalIt += num; label6.Text = totalIt.ToString(); }
private Thread MakeThread(List <Aprox> AproxesCopy, Image orginl, int numCopy, int ShapeNumCopy, int ShapeNumMinCopy, Random rndCopy, int podzialIDCopy) { Thread t1 = new Thread(() => { for (int i = 0; i < numCopy; i++)//, numericUpDown2.Value--) { Aprox apx; if (rndCopy.NextDouble() > 0.15) { int ID = rndCopy.Next(podzialIDCopy); apx = new Aprox(AproxesCopy[ID], (Image)orginl.Clone(), rndCopy.Next(), ID); bool ok = true; for (int idx = 0; idx < podzialIDCopy; idx++) { if (AproxesCopy[idx].score == apx.score) { ok = false; } } if (apx.score < AproxesCopy[ID].score && ok) { //flowLayoutPanel1.SuspendLayout(); // //flowLayoutPanel1.Controls.RemoveAt(LOCALlowestApxScoreID[0]); // //Scores.Items.RemoveAt(LOCALlowestApxScoreID[0]); // AproxesCopy.RemoveAt(LOCALlowestApxScoreID[0]); AproxesCopy.Insert(LOCALlowestApxScoreID[0], apx); //AddApxToSelection(LOCALlowestApxScoreID[0], apx); // SetLocalLowApxScrID(podzialIDCopy, 0); //flowLayoutPanel1.Update(); // //Scores.Update(); // //flowLayoutPanel1.ResumeLayout(); // } } else { apx = new Aprox(rndCopy.Next(ShapeNumCopy) + ShapeNumMinCopy, (Image)orginl.Clone(), rndCopy.Next(), -1); bool ok = true; for (int idx = 0; idx < podzialIDCopy; idx++) { if (AproxesCopy[idx].score == apx.score) { ok = false; } } if (apx.score < AproxesCopy[LOCALlowestApxScoreID[0]].score && ok) { //flowLayoutPanel1.SuspendLayout(); // //flowLayoutPanel1.Controls.RemoveAt(LOCALlowestApxScoreID[0]); // //Scores.Items.RemoveAt(LOCALlowestApxScoreID[0]); // AproxesCopy.RemoveAt(LOCALlowestApxScoreID[0]); AproxesCopy.Insert(LOCALlowestApxScoreID[0], apx); //AddApxToSelection(LOCALlowestApxScoreID[0], apx); // SetLocalLowApxScrID2(podzialIDCopy, 0); //flowLayoutPanel1.Update(); // //Scores.Update(); // //flowLayoutPanel1.ResumeLayout(); // } } } }); return(t1); }
public Aprox(Aprox apx, Image orginal, int seed, int id) { Generation = apx.Generation++; rectangles = new List <Rectangle>(); brushes = new List <SolidBrush>(); r = new Random(seed); this.parentID = id; ShapeNum = apx.ShapeNum; ImageWidth = apx.ImageWidth; ImageHeight = apx.ImageHeight; madeImage = new Bitmap(ImageWidth, ImageHeight); // rectangles =apx.rectangles; rectangles.Clear(); foreach (Rectangle re in apx.rectangles) { rectangles.Add(new Rectangle(re.X, re.Y, re.Width, re.Height)); } // brushes = apx.brushes; brushes.Clear(); foreach (SolidBrush sb in apx.brushes) { brushes.Add(new SolidBrush(sb.Color)); } // modify int k; for (k = 0; k < 1 + r.Next(3); k++) { double tmp = r.NextDouble(); if (tmp < 0.33 || tmp > 0.98) { int IDMod = r.Next(ShapeNum - 1) + 1; if (r.NextDouble() > 0.2) { rectangles[IDMod] = ChangeRectangle(rectangles[IDMod], ImageWidth, ImageHeight, r); } else { int sw = r.Next(ImageWidth); int sh = r.Next(ImageHeight); rectangles[IDMod] = new Rectangle(sw, sh, r.Next(Math.Min(ImageWidth - sw, ImageWidth / 7)), r.Next(Math.Min(ImageHeight - sh, ImageHeight / 7))); } } if (tmp < 0.66 && tmp > 0.3) { int IDMod = r.Next(ShapeNum); if (r.NextDouble() > 0.8) { brushes[IDMod].Color = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)); } else { brushes[IDMod].Color = ChangeColour(brushes[IDMod].Color, r); } } if (tmp > 0.6 || tmp < 0.02) { int IDMod1 = r.Next(ShapeNum - 1) + 1; int IDMod2 = r.Next(ShapeNum - 1) + 1; Rectangle tempR = rectangles[IDMod1]; SolidBrush tempB = brushes[IDMod1]; rectangles[IDMod1] = rectangles[IDMod2]; brushes[IDMod1] = brushes[IDMod2]; rectangles[IDMod2] = tempR; brushes[IDMod2] = tempB; } } int i; for (i = 0; i < ShapeNum; i++) { // int sw = r.Next(ImageWidth); // int sh = r.Next(ImageHeight); // rectangles.Add(new Rectangle(sw, sh, r.Next(ImageWidth - sw), r.Next(ImageHeight - sh))); // brushes.Add(new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)))); using (Graphics g = Graphics.FromImage(madeImage)) { g.FillRectangle(brushes[i], rectangles[i]); } } SetScore(orginal); }