[9440] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
| 27 | using HeuristicLab.Problems.BinPacking.PackingItem;
|
---|
| 28 | using HeuristicLab.Problems.BinPacking.Shapes;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
| 31 | using HeuristicLab.Common;
|
---|
| 32 | using HeuristicLab.Parameters;
|
---|
| 33 | using HeuristicLab.Data;
|
---|
| 34 | using HeuristicLab.Collections;
|
---|
[9593] | 35 | using HeuristicLab.Problems.BinPacking.Dimensions;
|
---|
| 36 | using HeuristicLab.Problems.BinPacking.PackingBin;
|
---|
[9596] | 37 | using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
|
---|
| 38 | using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
|
---|
| 39 | using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
|
---|
[9599] | 40 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
[9440] | 41 |
|
---|
| 42 | namespace HeuristicLab.Encodings.PackingEncoding.PackingPlan {
|
---|
| 43 | [Item("PackingPlan", "Represents a concrete solution for a bin-packing problem.")]
|
---|
| 44 | [StorableClass]
|
---|
[9596] | 45 | public abstract class PackingPlan<D, B, I> : ParameterizedNamedItem, IPackingPlan
|
---|
[9440] | 46 | where D : class, IPackingDimensions
|
---|
| 47 | where B : PackingShape<D>, IPackingBin
|
---|
| 48 | where I : PackingShape<D>, IPackingItem {
|
---|
| 49 |
|
---|
| 50 | #region Properties
|
---|
| 51 | public int NrOfBins {
|
---|
| 52 | get {
|
---|
[9593] | 53 | if (BinPackings != null)
|
---|
| 54 | return BinPackings.Count;
|
---|
[9440] | 55 | else return 0;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
[9596] | 58 | [Storable]
|
---|
| 59 | protected bool StackingConstraints { get; set; }
|
---|
| 60 | [Storable]
|
---|
| 61 | protected bool UseExtremePoints { get; set; }
|
---|
[9440] | 62 |
|
---|
| 63 | [Storable]
|
---|
[9596] | 64 | public B BinMeasures { get; private set; }
|
---|
| 65 |
|
---|
| 66 | [Storable]
|
---|
[9593] | 67 | public ObservableList<BinPacking<D, B, I>> BinPackings { get; set; }
|
---|
[9440] | 68 |
|
---|
| 69 | [Storable]
|
---|
| 70 | private DoubleValue quality;
|
---|
| 71 | public DoubleValue Quality {
|
---|
| 72 | get { return quality; }
|
---|
| 73 | set {
|
---|
| 74 | if (quality != value) {
|
---|
| 75 | if (quality != null) DeregisterQualityEvents();
|
---|
| 76 | quality = value;
|
---|
| 77 | if (quality != null) RegisterQualityEvents();
|
---|
| 78 | OnQualityChanged();
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | #endregion
|
---|
| 83 |
|
---|
[9596] | 84 | public PackingPlan(B binMeasures, bool useExtremePoints, bool stackingConstraints)
|
---|
| 85 | : base(){
|
---|
| 86 | BinMeasures = (B)binMeasures.Clone();
|
---|
| 87 | StackingConstraints = stackingConstraints;
|
---|
| 88 | UseExtremePoints = useExtremePoints;
|
---|
| 89 | BinPackings = new ObservableList<BinPacking<D, B, I>>();
|
---|
[9440] | 90 | }
|
---|
| 91 |
|
---|
| 92 | [StorableConstructor]
|
---|
| 93 | protected PackingPlan(bool deserializing) : base(deserializing) { }
|
---|
| 94 | protected PackingPlan(PackingPlan<D,B,I> original, Cloner cloner)
|
---|
| 95 | : base(original, cloner) {
|
---|
[9593] | 96 | this.BinPackings = new ObservableList<BinPacking<D, B, I>>(original.BinPackings);
|
---|
[9440] | 97 | }
|
---|
[9596] | 98 |
|
---|
| 99 |
|
---|
| 100 | public abstract BinPacking<D, B, I> NewBinPacking();
|
---|
| 101 | public void UpdateBinPackings() {
|
---|
| 102 | BinPackings.RemoveAll(x => x.ItemPositions.Count == 0);
|
---|
| 103 | BinPackings = new ObservableList<BinPacking<D, B, I>>(BinPackings.OrderByDescending (bp => bp.PackingDensity));
|
---|
[9440] | 104 | }
|
---|
| 105 |
|
---|
[9596] | 106 | public void Pack(MultiComponentVectorEncoding solution, ItemList<I> itemMeasures) {
|
---|
| 107 | var sequenceMatrix = solution.GenerateSequenceMatrix();
|
---|
| 108 | Dictionary<int, bool> rotated = solution.GenerateRotationArray();
|
---|
| 109 |
|
---|
| 110 | //Fill bins according to grouping vector
|
---|
| 111 | List<int> remainingIDs = new List<int>();
|
---|
| 112 | foreach (var sequence in sequenceMatrix) {
|
---|
| 113 | remainingIDs = remainingIDs.Concat(sequence).ToList();
|
---|
| 114 | var bp = NewBinPacking();
|
---|
| 115 | if (!UseExtremePoints)
|
---|
| 116 | bp.SlidingBasedPacking(ref remainingIDs, itemMeasures, rotated);
|
---|
| 117 | else
|
---|
| 118 | bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, StackingConstraints, rotated);
|
---|
| 119 | BinPackings.Add(bp);
|
---|
| 120 | }
|
---|
| 121 | UpdateBinPackings();
|
---|
| 122 |
|
---|
| 123 | //Try to put remaining items in existing bins
|
---|
| 124 | var temp = new List<int>(remainingIDs);
|
---|
| 125 | foreach (int id in temp) {
|
---|
| 126 | foreach (var bp in BinPackings) {
|
---|
| 127 | var position = UseExtremePoints ? bp.FindExtremePointForItem(itemMeasures[id], rotated[id], StackingConstraints) : bp.FindPositionBySliding(itemMeasures[id], rotated[id]);
|
---|
| 128 | if (position != null) {
|
---|
| 129 | bp.PackItem(id, itemMeasures[id], position);
|
---|
| 130 | remainingIDs.Remove(id);
|
---|
[9599] | 131 | break;
|
---|
[9596] | 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | //Put still remaining items in new bins
|
---|
| 137 | while (remainingIDs.Count > 0) {
|
---|
| 138 | var bp = NewBinPacking();
|
---|
| 139 | if (!UseExtremePoints)
|
---|
| 140 | bp.SlidingBasedPacking(ref remainingIDs, itemMeasures, rotated);
|
---|
| 141 | else
|
---|
| 142 | bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, StackingConstraints, rotated);
|
---|
| 143 | BinPackings.Add(bp);
|
---|
| 144 | }
|
---|
| 145 | UpdateBinPackings();
|
---|
[9599] | 146 |
|
---|
| 147 | var newSolution = new ObservableDictionary<int,ItemList<PackingInformation>> ();
|
---|
| 148 | foreach (var bp in BinPackings) {
|
---|
| 149 | int binIndex = newSolution.Count;
|
---|
| 150 | newSolution[binIndex] = new ItemList<PackingInformation>();
|
---|
| 151 | foreach (var entry in bp.ItemPositions)
|
---|
| 152 | newSolution[binIndex].Add(new PackingInformation (entry.Key, entry.Value.Rotated));
|
---|
| 153 | }
|
---|
| 154 | solution.PackingInformations = newSolution;
|
---|
[9596] | 155 | }
|
---|
| 156 | public void Pack(GroupingVectorEncoding solution, ItemList<I> itemMeasures) {
|
---|
| 157 | var sequenceMatrix = solution.GenerateSequenceMatrix();
|
---|
| 158 |
|
---|
| 159 | //Fill bins according to grouping vector
|
---|
| 160 | List<int> remainingIDs = new List<int>();
|
---|
| 161 | foreach (var sequence in sequenceMatrix) {
|
---|
| 162 | remainingIDs = remainingIDs.Concat(sequence).ToList();
|
---|
| 163 | var bp = NewBinPacking();
|
---|
| 164 | if (!UseExtremePoints)
|
---|
| 165 | bp.SlidingBasedPacking(ref remainingIDs, itemMeasures);
|
---|
| 166 | else
|
---|
| 167 | bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, StackingConstraints);
|
---|
| 168 | BinPackings.Add(bp);
|
---|
| 169 | }
|
---|
| 170 | UpdateBinPackings();
|
---|
| 171 |
|
---|
| 172 | //Try to put remaining items in existing bins
|
---|
| 173 | var temp = new List<int>(remainingIDs);
|
---|
| 174 | foreach (int id in temp) {
|
---|
| 175 | foreach (var bp in BinPackings) {
|
---|
| 176 | var position = UseExtremePoints ? bp.FindExtremePointForItem (itemMeasures[id], false, StackingConstraints) : bp.FindPositionBySliding(itemMeasures[id], false);
|
---|
| 177 | if (position != null) {
|
---|
| 178 | bp.PackItem(id, itemMeasures[id], position);
|
---|
| 179 | remainingIDs.Remove(id);
|
---|
[9599] | 180 | break;
|
---|
[9596] | 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | //Put still remaining items in new bins
|
---|
| 186 | while (remainingIDs.Count > 0) {
|
---|
| 187 | var bp = NewBinPacking();
|
---|
| 188 | if (!UseExtremePoints)
|
---|
| 189 | bp.SlidingBasedPacking(ref remainingIDs, itemMeasures);
|
---|
| 190 | else
|
---|
| 191 | bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, StackingConstraints);
|
---|
| 192 | BinPackings.Add(bp);
|
---|
| 193 | }
|
---|
| 194 | UpdateBinPackings();
|
---|
[9599] | 195 |
|
---|
| 196 | var newSolution = new int[solution.GroupingVector.Length];
|
---|
| 197 | int binIndex = 0;
|
---|
| 198 | foreach (var bp in BinPackings) {
|
---|
| 199 | foreach (var entry in bp.ItemPositions)
|
---|
| 200 | newSolution[entry.Key] = binIndex;
|
---|
| 201 | binIndex++;
|
---|
| 202 | }
|
---|
| 203 | solution.GroupingVector = new IntegerVector (newSolution);
|
---|
[9596] | 204 | }
|
---|
| 205 | public void Pack(PackingSequenceEncoding solution, ItemList<I> itemMeasures) {
|
---|
| 206 | List<int> remainingIDs = new List<int>(solution.PackingSequence);
|
---|
| 207 | while (remainingIDs.Count > 0) {
|
---|
| 208 | var bp = NewBinPacking();
|
---|
| 209 | if (!UseExtremePoints)
|
---|
| 210 | bp.SlidingBasedPacking(ref remainingIDs, itemMeasures);
|
---|
| 211 | else
|
---|
| 212 | bp.ExtremePointBasedPacking(ref remainingIDs, itemMeasures, StackingConstraints);
|
---|
| 213 | BinPackings.Add(bp);
|
---|
| 214 | }
|
---|
| 215 | UpdateBinPackings();
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
[9440] | 219 | #region Events
|
---|
| 220 | public event EventHandler QualityChanged;
|
---|
| 221 | private void OnQualityChanged() {
|
---|
| 222 | var changed = QualityChanged;
|
---|
| 223 | if (changed != null)
|
---|
| 224 | changed(this, EventArgs.Empty);
|
---|
| 225 | }
|
---|
| 226 | private void RegisterQualityEvents() {
|
---|
| 227 | Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
|
---|
| 228 | }
|
---|
| 229 | private void DeregisterQualityEvents() {
|
---|
| 230 | Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
|
---|
| 231 | }
|
---|
| 232 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
| 233 | OnQualityChanged();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[9593] | 236 | public event EventHandler BinPackingsChanged;
|
---|
| 237 | private void OnBinPackingsChanged() {
|
---|
| 238 | var changed = BinPackingsChanged;
|
---|
[9440] | 239 | if (changed != null)
|
---|
| 240 | changed(this, EventArgs.Empty);
|
---|
| 241 | }
|
---|
[9593] | 242 | private void RegisterBinPackingsEvents() {
|
---|
| 243 | BinPackings.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(BinPackings_PropertyChanged);
|
---|
[9440] | 244 | }
|
---|
[9593] | 245 | private void DeregisterBinPackingsEvents() {
|
---|
| 246 | BinPackings.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(BinPackings_PropertyChanged);
|
---|
[9440] | 247 | }
|
---|
[9593] | 248 | private void BinPackings_PropertyChanged(object sender, EventArgs e) {
|
---|
| 249 | OnBinPackingsChanged();
|
---|
[9440] | 250 | }
|
---|
| 251 | #endregion
|
---|
| 252 | }
|
---|
[9593] | 253 |
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 |
|
---|
[9596] | 257 | [Item("PackingPlan2D", "Represents a concrete solution for a 2D bin-packing problem.")]
|
---|
[9593] | 258 | [StorableClass]
|
---|
[9596] | 259 | public class PackingPlan2D : PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> {
|
---|
| 260 | public PackingPlan2D(RectangularPackingBin binMeasures) : this(binMeasures, false, false) { }
|
---|
| 261 | public PackingPlan2D(RectangularPackingBin binMeasures, bool useExtremePoints, bool stackingConstraints) : base(binMeasures, useExtremePoints, stackingConstraints) { }
|
---|
[9593] | 262 | [StorableConstructor]
|
---|
[9596] | 263 | protected PackingPlan2D(bool deserializing) : base(deserializing) { }
|
---|
| 264 | protected PackingPlan2D(PackingPlan2D original, Cloner cloner)
|
---|
[9593] | 265 | : base(original, cloner) {
|
---|
| 266 | }
|
---|
| 267 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[9596] | 268 | return new PackingPlan2D(this, cloner);
|
---|
[9593] | 269 | }
|
---|
[9596] | 270 | public override BinPacking<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> NewBinPacking() {
|
---|
| 271 | return new BinPacking2D(BinMeasures);
|
---|
[9593] | 272 | }
|
---|
[9596] | 273 | }
|
---|
[9593] | 274 |
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 |
|
---|
| 278 |
|
---|
[9596] | 279 | [Item("PackingPlan3D", "Represents a concrete solution for a 3D bin-packing problem.")]
|
---|
[9593] | 280 | [StorableClass]
|
---|
[9596] | 281 | public class PackingPlan3D : PackingPlan<ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem> {
|
---|
| 282 | public PackingPlan3D(CuboidPackingBin binMeasures) : this(binMeasures, false, false) { }
|
---|
| 283 | public PackingPlan3D(CuboidPackingBin binMeasures, bool useExtremePoints, bool stackingConstraints) : base(binMeasures, useExtremePoints, stackingConstraints) { }
|
---|
[9593] | 284 | [StorableConstructor]
|
---|
[9596] | 285 | protected PackingPlan3D(bool deserializing) : base(deserializing) { }
|
---|
| 286 | protected PackingPlan3D(PackingPlan3D original, Cloner cloner)
|
---|
[9593] | 287 | : base(original, cloner) {
|
---|
| 288 | }
|
---|
| 289 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[9596] | 290 | return new PackingPlan3D(this, cloner);
|
---|
[9593] | 291 | }
|
---|
[9596] | 292 | public override BinPacking<ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem> NewBinPacking() {
|
---|
| 293 | return new BinPacking3D(BinMeasures);
|
---|
[9593] | 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[9440] | 297 | }
|
---|