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

Last change on this file since 14064 was 14064, checked in by gkronber, 5 years ago

#1966: first steps for refactoring to use new Encoding framework

File size: 3.2 KB
RevLine 
[14064]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.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.PackingEncoding.PackingSequence;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.BinPacking2d {
34  [StorableClass]
35  abstract class PackingSequenceProblem : SingleObjectiveBasicProblem<Encoding> {
36
37    // persistence
38    [StorableConstructor]
39    protected PackingSequenceProblem(bool deserializing) : base(deserializing) { }
40    [StorableHook(HookType.AfterDeserialization)]
41    private void AfterDeserialization() { }
42
43
44    // cloning
45    protected PackingSequenceProblem(PackingSequenceProblem original, Cloner cloner)
46      : base(original, cloner) {
47    }
48
49    protected PackingSequenceProblem() : base() { }
50
51    public virtual bool IsBetter(double quality, double bestQuality) {
52      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
53    }
54
55    public sealed override double Evaluate(Individual individual, IRandom random) {
56      return Evaluate(individual.PackingSequence(), random);
57    }
58    public abstract double Evaluate(PackingSequence sequence, IRandom random);
59
60    public virtual void Analyze(PackingSequence[] trees, 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(PackingSequence)));
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)trees[bestIdx].Clone();
75        results["Best Solution"].Value = bestClone;
76        results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
77      }
78    }
79
80    public sealed override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
81      Analyze(individuals.Select(ind => ind.PackingSequence()).ToArray(), qualities, results, random);
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.