Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Problems.TestFunctions.Views/3.3/SingleObjectiveTestFunctionSolutionView.cs @ 16716

Last change on this file since 16716 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 8.7 KB
RevLine 
[3647]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3647]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;
[16057]24using System.Linq;
[3647]25using System.Windows.Forms;
26using HeuristicLab.Core.Views;
[3665]27using HeuristicLab.Data;
[4068]28using HeuristicLab.Encodings.RealVectorEncoding;
[3647]29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Problems.TestFunctions.Views {
32  /// <summary>
[3661]33  /// A view for a SingleObjectiveTestFunctions solution.
[3647]34  /// </summary>
[3661]35  [View("Single Objective Test Functions View")]
[3647]36  [Content(typeof(SingleObjectiveTestFunctionSolution), true)]
[3665]37  public partial class SingleObjectiveTestFunctionSolutionView : ItemView {
38    private Bitmap backgroundImage;
39
[3647]40    public new SingleObjectiveTestFunctionSolution Content {
41      get { return (SingleObjectiveTestFunctionSolution)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public SingleObjectiveTestFunctionSolutionView() {
46      InitializeComponent();
[3665]47      pictureBox.SizeChanged += new EventHandler(pictureBox_SizeChanged);
[3647]48      qualityView.ReadOnly = true;
49      realVectorView.ReadOnly = true;
[3665]50      backgroundImage = null;
[3647]51    }
52
[3649]53    protected override void DeregisterContentEvents() {
[3665]54      Content.BestKnownRealVectorChanged -= new EventHandler(Content_BestKnownRealVectorChanged);
55      Content.BestRealVectorChanged -= new EventHandler(Content_BestRealVectorChanged);
[3649]56      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
[3665]57      Content.PopulationChanged -= new EventHandler(Content_PopulationChanged);
[3894]58      Content.BoundsChanged -= new EventHandler(Content_BoundsChanged);
[3649]59      base.DeregisterContentEvents();
60    }
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
[3665]63      Content.BestKnownRealVectorChanged += new EventHandler(Content_BestKnownRealVectorChanged);
64      Content.BestRealVectorChanged += new EventHandler(Content_BestRealVectorChanged);
[3649]65      Content.QualityChanged += new EventHandler(Content_QualityChanged);
[3665]66      Content.PopulationChanged += new EventHandler(Content_PopulationChanged);
[3894]67      Content.BoundsChanged += new EventHandler(Content_BoundsChanged);
[3649]68    }
69
[3665]70    private void Content_BestKnownRealVectorChanged(object sender, EventArgs e) {
[3649]71      if (InvokeRequired)
[3665]72        Invoke(new EventHandler(Content_BestKnownRealVectorChanged), sender, e);
[3649]73      else {
[3665]74        GenerateImage();
[3649]75      }
76    }
77
[3665]78    private void Content_BestRealVectorChanged(object sender, EventArgs e) {
[3649]79      if (InvokeRequired)
[3667]80        Invoke(new EventHandler(Content_BestRealVectorChanged), sender, e);
[3649]81      else {
[3661]82        realVectorView.Content = Content.BestRealVector;
83        pictureBox.Visible = Content.BestRealVector.Length == 2;
[3665]84        GenerateImage();
[3649]85      }
86    }
87
[3665]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
[3894]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
[3647]113    protected override void OnContentChanged() {
114      base.OnContentChanged();
115      if (Content == null) {
116        qualityView.Content = null;
117        realVectorView.Content = null;
118      } else {
[3661]119        qualityView.Content = Content.BestQuality;
120        realVectorView.Content = Content.BestRealVector;
121
122        pictureBox.Visible = Content.BestRealVector.Length == 2;
[3665]123        GenerateImage();
[3647]124      }
125    }
126
[3668]127    protected override void OnPaint(PaintEventArgs e) {
128      GenerateImage();
129      base.OnPaint(e);
130    }
131
[3904]132    protected override void SetEnabledStateOfControls() {
133      base.SetEnabledStateOfControls();
[3647]134      qualityView.Enabled = Content != null;
135      realVectorView.Enabled = Content != null;
136    }
[3665]137
138    private void GenerateImage() {
[16057]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);
[3665]161        }
[16057]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        }
[3665]169      }
170    }
171
172    private void GenerateBackgroundImage() {
173      if (backgroundImage != null)
174        backgroundImage.Dispose();
175      backgroundImage = new Bitmap(pictureBox.Width, pictureBox.Height);
[3894]176      DoubleMatrix bounds = Content.Bounds;
177      if (bounds == null) bounds = Content.Evaluator.Bounds;
[3665]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;
[4068]203      GenerateImage();
[3665]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    }
[3647]213  }
214}
Note: See TracBrowser for help on using the repository browser.