Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • reworked parameterization (one interface for every parameter resp. parameter group)
  • unified parameter descriptions
File size: 11.7 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, IAssignmentsAwareGQAPOperator,
40    IQualitiesAwareGQAPOperator, IDistancesAwareGQAPOperator, IWeightsAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
41    IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
42    IOverbookedCapacityPenaltyAwareGQAPOperator, IEquipmentNamesAwareGQAPOperator, ILocationNamesAwareGQAPOperator,
43    IBestKnownQualityAwareGQAPOperator, IBestKnownSolutionAwareGQAPOperator, IAnalyzer {
44
45    public bool EnabledByDefault {
46      get { return true; }
47    }
48
49    public IScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
50      get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
51    }
52    public ILookupParameter<BoolValue> MaximizationParameter {
53      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
54    }
55    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
56      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
57    }
58    public IScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
59      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
60    }
61    public IScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
62      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
63    }
64    public IScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
65      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
66    }
67    public ILookupParameter<DoubleMatrix> DistancesParameter {
68      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
69    }
70    public ILookupParameter<DoubleMatrix> WeightsParameter {
71      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
72    }
73    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
74      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
75    }
76    public ILookupParameter<DoubleArray> DemandsParameter {
77      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
78    }
79    public ILookupParameter<DoubleArray> CapacitiesParameter {
80      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
81    }
82    public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
83      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
84    }
85    public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
86      get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
87    }
88    public ILookupParameter<StringArray> EquipmentNamesParameter {
89      get { return (ILookupParameter<StringArray>)Parameters["EquipmentNames"]; }
90    }
91    public ILookupParameter<StringArray> LocationNamesParameter {
92      get { return (ILookupParameter<StringArray>)Parameters["LocationNames"]; }
93    }
94    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
95      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
96    }
97    public ILookupParameter<IntegerVector> BestKnownSolutionParameter {
98      get { return (ILookupParameter<IntegerVector>)Parameters["BestKnownSolution"]; }
99    }
100    public ILookupParameter<GQAPAssignment> BestSolutionParameter {
101      get { return (ILookupParameter<GQAPAssignment>)Parameters["BestSolution"]; }
102    }
103    public IValueLookupParameter<ResultCollection> ResultsParameter {
104      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
105    }
106
107    [StorableConstructor]
108    private BestGQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
109    private BestGQAPSolutionAnalyzer(BestGQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new BestGQAPSolutionAnalyzer(this, cloner);
112    }
113    public BestGQAPSolutionAnalyzer()
114      : base() {
115      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
116      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances between the locations."));
117      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the equipments."));
118      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The cost of installing equipment x at location y."));
119      Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands of the equipments."));
120      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities at the locations."));
121      Parameters.Add(new ValueLookupParameter<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."));
122      Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality."));
123      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", "The GQAP solutions from which the best solution should be analyzed."));
124      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the GQAP solutions which should be analyzed."));
125      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", "The flow-distance qualities of the GQAP solutions which should be analyzed."));
126      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", "The installation qualities of the GQAP solutions which should be analyzed."));
127      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", "The overbooked capacities of the GQAP solutions which should be analyzed."));
128      Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution."));
129      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
130      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this GQAP instance."));
131      Parameters.Add(new LookupParameter<IntegerVector>("BestKnownSolution", "The best known solution of this GQAP instance."));
132      Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", "A list of names that describes the equipments."));
133      Parameters.Add(new LookupParameter<StringArray>("LocationNames", "A list of names that describes the locations."));
134    }
135
136    public override IOperation Apply() {
137      var assignments = AssignmentParameter.ActualValue;
138      var qualities = QualityParameter.ActualValue;
139      var equipmentNames = EquipmentNamesParameter.ActualValue;
140      var locationNames = LocationNamesParameter.ActualValue;
141      var flowDistanceQualities = FlowDistanceQualityParameter.ActualValue;
142      var installationQualities = InstallationQualityParameter.ActualValue;
143      var overbookedCapacities = OverbookedCapacityParameter.ActualValue;
144      var distances = DistancesParameter.ActualValue;
145      var weights = WeightsParameter.ActualValue;
146      var installationCosts = InstallationCostsParameter.ActualValue;
147      var demands = DemandsParameter.ActualValue;
148      var capacities = CapacitiesParameter.ActualValue;
149      var transportationCosts = TransportationCostsParameter.ActualValue;
150      var overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue;
151      var results = ResultsParameter.ActualValue;
152      var maximization = MaximizationParameter.ActualValue.Value;
153      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
154
155      int bestIndex;
156      var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
157      if (maximization) bestIndex = tmp.SelectMax(x => x.Value).Index;
158      else bestIndex = tmp.SelectMin(x => x.Value).Index;
159
160      if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
161        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
162        BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[bestIndex].Clone();
163      }
164
165      GQAPAssignment assignment = BestSolutionParameter.ActualValue;
166      if (assignment == null) {
167        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);
168        assignment.Distances = distances;
169        BestSolutionParameter.ActualValue = assignment;
170        results.Add(new Result("Best GQAP Solution", assignment));
171      } else {
172        if (HasSolutionImproved(assignment.Solution.Quality.Value, qualities[bestIndex].Value, maximization)) {
173          assignment.Solution = new GQAPSolution((IntegerVector)assignments[bestIndex].Clone(),
174            (DoubleValue)qualities[bestIndex].Clone(), (DoubleValue)flowDistanceQualities[bestIndex].Clone(),
175            (DoubleValue)installationQualities[bestIndex].Clone(), (DoubleValue)overbookedCapacities[bestIndex].Clone());
176          assignment.EquipmentNames = equipmentNames;
177          assignment.LocationNames = locationNames;
178          assignment.Distances = distances;
179          assignment.Weights = weights;
180          assignment.InstallationCosts = installationCosts;
181          assignment.Demands = demands;
182          assignment.Capacities = capacities;
183          assignment.TransportationCosts = transportationCosts;
184          assignment.OverbookedCapacityPenalty = overbookedCapacityPenalty;
185        }
186      }
187
188      return base.Apply();
189    }
190
191    private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
192      return maximization && oldQuality < quality || !maximization && oldQuality > quality;
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.