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