Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/06/10 05:02:15 (14 years ago)
Author:
abeham
Message:

#934

  • added visualization for test functions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TestFunctions.Views/3.3/SingleObjectiveTestFunctionSolutionView.cs

    r3661 r3665  
    2424using System.Windows.Forms;
    2525using HeuristicLab.Core.Views;
     26using HeuristicLab.Data;
    2627using HeuristicLab.MainForm;
    27 using HeuristicLab.Problems.TestFunctions;
    28 using HeuristicLab.Data;
    29 using System.Collections.Generic;
     28using HeuristicLab.Encodings.RealVectorEncoding;
    3029
    3130namespace HeuristicLab.Problems.TestFunctions.Views {
     
    3534  [View("Single Objective Test Functions View")]
    3635  [Content(typeof(SingleObjectiveTestFunctionSolution), true)]
    37   public partial class SingleObjectiveTestFunctionSolutionView : HeuristicLab.Core.Views.ItemView {
     36  public partial class SingleObjectiveTestFunctionSolutionView : ItemView {
     37    private Bitmap backgroundImage;
     38
    3839    public new SingleObjectiveTestFunctionSolution Content {
    3940      get { return (SingleObjectiveTestFunctionSolution)base.Content; }
     
    4344    public SingleObjectiveTestFunctionSolutionView() {
    4445      InitializeComponent();
    45 
     46      pictureBox.SizeChanged += new EventHandler(pictureBox_SizeChanged);
    4647      qualityView.ReadOnly = true;
    4748      realVectorView.ReadOnly = true;
     49      backgroundImage = null;
    4850    }
    4951
    5052    protected override void DeregisterContentEvents() {
    51       Content.RealVectorChanged -= new EventHandler(Content_RealVectorChanged);
     53      Content.BestKnownRealVectorChanged -= new EventHandler(Content_BestKnownRealVectorChanged);
     54      Content.BestRealVectorChanged -= new EventHandler(Content_BestRealVectorChanged);
    5255      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
     56      Content.PopulationChanged -= new EventHandler(Content_PopulationChanged);
    5357      base.DeregisterContentEvents();
    5458    }
    5559    protected override void RegisterContentEvents() {
    5660      base.RegisterContentEvents();
    57       Content.RealVectorChanged += new EventHandler(Content_RealVectorChanged);
     61      Content.BestKnownRealVectorChanged += new EventHandler(Content_BestKnownRealVectorChanged);
     62      Content.BestRealVectorChanged += new EventHandler(Content_BestRealVectorChanged);
    5863      Content.QualityChanged += new EventHandler(Content_QualityChanged);
    59     }
    60 
    61     void Content_QualityChanged(object sender, EventArgs e) {
    62       if (InvokeRequired)
    63         Invoke(new EventHandler(Content_QualityChanged), sender, e);
    64       else {
    65         qualityView.ViewType = null;
    66         qualityView.Content = Content.BestQuality;
    67       }
    68     }
    69 
    70     void Content_RealVectorChanged(object sender, EventArgs e) {
     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) {
    7176      if (InvokeRequired)
    7277        Invoke(new EventHandler(Content_QualityChanged), sender, e);
     
    7580        realVectorView.Content = Content.BestRealVector;
    7681        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();
    77102      }
    78103    }
     
    92117
    93118        pictureBox.Visible = Content.BestRealVector.Length == 2;
     119        GenerateImage();
    94120      }
    95121
     
    101127      realVectorView.Enabled = Content != null;
    102128    }
     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    }
    103201  }
    104202}
Note: See TracChangeset for help on using the changeset viewer.