Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7418 was 7418, checked in by abeham, 13 years ago

#1614

  • Added shaking operator based on n-moves
  • Added pareto analyzer regarding flowdistance and installation qualities
File size: 11.2 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    public override IOperation Apply() {
133      var assignments = AssignmentParameter.ActualValue;
134      var qualities = QualityParameter.ActualValue;
135      var equipmentNames = EquipmentNamesParameter.ActualValue;
136      var locationNames = LocationNamesParameter.ActualValue;
137      var flowDistanceQualities = FlowDistanceQualityParameter.ActualValue;
138      var installationQualities = InstallationQualityParameter.ActualValue;
139      var overbookedCapacities = OverbookedCapacityParameter.ActualValue;
140      var distances = DistancesParameter.ActualValue;
141      var weights = WeightsParameter.ActualValue;
142      var installationCosts = InstallationCostsParameter.ActualValue;
143      var demands = DemandsParameter.ActualValue;
144      var capacities = CapacitiesParameter.ActualValue;
145      var transportationCosts = TransportationCostsParameter.ActualValue;
146      var overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue;
147      var results = ResultsParameter.ActualValue;
148      var maximization = MaximizationParameter.ActualValue.Value;
149      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
150
151      int bestIndex;
152      var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
153      if (maximization) bestIndex = tmp.SelectMax(x => x.Value).Index;
154      else bestIndex = tmp.SelectMin(x => x.Value).Index;
155
156      if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
157        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
158        BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[bestIndex].Clone();
159      }
160
161      GQAPAssignment assignment = BestSolutionParameter.ActualValue;
162      if (assignment == null) {
163        assignment = new GQAPAssignment((IntegerVector)assignments[bestIndex].Clone(), (DoubleValue)qualities[bestIndex].Clone(), flowDistanceQualities[bestIndex], overbookedCapacities[bestIndex], installationQualities[bestIndex], equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
164        assignment.Distances = distances;
165        BestSolutionParameter.ActualValue = assignment;
166        results.Add(new Result("Best GQAP Solution", assignment));
167      } else {
168        if (HasSolutionImproved(assignment.Solution.Quality.Value, qualities[bestIndex].Value, maximization)) {
169          assignment.Solution = new GQAPSolution((IntegerVector)assignments[bestIndex].Clone(),
170            (DoubleValue)qualities[bestIndex].Clone(), (DoubleValue)flowDistanceQualities[bestIndex].Clone(),
171            (DoubleValue)installationQualities[bestIndex].Clone(), (DoubleValue)overbookedCapacities[bestIndex].Clone());
172          assignment.EquipmentNames = equipmentNames;
173          assignment.LocationNames = locationNames;
174          assignment.Distances = distances;
175          assignment.Weights = weights;
176          assignment.InstallationCosts = installationCosts;
177          assignment.Demands = demands;
178          assignment.Capacities = capacities;
179          assignment.TransportationCosts = transportationCosts;
180          assignment.OverbookedCapacityPenalty = overbookedCapacityPenalty;
181        }
182      }
183
184      return base.Apply();
185    }
186
187    private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
188      return maximization && oldQuality < quality || !maximization && oldQuality > quality;
189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.