[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;
|
---|
| 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]
|
---|
[7419] | 38 | public sealed class BestGQAPSolutionAnalyzer : SingleSuccessorOperator, IAssignmentsAwareGQAPOperator,
|
---|
| 39 | IQualitiesAwareGQAPOperator, IDistancesAwareGQAPOperator, IWeightsAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
|
---|
| 40 | IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
|
---|
[7970] | 41 | IExpectedRandomQualityAwareGQAPOperator, IEquipmentNamesAwareGQAPOperator, ILocationNamesAwareGQAPOperator,
|
---|
| 42 | IBestKnownQualityAwareGQAPOperator, IBestKnownSolutionAwareGQAPOperator,
|
---|
| 43 | IEvaluatorAwareGQAPOperator, IAnalyzer {
|
---|
[7311] | 44 |
|
---|
| 45 | public bool EnabledByDefault {
|
---|
| 46 | get { return true; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[7438] | 49 | #region Parameter Properties
|
---|
[7419] | 50 | public IScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
|
---|
| 51 | get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
|
---|
[6956] | 52 | }
|
---|
[7419] | 53 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
| 54 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[6956] | 55 | }
|
---|
[7419] | 56 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 57 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[6956] | 58 | }
|
---|
[7419] | 59 | public IScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
|
---|
| 60 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
|
---|
[7415] | 61 | }
|
---|
[7419] | 62 | public IScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
|
---|
| 63 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
|
---|
[7415] | 64 | }
|
---|
[7419] | 65 | public IScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
|
---|
| 66 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
|
---|
[7415] | 67 | }
|
---|
[7419] | 68 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
| 69 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
[7415] | 70 | }
|
---|
[7419] | 71 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
| 72 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
[7415] | 73 | }
|
---|
[7419] | 74 | public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
|
---|
| 75 | get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
|
---|
[6956] | 76 | }
|
---|
[7419] | 77 | public ILookupParameter<DoubleArray> DemandsParameter {
|
---|
| 78 | get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
|
---|
[6956] | 79 | }
|
---|
[7419] | 80 | public ILookupParameter<DoubleArray> CapacitiesParameter {
|
---|
| 81 | get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
|
---|
[7415] | 82 | }
|
---|
[7419] | 83 | public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
|
---|
| 84 | get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
|
---|
[7415] | 85 | }
|
---|
[7970] | 86 | public IValueLookupParameter<DoubleValue> ExpectedRandomQualityParameter {
|
---|
| 87 | get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
|
---|
[7415] | 88 | }
|
---|
[7311] | 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 | }
|
---|
[7419] | 95 | public ILookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 96 | get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 97 | }
|
---|
[7470] | 98 | public ILookupParameter<GQAPAssignment> BestKnownSolutionParameter {
|
---|
| 99 | get { return (ILookupParameter<GQAPAssignment>)Parameters["BestKnownSolution"]; }
|
---|
[7419] | 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 | }
|
---|
[7970] | 107 | public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
|
---|
| 108 | get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
|
---|
| 109 | }
|
---|
[7438] | 110 | #endregion
|
---|
[6956] | 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() {
|
---|
[7423] | 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));
|
---|
[7970] | 127 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
|
---|
[7423] | 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));
|
---|
[7470] | 136 | Parameters.Add(new LookupParameter<GQAPAssignment>("BestKnownSolution", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionDescription));
|
---|
[7423] | 137 | Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution found so far."));
|
---|
[6956] | 138 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
|
---|
[7970] | 139 | Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
|
---|
[6956] | 140 | }
|
---|
| 141 |
|
---|
| 142 | public override IOperation Apply() {
|
---|
[7970] | 143 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
[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;
|
---|
[7970] | 157 | var expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue;
|
---|
[7415] | 158 | var results = ResultsParameter.ActualValue;
|
---|
| 159 | var maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 160 | var bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
[6956] | 161 |
|
---|
[7415] | 162 | int bestIndex;
|
---|
| 163 | var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
|
---|
[7807] | 164 | if (maximization) bestIndex = tmp.MaxItems(x => x.Value).First().Index;
|
---|
| 165 | else bestIndex = tmp.MinItems(x => x.Value).First().Index;
|
---|
[6956] | 166 |
|
---|
[7415] | 167 | if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
|
---|
| 168 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
|
---|
[7970] | 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);
|
---|
[6956] | 170 | }
|
---|
| 171 |
|
---|
[7470] | 172 | var assignment = BestSolutionParameter.ActualValue;
|
---|
[6956] | 173 | if (assignment == null) {
|
---|
[7970] | 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);
|
---|
[6956] | 175 | assignment.Distances = distances;
|
---|
| 176 | BestSolutionParameter.ActualValue = assignment;
|
---|
| 177 | } else {
|
---|
[7470] | 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();
|
---|
[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;
|
---|
[7970] | 192 | assignment.ExpectedRandomQuality = expectedRandomQuality;
|
---|
| 193 | assignment.Evaluator = evaluator;
|
---|
[6956] | 194 | }
|
---|
| 195 | }
|
---|
[7470] | 196 | if (!results.ContainsKey("Best GQAP Assignment"))
|
---|
| 197 | results.Add(new Result("Best GQAP Assignment", assignment));
|
---|
[6956] | 198 |
|
---|
| 199 | return base.Apply();
|
---|
| 200 | }
|
---|
[7415] | 201 |
|
---|
| 202 | private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
|
---|
| 203 | return maximization && oldQuality < quality || !maximization && oldQuality > quality;
|
---|
| 204 | }
|
---|
[6956] | 205 | }
|
---|
| 206 | }
|
---|