Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Knapsack.Views/3.3/KnapsackSolutionView.cs @ 3537

Last change on this file since 3537 was 3537, checked in by svonolfe, 14 years ago

Implemented reviewers comments for the KnapsackProblem (#917)

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.Problems.Knapsack;
28using HeuristicLab.Data;
29using System.Collections.Generic;
30
31namespace HeuristicLab.Problems.Knapsack.Views {
32  /// <summary>
33  /// A view for a knapsack solution.
34  /// </summary>
35  [View("Knapsack View")]
36  [Content(typeof(KnapsackSolution), true)]
37  public partial class KnapsackSolutionView : HeuristicLab.Core.Views.ItemView {
38    public new KnapsackSolution Content {
39      get { return (KnapsackSolution)base.Content; }
40      set { base.Content = value; }
41    }
42   
43    public KnapsackSolutionView() {
44      InitializeComponent();
45    }
46
47    public KnapsackSolutionView(KnapsackSolution content)
48      : this() {
49      Content = content;
50    }
51
52    protected override void DeregisterContentEvents() {
53      Content.BinaryVectorChanged -= new EventHandler(Content_BinaryVectorChanged);
54      Content.CapacityChanged -= new EventHandler(Content_CapacityChanged);
55      Content.WeightsChanged -= new EventHandler(Content_WeightsChanged);
56      Content.ValuesChanged -= new EventHandler(Content_ValuesChanged);
57      base.DeregisterContentEvents();
58    }
59
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.BinaryVectorChanged += new EventHandler(Content_BinaryVectorChanged);
63      Content.CapacityChanged += new EventHandler(Content_CapacityChanged);
64      Content.WeightsChanged += new EventHandler(Content_WeightsChanged);
65      Content.ValuesChanged += new EventHandler(Content_ValuesChanged);
66    }
67
68    void Content_BinaryVectorChanged(object sender, EventArgs e) {
69      if (InvokeRequired)
70        Invoke(new EventHandler(Content_BinaryVectorChanged), sender, e);
71      else
72        GenerateImage();
73    }
74
75    void Content_CapacityChanged(object sender, EventArgs e) {
76      if (InvokeRequired)
77        Invoke(new EventHandler(Content_CapacityChanged), sender, e);
78      else
79        GenerateImage();
80    }
81
82    void Content_WeightsChanged(object sender, EventArgs e) {
83      if (InvokeRequired)
84        Invoke(new EventHandler(Content_WeightsChanged), sender, e);
85      else
86        GenerateImage();
87    }
88
89    void Content_ValuesChanged(object sender, EventArgs e) {
90      if (InvokeRequired)
91        Invoke(new EventHandler(Content_ValuesChanged), sender, e);
92      else
93        GenerateImage();
94    }
95
96    protected override void OnContentChanged() {
97      base.OnContentChanged();
98      if (Content == null) {
99        pictureBox.Image = null;
100      } else {
101        GenerateImage();
102      }
103      SetEnabledStateOfControls();
104    }
105
106    protected override void OnReadOnlyChanged() {
107      base.OnReadOnlyChanged();
108      SetEnabledStateOfControls();
109    }
110
111    private void SetEnabledStateOfControls() {
112      pictureBox.Enabled = Content != null;
113    }
114
115    private void GenerateImage() {
116      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
117        if (Content == null) {
118          pictureBox.Image = null;
119        } else {
120          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
121          using (Graphics graphics = Graphics.FromImage(bitmap)) {
122            int border = 10;
123
124            int borderX = border;
125            if (pictureBox.Width - border * 2 < 0)
126              borderX = 0;
127            int width = pictureBox.Width - borderX * 2;
128
129            int borderY = border;
130            if (pictureBox.Height - border * 2 < 0)
131              borderY = 0;
132            int height = pictureBox.Height - borderY * 2;
133
134            //preprocess
135            double capacity = Content.Capacity.Value;
136
137            if (capacity != 0) {
138              double packedCapacity = 0;
139              double maxValue = 0;
140              for (int i = 0; i < Content.BinaryVector.Length; i++) {
141                if (Content.BinaryVector[i]) {
142                  packedCapacity += Content.Weights[i];
143                }
144
145                if (Content.Values[i] > maxValue)
146                  maxValue = Content.Values[i];
147              }
148
149              int knapsackHeight = height;
150              if (packedCapacity > capacity) {
151                knapsackHeight = (int)Math.Round(capacity / packedCapacity * (double)height);
152              }
153
154              //draw knapsack
155              graphics.DrawRectangle(Pens.Black,
156                borderX - 1, pictureBox.Height - borderY - knapsackHeight - 1, width + 2, knapsackHeight + 2);
157
158              //draw items sorted by value
159              List<int> sortedIndices = new List<int>();
160              for (int i = 0; i < Content.BinaryVector.Length; i++) {
161                if(Content.BinaryVector[i]) {
162                  sortedIndices.Add(i);
163                }
164              }
165
166              sortedIndices.Sort(
167                delegate(int i, int j) {
168                  if (Content.Values[i] < Content.Values[j])
169                    return -1;
170                  else if (Content.Values[i] > Content.Values[j])
171                    return 1;
172                  else
173                    return 0;
174                });
175
176              int currentPosition = pictureBox.Height - borderY;
177              foreach (int i in sortedIndices) {
178                if (Content.BinaryVector[i]) {
179                 
180                  double weight = Content.Weights[i];
181                  double factor = weight / capacity;
182                  int elementHeight = (int)Math.Floor(knapsackHeight * factor);
183
184                  double value = Content.Values[i];
185                  //color according to value
186                  int colorValue = 0;
187                  if (value != 0)
188                    colorValue = (int)Math.Round(255.0 * value / maxValue);
189                  Color color = Color.FromArgb(
190                    0, 0, colorValue);
191
192                  using (Brush brush = new SolidBrush(color)) {
193                    graphics.FillRectangle(brush,
194                      borderX, currentPosition - elementHeight, width, elementHeight);
195                  }
196                  graphics.DrawRectangle(Pens.White,
197                    borderX, currentPosition - elementHeight, width, elementHeight);
198
199                  currentPosition -= elementHeight;
200                }
201              }
202            }
203          }
204          pictureBox.Image = bitmap;
205        }
206      }
207    }
208
209    private void pictureBox_SizeChanged(object sender, EventArgs e) {
210      GenerateImage();
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.