Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TestFunctions.Views/3.3/SingleObjectiveTestFunctionSolutionView.cs @ 3665

Last change on this file since 3665 was 3665, checked in by abeham, 15 years ago

#934

  • added visualization for test functions
File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.MainForm;
28using HeuristicLab.Encodings.RealVectorEncoding;
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      base.DeregisterContentEvents();
58    }
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.BestKnownRealVectorChanged += new EventHandler(Content_BestKnownRealVectorChanged);
62      Content.BestRealVectorChanged += new EventHandler(Content_BestRealVectorChanged);
63      Content.QualityChanged += new EventHandler(Content_QualityChanged);
64      Content.PopulationChanged += new EventHandler(Content_PopulationChanged);
65    }
66
67    private void Content_BestKnownRealVectorChanged(object sender, EventArgs e) {
68      if (InvokeRequired)
69        Invoke(new EventHandler(Content_BestKnownRealVectorChanged), sender, e);
70      else {
71        GenerateImage();
72      }
73    }
74
75    private void Content_BestRealVectorChanged(object sender, EventArgs e) {
76      if (InvokeRequired)
77        Invoke(new EventHandler(Content_QualityChanged), sender, e);
78      else {
79        realVectorView.ViewType = null;
80        realVectorView.Content = Content.BestRealVector;
81        pictureBox.Visible = Content.BestRealVector.Length == 2;
82        GenerateImage();
83      }
84    }
85
86    private void Content_QualityChanged(object sender, EventArgs e) {
87      if (InvokeRequired)
88        Invoke(new EventHandler(Content_QualityChanged), sender, e);
89      else {
90        qualityView.ViewType = null;
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    protected override void OnContentChanged() {
106      base.OnContentChanged();
107
108      if (Content == null) {
109        qualityView.Content = null;
110        realVectorView.Content = null;
111      } else {
112        qualityView.ViewType = null;
113        qualityView.Content = Content.BestQuality;
114
115        realVectorView.ViewType = null;
116        realVectorView.Content = Content.BestRealVector;
117
118        pictureBox.Visible = Content.BestRealVector.Length == 2;
119        GenerateImage();
120      }
121
122      SetEnabledStateOfControls();
123    }
124
125    private void SetEnabledStateOfControls() {
126      qualityView.Enabled = Content != null;
127      realVectorView.Enabled = Content != null;
128    }
129
130    private void GenerateImage() {
131      if (pictureBox.Enabled && pictureBox.Width > 0 && pictureBox.Height > 0) {
132        if (Content == null) {
133          pictureBox.Image = null;
134        } else {
135          if (backgroundImage == null) {
136            GenerateBackgroundImage();
137            pictureBox.Image = backgroundImage;
138          }
139          pictureBox.Refresh();
140          DoubleMatrix bounds = Content.Evaluator.Bounds;
141          double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
142          double xStep = backgroundImage.Width / (xMax - xMin), yStep = backgroundImage.Height / (yMax - yMin);
143          using (Graphics graphics = pictureBox.CreateGraphics()) {
144            if (Content.BestKnownRealVector != null) {
145              Pen cross = new Pen(Brushes.Red, 2.0f);
146              float a = (float)((Content.BestKnownRealVector[0] - xMin) * xStep);
147              float b = (float)((Content.BestKnownRealVector[1] - yMin) * yStep);
148              graphics.DrawLine(cross, a - 4, b - 4, a + 4, b + 4);
149              graphics.DrawLine(cross, a - 4, b + 4, a + 4, b - 4);
150            }
151            foreach (RealVector vector in Content.Population)
152              graphics.FillEllipse(Brushes.Blue, (float)((vector[0] - xMin) * xStep - 4), (float)((vector[1] - yMin) * yStep - 4), 8.0f, 8.0f);
153            if (Content.BestRealVector != null) {
154              graphics.FillEllipse(Brushes.Green, (float)((Content.BestRealVector[0] - xMin) * xStep - 5), (float)((Content.BestRealVector[1] - yMin) * yStep - 5), 10.0f, 10.0f);
155            }
156          }
157        }
158      }
159    }
160
161    private void GenerateBackgroundImage() {
162      if (backgroundImage != null)
163        backgroundImage.Dispose();
164      backgroundImage = new Bitmap(pictureBox.Width, pictureBox.Height);
165      DoubleMatrix bounds = Content.Evaluator.Bounds;
166      double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
167      double xStep = (xMax - xMin) / backgroundImage.Width, yStep = (yMax - yMin) / backgroundImage.Height;
168      double minPoint = Double.MaxValue, maxPoint = Double.MinValue;
169      DoubleMatrix points = new DoubleMatrix(backgroundImage.Height, backgroundImage.Width);
170      for (int i = 0; i < backgroundImage.Width; i++)
171        for (int j = 0; j < backgroundImage.Height; j++) {
172          points[j, i] = Content.Evaluator.Evaluate2D(xMin + i * xStep, yMin + j * yStep);
173          if (points[j, i] < minPoint) minPoint = points[j, i];
174          if (points[j, i] > maxPoint) maxPoint = points[j, i];
175        }
176      double grayStep;
177      if (maxPoint == minPoint) grayStep = -1;
178      else grayStep = 100 / (maxPoint - minPoint);
179
180      for (int i = 0; i < backgroundImage.Width; i++)
181        for (int j = 0; j < backgroundImage.Height; j++) {
182          int luminosity = (grayStep > 0) ? (int)Math.Round((points[j, i] - minPoint) * grayStep) : (128);
183          backgroundImage.SetPixel(i, j, Color.FromArgb(255 - luminosity, 255 - luminosity, 255 - luminosity));
184        }
185    }
186
187    private void pictureBox_SizeChanged(object sender, EventArgs e) {
188      if (backgroundImage != null) backgroundImage.Dispose();
189      backgroundImage = null;
190      pictureBox.Image = null;
191      GenerateImage();
192    }
193
194    protected override void OnClosing(FormClosingEventArgs e) {
195      pictureBox.Enabled = false;
196      if (backgroundImage != null) backgroundImage.Dispose();
197      backgroundImage = null;
198      pictureBox.Image = null;
199      base.OnClosing(e);
200    }
201  }
202}
Note: See TracBrowser for help on using the repository browser.