Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/BestGQAPSolutionAnalyzer.cs @ 7412

Last change on this file since 7412 was 7412, checked in by abeham, 12 years ago

#1614: worked on GQAP and GRASP+PR

File size: 7.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.IntegerVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
33  /// <summary>
34  /// An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.
35  /// </summary>
36  [Item("Best GeneralizedQAP Solution Analyzer", "An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.")]
37  [StorableClass]
38  public sealed class BestGQAPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
39
40    public bool EnabledByDefault {
41      get { return true; }
42    }
43
44    public LookupParameter<BoolValue> MaximizationParameter {
45      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
46    }
47    public LookupParameter<DoubleMatrix> DistancesParameter {
48      get { return (LookupParameter<DoubleMatrix>)Parameters["Distances"]; }
49    }
50    public LookupParameter<DoubleMatrix> WeightsParameter {
51      get { return (LookupParameter<DoubleMatrix>)Parameters["Weights"]; }
52    }
53    public ScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
54      get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
55    }
56    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
57      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
58    }
59    public LookupParameter<GQAPAssignment> BestSolutionParameter {
60      get { return (LookupParameter<GQAPAssignment>)Parameters["BestSolution"]; }
61    }
62    public ValueLookupParameter<ResultCollection> ResultsParameter {
63      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
64    }
65    public LookupParameter<DoubleValue> BestKnownQualityParameter {
66      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
67    }
68    public LookupParameter<IntegerVector> BestKnownSolutionParameter {
69      get { return (LookupParameter<IntegerVector>)Parameters["BestKnownSolution"]; }
70    }
71    public ILookupParameter<StringArray> EquipmentNamesParameter {
72      get { return (ILookupParameter<StringArray>)Parameters["EquipmentNames"]; }
73    }
74    public ILookupParameter<StringArray> LocationNamesParameter {
75      get { return (ILookupParameter<StringArray>)Parameters["LocationNames"]; }
76    }
77
78    [StorableConstructor]
79    private BestGQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
80    private BestGQAPSolutionAnalyzer(BestGQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new BestGQAPSolutionAnalyzer(this, cloner);
83    }
84    public BestGQAPSolutionAnalyzer()
85      : base() {
86      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
87      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances between the locations."));
88      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the facilities."));
89      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", "The GQAP solutions from which the best solution should be analyzed."));
90      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the GQAP solutions which should be analyzed."));
91      Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution."));
92      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
93      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this GQAP instance."));
94      Parameters.Add(new LookupParameter<IntegerVector>("BestKnownSolution", "The best known solution of this GQAP instance."));
95      Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", "A list of names that describes the equipments."));
96      Parameters.Add(new LookupParameter<StringArray>("LocationNames", "A list of names that describes the locations."));
97    }
98
99    [StorableHook(HookType.AfterDeserialization)]
100    private void AfterDeserialization() {
101      // BackwardsCompatibility3.3
102      #region Backwards compatible code, remove with 3.4
103      if (!Parameters.ContainsKey("EquipmentNames"))
104        Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", "Optional: A list of names that describes the equipments."));
105      if (!Parameters.ContainsKey("LocationNames"))
106        Parameters.Add(new LookupParameter<StringArray>("LocationNames", "Optional: A list of names that describes the locations."));
107      #endregion
108    }
109
110    public override IOperation Apply() {
111      DoubleMatrix distances = DistancesParameter.ActualValue;
112      DoubleMatrix weights = WeightsParameter.ActualValue;
113      ItemArray<IntegerVector> assignments = AssignmentParameter.ActualValue;
114      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
115      ResultCollection results = ResultsParameter.ActualValue;
116      bool max = MaximizationParameter.ActualValue.Value;
117      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
118
119      var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray();
120      if (max) sorted = sorted.Reverse().ToArray();
121      int i = sorted.First().index;
122
123      if (bestKnownQuality == null
124          || max && qualities[i].Value > bestKnownQuality.Value
125          || !max && qualities[i].Value < bestKnownQuality.Value) {
126        // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known
127        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
128        BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[i].Clone();
129      }
130
131      GQAPAssignment assignment = BestSolutionParameter.ActualValue;
132      if (assignment == null) {
133        StringArray equipmentNames = EquipmentNamesParameter.ActualValue;
134        StringArray locationNames = LocationNamesParameter.ActualValue;
135        assignment = new GQAPAssignment(weights, (IntegerVector)assignments[i].Clone(), new DoubleValue(qualities[i].Value), equipmentNames, locationNames);
136        assignment.Distances = distances;
137        BestSolutionParameter.ActualValue = assignment;
138        results.Add(new Result("Best GQAP Solution", assignment));
139      } else {
140        if (max && assignment.Quality.Value < qualities[i].Value ||
141          !max && assignment.Quality.Value > qualities[i].Value) {
142          assignment.Distances = distances;
143          assignment.Weights = weights;
144          assignment.Assignment = (IntegerVector)assignments[i].Clone();
145          assignment.Quality.Value = qualities[i].Value;
146        }
147      }
148
149      return base.Apply();
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.