Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1966_HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingPlans/PackingPlan.cs @ 17514

Last change on this file since 17514 was 14154, checked in by gkronber, 8 years ago

#1966: refactoring

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