Changeset 3665
- Timestamp:
- 05/06/10 05:02:15 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.TestFunctions.Views/3.3/SingleObjectiveTestFunctionSolutionView.cs
r3661 r3665 24 24 using System.Windows.Forms; 25 25 using HeuristicLab.Core.Views; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.MainForm; 27 using HeuristicLab.Problems.TestFunctions; 28 using HeuristicLab.Data; 29 using System.Collections.Generic; 28 using HeuristicLab.Encodings.RealVectorEncoding; 30 29 31 30 namespace HeuristicLab.Problems.TestFunctions.Views { … … 35 34 [View("Single Objective Test Functions View")] 36 35 [Content(typeof(SingleObjectiveTestFunctionSolution), true)] 37 public partial class SingleObjectiveTestFunctionSolutionView : HeuristicLab.Core.Views.ItemView { 36 public partial class SingleObjectiveTestFunctionSolutionView : ItemView { 37 private Bitmap backgroundImage; 38 38 39 public new SingleObjectiveTestFunctionSolution Content { 39 40 get { return (SingleObjectiveTestFunctionSolution)base.Content; } … … 43 44 public SingleObjectiveTestFunctionSolutionView() { 44 45 InitializeComponent(); 45 46 pictureBox.SizeChanged += new EventHandler(pictureBox_SizeChanged); 46 47 qualityView.ReadOnly = true; 47 48 realVectorView.ReadOnly = true; 49 backgroundImage = null; 48 50 } 49 51 50 52 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); 52 55 Content.QualityChanged -= new EventHandler(Content_QualityChanged); 56 Content.PopulationChanged -= new EventHandler(Content_PopulationChanged); 53 57 base.DeregisterContentEvents(); 54 58 } 55 59 protected override void RegisterContentEvents() { 56 60 base.RegisterContentEvents(); 57 Content.RealVectorChanged += new EventHandler(Content_RealVectorChanged); 61 Content.BestKnownRealVectorChanged += new EventHandler(Content_BestKnownRealVectorChanged); 62 Content.BestRealVectorChanged += new EventHandler(Content_BestRealVectorChanged); 58 63 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) { 71 76 if (InvokeRequired) 72 77 Invoke(new EventHandler(Content_QualityChanged), sender, e); … … 75 80 realVectorView.Content = Content.BestRealVector; 76 81 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(); 77 102 } 78 103 } … … 92 117 93 118 pictureBox.Visible = Content.BestRealVector.Length == 2; 119 GenerateImage(); 94 120 } 95 121 … … 101 127 realVectorView.Enabled = Content != null; 102 128 } 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 } 103 201 } 104 202 } -
trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Evaluators/SingleObjectiveTestFunctionProblemEvaluator.cs
r3520 r3665 84 84 } 85 85 86 public virtual double Evaluate2D(double x, double y) { 87 return EvaluateFunction(new RealVector(new double[] { x, y })); 88 } 89 86 90 /// <summary> 87 91 /// Evaluates the test function for a specific <paramref name="point"/>. -
trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Interfaces/ISingleObjectiveTestFunctionProblemEvaluator.cs
r3376 r3665 38 38 39 39 ILookupParameter<RealVector> PointParameter { get; } 40 41 double Evaluate2D(double x, double y); 40 42 } 41 43 } -
trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionSolution.cs
r3661 r3665 60 60 set { 61 61 if (bestRealVector != value) { 62 if (bestRealVector != null) Deregister RealVectorEvents();62 if (bestRealVector != null) DeregisterBestRealVectorEvents(); 63 63 bestRealVector = value; 64 if (bestRealVector != null) Register RealVectorEvents();65 On RealVectorChanged();64 if (bestRealVector != null) RegisterBestRealVectorEvents(); 65 OnBestRealVectorChanged(); 66 66 } 67 67 } … … 106 106 } 107 107 } 108 }109 110 private Image fitnessLandscape;111 public Image FitnessLandscape {112 get { return fitnessLandscape; }113 set { fitnessLandscape = value; }114 108 } 115 109 … … 128 122 private void Initialize() { 129 123 if (bestKnownRealVector != null) RegisterBestKnownRealVectorEvents(); 130 if (bestRealVector != null) Register RealVectorEvents();124 if (bestRealVector != null) RegisterBestRealVectorEvents(); 131 125 if (bestQuality != null) RegisterQualityEvents(); 132 126 if (population != null) RegisterPopulationEvents(); … … 141 135 clone.population = (ItemArray<RealVector>)cloner.Clone(population); 142 136 clone.evaluator = (ISingleObjectiveTestFunctionProblemEvaluator)cloner.Clone(evaluator); 143 clone.fitnessLandscape = null;144 137 clone.Initialize(); 145 138 return clone; … … 154 147 } 155 148 156 public event EventHandler RealVectorChanged;157 private void On RealVectorChanged() {158 var changed = RealVectorChanged;149 public event EventHandler BestRealVectorChanged; 150 private void OnBestRealVectorChanged() { 151 var changed = BestRealVectorChanged; 159 152 if (changed != null) 160 153 changed(this, EventArgs.Empty); … … 190 183 BestKnownRealVector.Reset -= new EventHandler(BestKnownRealVector_Reset); 191 184 } 192 private void Register RealVectorEvents() {193 BestRealVector.ItemChanged += new EventHandler<EventArgs<int>>( RealVector_ItemChanged);194 BestRealVector.Reset += new EventHandler( RealVector_Reset);195 } 196 private void Deregister RealVectorEvents() {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); 199 192 } 200 193 private void RegisterQualityEvents() { … … 221 214 OnBestKnownRealVectorChanged(); 222 215 } 223 private void RealVector_ItemChanged(object sender, EventArgs<int> e) {224 On RealVectorChanged();225 } 226 private void RealVector_Reset(object sender, EventArgs e) {227 On RealVectorChanged();216 private void BestRealVector_ItemChanged(object sender, EventArgs<int> e) { 217 OnBestRealVectorChanged(); 218 } 219 private void BestRealVector_Reset(object sender, EventArgs e) { 220 OnBestRealVectorChanged(); 228 221 } 229 222 private void Quality_ValueChanged(object sender, EventArgs e) {
Note: See TracChangeset
for help on using the changeset viewer.