Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingPlans/PackingPlan.cs @ 9563

Last change on this file since 9563 was 9563, checked in by jhelm, 11 years ago

#1966: Implemented additional Operator-Wrappers for PackingSequence and GroupingVector; Implemented additional problem-class for Rosenbauer-Problemstatement; Added marker-interfaces for decoder-types;

File size: 5.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Problems.BinPacking.Interfaces;
27using HeuristicLab.Problems.BinPacking.PackingItem;
28using HeuristicLab.Problems.BinPacking.Shapes;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Core;
31using HeuristicLab.Common;
32using HeuristicLab.Parameters;
33using HeuristicLab.Data;
34using HeuristicLab.Collections;
35
36namespace HeuristicLab.Encodings.PackingEncoding.PackingPlan {
37  [Item("PackingPlan", "Represents a concrete solution for a bin-packing problem.")]
38  [StorableClass]
39  public class PackingPlan<D, B, I> : ParameterizedNamedItem, IPackingPlan
40    where D : class, IPackingDimensions
41    where B : PackingShape<D>, IPackingBin
42    where I : PackingShape<D>, IPackingItem {
43
44    #region Properties
45    public int NrOfBins {
46      get {
47        if (PackingItemPositions != null)
48          return PackingItemPositions.Select(p => p.Value.AssignedBin).Max() + 1;
49        else return 0;
50      }
51    }
52
53    [Storable]
54    public ObservableDictionary<int, D> PackingItemPositions { get; set; }
55    [Storable]
56    public ObservableDictionary<int, B> PackingBinMeasures { get; set; }
57
58    [Storable]
59    public ItemList<I> PackingItemMeasures {
60      get;
61      set;
62    }
63    [Storable]
64    private DoubleValue quality;
65    public DoubleValue Quality {
66      get { return quality; }
67      set {
68        if (quality != value) {
69          if (quality != null) DeregisterQualityEvents();
70          quality = value;
71          if (quality != null) RegisterQualityEvents();
72          OnQualityChanged();
73        }
74      }
75    }
76    #endregion
77
78    public PackingPlan(B binMeasures, ItemList<I> itemMeasures)
79      : base() {
80      this.PackingItemMeasures = new ItemList<I> (itemMeasures);
81      this.PackingBinMeasures = new ObservableDictionary<int, B>();
82      this.PackingBinMeasures[0] = binMeasures;
83    }
84    public PackingPlan(ObservableDictionary<int, B> binMeasures, ItemList<I> itemMeasures)
85      : base() {
86      this.PackingItemMeasures = itemMeasures;
87      this.PackingBinMeasures = binMeasures;
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) {
94      PackingItemPositions = new ObservableDictionary<int, D>(original.PackingItemPositions);
95      PackingBinMeasures = new ObservableDictionary<int, B>(original.PackingBinMeasures);
96      PackingItemMeasures = original.PackingItemMeasures;
97    }
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new PackingPlan<D,B,I>(this, cloner);
100    }
101
102    public B GetPackingBinMeasuresForBinNr(int binNr) {
103      if (PackingBinMeasures.ContainsKey(binNr))
104        return PackingBinMeasures[binNr];
105      else
106        return PackingBinMeasures[0];
107    }
108
109
110    #region Events
111    public event EventHandler QualityChanged;
112    private void OnQualityChanged() {
113      var changed = QualityChanged;
114      if (changed != null)
115        changed(this, EventArgs.Empty);
116    }
117    private void RegisterQualityEvents() {
118      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
119    }
120    private void DeregisterQualityEvents() {
121      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
122    }
123    private void Quality_ValueChanged(object sender, EventArgs e) {
124      OnQualityChanged();
125    }
126
127    public event EventHandler PackingItemPositionChanged;
128    private void OnPackingItemPositionChanged() {
129      var changed = PackingItemPositionChanged;
130      if (changed != null)
131        changed(this, EventArgs.Empty);
132    }
133    private void RegisterPackingItemPositionEvents() {
134      PackingItemPositions.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(PackingItemPosition_PropertyChanged);
135    }
136    private void DeregisterPackingItemPositionEvents() {
137      PackingItemPositions.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(PackingItemPosition_PropertyChanged);
138    }
139    private void PackingItemPosition_PropertyChanged(object sender, EventArgs e) {
140      OnPackingItemPositionChanged();
141    }
142    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.