source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/PackingSequenceProblem.cs @ 14128

Last change on this file since 14128 was 14128, checked in by gkronber, 3 years ago

#1966: refactoring of bin packing implementation

File size: 3.0 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.CodeDom;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.PermutationEncoding;
32using HeuristicLab.Optimization;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Problems.BinPacking;
35
36namespace HeuristicLab.Problems.BinPacking {
37  [StorableClass]
38  public abstract class PackingSequenceProblem<TSolution, TEncoding> : SingleObjectiveBasicProblem<TEncoding>
39    where TSolution : class, IItem
40    where TEncoding : class, IEncoding {
41
42    // persistence
43    [StorableConstructor]
44    protected PackingSequenceProblem(bool deserializing) : base(deserializing) { }
45    [StorableHook(HookType.AfterDeserialization)]
46    private void AfterDeserialization() { }
47
48
49    // cloning
50    protected PackingSequenceProblem(PackingSequenceProblem<TSolution, TEncoding> original, Cloner cloner)
51      : base(original, cloner) {
52    }
53
54    protected PackingSequenceProblem() : base() { }
55
56    public virtual bool IsBetter(double quality, double bestQuality) {
57      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
58    }
59
60    public virtual void Analyze(TSolution[] solutions, double[] qualities, ResultCollection results,
61      IRandom random) {
62      if (!results.ContainsKey("Best Solution Quality")) {
63        results.Add(new Result("Best Solution Quality", typeof(DoubleValue)));
64      }
65      if (!results.ContainsKey("Best Solution")) {
66        results.Add(new Result("Best Solution", typeof(TSolution)));
67      }
68
69      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
70
71      if (results["Best Solution Quality"].Value == null ||
72          IsBetter(bestQuality, ((DoubleValue)results["Best Solution Quality"].Value).Value)) {
73        var bestIdx = Array.IndexOf(qualities, bestQuality);
74        var bestClone = (IItem)solutions[bestIdx].Clone();
75        results["Best Solution"].Value = bestClone;
76        results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
77      }
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.