Changeset 3665


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

#934

  • added visualization for test functions
Location:
trunk/sources
Files:
4 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}
  • trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Evaluators/SingleObjectiveTestFunctionProblemEvaluator.cs

    r3520 r3665  
    8484    }
    8585
     86    public virtual double Evaluate2D(double x, double y) {
     87      return EvaluateFunction(new RealVector(new double[] { x, y }));
     88    }
     89
    8690    /// <summary>
    8791    /// Evaluates the test function for a specific <paramref name="point"/>.
  • trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Interfaces/ISingleObjectiveTestFunctionProblemEvaluator.cs

    r3376 r3665  
    3838
    3939    ILookupParameter<RealVector> PointParameter { get; }
     40
     41    double Evaluate2D(double x, double y);
    4042  }
    4143}
  • trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionSolution.cs

    r3661 r3665  
    6060      set {
    6161        if (bestRealVector != value) {
    62           if (bestRealVector != null) DeregisterRealVectorEvents();
     62          if (bestRealVector != null) DeregisterBestRealVectorEvents();
    6363          bestRealVector = value;
    64           if (bestRealVector != null) RegisterRealVectorEvents();
    65           OnRealVectorChanged();
     64          if (bestRealVector != null) RegisterBestRealVectorEvents();
     65          OnBestRealVectorChanged();
    6666        }
    6767      }
     
    106106        }
    107107      }
    108     }
    109 
    110     private Image fitnessLandscape;
    111     public Image FitnessLandscape {
    112       get { return fitnessLandscape; }
    113       set { fitnessLandscape = value; }
    114108    }
    115109
     
    128122    private void Initialize() {
    129123      if (bestKnownRealVector != null) RegisterBestKnownRealVectorEvents();
    130       if (bestRealVector != null) RegisterRealVectorEvents();
     124      if (bestRealVector != null) RegisterBestRealVectorEvents();
    131125      if (bestQuality != null) RegisterQualityEvents();
    132126      if (population != null) RegisterPopulationEvents();
     
    141135      clone.population = (ItemArray<RealVector>)cloner.Clone(population);
    142136      clone.evaluator = (ISingleObjectiveTestFunctionProblemEvaluator)cloner.Clone(evaluator);
    143       clone.fitnessLandscape = null;
    144137      clone.Initialize();
    145138      return clone;
     
    154147    }
    155148
    156     public event EventHandler RealVectorChanged;
    157     private void OnRealVectorChanged() {
    158       var changed = RealVectorChanged;
     149    public event EventHandler BestRealVectorChanged;
     150    private void OnBestRealVectorChanged() {
     151      var changed = BestRealVectorChanged;
    159152      if (changed != null)
    160153        changed(this, EventArgs.Empty);
     
    190183      BestKnownRealVector.Reset -= new EventHandler(BestKnownRealVector_Reset);
    191184    }
    192     private void RegisterRealVectorEvents() {
    193       BestRealVector.ItemChanged += new EventHandler<EventArgs<int>>(RealVector_ItemChanged);
    194       BestRealVector.Reset += new EventHandler(RealVector_Reset);
    195     }
    196     private void DeregisterRealVectorEvents() {
    197       BestRealVector.ItemChanged -= new EventHandler<EventArgs<int>>(RealVector_ItemChanged);
    198       BestRealVector.Reset -= new EventHandler(RealVector_Reset);
     185    private void RegisterBestRealVectorEvents() {
     186      BestRealVector.ItemChanged += new EventHandler<EventArgs<int>>(BestRealVector_ItemChanged);
     187      BestRealVector.Reset += new EventHandler(BestRealVector_Reset);
     188    }
     189    private void DeregisterBestRealVectorEvents() {
     190      BestRealVector.ItemChanged -= new EventHandler<EventArgs<int>>(BestRealVector_ItemChanged);
     191      BestRealVector.Reset -= new EventHandler(BestRealVector_Reset);
    199192    }
    200193    private void RegisterQualityEvents() {
     
    221214      OnBestKnownRealVectorChanged();
    222215    }
    223     private void RealVector_ItemChanged(object sender, EventArgs<int> e) {
    224       OnRealVectorChanged();
    225     }
    226     private void RealVector_Reset(object sender, EventArgs e) {
    227       OnRealVectorChanged();
     216    private void BestRealVector_ItemChanged(object sender, EventArgs<int> e) {
     217      OnBestRealVectorChanged();
     218    }
     219    private void BestRealVector_Reset(object sender, EventArgs e) {
     220      OnBestRealVectorChanged();
    228221    }
    229222    private void Quality_ValueChanged(object sender, EventArgs e) {
Note: See TracChangeset for help on using the changeset viewer.