Free cookie consent management tool by TermsFeed Policy Generator

source: branches/BinPackingExtension/HeuristicLab.Problems.BinPacking.Views/3.3/BinPacking3DProblemView.cs @ 14897

Last change on this file since 14897 was 14876, checked in by dsouravl, 7 years ago

#2762: implemented sorting heuristics, starting to work on best fit heuristics

File size: 5.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using HeuristicLab.Optimization.Views;
10using HeuristicLab.MainForm;
11using HeuristicLab.MainForm.WindowsForms;
12using HeuristicLab.Core;
13using HeuristicLab.Problems.BinPacking3D;
14using HeuristicLab.Data;
15
16namespace HeuristicLab.Problems.BinPacking.Views {
17    [View("Permutation 3D Bin Packing Problem View")]
18    [Content(typeof(BinPacking3D.PermutationProblem), IsDefaultView = true)]
19    public partial class BinPacking3DProblemView : ProblemView {
20
21      public new BinPacking3D.PermutationProblem Content {
22        get { return (BinPacking3D.PermutationProblem)base.Content; }
23        set { base.Content = value; }
24      }
25
26      public BinPacking3DProblemView() {
27          InitializeComponent();
28      }
29
30      protected override void OnContentChanged() {
31        base.OnContentChanged();
32        sortingComboBox.SelectedIndex = 0;
33        heuristicComboBox.SelectedIndex = 0;
34      }
35
36      private void heuristicComboBox_SelectedIndexChanged(object sender, EventArgs e) {
37        if (Content == null) return;
38        if (heuristicComboBox.SelectedItem.ToString().CompareTo("Best Fit (Free Volume)") == 0) {
39         
40        }
41      }
42
43      private void sortingComboBox_SelectedIndexChanged(object sender, EventArgs e) {
44        if (Content == null) return;
45        deltaLabel.Visible = false;
46        deltaTextBox.Visible = false;
47        var items = new ItemList<PackingItem>(Content.Items);
48
49         if (sortingComboBox.SelectedItem.ToString().CompareTo("Volume-Height") == 0) {
50           var sortedItems = new ItemList<PackingItem>(items.OrderByDescending(x => x.Depth * x.Width * x.Height).ThenByDescending(x => x.Height));
51           Content.Items = sortedItems.AsReadOnly();
52          }
53          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Height-Volume") == 0) {
54            var sortedItems = new ItemList<PackingItem>(items.OrderByDescending(x => x.Height).ThenByDescending(x => x.Depth * x.Width * x.Height));
55            Content.Items = sortedItems.AsReadOnly();
56          }
57          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Area-Height") == 0) {
58            var sortedItems = new ItemList<PackingItem>(items.OrderByDescending(x => x.Depth * x.Width).ThenByDescending(x => x.Height));
59            Content.Items = sortedItems.AsReadOnly();
60          }
61          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Height-Area") == 0) {
62            var sortedItems = new ItemList<PackingItem>(items.OrderByDescending(x => x.Height).ThenByDescending(x => x.Depth * x.Width));
63            Content.Items = sortedItems.AsReadOnly();
64          }
65          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Clustered Area-Height") == 0) {
66            deltaLabel.Visible = true;
67            deltaTextBox.Visible = true;
68            deltaTextBox.Clear();
69          }
70          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Clustered Height-Area") == 0) {
71            deltaLabel.Visible = true;
72            deltaTextBox.Visible = true;
73            deltaTextBox.Clear();
74          }
75        }
76
77        private void deltaTextBox_TextChanged(object sender, EventArgs e) {
78          Dictionary<int, ItemList<PackingItem>> clusters = new Dictionary<int, ItemList<PackingItem>>();
79          var items = new ItemList<PackingItem>(Content.Items);
80
81          if (deltaTextBox.Text.Length > 0) {
82           int delta = Convert.ToInt32(deltaTextBox.Text);
83           
84           if (sortingComboBox.SelectedItem.ToString().CompareTo("Clustered Area-Height") == 0) {
85             // Cluster packing items with respect to area = width * depth
86             for (int i = 0; i < items.Count; i++) {
87               double clusterRange = items[i].TargetBin.Width * items[i].TargetBin.Depth * delta / 100.0;
88               int clusterId = (int)(Math.Ceiling(items[i].Width * items[i].Depth / clusterRange));
89
90               if (clusters.ContainsKey(clusterId)) clusters[clusterId].Add(items[i]);
91               else clusters.Add(clusterId, new ItemList<PackingItem> {items[i]});
92              }           
93
94              // Sort packing items inside the cluster where they belong with respect to height
95              foreach (KeyValuePair<int, ItemList<PackingItem>> entry in clusters) {
96                clusters[entry.Key].OrderByDescending(x => x.Height);
97              }
98            }
99
100            else if (sortingComboBox.SelectedItem.ToString().CompareTo("Clustered Height-Area") == 0) {
101              // Cluster packing items with respect to height
102              for (int i = 0; i < items.Count; i++) {
103                double clusterRange = items[i].TargetBin.Height * delta / 100.0;
104                int clusterId = Convert.ToInt32(Math.Ceiling(items[i].Height / clusterRange));
105
106                if (clusters.ContainsKey(clusterId)) clusters[clusterId].Add(items[i]);
107                else clusters.Add(clusterId, new ItemList<PackingItem> {items[i]});
108              }           
109
110              // Sort packing items inside the cluster where they belong with respect to area = width * depth
111              foreach (KeyValuePair<int, ItemList<PackingItem>> entry in clusters) {
112                  clusters[entry.Key].OrderByDescending(x => x.Width * x.Depth);
113              }
114            }
115           
116            // Insert all packing items into a single list
117            var sortedItems = new ItemList<PackingItem>(clusters.OrderByDescending(x => x.Key).SelectMany(x => x.Value));
118            Content.Items = sortedItems.AsReadOnly();
119        }
120      }
121    }
122  }
Note: See TracBrowser for help on using the repository browser.