Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/BestQAPSolutionAnalyzer.cs @ 12858

Last change on this file since 12858 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

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