Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.BinPacking/3.3/PackingPlan.cs @ 14721

Last change on this file since 14721 was 14170, checked in by gkronber, 8 years ago

#2641: merged r14162,r14163,r14167,r14168,r14169 from trunk to stable

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 (Bins != null)
41          return Bins.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 BinShape { get; private set; }
52
53    [Storable]
54    public ObservableList<BinPacking<D, B, I>> Bins { 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 binShape, bool useExtremePoints, bool stackingConstraints)
72      : base() {
73      BinShape = (B)binShape.Clone();
74      StackingConstraints = stackingConstraints;
75      UseExtremePoints = useExtremePoints;
76      Bins = 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.Bins = new ObservableList<BinPacking<D, B, I>>(original.Bins.Select(p => cloner.Clone(p)));
84      UseExtremePoints = original.UseExtremePoints;
85      StackingConstraints = original.StackingConstraints;
86      BinShape = cloner.Clone(original.BinShape);
87      Quality = cloner.Clone(original.Quality);
88    }
89
90
91    public void UpdateBinPackings() {
92      Bins.RemoveAll(x => x.Positions.Count == 0);
93      Bins = new ObservableList<BinPacking<D, B, I>>(Bins.OrderByDescending(bp => bp.PackingDensity));
94    }
95
96    #region Events
97    public event EventHandler QualityChanged;
98    private void OnQualityChanged() {
99      var changed = QualityChanged;
100      if (changed != null)
101        changed(this, EventArgs.Empty);
102    }
103    private void RegisterQualityEvents() {
104      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
105    }
106    private void DeregisterQualityEvents() {
107      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
108    }
109    private void Quality_ValueChanged(object sender, EventArgs e) {
110      OnQualityChanged();
111    }
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.