source: branches/ProblemRefactoring/HeuristicLab.Problems.TestFunctions.Views/3.3/SingleObjectiveTestFunctionSolutionView.cs @ 13403

Last change on this file since 13403 was 13403, checked in by abeham, 4 years ago

#2521:

  • Adapted single-objective test function problem to new problem infrastructure
  • Added additional interfaces to RealVectorEncoding
  • Fixed IParticleUpdater interface (must implement IStochasticOperator if it contains a Random parameter)
File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Problems.TestFunctions.Views {
31  /// <summary>
32  /// A view for a SingleObjectiveTestFunctions solution.
33  /// </summary>
34  [View("Single Objective Test Functions View")]
35  [Content(typeof(SingleObjectiveTestFunctionSolution), true)]
36  public partial class SingleObjectiveTestFunctionSolutionView : ItemView {
37    private Bitmap backgroundImage;
38
39    public new SingleObjectiveTestFunctionSolution Content {
40      get { return (SingleObjectiveTestFunctionSolution)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public SingleObjectiveTestFunctionSolutionView() {
45      InitializeComponent();
46      pictureBox.SizeChanged += new EventHandler(pictureBox_SizeChanged);
47      qualityView.ReadOnly = true;
48      realVectorView.ReadOnly = true;
49      backgroundImage = null;
50    }
51
52    protected override void DeregisterContentEvents() {
53      Content.BestKnownRealVectorChanged -= new EventHandler(Content_BestKnownRealVectorChanged);
54      Content.BestRealVectorChanged -= new EventHandler(Content_BestRealVectorChanged);
55      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
56      Content.PopulationChanged -= new EventHandler(Content_PopulationChanged);
57      Content.BoundsChanged -= new EventHandler(Content_BoundsChanged);
58      base.DeregisterContentEvents();
59    }
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.BestKnownRealVectorChanged += new EventHandler(Content_BestKnownRealVectorChanged);
63      Content.BestRealVectorChanged += new EventHandler(Content_BestRealVectorChanged);
64      Content.QualityChanged += new EventHandler(Content_QualityChanged);
65      Content.PopulationChanged += new EventHandler(Content_PopulationChanged);
66      Content.BoundsChanged += new EventHandler(Content_BoundsChanged);
67    }
68
69    private void Content_BestKnownRealVectorChanged(object sender, EventArgs e) {
70      if (InvokeRequired)
71        Invoke(new EventHandler(Content_BestKnownRealVectorChanged), sender, e);
72      else {
73        GenerateImage();
74      }
75    }
76
77    private void Content_BestRealVectorChanged(object sender, EventArgs e) {
78      if (InvokeRequired)
79        Invoke(new EventHandler(Content_BestRealVectorChanged), sender, e);
80      else {
81        realVectorView.Content = Content.BestRealVector;
82        pictureBox.Visible = Content.BestRealVector.Length == 2;
83        GenerateImage();
84      }
85    }
86
87    private void Content_QualityChanged(object sender, EventArgs e) {
88      if (InvokeRequired)
89        Invoke(new EventHandler(Content_QualityChanged), sender, e);
90      else {
91        qualityView.Content = Content.BestQuality;
92        pictureBox.Visible = Content.BestRealVector.Length == 2;
93        GenerateImage();
94      }
95    }
96
97    private void Content_PopulationChanged(object sender, EventArgs e) {
98      if (InvokeRequired)
99        Invoke(new EventHandler(Content_PopulationChanged), sender, e);
100      else {
101        GenerateImage();
102      }
103    }
104
105    private void Content_BoundsChanged(object sender, EventArgs e) {
106      if (InvokeRequired)
107        Invoke(new EventHandler(Content_BoundsChanged), sender, e);
108      else
109        GenerateImage();
110    }
111
112    protected override void OnContentChanged() {
113      base.OnContentChanged();
114      if (Content == null) {
115        qualityView.Content = null;
116        realVectorView.Content = null;
117      } else {
118        qualityView.Content = Content.BestQuality;
119        realVectorView.Content = Content.BestRealVector;
120
121        pictureBox.Visible = Content.BestRealVector.Length == 2;
122        GenerateImage();
123      }
124    }
125
126    protected override void OnPaint(PaintEventArgs e) {
127      GenerateImage();
128      base.OnPaint(e);
129    }
130
131    protected override void SetEnabledStateOfControls() {
132      base.SetEnabledStateOfControls();
133      qualityView.Enabled = Content != null;
134      realVectorView.Enabled = Content != null;
135    }
136
137    private void GenerateImage() {
138      if (pictureBox.Enabled && pictureBox.Width > 0 && pictureBox.Height > 0) {
139        if (Content == null) {
140          pictureBox.Image = null;
141        } else {
142          if (backgroundImage == null) {
143            GenerateBackgroundImage();
144            pictureBox.Image = backgroundImage;
145          }
146          pictureBox.Refresh();
147          DoubleMatrix bounds = Content.Bounds;
148          if (bounds == null) bounds = Content.TestFunction.Bounds;
149          double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
150          double xStep = backgroundImage.Width / (xMax - xMin), yStep = backgroundImage.Height / (yMax - yMin);
151          using (Graphics graphics = pictureBox.CreateGraphics()) {
152            if (Content.BestKnownRealVector != null) {
153              Pen cross = new Pen(Brushes.Red, 2.0f);
154              float a = (float)((Content.BestKnownRealVector[0] - xMin) * xStep);
155              float b = (float)((Content.BestKnownRealVector[1] - yMin) * yStep);
156              graphics.DrawLine(cross, a - 4, b - 4, a + 4, b + 4);
157              graphics.DrawLine(cross, a - 4, b + 4, a + 4, b - 4);
158            }
159            if (Content.Population != null) {
160              foreach (RealVector vector in Content.Population)
161                graphics.FillEllipse(Brushes.Blue, (float)((vector[0] - xMin) * xStep - 4), (float)((vector[1] - yMin) * yStep - 4), 8.0f, 8.0f);
162            }
163            if (Content.BestRealVector != null) {
164              graphics.FillEllipse(Brushes.Green, (float)((Content.BestRealVector[0] - xMin) * xStep - 5), (float)((Content.BestRealVector[1] - yMin) * yStep - 5), 10.0f, 10.0f);
165            }
166          }
167        }
168      }
169    }
170
171    private void GenerateBackgroundImage() {
172      if (backgroundImage != null)
173        backgroundImage.Dispose();
174      backgroundImage = new Bitmap(pictureBox.Width, pictureBox.Height);
175      DoubleMatrix bounds = Content.Bounds;
176      if (bounds == null) bounds = Content.TestFunction.Bounds;
177      double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
178      double xStep = (xMax - xMin) / backgroundImage.Width, yStep = (yMax - yMin) / backgroundImage.Height;
179      double minPoint = Double.MaxValue, maxPoint = Double.MinValue;
180      DoubleMatrix points = new DoubleMatrix(backgroundImage.Height, backgroundImage.Width);
181      for (int i = 0; i < backgroundImage.Width; i++)
182        for (int j = 0; j < backgroundImage.Height; j++) {
183          points[j, i] = Content.TestFunction.Evaluate2D(xMin + i * xStep, yMin + j * yStep);
184          if (points[j, i] < minPoint) minPoint = points[j, i];
185          if (points[j, i] > maxPoint) maxPoint = points[j, i];
186        }
187      double grayStep;
188      if (maxPoint == minPoint) grayStep = -1;
189      else grayStep = 100 / (maxPoint - minPoint);
190
191      for (int i = 0; i < backgroundImage.Width; i++)
192        for (int j = 0; j < backgroundImage.Height; j++) {
193          int luminosity = (grayStep > 0) ? (int)Math.Round((points[j, i] - minPoint) * grayStep) : (128);
194          backgroundImage.SetPixel(i, j, Color.FromArgb(255 - luminosity, 255 - luminosity, 255 - luminosity));
195        }
196    }
197
198    private void pictureBox_SizeChanged(object sender, EventArgs e) {
199      if (backgroundImage != null) backgroundImage.Dispose();
200      backgroundImage = null;
201      pictureBox.Image = null;
202      GenerateImage();
203    }
204
205    protected override void OnClosing(FormClosingEventArgs e) {
206      pictureBox.Enabled = false;
207      if (backgroundImage != null) backgroundImage.Dispose();
208      backgroundImage = null;
209      pictureBox.Image = null;
210      base.OnClosing(e);
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.