#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Linq;
using HEAL.Attic;
using HeuristicLab.Core;
using HeuristicLab.Common;
using HeuristicLab.Data;
using HeuristicLab.Collections;
namespace HeuristicLab.Problems.BinPacking {
[StorableType("2E5A4B2A-EB2B-49F8-9BE8-45DA1A421F3E")]
public abstract class PackingPlan : Item
where D : class, IPackingPosition
where B : PackingShape
where I : PackingShape, IPackingItem {
#region Properties
public int NrOfBins {
get {
if (Bins != null)
return Bins.Count;
else return 0;
}
}
[Storable]
protected bool StackingConstraints { get; set; }
[Storable]
protected bool UseExtremePoints { get; set; }
[Storable]
public B BinShape { get; private set; }
[Storable]
public ObservableList> Bins { get; set; }
[Storable]
private DoubleValue quality;
public DoubleValue Quality {
get { return quality; }
set {
if (quality != value) {
if (quality != null) DeregisterQualityEvents();
quality = value;
if (quality != null) RegisterQualityEvents();
OnQualityChanged();
}
}
}
#endregion
protected PackingPlan(B binShape, bool useExtremePoints, bool stackingConstraints)
: base() {
BinShape = (B)binShape.Clone();
StackingConstraints = stackingConstraints;
UseExtremePoints = useExtremePoints;
Bins = new ObservableList>();
}
[StorableConstructor]
protected PackingPlan(StorableConstructorFlag _) : base(_) { }
protected PackingPlan(PackingPlan original, Cloner cloner)
: base(original, cloner) {
this.Bins = new ObservableList>(original.Bins.Select(p => cloner.Clone(p)));
UseExtremePoints = original.UseExtremePoints;
StackingConstraints = original.StackingConstraints;
BinShape = cloner.Clone(original.BinShape);
Quality = cloner.Clone(original.Quality);
}
public void UpdateBinPackings() {
Bins.RemoveAll(x => x.Positions.Count == 0);
Bins = new ObservableList>(Bins.OrderByDescending(bp => bp.PackingDensity));
}
#region Events
public event EventHandler QualityChanged;
private void OnQualityChanged() {
var changed = QualityChanged;
if (changed != null)
changed(this, EventArgs.Empty);
}
private void RegisterQualityEvents() {
Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
}
private void DeregisterQualityEvents() {
Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
}
private void Quality_ValueChanged(object sender, EventArgs e) {
OnQualityChanged();
}
#endregion
}
}