Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TestFunctions.Views/3.3/SingleObjectiveTestFunctionSolutionView.cs @ 16435

Last change on this file since 16435 was 15584, checked in by swagner, 6 years ago

#2640: Updated year of copyrights in license headers on stable

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