Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • Removed incompatible problem linhp318.tsp (contains fixed edges)
  • Fixed AssemblyInfo for TSPLIB
  • Added unit tests
  • Worked on assignment / solution view
File size: 12.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, 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    #region Parameter Properties
50    public IScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
51      get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
52    }
53    public ILookupParameter<BoolValue> MaximizationParameter {
54      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
55    }
56    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
57      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
58    }
59    public IScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
60      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
61    }
62    public IScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
63      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
64    }
65    public IScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
66      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
67    }
68    public ILookupParameter<DoubleMatrix> DistancesParameter {
69      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
70    }
71    public ILookupParameter<DoubleMatrix> WeightsParameter {
72      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
73    }
74    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
75      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
76    }
77    public ILookupParameter<DoubleArray> DemandsParameter {
78      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
79    }
80    public ILookupParameter<DoubleArray> CapacitiesParameter {
81      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
82    }
83    public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
84      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
85    }
86    public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
87      get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
88    }
89    public ILookupParameter<StringArray> EquipmentNamesParameter {
90      get { return (ILookupParameter<StringArray>)Parameters["EquipmentNames"]; }
91    }
92    public ILookupParameter<StringArray> LocationNamesParameter {
93      get { return (ILookupParameter<StringArray>)Parameters["LocationNames"]; }
94    }
95    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
96      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
97    }
98    public ILookupParameter<GQAPAssignment> BestKnownSolutionParameter {
99      get { return (ILookupParameter<GQAPAssignment>)Parameters["BestKnownSolution"]; }
100    }
101    public ILookupParameter<GQAPAssignment> BestSolutionParameter {
102      get { return (ILookupParameter<GQAPAssignment>)Parameters["BestSolution"]; }
103    }
104    public IValueLookupParameter<ResultCollection> ResultsParameter {
105      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
106    }
107    #endregion
108
109    [StorableConstructor]
110    private BestGQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
111    private BestGQAPSolutionAnalyzer(BestGQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
112    public override IDeepCloneable Clone(Cloner cloner) {
113      return new BestGQAPSolutionAnalyzer(this, cloner);
114    }
115    public BestGQAPSolutionAnalyzer()
116      : base() {
117      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
118      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
119      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
120      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
121      Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
122      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
123      Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
124      Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", GeneralizedQuadraticAssignmentProblem.OverbookedCapacityPenaltyDescription));
125      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
126      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
127      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
128      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
129      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
130      Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", GeneralizedQuadraticAssignmentProblem.EquipmentNamesDescription));
131      Parameters.Add(new LookupParameter<StringArray>("LocationNames", GeneralizedQuadraticAssignmentProblem.LocationNamesDescription));
132      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", GeneralizedQuadraticAssignmentProblem.BestKnownQualityDescription));
133      Parameters.Add(new LookupParameter<GQAPAssignment>("BestKnownSolution", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionDescription));
134      Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution found so far."));
135      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
136    }
137
138    public override IOperation Apply() {
139      var assignments = AssignmentParameter.ActualValue;
140      var qualities = QualityParameter.ActualValue;
141      var equipmentNames = EquipmentNamesParameter.ActualValue;
142      var locationNames = LocationNamesParameter.ActualValue;
143      var flowDistanceQualities = FlowDistanceQualityParameter.ActualValue;
144      var installationQualities = InstallationQualityParameter.ActualValue;
145      var overbookedCapacities = OverbookedCapacityParameter.ActualValue;
146      var distances = DistancesParameter.ActualValue;
147      var weights = WeightsParameter.ActualValue;
148      var installationCosts = InstallationCostsParameter.ActualValue;
149      var demands = DemandsParameter.ActualValue;
150      var capacities = CapacitiesParameter.ActualValue;
151      var transportationCosts = TransportationCostsParameter.ActualValue;
152      var overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue;
153      var results = ResultsParameter.ActualValue;
154      var maximization = MaximizationParameter.ActualValue.Value;
155      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
156
157      int bestIndex;
158      var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
159      if (maximization) bestIndex = tmp.ChooseMax(x => x.Value).Index;
160      else bestIndex = tmp.ChooseMin(x => x.Value).Index;
161
162      if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
163        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
164        BestKnownSolutionParameter.ActualValue = 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);
165      }
166
167      var assignment = BestSolutionParameter.ActualValue;
168      if (assignment == null) {
169        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);
170        assignment.Distances = distances;
171        BestSolutionParameter.ActualValue = assignment;
172      } else {
173        if (HasSolutionImproved(assignment.Quality.Value, qualities[bestIndex].Value, maximization)) {
174          assignment.Assignment = (IntegerVector)assignments[bestIndex].Clone();
175          assignment.Quality = (DoubleValue)qualities[bestIndex].Clone();
176          assignment.FlowDistanceQuality = (DoubleValue)flowDistanceQualities[bestIndex].Clone();
177          assignment.InstallationQuality = (DoubleValue)installationQualities[bestIndex].Clone();
178          assignment.OverbookedCapacity = (DoubleValue)overbookedCapacities[bestIndex].Clone();
179          assignment.EquipmentNames = equipmentNames;
180          assignment.LocationNames = locationNames;
181          assignment.Distances = distances;
182          assignment.Weights = weights;
183          assignment.InstallationCosts = installationCosts;
184          assignment.Demands = demands;
185          assignment.Capacities = capacities;
186          assignment.TransportationCosts = transportationCosts;
187          assignment.OverbookedCapacityPenalty = overbookedCapacityPenalty;
188        }
189      }
190      if (!results.ContainsKey("Best GQAP Assignment"))
191        results.Add(new Result("Best GQAP Assignment", assignment));
192
193      return base.Apply();
194    }
195
196    private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
197      return maximization && oldQuality < quality || !maximization && oldQuality > quality;
198    }
199  }
200}
Note: See TracBrowser for help on using the repository browser.