Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4040 was 3904, checked in by mkommend, 14 years ago

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

File size: 8.6 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      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.Evaluator.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            foreach (RealVector vector in Content.Population)
160              graphics.FillEllipse(Brushes.Blue, (float)((vector[0] - xMin) * xStep - 4), (float)((vector[1] - yMin) * yStep - 4), 8.0f, 8.0f);
161            if (Content.BestRealVector != null) {
162              graphics.FillEllipse(Brushes.Green, (float)((Content.BestRealVector[0] - xMin) * xStep - 5), (float)((Content.BestRealVector[1] - yMin) * yStep - 5), 10.0f, 10.0f);
163            }
164          }
165        }
166      }
167    }
168
169    private void GenerateBackgroundImage() {
170      if (backgroundImage != null)
171        backgroundImage.Dispose();
172      backgroundImage = new Bitmap(pictureBox.Width, pictureBox.Height);
173      DoubleMatrix bounds = Content.Bounds;
174      if (bounds == null) bounds = Content.Evaluator.Bounds;
175      double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
176      double xStep = (xMax - xMin) / backgroundImage.Width, yStep = (yMax - yMin) / backgroundImage.Height;
177      double minPoint = Double.MaxValue, maxPoint = Double.MinValue;
178      DoubleMatrix points = new DoubleMatrix(backgroundImage.Height, backgroundImage.Width);
179      for (int i = 0; i < backgroundImage.Width; i++)
180        for (int j = 0; j < backgroundImage.Height; j++) {
181          points[j, i] = Content.Evaluator.Evaluate2D(xMin + i * xStep, yMin + j * yStep);
182          if (points[j, i] < minPoint) minPoint = points[j, i];
183          if (points[j, i] > maxPoint) maxPoint = points[j, i];
184        }
185      double grayStep;
186      if (maxPoint == minPoint) grayStep = -1;
187      else grayStep = 100 / (maxPoint - minPoint);
188
189      for (int i = 0; i < backgroundImage.Width; i++)
190        for (int j = 0; j < backgroundImage.Height; j++) {
191          int luminosity = (grayStep > 0) ? (int)Math.Round((points[j, i] - minPoint) * grayStep) : (128);
192          backgroundImage.SetPixel(i, j, Color.FromArgb(255 - luminosity, 255 - luminosity, 255 - luminosity));
193        }
194    }
195
196    private void pictureBox_SizeChanged(object sender, EventArgs e) {
197      if (backgroundImage != null) backgroundImage.Dispose();
198      backgroundImage = null;
199      pictureBox.Image = null;
200      GenerateImage();
201    }
202
203    protected override void OnClosing(FormClosingEventArgs e) {
204      pictureBox.Enabled = false;
205      if (backgroundImage != null) backgroundImage.Dispose();
206      backgroundImage = null;
207      pictureBox.Image = null;
208      base.OnClosing(e);
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.