[6956] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7319] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6956] | 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;
|
---|
[7415] | 31 | using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
|
---|
[6956] | 32 |
|
---|
| 33 | namespace 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]
|
---|
[7419] | 39 | public sealed class BestGQAPSolutionAnalyzer : SingleSuccessorOperator, IAssignmentsAwareGQAPOperator,
|
---|
| 40 | IQualitiesAwareGQAPOperator, IDistancesAwareGQAPOperator, IWeightsAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
|
---|
| 41 | IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
|
---|
| 42 | IOverbookedCapacityPenaltyAwareGQAPOperator, IEquipmentNamesAwareGQAPOperator, ILocationNamesAwareGQAPOperator,
|
---|
[7438] | 43 | IBestKnownQualityAwareGQAPOperator, IBestKnownSolutionAwareGQAPOperator, IBestKnownSolutionsAwareGQAPOperator,
|
---|
| 44 | IAnalyzer {
|
---|
[7311] | 45 |
|
---|
| 46 | public bool EnabledByDefault {
|
---|
| 47 | get { return true; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[7438] | 50 | #region Parameter Properties
|
---|
[7419] | 51 | public IScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
|
---|
| 52 | get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
|
---|
[6956] | 53 | }
|
---|
[7419] | 54 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
| 55 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[6956] | 56 | }
|
---|
[7419] | 57 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 58 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[6956] | 59 | }
|
---|
[7419] | 60 | public IScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
|
---|
| 61 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
|
---|
[7415] | 62 | }
|
---|
[7419] | 63 | public IScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
|
---|
| 64 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
|
---|
[7415] | 65 | }
|
---|
[7419] | 66 | public IScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
|
---|
| 67 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
|
---|
[7415] | 68 | }
|
---|
[7419] | 69 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
| 70 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
[7415] | 71 | }
|
---|
[7419] | 72 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
| 73 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
[7415] | 74 | }
|
---|
[7419] | 75 | public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
|
---|
| 76 | get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
|
---|
[6956] | 77 | }
|
---|
[7419] | 78 | public ILookupParameter<DoubleArray> DemandsParameter {
|
---|
| 79 | get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
|
---|
[6956] | 80 | }
|
---|
[7419] | 81 | public ILookupParameter<DoubleArray> CapacitiesParameter {
|
---|
| 82 | get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
|
---|
[7415] | 83 | }
|
---|
[7419] | 84 | public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
|
---|
| 85 | get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
|
---|
[7415] | 86 | }
|
---|
[7419] | 87 | public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
|
---|
| 88 | get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
|
---|
[7415] | 89 | }
|
---|
[7311] | 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 | }
|
---|
[7419] | 96 | public ILookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 97 | get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 98 | }
|
---|
[7444] | 99 | public ILookupParameter<GQAPSolution> BestKnownSolutionParameter {
|
---|
| 100 | get { return (ILookupParameter<GQAPSolution>)Parameters["BestKnownSolution"]; }
|
---|
[7419] | 101 | }
|
---|
[7438] | 102 | public ILookupParameter<GQAPAssignmentArchive> BestKnownSolutionsParameter {
|
---|
| 103 | get { return (ILookupParameter<GQAPAssignmentArchive>)Parameters["BestKnownSolutions"]; }
|
---|
| 104 | }
|
---|
[7419] | 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 | }
|
---|
[7438] | 111 | #endregion
|
---|
[6956] | 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() {
|
---|
[7423] | 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));
|
---|
[7444] | 137 | Parameters.Add(new LookupParameter<GQAPSolution>("BestKnownSolution", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionDescription));
|
---|
[7438] | 138 | Parameters.Add(new LookupParameter<GQAPAssignmentArchive>("BestKnownSolutions", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionsDescription));
|
---|
[7423] | 139 | Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution found so far."));
|
---|
[6956] | 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() {
|
---|
[7415] | 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;
|
---|
[7438] | 161 | var bestKnownSolutions = BestKnownSolutionsParameter.ActualValue;
|
---|
[6956] | 162 |
|
---|
[7415] | 163 | int bestIndex;
|
---|
| 164 | var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
|
---|
[7432] | 165 | if (maximization) bestIndex = tmp.ChooseMax(x => x.Value).Index;
|
---|
| 166 | else bestIndex = tmp.ChooseMin(x => x.Value).Index;
|
---|
[6956] | 167 |
|
---|
[7415] | 168 | if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
|
---|
| 169 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
|
---|
[7444] | 170 | BestKnownSolutionParameter.ActualValue = new GQAPSolution((IntegerVector)assignments[bestIndex].Clone(), qualities[bestIndex], flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex]);
|
---|
[6956] | 171 | }
|
---|
| 172 |
|
---|
| 173 | GQAPAssignment assignment = BestSolutionParameter.ActualValue;
|
---|
| 174 | if (assignment == null) {
|
---|
[7437] | 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);
|
---|
[6956] | 176 | assignment.Distances = distances;
|
---|
| 177 | BestSolutionParameter.ActualValue = assignment;
|
---|
| 178 | results.Add(new Result("Best GQAP Solution", assignment));
|
---|
| 179 | } else {
|
---|
[7418] | 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());
|
---|
[7415] | 184 | assignment.EquipmentNames = equipmentNames;
|
---|
| 185 | assignment.LocationNames = locationNames;
|
---|
[6956] | 186 | assignment.Distances = distances;
|
---|
| 187 | assignment.Weights = weights;
|
---|
[7415] | 188 | assignment.InstallationCosts = installationCosts;
|
---|
| 189 | assignment.Demands = demands;
|
---|
| 190 | assignment.Capacities = capacities;
|
---|
| 191 | assignment.TransportationCosts = transportationCosts;
|
---|
| 192 | assignment.OverbookedCapacityPenalty = overbookedCapacityPenalty;
|
---|
[6956] | 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[7438] | 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 |
|
---|
[6956] | 207 | return base.Apply();
|
---|
| 208 | }
|
---|
[7415] | 209 |
|
---|
| 210 | private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
|
---|
| 211 | return maximization && oldQuality < quality || !maximization && oldQuality > quality;
|
---|
| 212 | }
|
---|
[6956] | 213 | }
|
---|
| 214 | }
|
---|