Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3566 was 3566, checked in by mkommend, 14 years ago

removed ctors with contents in all views (ticket #972)

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    protected override void DeregisterContentEvents() {
48      Content.BinaryVectorChanged -= new EventHandler(Content_BinaryVectorChanged);
49      Content.CapacityChanged -= new EventHandler(Content_CapacityChanged);
50      Content.WeightsChanged -= new EventHandler(Content_WeightsChanged);
51      Content.ValuesChanged -= new EventHandler(Content_ValuesChanged);
52      base.DeregisterContentEvents();
53    }
54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.BinaryVectorChanged += new EventHandler(Content_BinaryVectorChanged);
58      Content.CapacityChanged += new EventHandler(Content_CapacityChanged);
59      Content.WeightsChanged += new EventHandler(Content_WeightsChanged);
60      Content.ValuesChanged += new EventHandler(Content_ValuesChanged);
61    }
62
63    void Content_BinaryVectorChanged(object sender, EventArgs e) {
64      if (InvokeRequired)
65        Invoke(new EventHandler(Content_BinaryVectorChanged), sender, e);
66      else
67        GenerateImage();
68    }
69
70    void Content_CapacityChanged(object sender, EventArgs e) {
71      if (InvokeRequired)
72        Invoke(new EventHandler(Content_CapacityChanged), sender, e);
73      else
74        GenerateImage();
75    }
76
77    void Content_WeightsChanged(object sender, EventArgs e) {
78      if (InvokeRequired)
79        Invoke(new EventHandler(Content_WeightsChanged), sender, e);
80      else
81        GenerateImage();
82    }
83
84    void Content_ValuesChanged(object sender, EventArgs e) {
85      if (InvokeRequired)
86        Invoke(new EventHandler(Content_ValuesChanged), sender, e);
87      else
88        GenerateImage();
89    }
90
91    protected override void OnContentChanged() {
92      base.OnContentChanged();
93      if (Content == null) {
94        pictureBox.Image = null;
95      } else {
96        GenerateImage();
97      }
98      SetEnabledStateOfControls();
99    }
100
101    protected override void OnReadOnlyChanged() {
102      base.OnReadOnlyChanged();
103      SetEnabledStateOfControls();
104    }
105
106    private void SetEnabledStateOfControls() {
107      pictureBox.Enabled = Content != null;
108    }
109
110    private void GenerateImage() {
111      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
112        if (Content == null) {
113          pictureBox.Image = null;
114        } else {
115          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
116          using (Graphics graphics = Graphics.FromImage(bitmap)) {
117            int border = 10;
118
119            int borderX = border;
120            if (pictureBox.Width - border * 2 < 0)
121              borderX = 0;
122            int width = pictureBox.Width - borderX * 2;
123
124            int borderY = border;
125            if (pictureBox.Height - border * 2 < 0)
126              borderY = 0;
127            int height = pictureBox.Height - borderY * 2;
128
129            //preprocess
130            double capacity = Content.Capacity.Value;
131
132            if (capacity != 0) {
133              double packedCapacity = 0;
134              double maxValue = 0;
135              for (int i = 0; i < Content.BinaryVector.Length; i++) {
136                if (Content.BinaryVector[i]) {
137                  packedCapacity += Content.Weights[i];
138                }
139
140                if (Content.Values[i] > maxValue)
141                  maxValue = Content.Values[i];
142              }
143
144              int knapsackHeight = height;
145              if (packedCapacity > capacity) {
146                knapsackHeight = (int)Math.Round(capacity / packedCapacity * (double)height);
147              }
148
149              //draw knapsack
150              using (Pen pen = new Pen(Color.Black, 2)) {
151                graphics.DrawRectangle(pen,
152                  borderX - 2, pictureBox.Height - borderY - knapsackHeight - 2, width + 4, knapsackHeight + 4);
153              }
154
155              //draw items sorted by value
156              List<int> sortedIndices = new List<int>();
157              for (int i = 0; i < Content.BinaryVector.Length; i++) {
158                if(Content.BinaryVector[i]) {
159                  sortedIndices.Add(i);
160                }
161              }
162
163              sortedIndices.Sort(
164                delegate(int i, int j) {
165                  if (Content.Values[i] < Content.Values[j])
166                    return -1;
167                  else if (Content.Values[i] > Content.Values[j])
168                    return 1;
169                  else
170                    return 0;
171                });
172
173              int currentPosition = pictureBox.Height - borderY;
174              foreach (int i in sortedIndices) {
175                if (Content.BinaryVector[i]) {
176                 
177                  double weight = Content.Weights[i];
178                  double factor = weight / capacity;
179                  int elementHeight = (int)Math.Floor(knapsackHeight * factor);
180
181                  double value = Content.Values[i];
182                  //color according to value
183                  int colorValue = 0;
184                  if (value != 0)
185                    colorValue = (int)Math.Round(255.0 * value / maxValue);
186                  Color color = Color.FromArgb(
187                    0, 0, colorValue);
188
189                  using (Brush brush = new SolidBrush(color)) {
190                    graphics.FillRectangle(brush,
191                      borderX, currentPosition - elementHeight, width, elementHeight);
192                  }
193                  graphics.DrawRectangle(Pens.White,
194                    borderX, currentPosition - elementHeight, width, elementHeight);
195
196                  currentPosition -= elementHeight;
197                }
198              }
199            }
200          }
201          pictureBox.Image = bitmap;
202        }
203      }
204    }
205
206    private void pictureBox_SizeChanged(object sender, EventArgs e) {
207      GenerateImage();
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.