1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Joseph Helm and 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Collections;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.BinPacking {
|
---|
31 | [Item("PackingPlan", "Represents a concrete solution for a bin-packing problem.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class PackingPlan<D, B, I> : Item, IPackingPlan
|
---|
34 | where D : class, IPackingPosition
|
---|
35 | where B : PackingShape<D>
|
---|
36 | where I : PackingShape<D>, IPackingItem {
|
---|
37 |
|
---|
38 | #region Properties
|
---|
39 | public int NrOfBins {
|
---|
40 | get {
|
---|
41 | if (BinPackings != null)
|
---|
42 | return BinPackings.Count;
|
---|
43 | else return 0;
|
---|
44 | }
|
---|
45 | }
|
---|
46 | [Storable]
|
---|
47 | protected bool StackingConstraints { get; set; }
|
---|
48 | [Storable]
|
---|
49 | protected bool UseExtremePoints { get; set; }
|
---|
50 |
|
---|
51 | [Storable]
|
---|
52 | public B BinMeasures { get; private set; }
|
---|
53 |
|
---|
54 | [Storable]
|
---|
55 | public ObservableList<BinPacking<D, B, I>> BinPackings { get; set; }
|
---|
56 |
|
---|
57 | [Storable]
|
---|
58 | private DoubleValue quality;
|
---|
59 | public DoubleValue Quality {
|
---|
60 | get { return quality; }
|
---|
61 | set {
|
---|
62 | if (quality != value) {
|
---|
63 | if (quality != null) DeregisterQualityEvents();
|
---|
64 | quality = value;
|
---|
65 | if (quality != null) RegisterQualityEvents();
|
---|
66 | OnQualityChanged();
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 | protected PackingPlan(B binMeasures, bool useExtremePoints, bool stackingConstraints)
|
---|
73 | : base() {
|
---|
74 | BinMeasures = (B)binMeasures.Clone();
|
---|
75 | StackingConstraints = stackingConstraints;
|
---|
76 | UseExtremePoints = useExtremePoints;
|
---|
77 | BinPackings = new ObservableList<BinPacking<D, B, I>>();
|
---|
78 | }
|
---|
79 |
|
---|
80 | [StorableConstructor]
|
---|
81 | protected PackingPlan(bool deserializing) : base(deserializing) { }
|
---|
82 | protected PackingPlan(PackingPlan<D, B, I> original, Cloner cloner)
|
---|
83 | : base(original, cloner) {
|
---|
84 | this.BinPackings = new ObservableList<BinPacking<D, B, I>>(original.BinPackings);
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | public abstract BinPacking<D, B, I> NewBinPacking();
|
---|
89 | public void UpdateBinPackings() {
|
---|
90 | BinPackings.RemoveAll(x => x.ItemPositions.Count == 0);
|
---|
91 | BinPackings = new ObservableList<BinPacking<D, B, I>>(BinPackings.OrderByDescending(bp => bp.PackingDensity));
|
---|
92 | }
|
---|
93 |
|
---|
94 | /*
|
---|
95 | public void Pack(MultiComponentVectorEncoding solution, ItemList<I> itemMeasures) {
|
---|
96 | var sequenceMatrix = solution.GenerateSequenceMatrix();
|
---|
97 | Dictionary<int, bool> rotated = solution.GenerateRotationArray();
|
---|
98 |
|
---|
99 | //Fill bins according to grouping vector
|
---|
100 | List<int> remainingIDs = new List<int>();
|
---|
101 | foreach (var sequence in sequenceMatrix) {
|
---|
102 | remainingIDs = remainingIDs.Concat(sequence).ToList();
|
---|
103 | var bp = NewBinPacking();
|
---|
104 | if (!UseExtremePoints)
|
---|
105 | bp.SlidingBasedPacking(ref remainingIDs, itemMeasures, rotated);
|
---|
106 | else
|
---|
107 | bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, StackingConstraints, rotated);
|
---|
108 | BinPackings.Add(bp);
|
---|
109 | }
|
---|
110 | UpdateBinPackings();
|
---|
111 |
|
---|
112 | //Try to put remaining items in existing bins
|
---|
113 | var temp = new List<int>(remainingIDs);
|
---|
114 | foreach (int id in temp) {
|
---|
115 | foreach (var bp in BinPackings) {
|
---|
116 | var position = UseExtremePoints ? bp.FindExtremePointForItem(itemMeasures[id], rotated[id], StackingConstraints) : bp.FindPositionBySliding(itemMeasures[id], rotated[id]);
|
---|
117 | if (position != null) {
|
---|
118 | bp.PackItem(id, itemMeasures[id], position);
|
---|
119 | remainingIDs.Remove(id);
|
---|
120 | break;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | //Put still remaining items in new bins
|
---|
126 | while (remainingIDs.Count > 0) {
|
---|
127 | var bp = NewBinPacking();
|
---|
128 | if (!UseExtremePoints)
|
---|
129 | bp.SlidingBasedPacking(ref remainingIDs, itemMeasures, rotated);
|
---|
130 | else
|
---|
131 | bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, StackingConstraints, rotated);
|
---|
132 | BinPackings.Add(bp);
|
---|
133 | }
|
---|
134 | UpdateBinPackings();
|
---|
135 |
|
---|
136 | var newSolution = new ObservableDictionary<int, ItemList<PackingInformation>>();
|
---|
137 | foreach (var bp in BinPackings) {
|
---|
138 | int binIndex = newSolution.Count;
|
---|
139 | newSolution[binIndex] = new ItemList<PackingInformation>();
|
---|
140 | foreach (var entry in bp.ItemPositions)
|
---|
141 | newSolution[binIndex].Add(new PackingInformation(entry.Key, entry.Value.Rotated));
|
---|
142 | }
|
---|
143 | solution.PackingInformations = newSolution;
|
---|
144 | }
|
---|
145 | public void Pack(GroupingVectorEncoding solution, ItemList<I> itemMeasures) {
|
---|
146 | }
|
---|
147 | *
|
---|
148 |
|
---|
149 |
|
---|
150 | */
|
---|
151 | #region Events
|
---|
152 | public event EventHandler QualityChanged;
|
---|
153 | private void OnQualityChanged() {
|
---|
154 | var changed = QualityChanged;
|
---|
155 | if (changed != null)
|
---|
156 | changed(this, EventArgs.Empty);
|
---|
157 | }
|
---|
158 | private void RegisterQualityEvents() {
|
---|
159 | Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
|
---|
160 | }
|
---|
161 | private void DeregisterQualityEvents() {
|
---|
162 | Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
|
---|
163 | }
|
---|
164 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
165 | OnQualityChanged();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public event EventHandler BinPackingsChanged;
|
---|
169 | private void OnBinPackingsChanged() {
|
---|
170 | var changed = BinPackingsChanged;
|
---|
171 | if (changed != null)
|
---|
172 | changed(this, EventArgs.Empty);
|
---|
173 | }
|
---|
174 | private void RegisterBinPackingsEvents() {
|
---|
175 | BinPackings.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(BinPackings_PropertyChanged);
|
---|
176 | }
|
---|
177 | private void DeregisterBinPackingsEvents() {
|
---|
178 | BinPackings.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(BinPackings_PropertyChanged);
|
---|
179 | }
|
---|
180 | private void BinPackings_PropertyChanged(object sender, EventArgs e) {
|
---|
181 | OnBinPackingsChanged();
|
---|
182 | }
|
---|
183 | #endregion
|
---|
184 | }
|
---|
185 | }
|
---|