Changeset 7438 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views/3.3/GQAPAssignmentView.cs
- Timestamp:
- 02/01/12 13:28:54 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views/3.3/GQAPAssignmentView.cs
r7418 r7438 23 23 using System.Collections.Generic; 24 24 using System.ComponentModel; 25 using System.Drawing; 25 26 using System.Linq; 26 27 using System.Windows.Forms; … … 34 35 [Content(typeof(GQAPAssignment), IsDefaultView = true)] 35 36 public partial class GQAPAssignmentView : ItemView { 37 private static readonly double BrightnessLevel = 0.5; 38 36 39 public new GQAPAssignment Content { 37 40 get { return (GQAPAssignment)base.Content; } … … 69 72 } 70 73 71 #region Event Handlers74 #region Content Event Handlers 72 75 private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) { 73 76 switch (e.PropertyName) { … … 89 92 #endregion 90 93 94 #region Event Handlers 95 private void assignmentTreeView_AfterSelect(object sender, TreeViewEventArgs e) { 96 if (Content != null) { 97 assignmentTreeView.BeginUpdate(); 98 try { 99 if (assignmentTreeView.SelectedNode == null) { 100 ColorByInstallationCosts(); 101 } else { 102 var equipmentNode = (assignmentTreeView.SelectedNode as EquipmentNode); 103 if (equipmentNode != null) { 104 ColorByWeight(equipmentNode); 105 } 106 var locationNode = (assignmentTreeView.SelectedNode as LocationNode); 107 if (locationNode != null) { 108 ColorByWeight(locationNode); 109 } 110 } 111 } finally { assignmentTreeView.EndUpdate(); } 112 } 113 } 114 115 private void assignmentTreeView_MouseUp(object sender, MouseEventArgs e) { 116 var hit = assignmentTreeView.HitTest(e.X, e.Y); 117 if (hit.Node == null || hit.Location == TreeViewHitTestLocations.None || hit.Location == TreeViewHitTestLocations.RightOfLabel) { 118 assignmentTreeView.SelectedNode = null; 119 ColorByInstallationCosts(); 120 } 121 } 122 #endregion 123 124 #region Helpers 91 125 private void UpdateQuality() { 92 126 if (InvokeRequired) Invoke((Action)UpdateQuality); … … 145 179 if (Content.LocationNames != null && Content.LocationNames.Length > assignment[i]) 146 180 locationName = Content.LocationNames[assignment[i]]; 147 locationNodes.Add(assignment[i], new TreeNode(locationName));181 locationNodes.Add(assignment[i], new LocationNode(assignment[i], locationName)); 148 182 } 149 183 string equipmentName = i.ToString(); 150 184 if (Content.EquipmentNames != null && Content.EquipmentNames.Length > i) 151 185 equipmentName = Content.EquipmentNames[i]; 152 locationNodes[assignment[i]].Nodes.Add( equipmentName);186 locationNodes[assignment[i]].Nodes.Add(new EquipmentNode(i, equipmentName)); 153 187 } 154 188 … … 159 193 node.Expand(); 160 194 } 195 ColorByInstallationCosts(); 161 196 } finally { assignmentTreeView.EndUpdate(); } 162 197 } 163 198 } 164 199 } 200 201 private void ColorByInstallationCosts() { 202 var installationCosts = new Dictionary<EquipmentNode, double>(); 203 foreach (var node in GetAllSubNodes(assignmentTreeView.Nodes).OfType<EquipmentNode>()) { 204 int location = Content.Solution.Assignment[node.Equipment]; 205 installationCosts[node] = Content.InstallationCosts[node.Equipment, location]; 206 } 207 double max = installationCosts.Values.Max(); 208 foreach (var kvp in installationCosts) { 209 if (max == 0) { 210 kvp.Key.BackColor = assignmentTreeView.BackColor; 211 kvp.Key.ForeColor = assignmentTreeView.ForeColor; 212 } 213 int colorComponent = (int)(255 * Math.Pow((max - kvp.Value) / max, 2)); 214 kvp.Key.BackColor = Color.FromArgb(255, colorComponent, colorComponent); 215 if (kvp.Key.BackColor.GetBrightness() < BrightnessLevel) kvp.Key.ForeColor = Color.White; 216 else kvp.Key.ForeColor = Color.Black; 217 } 218 } 219 220 private void ColorByWeight(EquipmentNode selectedNode) { 221 int equipment = selectedNode.Equipment; 222 double rowSum = 0, colSum = 0; 223 for (int i = 0; i < Content.Weights.Columns; i++) 224 if (i != equipment) rowSum += Content.Weights[equipment, i]; 225 for (int i = 0; i < Content.Weights.Rows; i++) 226 if (i != equipment) colSum += Content.Weights[i, equipment]; 227 228 var otherEquipments = GetAllSubNodes(assignmentTreeView.Nodes) 229 .OfType<EquipmentNode>() 230 .Where(x => x != selectedNode); 231 232 selectedNode.BackColor = assignmentTreeView.BackColor; 233 selectedNode.ForeColor = assignmentTreeView.ForeColor; 234 foreach (var other in otherEquipments) { 235 if (rowSum == 0 && colSum == 0) { 236 other.BackColor = assignmentTreeView.BackColor; 237 other.ForeColor = assignmentTreeView.ForeColor; 238 } else { 239 double rowInvProportion = rowSum == 0 ? 1.0 : (rowSum - Content.Weights[equipment, other.Equipment]) / rowSum; 240 double colInvProportion = colSum == 0 ? 1.0 : (colSum - Content.Weights[other.Equipment, equipment]) / colSum; 241 int colorComponent = (int)(255 * Math.Pow(Math.Min(rowInvProportion, colInvProportion), 2)); 242 other.BackColor = Color.FromArgb(colorComponent, 255, colorComponent); 243 if (other.BackColor.GetBrightness() < BrightnessLevel) other.ForeColor = Color.White; 244 else other.ForeColor = Color.Black; 245 } 246 } 247 } 248 249 private void ColorByWeight(LocationNode selectedNode) { 250 var equipments = new HashSet<int>(Content.Solution.Assignment.Select((v, i) => new { Index = i, Value = v }) 251 .Where(x => x.Value == selectedNode.Location).Select(x => x.Index)); 252 var rowSums = new Dictionary<int, double>(equipments.Count); 253 var colSums = new Dictionary<int, double>(equipments.Count); 254 foreach (var e in equipments) { 255 rowSums[e] = 0; 256 colSums[e] = 0; 257 for (int i = 0; i < Content.Weights.Columns; i++) { 258 if (!equipments.Contains(i)) rowSums[e] += Content.Weights[e, i]; 259 if (!equipments.Contains(i)) colSums[e] += Content.Weights[i, e]; 260 } 261 } 262 var equipmentNodes = GetAllSubNodes(assignmentTreeView.Nodes) 263 .OfType<EquipmentNode>(); 264 265 var relevantEquipments = new HashSet<int>(equipments.Where(x => rowSums[x] > 0 || colSums[x] > 0)); 266 foreach (var other in equipmentNodes) { 267 if (!relevantEquipments.Any() || equipments.Contains(other.Equipment)) { 268 other.BackColor = assignmentTreeView.BackColor; 269 other.ForeColor = assignmentTreeView.ForeColor; 270 } else { 271 double rowInvProportion = relevantEquipments.Min(x => rowSums[x] == 0 ? 1.0 : (rowSums[x] - Content.Weights[x, other.Equipment]) / rowSums[x]); 272 double colInvProportion = relevantEquipments.Min(x => colSums[x] == 0 ? 1.0 : (colSums[x] - Content.Weights[other.Equipment, x]) / colSums[x]); 273 int colorComponent = (int)(255 * Math.Pow(Math.Min(rowInvProportion, colInvProportion), 2)); 274 other.BackColor = Color.FromArgb(colorComponent, 255, colorComponent); 275 if (other.BackColor.GetBrightness() < BrightnessLevel) other.ForeColor = Color.White; 276 else other.ForeColor = Color.Black; 277 } 278 } 279 } 280 281 private IEnumerable<TreeNode> GetAllSubNodes(TreeNodeCollection nodes) { 282 foreach (TreeNode node in nodes) { 283 yield return node; 284 foreach (TreeNode subNode in GetAllSubNodes(node.Nodes)) 285 yield return subNode; 286 } 287 } 288 #endregion 289 290 #region Helper Classes 291 private class LocationNode : TreeNode { 292 public int Location { get; set; } 293 294 public LocationNode() : base() { } 295 public LocationNode(int location, string name) 296 : base(name) { 297 Location = location; 298 } 299 } 300 301 private class EquipmentNode : TreeNode { 302 public int Equipment { get; set; } 303 304 public EquipmentNode() : base() { } 305 public EquipmentNode(int equipment, string name) 306 : base(name) { 307 Equipment = equipment; 308 } 309 } 310 #endregion 165 311 } 166 312 }
Note: See TracChangeset
for help on using the changeset viewer.