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 |
|
---|
22 | using HeuristicLab.Optimization;
|
---|
23 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
24 | using HeuristicLab.Problems.BinPacking.Shapes;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.PackingEncoding.MultiComponentVector;
|
---|
32 | using HeuristicLab.Problems.BinPacking.Decoders;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using System.Linq;
|
---|
35 | using HeuristicLab.Problems.BinPacking.Analyzers;
|
---|
36 | using System;
|
---|
37 | using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
|
---|
38 | using HeuristicLab.Encodings.PackingEncoding.GroupingVector;
|
---|
39 |
|
---|
40 | namespace HeuristicLab.Problems.BinPacking.Problem {
|
---|
41 | [Item("BinPackingProblem", "Abstract class that represents a BinPacking Problem")]
|
---|
42 | [StorableClass]
|
---|
43 | public abstract class BinPackingProblem<D, B, I> : SingleObjectiveHeuristicOptimizationProblem<IPackingPlanEvaluationAlgorithm, IPackingSolutionCreator>
|
---|
44 | where D : class, IPackingDimensions
|
---|
45 | where B : PackingShape<D>, IPackingBin, IRegularPackingShape, new()
|
---|
46 | where I : PackingShape<D>, IPackingItem, IRegularPackingShape, new () {
|
---|
47 |
|
---|
48 |
|
---|
49 | #region Parameter Properties
|
---|
50 | public OptionalValueParameter<PackingPlan<D,B,I>> BestKnownSolutionParameter {
|
---|
51 | get { return (OptionalValueParameter<PackingPlan<D, B, I>>)Parameters["BestKnownSolution"]; }
|
---|
52 | }
|
---|
53 | public ValueParameter<ItemList<I>> PackingItemMeasuresParameter {
|
---|
54 | get { return (ValueParameter<ItemList<I>>)Parameters["PackingItemMeasures"]; }
|
---|
55 | }
|
---|
56 | public IFixedValueParameter<IntValue> PackingItemsParameter {
|
---|
57 | get { return (IFixedValueParameter<IntValue>)Parameters["PackingItems"]; }
|
---|
58 | }
|
---|
59 | public IFixedValueParameter<IntValue> LowerBoundParameter {
|
---|
60 | get { return (IFixedValueParameter<IntValue>)Parameters["LowerBound"]; }
|
---|
61 | }
|
---|
62 | public ValueParameter<IPackingPlanEvaluator> PackingPlanEvaluatorParameter {
|
---|
63 | get { return (ValueParameter<IPackingPlanEvaluator>)Parameters["PackingPlanEvaluator"]; }
|
---|
64 | }
|
---|
65 | public IValueParameter<B> PackingBinMeasuresParameter {
|
---|
66 | get { return (IValueParameter<B>)Parameters["PackingBinMeasures"]; }
|
---|
67 | }
|
---|
68 | public IConstrainedValueParameter<IPackingSolutionDecoder> PackingSolutionDecoderParameter {
|
---|
69 | get { return (IConstrainedValueParameter<IPackingSolutionDecoder>)Parameters["PackingSolutionDecoder"]; }
|
---|
70 | }
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | #region Properties
|
---|
74 | public PackingPlan<D, B, I> BestKnownSolution {
|
---|
75 | get { return BestKnownSolutionParameter.Value; }
|
---|
76 | set { BestKnownSolutionParameter.Value = value; }
|
---|
77 | }
|
---|
78 | public ItemList<I> PackingItemMeasures {
|
---|
79 | get { return PackingItemMeasuresParameter.Value; }
|
---|
80 | set { PackingItemMeasuresParameter.Value = value; }
|
---|
81 | }
|
---|
82 | public IPackingPlanEvaluator PackingPlanEvaluator {
|
---|
83 | get { return PackingPlanEvaluatorParameter.Value; }
|
---|
84 | set { PackingPlanEvaluatorParameter.Value = value; }
|
---|
85 | }
|
---|
86 | public B PackingBinMeasures {
|
---|
87 | get { return PackingBinMeasuresParameter.Value; }
|
---|
88 | set { PackingBinMeasuresParameter.Value = value; }
|
---|
89 | }
|
---|
90 | public PackingSolutionDecoder<D, B, I> PackingSolutionDecoder {
|
---|
91 | get { return PackingSolutionDecoderParameter.Value as PackingSolutionDecoder<D, B, I>; }
|
---|
92 | set { PackingSolutionDecoderParameter.Value = value; }
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 |
|
---|
97 | [StorableConstructor]
|
---|
98 | protected BinPackingProblem(bool deserializing) : base(deserializing) { }
|
---|
99 | protected BinPackingProblem(BinPackingProblem<D,B,I> original, Cloner cloner)
|
---|
100 | : base(original, cloner) {
|
---|
101 | InitializeEventHandlers();
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected BinPackingProblem(IPackingPlanEvaluationAlgorithm e) : this(e, new MultiComponentVectorRandomCreator()) { }
|
---|
105 |
|
---|
106 | protected BinPackingProblem(IPackingPlanEvaluationAlgorithm e, IPackingSolutionCreator c) : base (e, c){
|
---|
107 | Parameters.Add(new OptionalValueParameter<PackingPlan<D, B, I>>("BestKnownSolution", "The best known solution of this bin-packing instance."));
|
---|
108 | Parameters.Add(new ValueParameter<ItemList<I>>("PackingItemMeasures", "Packing-item data defining the measures of the different items that need to be packed.", new ItemList<I>()));
|
---|
109 |
|
---|
110 | Parameters.Add(new FixedValueParameter<IntValue>("PackingItems", "The number of packing-items used in this bin packing instance.", new IntValue()));
|
---|
111 | Parameters.Add(new FixedValueParameter<IntValue>("LowerBound", "The lower possible number of bins needed to solve this problem (taken from Dell'Amico, Martello and Vigo; 2002)", new IntValue()));
|
---|
112 | Parameters.Add(new ValueParameter<B>("PackingBinMeasures", "Packing-bin data defining the measures of the used bins.", new B()));
|
---|
113 | Parameters.Add(new ValueParameter<IPackingPlanEvaluator>("PackingPlanEvaluator", "The evaluator is used to determine the quality of a solution.", CreateDefaultEvaluator()));
|
---|
114 | Parameters.Add(new ConstrainedValueParameter<IPackingSolutionDecoder>("PackingSolutionDecoder", "The operator that decodes the representation and creates a packing plan."));
|
---|
115 | this.Maximization.Value = true;
|
---|
116 | InitializeProblemInstance();
|
---|
117 | InitializeOperators();
|
---|
118 | InitializeEventHandlers();
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected abstract void InitializeDecoder();
|
---|
122 | protected virtual void InitializeProblemInstance() {
|
---|
123 | InitializeProblemData();
|
---|
124 | ApplyHorizontalOrientation();
|
---|
125 | SortItems();
|
---|
126 | RemoveTooBigItems();
|
---|
127 | PackingItemsParameter.Value.Value = PackingItemMeasures.Count;
|
---|
128 | LowerBoundParameter.Value.Value = CalculateLowerBound();
|
---|
129 | }
|
---|
130 | protected virtual void InitializeOperators() {
|
---|
131 | Operators.Clear();
|
---|
132 | Operators.Add(new BestBinPackingSolutionAnalyzer<D, B, I>());
|
---|
133 |
|
---|
134 | ApplyEncoding();
|
---|
135 | ParameterizeOperators();
|
---|
136 | }
|
---|
137 | protected virtual void InitializeEventHandlers() {
|
---|
138 | PackingPlanEvaluatorParameter.ValueChanged += PackingPlanEvaluatorParameter_ValueChanged;
|
---|
139 | PackingPlanEvaluator.QualityParameter.ActualNameChanged += PackingPlanEvaluator_QualityParameter_ActualNameChanged;
|
---|
140 | SolutionCreatorParameter.ValueChanged += SolutionCreatorParameter_ValueChanged;
|
---|
141 | SolutionCreator.EncodedSolutionParameter.ActualNameChanged += SolutionCreator_EncodedSolutionParameter_ActualNameChanged;
|
---|
142 | PackingSolutionDecoderParameter.ValueChanged += PackingSolutionDecoderParameter_ValueChanged;
|
---|
143 | if (PackingSolutionDecoder != null) PackingSolutionDecoder.PackingPlanParameter.ActualNameChanged += PackingSolutionDecoder_PackingPlanParameter_ActualNameChanged;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | protected abstract void RemoveTooBigItems();
|
---|
148 | protected abstract void InitializeProblemData();
|
---|
149 | protected abstract IPackingPlanEvaluator CreateDefaultEvaluator();
|
---|
150 | protected void ApplyHorizontalOrientation() {
|
---|
151 | PackingBinMeasures.ApplyHorizontalOrientation();
|
---|
152 | foreach (IRegularPackingShape shape in PackingItemMeasures) {
|
---|
153 | shape.ApplyHorizontalOrientation();
|
---|
154 | }
|
---|
155 | }
|
---|
156 | protected void SortItems() {
|
---|
157 | PackingItemMeasures.Sort((x, y) => y.CompareTo(x));
|
---|
158 | }
|
---|
159 | protected int CalculateLowerBound() {
|
---|
160 | //This is the obvious continuous lower bound calculation; Martello and Vigo proposed a better way but it has not been implemented yet;
|
---|
161 | int items = PackingItemMeasures.Select(x => x.Volume).Sum();
|
---|
162 | int bin = PackingBinMeasures.Volume;
|
---|
163 | return (items + bin - 1) / (bin);
|
---|
164 | }
|
---|
165 |
|
---|
166 | private void ApplyEncoding() {
|
---|
167 | if (SolutionCreator.GetType() == typeof(PackingSequenceRandomCreator)) {
|
---|
168 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPackingSequenceOperator>());
|
---|
169 | InitializeDecoder();
|
---|
170 | } else if (SolutionCreator.GetType() == typeof(GroupingVectorRandomCreator)) {
|
---|
171 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IGroupingVectorOperator>());
|
---|
172 | InitializeDecoder();
|
---|
173 | } else if (SolutionCreator.GetType() == typeof(MultiComponentVectorRandomCreator)) {
|
---|
174 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IMultiComponentVectorOperator>());
|
---|
175 | InitializeDecoder();
|
---|
176 | }
|
---|
177 | }
|
---|
178 | private void ParameterizeOperators() {
|
---|
179 | foreach (var op in Operators.OfType<BestBinPackingSolutionAnalyzer<D, B, I>>()) {
|
---|
180 | op.QualityParameter.ActualName = PackingPlanEvaluator.QualityParameter.ActualName;
|
---|
181 | }
|
---|
182 |
|
---|
183 | Evaluator.PackingSolutionDecoderParameter.ActualName = PackingSolutionDecoderParameter.Name;
|
---|
184 | Evaluator.PackingSolutionDecoderParameter.Hidden = true;
|
---|
185 | Evaluator.PackingPlanEvaluatorParameter.ActualName = PackingPlanEvaluatorParameter.Name;
|
---|
186 | Evaluator.PackingPlanEvaluatorParameter.Hidden = true;
|
---|
187 | Evaluator.QualityParameter.ActualName = PackingPlanEvaluator.QualityParameter.ActualName;
|
---|
188 | Evaluator.QualityParameter.Hidden = true;
|
---|
189 |
|
---|
190 | if (PackingSolutionDecoder != null)
|
---|
191 | PackingSolutionDecoder.EncodedSolutionParameter.ActualName = SolutionCreator.EncodedSolutionParameter.ActualName;
|
---|
192 |
|
---|
193 | if (PackingSolutionDecoder != null) {
|
---|
194 | PackingPlanEvaluator.PackingPlanParameter.ActualName = PackingSolutionDecoder.PackingPlanParameter.ActualName;
|
---|
195 | PackingPlanEvaluator.PackingPlanParameter.Hidden = true;
|
---|
196 | } else {
|
---|
197 | PackingPlanEvaluator.PackingPlanParameter.ActualName = PackingPlanEvaluator.PackingPlanParameter.Name;
|
---|
198 | PackingPlanEvaluator.PackingPlanParameter.Hidden = false;
|
---|
199 | }
|
---|
200 |
|
---|
201 | foreach (var op in Operators.OfType<IPackingSolutionManipulator>()) {
|
---|
202 | op.EncodedSolutionParameter.ActualName = SolutionCreator.EncodedSolutionParameter.ActualName;
|
---|
203 | op.EncodedSolutionParameter.Hidden = true;
|
---|
204 | }
|
---|
205 |
|
---|
206 | foreach (var op in Operators.OfType<IPackingSolutionCrossover>()) {
|
---|
207 | op.ChildParameter.ActualName = SolutionCreator.EncodedSolutionParameter.ActualName;
|
---|
208 | op.ChildParameter.Hidden = true;
|
---|
209 | op.ParentsParameter.ActualName = SolutionCreator.EncodedSolutionParameter.ActualName;
|
---|
210 | op.ParentsParameter.Hidden = true;
|
---|
211 | }
|
---|
212 |
|
---|
213 | foreach (var op in Operators.OfType<BestBinPackingSolutionAnalyzer<D, B, I>>()) {
|
---|
214 | op.QualityParameter.ActualName = PackingPlanEvaluator.QualityParameter.ActualName;
|
---|
215 | if (PackingSolutionDecoder != null) {
|
---|
216 | op.PackingPlanParameter.ActualName = PackingSolutionDecoder.PackingPlanParameter.ActualName;
|
---|
217 | op.PackingPlanParameter.Hidden = true;
|
---|
218 | } else {
|
---|
219 | op.PackingPlanParameter.ActualName = op.PackingPlanParameter.Name;
|
---|
220 | op.PackingPlanParameter.Hidden = false;
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | }
|
---|
225 |
|
---|
226 | #region Events
|
---|
227 | protected override void OnSolutionCreatorChanged() {
|
---|
228 | InitializeOperators();
|
---|
229 | }
|
---|
230 | protected override void OnEvaluatorChanged() {
|
---|
231 | base.OnEvaluatorChanged();
|
---|
232 | ParameterizeOperators();
|
---|
233 | }
|
---|
234 | private void PackingPlanEvaluatorParameter_ValueChanged(object sender, EventArgs eventArgs) {
|
---|
235 | PackingPlanEvaluator.QualityParameter.ActualNameChanged += PackingPlanEvaluator_QualityParameter_ActualNameChanged;
|
---|
236 | ParameterizeOperators();
|
---|
237 | }
|
---|
238 | private void PackingPlanEvaluator_QualityParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
|
---|
239 | ParameterizeOperators();
|
---|
240 | }
|
---|
241 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs eventArgs) {
|
---|
242 | SolutionCreator.EncodedSolutionParameter.ActualNameChanged += SolutionCreator_EncodedSolutionParameter_ActualNameChanged;
|
---|
243 | ParameterizeOperators();
|
---|
244 | }
|
---|
245 | private void SolutionCreator_EncodedSolutionParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
|
---|
246 | ParameterizeOperators();
|
---|
247 | }
|
---|
248 | private void PackingSolutionDecoderParameter_ValueChanged(object sender, EventArgs eventArgs) {
|
---|
249 | if (PackingSolutionDecoder != null) PackingSolutionDecoder.PackingPlanParameter.ActualNameChanged += PackingSolutionDecoder_PackingPlanParameter_ActualNameChanged;
|
---|
250 | ParameterizeOperators();
|
---|
251 | }
|
---|
252 | private void PackingSolutionDecoder_PackingPlanParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
|
---|
253 | ParameterizeOperators();
|
---|
254 | }
|
---|
255 | #endregion
|
---|
256 | }
|
---|
257 | }
|
---|