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