[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 |
|
---|
| 24 | using System;
|
---|
[14128] | 25 | using System.CodeDom;
|
---|
[14064] | 26 | using System.Linq;
|
---|
[14128] | 27 | using System.Text;
|
---|
[14064] | 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Data;
|
---|
[14128] | 31 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[14064] | 32 | using HeuristicLab.Optimization;
|
---|
| 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[14128] | 34 | using HeuristicLab.Problems.BinPacking;
|
---|
[14064] | 35 |
|
---|
[14128] | 36 | namespace HeuristicLab.Problems.BinPacking {
|
---|
[14064] | 37 | [StorableClass]
|
---|
[14128] | 38 | public abstract class PackingSequenceProblem<TSolution, TEncoding> : SingleObjectiveBasicProblem<TEncoding>
|
---|
| 39 | where TSolution : class, IItem
|
---|
| 40 | where TEncoding : class, IEncoding {
|
---|
[14064] | 41 |
|
---|
| 42 | // persistence
|
---|
| 43 | [StorableConstructor]
|
---|
| 44 | protected PackingSequenceProblem(bool deserializing) : base(deserializing) { }
|
---|
| 45 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 46 | private void AfterDeserialization() { }
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | // cloning
|
---|
[14128] | 50 | protected PackingSequenceProblem(PackingSequenceProblem<TSolution, TEncoding> original, Cloner cloner)
|
---|
[14064] | 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 |
|
---|
[14128] | 60 | public virtual void Analyze(TSolution[] solutions, double[] qualities, ResultCollection results,
|
---|
[14064] | 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")) {
|
---|
[14128] | 66 | results.Add(new Result("Best Solution", typeof(TSolution)));
|
---|
[14064] | 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);
|
---|
[14128] | 74 | var bestClone = (IItem)solutions[bestIdx].Clone();
|
---|
[14064] | 75 | results["Best Solution"].Value = bestClone;
|
---|
| 76 | results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|