Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/GQAPSolutionArchiveAnalyzer.cs @ 11140

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

#1614: restructured architecture to allow for different evaluator with different penalty strategies

File size: 10.8 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;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
33  /// <summary>
34  /// An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.
35  /// </summary>
36  [Item("GeneralizedQAP Solution Archive Analyzer", "An operator for analyzing the archive of found solutions for the GQAP.")]
37  [StorableClass]
38  public sealed class GQAPSolutionArchiveAnalyzer : SingleSuccessorOperator, IAssignmentsAwareGQAPOperator,
39    IQualitiesAwareGQAPOperator, IDistancesAwareGQAPOperator, IWeightsAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
40    IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
41    IExpectedRandomQualityAwareGQAPOperator, IEquipmentNamesAwareGQAPOperator, ILocationNamesAwareGQAPOperator,
42    IBestKnownSolutionsAwareGQAPOperator, IEvaluatorAwareGQAPOperator, IAnalyzer {
43
44    public bool EnabledByDefault {
45      get { return true; }
46    }
47
48    public IScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
49      get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
50    }
51    public ILookupParameter<BoolValue> MaximizationParameter {
52      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
53    }
54    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
55      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
56    }
57    public IScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
58      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
59    }
60    public IScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
61      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
62    }
63    public IScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
64      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
65    }
66    public ILookupParameter<DoubleMatrix> DistancesParameter {
67      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
68    }
69    public ILookupParameter<DoubleMatrix> WeightsParameter {
70      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
71    }
72    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
73      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
74    }
75    public ILookupParameter<DoubleArray> DemandsParameter {
76      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
77    }
78    public ILookupParameter<DoubleArray> CapacitiesParameter {
79      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
80    }
81    public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
82      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
83    }
84    public IValueLookupParameter<DoubleValue> ExpectedRandomQualityParameter {
85      get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
86    }
87    public ILookupParameter<StringArray> EquipmentNamesParameter {
88      get { return (ILookupParameter<StringArray>)Parameters["EquipmentNames"]; }
89    }
90    public ILookupParameter<StringArray> LocationNamesParameter {
91      get { return (ILookupParameter<StringArray>)Parameters["LocationNames"]; }
92    }
93    public IValueLookupParameter<ResultCollection> ResultsParameter {
94      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
95    }
96    public ILookupParameter<GQAPAssignmentArchive> BestKnownSolutionsParameter {
97      get { return (ILookupParameter<GQAPAssignmentArchive>)Parameters["BestKnownSolutions"]; }
98    }
99    public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
100      get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
101    }
102
103    [StorableConstructor]
104    private GQAPSolutionArchiveAnalyzer(bool deserializing) : base(deserializing) { }
105    private GQAPSolutionArchiveAnalyzer(GQAPSolutionArchiveAnalyzer original, Cloner cloner) : base(original, cloner) { }
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new GQAPSolutionArchiveAnalyzer(this, cloner);
108    }
109    public GQAPSolutionArchiveAnalyzer()
110      : base() {
111      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
112      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
113      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
114      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
115      Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
116      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
117      Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
118      Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
119      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
120      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
121      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
122      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
123      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
124      Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", GeneralizedQuadraticAssignmentProblem.EquipmentNamesDescription));
125      Parameters.Add(new LookupParameter<StringArray>("LocationNames", GeneralizedQuadraticAssignmentProblem.LocationNamesDescription));
126      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
127      Parameters.Add(new LookupParameter<GQAPAssignmentArchive>("BestKnownSolutions", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionsDescription));
128      Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
129    }
130
131    public override IOperation Apply() {
132      var evaluator = EvaluatorParameter.ActualValue;
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 expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue;
147      var results = ResultsParameter.ActualValue;
148      var maximization = MaximizationParameter.ActualValue.Value;
149
150      GQAPAssignmentArchive archive = results.ContainsKey("Solution Archive") ? results["Solution Archive"].Value as GQAPAssignmentArchive : null;
151      if (archive == null) {
152        archive = new GQAPAssignmentArchive(equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts, expectedRandomQuality, evaluator);
153        results.Add(new Result("Solution Archive", archive));
154      } else {
155        archive.EquipmentNames = equipmentNames;
156        archive.LocationNames = locationNames;
157        archive.Distances = distances;
158        archive.Weights = weights;
159        archive.InstallationCosts = installationCosts;
160        archive.Demands = demands;
161        archive.Capacities = capacities;
162        archive.TransportationCosts = transportationCosts;
163        archive.ExpectedRandomQuality = expectedRandomQuality;
164        archive.Evaluator = evaluator;
165      }
166
167      var solutions = Enumerable.Range(0, assignments.Length)
168        .Select(i => new GQAPSolution(assignments[i], qualities[i], flowDistanceQualities[i], installationQualities[i], overbookedCapacities[i]))
169        .Concat(archive.Solutions);
170      archive.Solutions = GQAPSolutionArchiveUpdater.GetFeasibleParetoFront(solutions);
171
172      GQAPAssignmentArchive bestKnownArchive = BestKnownSolutionsParameter.ActualValue;
173      if (bestKnownArchive == null) {
174        bestKnownArchive = new GQAPAssignmentArchive();
175        BestKnownSolutionsParameter.ActualValue = bestKnownArchive;
176      } else {
177        var archiveSolutions = Enumerable.Range(0, assignments.Length)
178        .Select(i => new GQAPSolution(assignments[i], qualities[i], flowDistanceQualities[i], installationQualities[i], overbookedCapacities[i]))
179        .Concat(archive.Solutions);
180        bestKnownArchive.Solutions = GQAPSolutionArchiveUpdater.GetFeasibleParetoFront(archiveSolutions);
181      }
182
183      return base.Apply();
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.