Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614: improved results output of GQAP

File size: 12.0 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;
31using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
34  /// <summary>
35  /// An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.
36  /// </summary>
37  [Item("Best GeneralizedQAP Solution Analyzer", "An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.")]
38  [StorableClass]
39  public sealed class BestGQAPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
40
41    public bool EnabledByDefault {
42      get { return true; }
43    }
44
45    public LookupParameter<BoolValue> MaximizationParameter {
46      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
47    }
48    public LookupParameter<DoubleMatrix> DistancesParameter {
49      get { return (LookupParameter<DoubleMatrix>)Parameters["Distances"]; }
50    }
51    public LookupParameter<DoubleMatrix> WeightsParameter {
52      get { return (LookupParameter<DoubleMatrix>)Parameters["Weights"]; }
53    }
54    public LookupParameter<DoubleMatrix> InstallationCostsParameter {
55      get { return (LookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
56    }
57    public LookupParameter<DoubleArray> DemandsParameter {
58      get { return (LookupParameter<DoubleArray>)Parameters["Demands"]; }
59    }
60    public LookupParameter<DoubleArray> CapacitiesParameter {
61      get { return (LookupParameter<DoubleArray>)Parameters["Capacities"]; }
62    }
63    public LookupParameter<DoubleValue> TransportationCostsParameter {
64      get { return (LookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
65    }
66    public LookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
67      get { return (LookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
68    }
69    public ScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
70      get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
71    }
72    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
73      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
74    }
75    public ScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
76      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
77    }
78    public ScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
79      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
80    }
81    public ScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
82      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
83    }
84    public LookupParameter<GQAPAssignment> BestSolutionParameter {
85      get { return (LookupParameter<GQAPAssignment>)Parameters["BestSolution"]; }
86    }
87    public ValueLookupParameter<ResultCollection> ResultsParameter {
88      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
89    }
90    public LookupParameter<DoubleValue> BestKnownQualityParameter {
91      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
92    }
93    public LookupParameter<IntegerVector> BestKnownSolutionParameter {
94      get { return (LookupParameter<IntegerVector>)Parameters["BestKnownSolution"]; }
95    }
96    public ILookupParameter<StringArray> EquipmentNamesParameter {
97      get { return (ILookupParameter<StringArray>)Parameters["EquipmentNames"]; }
98    }
99    public ILookupParameter<StringArray> LocationNamesParameter {
100      get { return (ILookupParameter<StringArray>)Parameters["LocationNames"]; }
101    }
102
103    [StorableConstructor]
104    private BestGQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
105    private BestGQAPSolutionAnalyzer(BestGQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new BestGQAPSolutionAnalyzer(this, cloner);
108    }
109    public BestGQAPSolutionAnalyzer()
110      : base() {
111      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
112      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances between the locations."));
113      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the equipments."));
114      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The cost of installing equipment x at location y."));
115      Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands of the equipments."));
116      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities at the locations."));
117      Parameters.Add(new LookupParameter<DoubleValue>("TransportationCosts", "The transportation cost represents the flow-unit per distance-unit cost factor. This value can also be set to 1 if these costs are factored into the weights matrix already."));
118      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality."));
119      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", "The GQAP solutions from which the best solution should be analyzed."));
120      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the GQAP solutions which should be analyzed."));
121      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", "The flow-distance qualities of the GQAP solutions which should be analyzed."));
122      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", "The installation qualities of the GQAP solutions which should be analyzed."));
123      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", "The overbooked capacities of the GQAP solutions which should be analyzed."));
124      Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution."));
125      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
126      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this GQAP instance."));
127      Parameters.Add(new LookupParameter<IntegerVector>("BestKnownSolution", "The best known solution of this GQAP instance."));
128      Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", "A list of names that describes the equipments."));
129      Parameters.Add(new LookupParameter<StringArray>("LocationNames", "A list of names that describes the locations."));
130    }
131
132    [StorableHook(HookType.AfterDeserialization)]
133    private void AfterDeserialization() {
134      // BackwardsCompatibility3.3
135      #region Backwards compatible code, remove with 3.4
136      if (!Parameters.ContainsKey("EquipmentNames"))
137        Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", "Optional: A list of names that describes the equipments."));
138      if (!Parameters.ContainsKey("LocationNames"))
139        Parameters.Add(new LookupParameter<StringArray>("LocationNames", "Optional: A list of names that describes the locations."));
140      #endregion
141    }
142
143    public override IOperation Apply() {
144      var assignments = AssignmentParameter.ActualValue;
145      var qualities = QualityParameter.ActualValue;
146      var equipmentNames = EquipmentNamesParameter.ActualValue;
147      var locationNames = LocationNamesParameter.ActualValue;
148      var flowDistanceQualities = FlowDistanceQualityParameter.ActualValue;
149      var installationQualities = InstallationQualityParameter.ActualValue;
150      var overbookedCapacities = OverbookedCapacityParameter.ActualValue;
151      var distances = DistancesParameter.ActualValue;
152      var weights = WeightsParameter.ActualValue;
153      var installationCosts = InstallationCostsParameter.ActualValue;
154      var demands = DemandsParameter.ActualValue;
155      var capacities = CapacitiesParameter.ActualValue;
156      var transportationCosts = TransportationCostsParameter.ActualValue;
157      var overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue;
158      var results = ResultsParameter.ActualValue;
159      var maximization = MaximizationParameter.ActualValue.Value;
160      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
161
162      int bestIndex;
163      var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
164      if (maximization) bestIndex = tmp.SelectMax(x => x.Value).Index;
165      else bestIndex = tmp.SelectMin(x => x.Value).Index;
166
167      if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
168        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
169        BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[bestIndex].Clone();
170      }
171
172      GQAPAssignment assignment = BestSolutionParameter.ActualValue;
173      if (assignment == null) {
174        assignment = new GQAPAssignment((IntegerVector)assignments[bestIndex].Clone(), (DoubleValue)qualities[bestIndex].Clone(),
175          equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts,
176          overbookedCapacityPenalty, flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex]);
177        assignment.Distances = distances;
178        BestSolutionParameter.ActualValue = assignment;
179        results.Add(new Result("Best GQAP Solution", assignment));
180      } else {
181        if (HasSolutionImproved(assignment.Quality.Value, qualities[bestIndex].Value, maximization)) {
182          assignment.Assignment = (IntegerVector)assignments[bestIndex].Clone();
183          assignment.Quality = (DoubleValue)qualities[bestIndex].Clone();
184          assignment.EquipmentNames = equipmentNames;
185          assignment.LocationNames = locationNames;
186          assignment.Distances = distances;
187          assignment.Weights = weights;
188          assignment.InstallationCosts = installationCosts;
189          assignment.Demands = demands;
190          assignment.Capacities = capacities;
191          assignment.TransportationCosts = transportationCosts;
192          assignment.OverbookedCapacityPenalty = overbookedCapacityPenalty;
193          assignment.FlowDistanceQuality = (DoubleValue)flowDistanceQualities[bestIndex].Clone();
194          assignment.InstallationQuality = (DoubleValue)installationQualities[bestIndex].Clone();
195          assignment.OverbookedCapacity = (DoubleValue)overbookedCapacities[bestIndex].Clone();
196        }
197      }
198
199      return base.Apply();
200    }
201
202    private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
203      return maximization && oldQuality < quality || !maximization && oldQuality > quality;
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.