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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
33 | /// <summary>
|
---|
34 | /// An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.
|
---|
35 | /// </summary>
|
---|
36 | [Item("Best GeneralizedQAP Solution Analyzer", "An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class BestGQAPSolutionAnalyzer : SingleSuccessorOperator, IAssignmentsAwareGQAPOperator,
|
---|
39 | IQualitiesAwareGQAPOperator, IDistancesAwareGQAPOperator, IWeightsAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
|
---|
40 | IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
|
---|
41 | IExpectedRandomQualityAwareGQAPOperator, IEquipmentNamesAwareGQAPOperator, ILocationNamesAwareGQAPOperator,
|
---|
42 | IBestKnownQualityAwareGQAPOperator, IBestKnownSolutionAwareGQAPOperator,
|
---|
43 | IEvaluatorAwareGQAPOperator, 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> ExpectedRandomQualityParameter {
|
---|
87 | get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
|
---|
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 | public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
|
---|
108 | get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | [StorableConstructor]
|
---|
113 | private BestGQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
114 | private BestGQAPSolutionAnalyzer(BestGQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
115 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
116 | return new BestGQAPSolutionAnalyzer(this, cloner);
|
---|
117 | }
|
---|
118 | public BestGQAPSolutionAnalyzer()
|
---|
119 | : base() {
|
---|
120 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
|
---|
121 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
|
---|
122 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
|
---|
123 | Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
|
---|
124 | Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
|
---|
125 | Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
|
---|
126 | Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
|
---|
127 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
|
---|
128 | Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
|
---|
129 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
|
---|
130 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
|
---|
131 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
|
---|
132 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
|
---|
133 | Parameters.Add(new LookupParameter<StringArray>("EquipmentNames", GeneralizedQuadraticAssignmentProblem.EquipmentNamesDescription));
|
---|
134 | Parameters.Add(new LookupParameter<StringArray>("LocationNames", GeneralizedQuadraticAssignmentProblem.LocationNamesDescription));
|
---|
135 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", GeneralizedQuadraticAssignmentProblem.BestKnownQualityDescription));
|
---|
136 | Parameters.Add(new LookupParameter<GQAPAssignment>("BestKnownSolution", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionDescription));
|
---|
137 | Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution found so far."));
|
---|
138 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
|
---|
139 | Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
|
---|
140 | }
|
---|
141 |
|
---|
142 | public override IOperation Apply() {
|
---|
143 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
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 expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue;
|
---|
158 | var results = ResultsParameter.ActualValue;
|
---|
159 | var maximization = MaximizationParameter.ActualValue.Value;
|
---|
160 | var bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
161 |
|
---|
162 | int bestIndex;
|
---|
163 | var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
|
---|
164 | if (maximization) bestIndex = tmp.MaxItems(x => x.Value).First().Index;
|
---|
165 | else bestIndex = tmp.MinItems(x => x.Value).First().Index;
|
---|
166 |
|
---|
167 | if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
|
---|
168 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
|
---|
169 | 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, expectedRandomQuality, evaluator);
|
---|
170 | }
|
---|
171 |
|
---|
172 | var assignment = BestSolutionParameter.ActualValue;
|
---|
173 | if (assignment == null) {
|
---|
174 | 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, expectedRandomQuality, evaluator);
|
---|
175 | assignment.Distances = distances;
|
---|
176 | BestSolutionParameter.ActualValue = assignment;
|
---|
177 | } else {
|
---|
178 | if (HasSolutionImproved(assignment.Quality.Value, qualities[bestIndex].Value, maximization)) {
|
---|
179 | assignment.Assignment = (IntegerVector)assignments[bestIndex].Clone();
|
---|
180 | assignment.Quality = (DoubleValue)qualities[bestIndex].Clone();
|
---|
181 | assignment.FlowDistanceQuality = (DoubleValue)flowDistanceQualities[bestIndex].Clone();
|
---|
182 | assignment.InstallationQuality = (DoubleValue)installationQualities[bestIndex].Clone();
|
---|
183 | assignment.OverbookedCapacity = (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.ExpectedRandomQuality = expectedRandomQuality;
|
---|
193 | assignment.Evaluator = evaluator;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | if (!results.ContainsKey("Best GQAP Assignment"))
|
---|
197 | results.Add(new Result("Best GQAP Assignment", assignment));
|
---|
198 |
|
---|
199 | return base.Apply();
|
---|
200 | }
|
---|
201 |
|
---|
202 | private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
|
---|
203 | return maximization && oldQuality < quality || !maximization && oldQuality > quality;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|