Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15081 was 14976, checked in by dsouravl, 8 years ago

#2762: worked 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      private void sortingComboBox_SelectedIndexChanged(object sender, EventArgs e) {
43        if (Content == null) return;
44        deltaLabel.Visible = false;
45        deltaTextBox.Visible = false;
46        var items = new ItemList<PackingItem>(Content.Items);
47
48         if (sortingComboBox.SelectedItem.ToString().CompareTo("Volume-Height") == 0) {
49           var sortedItems = new ItemList<PackingItem>(items.OrderByDescending(x => x.Depth * x.Width * x.Height).ThenByDescending(x => x.Height));
50           Content.Items = sortedItems.AsReadOnly();
51          }
52          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Height-Volume") == 0) {
53            var sortedItems = new ItemList<PackingItem>(items.OrderByDescending(x => x.Height).ThenByDescending(x => x.Depth * x.Width * x.Height));
54            Content.Items = sortedItems.AsReadOnly();
55          }
56          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Area-Height") == 0) {
57            var sortedItems = new ItemList<PackingItem>(items.OrderByDescending(x => x.Depth * x.Width).ThenByDescending(x => x.Height));
58            Content.Items = sortedItems.AsReadOnly();
59          }
60          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Height-Area") == 0) {
61            var sortedItems = new ItemList<PackingItem>(items.OrderByDescending(x => x.Height).ThenByDescending(x => x.Depth * x.Width));
62            Content.Items = sortedItems.AsReadOnly();
63          }
64          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Clustered Area-Height") == 0) {
65            deltaLabel.Visible = true;
66            deltaTextBox.Visible = true;
67            deltaTextBox.Clear();
68          }
69          else if (sortingComboBox.SelectedItem.ToString().CompareTo("Clustered Height-Area") == 0) {
70            deltaLabel.Visible = true;
71            deltaTextBox.Visible = true;
72            deltaTextBox.Clear();
73          }
74        }
75
76        private void deltaTextBox_TextChanged(object sender, EventArgs e) {
77          Dictionary<int, ItemList<PackingItem>> clusters = new Dictionary<int, ItemList<PackingItem>>();
78          var items = new ItemList<PackingItem>(Content.Items);
79
80          if (deltaTextBox.Text.Length > 0) {
81           int delta = Convert.ToInt32(deltaTextBox.Text);
82           
83           if (sortingComboBox.SelectedItem.ToString().CompareTo("Clustered Area-Height") == 0) {
84             // Cluster packing items with respect to area = width * depth
85             for (int i = 0; i < items.Count; i++) {
86               double clusterRange = items[i].TargetBin.Width * items[i].TargetBin.Depth * delta / 100.0;
87               int clusterId = (int)(Math.Ceiling(items[i].Width * items[i].Depth / clusterRange));
88
89               if (clusters.ContainsKey(clusterId)) clusters[clusterId].Add(items[i]);
90               else clusters.Add(clusterId, new ItemList<PackingItem> {items[i]});
91              }           
92
93              // Sort packing items inside the cluster where they belong with respect to height
94              foreach (KeyValuePair<int, ItemList<PackingItem>> entry in clusters) {
95                clusters[entry.Key].OrderByDescending(x => x.Height);
96              }
97            }
98
99            else if (sortingComboBox.SelectedItem.ToString().CompareTo("Clustered Height-Area") == 0) {
100              // Cluster packing items with respect to height
101              for (int i = 0; i < items.Count; i++) {
102                double clusterRange = items[i].TargetBin.Height * delta / 100.0;
103                int clusterId = Convert.ToInt32(Math.Ceiling(items[i].Height / clusterRange));
104
105                if (clusters.ContainsKey(clusterId)) clusters[clusterId].Add(items[i]);
106                else clusters.Add(clusterId, new ItemList<PackingItem> {items[i]});
107              }           
108
109              // Sort packing items inside the cluster where they belong with respect to area = width * depth
110              foreach (KeyValuePair<int, ItemList<PackingItem>> entry in clusters) {
111                  clusters[entry.Key].OrderByDescending(x => x.Width * x.Depth);
112              }
113            }
114           
115            // Insert all packing items into a single list
116            var sortedItems = new ItemList<PackingItem>(clusters.OrderByDescending(x => x.Key).SelectMany(x => x.Value));
117            Content.Items = sortedItems.AsReadOnly();
118        }
119      }
120    }
121  }
Note: See TracBrowser for help on using the repository browser.