Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • fixed build
File size: 13.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, IAssignmentsAwareGQAPOperator,
40    IQualitiesAwareGQAPOperator, IDistancesAwareGQAPOperator, IWeightsAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
41    IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
42    IOverbookedCapacityPenaltyAwareGQAPOperator, IEquipmentNamesAwareGQAPOperator, ILocationNamesAwareGQAPOperator,
43    IBestKnownQualityAwareGQAPOperator, IBestKnownSolutionAwareGQAPOperator, IBestKnownSolutionsAwareGQAPOperator,
44    IAnalyzer {
45
46    public bool EnabledByDefault {
47      get { return true; }
48    }
49
50    #region Parameter Properties
51    public IScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
52      get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
53    }
54    public ILookupParameter<BoolValue> MaximizationParameter {
55      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
56    }
57    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
58      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
59    }
60    public IScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
61      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
62    }
63    public IScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
64      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
65    }
66    public IScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
67      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
68    }
69    public ILookupParameter<DoubleMatrix> DistancesParameter {
70      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
71    }
72    public ILookupParameter<DoubleMatrix> WeightsParameter {
73      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
74    }
75    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
76      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
77    }
78    public ILookupParameter<DoubleArray> DemandsParameter {
79      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
80    }
81    public ILookupParameter<DoubleArray> CapacitiesParameter {
82      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
83    }
84    public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
85      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
86    }
87    public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
88      get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
89    }
90    public ILookupParameter<StringArray> EquipmentNamesParameter {
91      get { return (ILookupParameter<StringArray>)Parameters["EquipmentNames"]; }
92    }
93    public ILookupParameter<StringArray> LocationNamesParameter {
94      get { return (ILookupParameter<StringArray>)Parameters["LocationNames"]; }
95    }
96    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
97      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
98    }
99    public ILookupParameter<GQAPSolution> BestKnownSolutionParameter {
100      get { return (ILookupParameter<GQAPSolution>)Parameters["BestKnownSolution"]; }
101    }
102    public ILookupParameter<GQAPAssignmentArchive> BestKnownSolutionsParameter {
103      get { return (ILookupParameter<GQAPAssignmentArchive>)Parameters["BestKnownSolutions"]; }
104    }
105    public ILookupParameter<GQAPAssignment> BestSolutionParameter {
106      get { return (ILookupParameter<GQAPAssignment>)Parameters["BestSolution"]; }
107    }
108    public IValueLookupParameter<ResultCollection> ResultsParameter {
109      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
110    }
111    #endregion
112
113    [StorableConstructor]
114    private BestGQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
115    private BestGQAPSolutionAnalyzer(BestGQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
116    public override IDeepCloneable Clone(Cloner cloner) {
117      return new BestGQAPSolutionAnalyzer(this, cloner);
118    }
119    public BestGQAPSolutionAnalyzer()
120      : base() {
121      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
122      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
123      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
124      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
125      Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
126      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
127      Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
128      Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", GeneralizedQuadraticAssignmentProblem.OverbookedCapacityPenaltyDescription));
129      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
130      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
131      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
132      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
133      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
134      Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", GeneralizedQuadraticAssignmentProblem.EquipmentNamesDescription));
135      Parameters.Add(new LookupParameter<StringArray>("LocationNames", GeneralizedQuadraticAssignmentProblem.LocationNamesDescription));
136      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", GeneralizedQuadraticAssignmentProblem.BestKnownQualityDescription));
137      Parameters.Add(new LookupParameter<GQAPSolution>("BestKnownSolution", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionDescription));
138      Parameters.Add(new LookupParameter<GQAPAssignmentArchive>("BestKnownSolutions", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionsDescription));
139      Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution found so far."));
140      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
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      var bestKnownSolutions = BestKnownSolutionsParameter.ActualValue;
162
163      int bestIndex;
164      var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
165      if (maximization) bestIndex = tmp.ChooseMax(x => x.Value).Index;
166      else bestIndex = tmp.ChooseMin(x => x.Value).Index;
167
168      if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
169        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
170        BestKnownSolutionParameter.ActualValue = new GQAPSolution((IntegerVector)assignments[bestIndex].Clone(), qualities[bestIndex], flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex]);
171      }
172
173      GQAPAssignment assignment = BestSolutionParameter.ActualValue;
174      if (assignment == null) {
175        assignment = new GQAPAssignment((IntegerVector)assignments[bestIndex].Clone(), (DoubleValue)qualities[bestIndex].Clone(), flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex], equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
176        assignment.Distances = distances;
177        BestSolutionParameter.ActualValue = assignment;
178        results.Add(new Result("Best GQAP Solution", assignment));
179      } else {
180        if (HasSolutionImproved(assignment.Solution.Quality.Value, qualities[bestIndex].Value, maximization)) {
181          assignment.Solution = new GQAPSolution((IntegerVector)assignments[bestIndex].Clone(),
182            (DoubleValue)qualities[bestIndex].Clone(), (DoubleValue)flowDistanceQualities[bestIndex].Clone(),
183            (DoubleValue)installationQualities[bestIndex].Clone(), (DoubleValue)overbookedCapacities[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        }
194      }
195
196      GQAPAssignmentArchive archive = BestKnownSolutionsParameter.ActualValue;
197      if (archive == null) {
198        archive = new GQAPAssignmentArchive(equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
199        BestKnownSolutionsParameter.ActualValue = archive;
200      } else {
201        var solutions = Enumerable.Range(0, assignments.Length)
202        .Select(i => new GQAPSolution(assignments[i], qualities[i], flowDistanceQualities[i], installationQualities[i], overbookedCapacities[i]))
203        .Concat(archive.Solutions);
204        archive.Solutions = GQAPSolutionArchiveUpdater.GetFeasibleParetoFront(solutions);
205      }
206
207      return base.Apply();
208    }
209
210    private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
211      return maximization && oldQuality < quality || !maximization && oldQuality > quality;
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.