Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3729 was 3668, checked in by svonolfe, 15 years ago

Fixed refreshing of TF visualization (#934)

File size: 8.2 KB
RevLine 
[3647]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;
[3665]26using HeuristicLab.Data;
[3647]27using HeuristicLab.MainForm;
[3665]28using HeuristicLab.Encodings.RealVectorEncoding;
[3647]29
30namespace HeuristicLab.Problems.TestFunctions.Views {
31  /// <summary>
[3661]32  /// A view for a SingleObjectiveTestFunctions solution.
[3647]33  /// </summary>
[3661]34  [View("Single Objective Test Functions View")]
[3647]35  [Content(typeof(SingleObjectiveTestFunctionSolution), true)]
[3665]36  public partial class SingleObjectiveTestFunctionSolutionView : ItemView {
37    private Bitmap backgroundImage;
38
[3647]39    public new SingleObjectiveTestFunctionSolution Content {
40      get { return (SingleObjectiveTestFunctionSolution)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public SingleObjectiveTestFunctionSolutionView() {
45      InitializeComponent();
[3665]46      pictureBox.SizeChanged += new EventHandler(pictureBox_SizeChanged);
[3647]47      qualityView.ReadOnly = true;
48      realVectorView.ReadOnly = true;
[3665]49      backgroundImage = null;
[3647]50    }
51
[3649]52    protected override void DeregisterContentEvents() {
[3665]53      Content.BestKnownRealVectorChanged -= new EventHandler(Content_BestKnownRealVectorChanged);
54      Content.BestRealVectorChanged -= new EventHandler(Content_BestRealVectorChanged);
[3649]55      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
[3665]56      Content.PopulationChanged -= new EventHandler(Content_PopulationChanged);
[3649]57      base.DeregisterContentEvents();
58    }
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
[3665]61      Content.BestKnownRealVectorChanged += new EventHandler(Content_BestKnownRealVectorChanged);
62      Content.BestRealVectorChanged += new EventHandler(Content_BestRealVectorChanged);
[3649]63      Content.QualityChanged += new EventHandler(Content_QualityChanged);
[3665]64      Content.PopulationChanged += new EventHandler(Content_PopulationChanged);
[3649]65    }
66
[3665]67    private void Content_BestKnownRealVectorChanged(object sender, EventArgs e) {
[3649]68      if (InvokeRequired)
[3665]69        Invoke(new EventHandler(Content_BestKnownRealVectorChanged), sender, e);
[3649]70      else {
[3665]71        GenerateImage();
[3649]72      }
73    }
74
[3665]75    private void Content_BestRealVectorChanged(object sender, EventArgs e) {
[3649]76      if (InvokeRequired)
[3667]77        Invoke(new EventHandler(Content_BestRealVectorChanged), sender, e);
[3649]78      else {
[3661]79        realVectorView.Content = Content.BestRealVector;
80        pictureBox.Visible = Content.BestRealVector.Length == 2;
[3665]81        GenerateImage();
[3649]82      }
83    }
84
[3665]85    private void Content_QualityChanged(object sender, EventArgs e) {
86      if (InvokeRequired)
87        Invoke(new EventHandler(Content_QualityChanged), sender, e);
88      else {
89        qualityView.Content = Content.BestQuality;
90        pictureBox.Visible = Content.BestRealVector.Length == 2;
91        GenerateImage();
92      }
93    }
94
95    private void Content_PopulationChanged(object sender, EventArgs e) {
96      if (InvokeRequired)
97        Invoke(new EventHandler(Content_PopulationChanged), sender, e);
98      else {
99        GenerateImage();
100      }
101    }
102
[3647]103    protected override void OnContentChanged() {
104      base.OnContentChanged();
105
106      if (Content == null) {
107        qualityView.Content = null;
108        realVectorView.Content = null;
109      } else {
[3661]110        qualityView.Content = Content.BestQuality;
111        realVectorView.Content = Content.BestRealVector;
112
113        pictureBox.Visible = Content.BestRealVector.Length == 2;
[3665]114        GenerateImage();
[3647]115      }
116
117      SetEnabledStateOfControls();
118    }
119
[3668]120    protected override void OnPaint(PaintEventArgs e) {
121      GenerateImage();
122     
123      base.OnPaint(e);
124    }
125
[3647]126    private void SetEnabledStateOfControls() {
127      qualityView.Enabled = Content != null;
128      realVectorView.Enabled = Content != null;
129    }
[3665]130
131    private void GenerateImage() {
132      if (pictureBox.Enabled && pictureBox.Width > 0 && pictureBox.Height > 0) {
133        if (Content == null) {
134          pictureBox.Image = null;
135        } else {
136          if (backgroundImage == null) {
137            GenerateBackgroundImage();
138            pictureBox.Image = backgroundImage;
139          }
140          pictureBox.Refresh();
141          DoubleMatrix bounds = Content.Evaluator.Bounds;
142          double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
143          double xStep = backgroundImage.Width / (xMax - xMin), yStep = backgroundImage.Height / (yMax - yMin);
144          using (Graphics graphics = pictureBox.CreateGraphics()) {
145            if (Content.BestKnownRealVector != null) {
146              Pen cross = new Pen(Brushes.Red, 2.0f);
147              float a = (float)((Content.BestKnownRealVector[0] - xMin) * xStep);
148              float b = (float)((Content.BestKnownRealVector[1] - yMin) * yStep);
149              graphics.DrawLine(cross, a - 4, b - 4, a + 4, b + 4);
150              graphics.DrawLine(cross, a - 4, b + 4, a + 4, b - 4);
151            }
152            foreach (RealVector vector in Content.Population)
153              graphics.FillEllipse(Brushes.Blue, (float)((vector[0] - xMin) * xStep - 4), (float)((vector[1] - yMin) * yStep - 4), 8.0f, 8.0f);
154            if (Content.BestRealVector != null) {
155              graphics.FillEllipse(Brushes.Green, (float)((Content.BestRealVector[0] - xMin) * xStep - 5), (float)((Content.BestRealVector[1] - yMin) * yStep - 5), 10.0f, 10.0f);
156            }
157          }
158        }
159      }
160    }
161
162    private void GenerateBackgroundImage() {
163      if (backgroundImage != null)
164        backgroundImage.Dispose();
165      backgroundImage = new Bitmap(pictureBox.Width, pictureBox.Height);
166      DoubleMatrix bounds = Content.Evaluator.Bounds;
167      double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
168      double xStep = (xMax - xMin) / backgroundImage.Width, yStep = (yMax - yMin) / backgroundImage.Height;
169      double minPoint = Double.MaxValue, maxPoint = Double.MinValue;
170      DoubleMatrix points = new DoubleMatrix(backgroundImage.Height, backgroundImage.Width);
171      for (int i = 0; i < backgroundImage.Width; i++)
172        for (int j = 0; j < backgroundImage.Height; j++) {
173          points[j, i] = Content.Evaluator.Evaluate2D(xMin + i * xStep, yMin + j * yStep);
174          if (points[j, i] < minPoint) minPoint = points[j, i];
175          if (points[j, i] > maxPoint) maxPoint = points[j, i];
176        }
177      double grayStep;
178      if (maxPoint == minPoint) grayStep = -1;
179      else grayStep = 100 / (maxPoint - minPoint);
180
181      for (int i = 0; i < backgroundImage.Width; i++)
182        for (int j = 0; j < backgroundImage.Height; j++) {
183          int luminosity = (grayStep > 0) ? (int)Math.Round((points[j, i] - minPoint) * grayStep) : (128);
184          backgroundImage.SetPixel(i, j, Color.FromArgb(255 - luminosity, 255 - luminosity, 255 - luminosity));
185        }
186    }
187
188    private void pictureBox_SizeChanged(object sender, EventArgs e) {
189      if (backgroundImage != null) backgroundImage.Dispose();
190      backgroundImage = null;
191      pictureBox.Image = null;
192      GenerateImage();
193    }
194
195    protected override void OnClosing(FormClosingEventArgs e) {
196      pictureBox.Enabled = false;
197      if (backgroundImage != null) backgroundImage.Dispose();
198      backgroundImage = null;
199      pictureBox.Image = null;
200      base.OnClosing(e);
201    }
[3647]202  }
203}
Note: See TracBrowser for help on using the repository browser.