[5583] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5583] | 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 System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// An operator for analyzing the best solution of Quadratic Assignment Problems.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("BestQAPSolutionAnalyzer", "An operator for analyzing the best solution of Quadratic Assignment Problems.")]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | public sealed class BestQAPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
[7172] | 39 | public bool EnabledByDefault {
|
---|
| 40 | get { return true; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[5583] | 43 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
| 44 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 45 | }
|
---|
| 46 | public LookupParameter<DoubleMatrix> DistancesParameter {
|
---|
| 47 | get { return (LookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
| 48 | }
|
---|
| 49 | public LookupParameter<DoubleMatrix> WeightsParameter {
|
---|
| 50 | get { return (LookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
| 51 | }
|
---|
| 52 | public ScopeTreeLookupParameter<Permutation> PermutationParameter {
|
---|
| 53 | get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
| 54 | }
|
---|
| 55 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 56 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 57 | }
|
---|
| 58 | public LookupParameter<QAPAssignment> BestSolutionParameter {
|
---|
| 59 | get { return (LookupParameter<QAPAssignment>)Parameters["BestSolution"]; }
|
---|
| 60 | }
|
---|
| 61 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 62 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 63 | }
|
---|
| 64 | public LookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 65 | get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 66 | }
|
---|
[6525] | 67 | public LookupParameter<ItemSet<Permutation>> BestKnownSolutionsParameter {
|
---|
| 68 | get { return (LookupParameter<ItemSet<Permutation>>)Parameters["BestKnownSolutions"]; }
|
---|
[6342] | 69 | }
|
---|
[5583] | 70 | public LookupParameter<Permutation> BestKnownSolutionParameter {
|
---|
| 71 | get { return (LookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | [StorableConstructor]
|
---|
| 75 | private BestQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 76 | private BestQAPSolutionAnalyzer(BestQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 78 | return new BestQAPSolutionAnalyzer(this, cloner);
|
---|
| 79 | }
|
---|
| 80 | public BestQAPSolutionAnalyzer()
|
---|
| 81 | : base() {
|
---|
| 82 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
|
---|
| 83 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances between the locations."));
|
---|
| 84 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the facilities."));
|
---|
| 85 | Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Permutation", "The QAP solutions from which the best solution should be analyzed."));
|
---|
| 86 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the QAP solutions which should be analyzed."));
|
---|
| 87 | Parameters.Add(new LookupParameter<QAPAssignment>("BestSolution", "The best QAP solution."));
|
---|
| 88 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best QAP solution should be stored."));
|
---|
| 89 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this QAP instance."));
|
---|
[6525] | 90 | Parameters.Add(new LookupParameter<ItemSet<Permutation>>("BestKnownSolutions", "The best known solutions (there may be multiple) of this QAP instance."));
|
---|
[5583] | 91 | Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution of this QAP instance."));
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[6342] | 94 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 95 | private void AfterDeserialization() {
|
---|
| 96 | // BackwardsCompatibility3.3
|
---|
| 97 | #region Backwards compatible code, remove with 3.4
|
---|
| 98 | if (!Parameters.ContainsKey("BestKnownSolutions")) {
|
---|
[6540] | 99 | Parameters.Add(new LookupParameter<ItemSet<Permutation>>("BestKnownSolutions", "The best known solutions of this QAP instance."));
|
---|
| 100 | } else if (Parameters["BestKnownSolutions"].GetType().Equals(typeof(LookupParameter<ItemList<Permutation>>))) {
|
---|
| 101 | string actualName = (Parameters["BestKnownSolutions"] as LookupParameter<ItemList<Permutation>>).ActualName;
|
---|
| 102 | Parameters.Remove("BestKnownSolutions");
|
---|
| 103 | Parameters.Add(new LookupParameter<ItemSet<Permutation>>("BestKnownSolutions", "The best known solutions of this QAP instance.", actualName));
|
---|
[6342] | 104 | }
|
---|
| 105 | #endregion
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[5583] | 108 | public override IOperation Apply() {
|
---|
| 109 | DoubleMatrix distances = DistancesParameter.ActualValue;
|
---|
| 110 | DoubleMatrix weights = WeightsParameter.ActualValue;
|
---|
| 111 | ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
|
---|
| 112 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
| 113 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
| 114 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
| 115 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
| 116 |
|
---|
[6342] | 117 | var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray();
|
---|
| 118 | if (max) sorted = sorted.Reverse().ToArray();
|
---|
| 119 | int i = sorted.First().index;
|
---|
[5583] | 120 |
|
---|
[6342] | 121 | if (bestKnownQuality == null
|
---|
| 122 | || max && qualities[i].Value > bestKnownQuality.Value
|
---|
| 123 | || !max && qualities[i].Value < bestKnownQuality.Value) {
|
---|
| 124 | // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known
|
---|
[5583] | 125 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
|
---|
| 126 | BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
|
---|
[6525] | 127 | BestKnownSolutionsParameter.ActualValue = new ItemSet<Permutation>(new PermutationEqualityComparer());
|
---|
[6342] | 128 | BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[i].Clone());
|
---|
| 129 | } else if (bestKnownQuality.Value == qualities[i].Value) {
|
---|
| 130 | // if we matched the best-known quality we'll try to set the best-known solution if it isn't null
|
---|
| 131 | // and try to add it to the pool of best solutions if it is different
|
---|
| 132 | if (BestKnownSolutionParameter.ActualValue == null)
|
---|
| 133 | BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
|
---|
| 134 | if (BestKnownSolutionsParameter.ActualValue == null)
|
---|
[6525] | 135 | BestKnownSolutionsParameter.ActualValue = new ItemSet<Permutation>(new PermutationEqualityComparer());
|
---|
[6342] | 136 | foreach (var k in sorted) { // for each solution that we found check if it is in the pool of best-knowns
|
---|
| 137 | if (!max && k.Value > qualities[i].Value
|
---|
| 138 | || max && k.Value < qualities[i].Value) break; // stop when we reached a solution worse than the best-known quality
|
---|
| 139 | Permutation p = permutations[k.index];
|
---|
[6525] | 140 | if (!BestKnownSolutionsParameter.ActualValue.Contains(p))
|
---|
[6342] | 141 | BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[k.index].Clone());
|
---|
| 142 | }
|
---|
[5583] | 143 | }
|
---|
| 144 |
|
---|
| 145 | QAPAssignment assignment = BestSolutionParameter.ActualValue;
|
---|
| 146 | if (assignment == null) {
|
---|
| 147 | assignment = new QAPAssignment(weights, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
|
---|
| 148 | assignment.Distances = distances;
|
---|
| 149 | BestSolutionParameter.ActualValue = assignment;
|
---|
| 150 | results.Add(new Result("Best QAP Solution", assignment));
|
---|
| 151 | } else {
|
---|
| 152 | if (max && assignment.Quality.Value < qualities[i].Value ||
|
---|
| 153 | !max && assignment.Quality.Value > qualities[i].Value) {
|
---|
| 154 | assignment.Distances = distances;
|
---|
| 155 | assignment.Weights = weights;
|
---|
| 156 | assignment.Assignment = (Permutation)permutations[i].Clone();
|
---|
| 157 | assignment.Quality.Value = qualities[i].Value;
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | return base.Apply();
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|