[2796] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2796] | 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 |
|
---|
[2865] | 22 | using System;
|
---|
[2975] | 23 | using System.Collections.Generic;
|
---|
[3153] | 24 | using System.IO;
|
---|
[2865] | 25 | using System.Linq;
|
---|
[8660] | 26 | using HeuristicLab.Analysis;
|
---|
[2975] | 27 | using HeuristicLab.Common;
|
---|
[2796] | 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
[3053] | 30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[2834] | 31 | using HeuristicLab.Optimization;
|
---|
[2805] | 32 | using HeuristicLab.Parameters;
|
---|
[2796] | 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2865] | 34 | using HeuristicLab.PluginInfrastructure;
|
---|
[7558] | 35 | using HeuristicLab.Problems.Instances;
|
---|
[2796] | 36 |
|
---|
[3158] | 37 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
[3159] | 38 | [Item("Traveling Salesman Problem", "Represents a symmetric Traveling Salesman Problem.")]
|
---|
[2796] | 39 | [Creatable("Problems")]
|
---|
[3017] | 40 | [StorableClass]
|
---|
[7558] | 41 | public sealed class TravelingSalesmanProblem : SingleObjectiveHeuristicOptimizationProblem<ITSPEvaluator, IPermutationCreator>, IStorableContent,
|
---|
| 42 | IProblemInstanceConsumer<TSPData> {
|
---|
| 43 | private static readonly int DistanceMatrixSizeLimit = 1000;
|
---|
[4419] | 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
[2975] | 46 | #region Parameter Properties
|
---|
[7621] | 47 | public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 48 | get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
[2865] | 49 | }
|
---|
[4825] | 50 | public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
| 51 | get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
[3066] | 52 | }
|
---|
| 53 | public ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
| 54 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
| 55 | }
|
---|
[3151] | 56 | public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
| 57 | get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
| 58 | }
|
---|
[2975] | 59 | #endregion
|
---|
[2805] | 60 |
|
---|
[2975] | 61 | #region Properties
|
---|
[3048] | 62 | public DoubleMatrix Coordinates {
|
---|
[2975] | 63 | get { return CoordinatesParameter.Value; }
|
---|
| 64 | set { CoordinatesParameter.Value = value; }
|
---|
[2865] | 65 | }
|
---|
[4825] | 66 | public DistanceMatrix DistanceMatrix {
|
---|
[3066] | 67 | get { return DistanceMatrixParameter.Value; }
|
---|
| 68 | set { DistanceMatrixParameter.Value = value; }
|
---|
| 69 | }
|
---|
| 70 | public BoolValue UseDistanceMatrix {
|
---|
| 71 | get { return UseDistanceMatrixParameter.Value; }
|
---|
| 72 | set { UseDistanceMatrixParameter.Value = value; }
|
---|
| 73 | }
|
---|
[3151] | 74 | public Permutation BestKnownSolution {
|
---|
| 75 | get { return BestKnownSolutionParameter.Value; }
|
---|
| 76 | set { BestKnownSolutionParameter.Value = value; }
|
---|
| 77 | }
|
---|
[3663] | 78 | private BestTSPSolutionAnalyzer BestTSPSolutionAnalyzer {
|
---|
[6938] | 79 | get { return Operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
|
---|
[3616] | 80 | }
|
---|
[4623] | 81 | private TSPAlleleFrequencyAnalyzer TSPAlleleFrequencyAnalyzer {
|
---|
[6938] | 82 | get { return Operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
|
---|
[4623] | 83 | }
|
---|
[4703] | 84 | private TSPPopulationDiversityAnalyzer TSPPopulationDiversityAnalyzer {
|
---|
[6938] | 85 | get { return Operators.OfType<TSPPopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
[4703] | 86 | }
|
---|
[8660] | 87 | private SingleObjectivePopulationDiversityAnalyzer SingleObjectivePopulationDiversityAnalyzer {
|
---|
| 88 | get { return Operators.OfType<SingleObjectivePopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
| 89 | }
|
---|
[2975] | 90 | #endregion
|
---|
[2865] | 91 |
|
---|
[6938] | 92 | // BackwardsCompatibility3.3
|
---|
| 93 | #region Backwards compatible code, remove with 3.4
|
---|
| 94 | [Obsolete]
|
---|
| 95 | [Storable(Name = "operators")]
|
---|
| 96 | private IEnumerable<IOperator> oldOperators {
|
---|
| 97 | get { return null; }
|
---|
| 98 | set {
|
---|
| 99 | if (value != null && value.Any())
|
---|
| 100 | Operators.AddRange(value);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | #endregion
|
---|
[4098] | 104 |
|
---|
| 105 | [StorableConstructor]
|
---|
[4118] | 106 | private TravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 107 | private TravelingSalesmanProblem(TravelingSalesmanProblem original, Cloner cloner)
|
---|
| 108 | : base(original, cloner) {
|
---|
[7351] | 109 | RegisterEventHandlers();
|
---|
[4722] | 110 | }
|
---|
| 111 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 112 | return new TravelingSalesmanProblem(this, cloner);
|
---|
| 113 | }
|
---|
[3159] | 114 | public TravelingSalesmanProblem()
|
---|
[6938] | 115 | : base(new TSPRoundedEuclideanPathEvaluator(), new RandomPermutationCreator()) {
|
---|
[7621] | 116 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
[4825] | 117 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
[8660] | 118 | Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if the coordinates based evaluators should calculate the distance matrix from the coordinates and use it for evaluation similar to the distance matrix evaluator, otherwise false.", new BoolValue(true)));
|
---|
[3151] | 119 | Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
|
---|
[2865] | 120 |
|
---|
[6939] | 121 | Maximization.Value = false;
|
---|
[6051] | 122 | MaximizationParameter.Hidden = true;
|
---|
[8660] | 123 | UseDistanceMatrixParameter.Hidden = true;
|
---|
[4825] | 124 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
| 125 |
|
---|
[3504] | 126 | Coordinates = new DoubleMatrix(new double[,] {
|
---|
| 127 | { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
|
---|
| 128 | { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
|
---|
| 129 | { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
|
---|
| 130 | { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
|
---|
| 131 | });
|
---|
| 132 |
|
---|
[6938] | 133 | SolutionCreator.PermutationParameter.ActualName = "TSPTour";
|
---|
| 134 | Evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
[2975] | 135 | ParameterizeSolutionCreator();
|
---|
| 136 | ParameterizeEvaluator();
|
---|
[2865] | 137 |
|
---|
[4098] | 138 | InitializeOperators();
|
---|
[7351] | 139 | RegisterEventHandlers();
|
---|
[2975] | 140 | }
|
---|
[2891] | 141 |
|
---|
[2975] | 142 | #region Events
|
---|
[6938] | 143 | protected override void OnSolutionCreatorChanged() {
|
---|
| 144 | base.OnSolutionCreatorChanged();
|
---|
| 145 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 146 | ParameterizeSolutionCreator();
|
---|
| 147 | ParameterizeEvaluator();
|
---|
| 148 | ParameterizeAnalyzers();
|
---|
| 149 | ParameterizeOperators();
|
---|
[2865] | 150 | }
|
---|
[6938] | 151 | protected override void OnEvaluatorChanged() {
|
---|
| 152 | base.OnEvaluatorChanged();
|
---|
| 153 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 154 | ParameterizeEvaluator();
|
---|
[7621] | 155 | ParameterizeSolutionCreator();
|
---|
[6938] | 156 | UpdateMoveEvaluators();
|
---|
| 157 | ParameterizeAnalyzers();
|
---|
[8660] | 158 | if (Evaluator is ITSPCoordinatesPathEvaluator && Coordinates != null)
|
---|
| 159 | ClearDistanceMatrix();
|
---|
[2975] | 160 | }
|
---|
| 161 | private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[7621] | 162 | if (Coordinates != null) {
|
---|
| 163 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
| 164 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
| 165 | }
|
---|
[8660] | 166 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
| 167 | ParameterizeSolutionCreator();
|
---|
| 168 | ClearDistanceMatrix();
|
---|
| 169 | }
|
---|
[2975] | 170 | }
|
---|
| 171 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[8660] | 172 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
| 173 | ClearDistanceMatrix();
|
---|
| 174 | }
|
---|
[2975] | 175 | }
|
---|
| 176 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
[8660] | 177 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
| 178 | ParameterizeSolutionCreator();
|
---|
| 179 | ClearDistanceMatrix();
|
---|
| 180 | }
|
---|
[2975] | 181 | }
|
---|
| 182 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 183 | ParameterizeEvaluator();
|
---|
[4623] | 184 | ParameterizeAnalyzers();
|
---|
[2975] | 185 | ParameterizeOperators();
|
---|
| 186 | }
|
---|
[3139] | 187 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[4623] | 188 | ParameterizeAnalyzers();
|
---|
[3139] | 189 | }
|
---|
[2975] | 190 | #endregion
|
---|
[2865] | 191 |
|
---|
[2975] | 192 | #region Helpers
|
---|
[2986] | 193 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 194 | private void AfterDeserialization() {
|
---|
[4118] | 195 | // BackwardsCompatibility3.3
|
---|
| 196 | #region Backwards compatible code (remove with 3.4)
|
---|
[4825] | 197 | OptionalValueParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as OptionalValueParameter<DoubleMatrix>;
|
---|
| 198 | if (oldDistanceMatrixParameter != null) {
|
---|
| 199 | Parameters.Remove(oldDistanceMatrixParameter);
|
---|
| 200 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
| 201 | DistanceMatrixParameter.GetsCollected = oldDistanceMatrixParameter.GetsCollected;
|
---|
| 202 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
| 203 | if (oldDistanceMatrixParameter.Value != null) {
|
---|
| 204 | DoubleMatrix oldDM = oldDistanceMatrixParameter.Value;
|
---|
| 205 | DistanceMatrix newDM = new DistanceMatrix(oldDM.Rows, oldDM.Columns, oldDM.ColumnNames, oldDM.RowNames);
|
---|
| 206 | newDM.SortableView = oldDM.SortableView;
|
---|
| 207 | for (int i = 0; i < newDM.Rows; i++)
|
---|
| 208 | for (int j = 0; j < newDM.Columns; j++)
|
---|
| 209 | newDM[i, j] = oldDM[i, j];
|
---|
| 210 | DistanceMatrixParameter.Value = (DistanceMatrix)newDM.AsReadOnly();
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[7621] | 214 | ValueParameter<DoubleMatrix> oldCoordinates = (Parameters["Coordinates"] as ValueParameter<DoubleMatrix>);
|
---|
| 215 | if (oldCoordinates != null) {
|
---|
| 216 | Parameters.Remove(oldCoordinates);
|
---|
| 217 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", oldCoordinates.Value, oldCoordinates.GetsCollected));
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[6938] | 220 | if (Operators.Count == 0) InitializeOperators();
|
---|
[4118] | 221 | #endregion
|
---|
[7351] | 222 | RegisterEventHandlers();
|
---|
[4118] | 223 | }
|
---|
| 224 |
|
---|
[7351] | 225 | private void RegisterEventHandlers() {
|
---|
[2975] | 226 | CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
|
---|
[7621] | 227 | if (Coordinates != null) {
|
---|
| 228 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
| 229 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
| 230 | }
|
---|
[2975] | 231 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
[3139] | 232 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[2865] | 233 | }
|
---|
[3139] | 234 |
|
---|
[3099] | 235 | private void InitializeOperators() {
|
---|
[8660] | 236 | Operators.Add(new TSPImprovementOperator());
|
---|
| 237 | Operators.Add(new TSPMultipleGuidesPathRelinker());
|
---|
| 238 | Operators.Add(new TSPPathRelinker());
|
---|
| 239 | Operators.Add(new TSPSimultaneousPathRelinker());
|
---|
| 240 | Operators.Add(new TSPSimilarityCalculator());
|
---|
| 241 |
|
---|
[6938] | 242 | Operators.Add(new BestTSPSolutionAnalyzer());
|
---|
| 243 | Operators.Add(new TSPAlleleFrequencyAnalyzer());
|
---|
[8660] | 244 | Operators.Add(new SingleObjectivePopulationDiversityAnalyzer());
|
---|
[4623] | 245 | ParameterizeAnalyzers();
|
---|
[7658] | 246 | var operators = new HashSet<IPermutationOperator>(new IPermutationOperator[] {
|
---|
[7626] | 247 | new OrderCrossover2(),
|
---|
| 248 | new InversionManipulator(),
|
---|
| 249 | new StochasticInversionMultiMoveGenerator()
|
---|
[7658] | 250 | }, new TypeEqualityComparer<IPermutationOperator>());
|
---|
| 251 | foreach (var op in ApplicationManager.Manager.GetInstances<IPermutationOperator>())
|
---|
| 252 | operators.Add(op);
|
---|
| 253 | Operators.AddRange(operators);
|
---|
[3199] | 254 | ParameterizeOperators();
|
---|
[3209] | 255 | UpdateMoveEvaluators();
|
---|
[3099] | 256 | }
|
---|
[3209] | 257 | private void UpdateMoveEvaluators() {
|
---|
[6938] | 258 | Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
[3303] | 259 | foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
|
---|
| 260 | if (op.EvaluatorType == Evaluator.GetType()) {
|
---|
[6938] | 261 | Operators.Add(op);
|
---|
[3303] | 262 | }
|
---|
| 263 | ParameterizeOperators();
|
---|
| 264 | OnOperatorsChanged();
|
---|
[3209] | 265 | }
|
---|
[2975] | 266 | private void ParameterizeSolutionCreator() {
|
---|
[7621] | 267 | if (Evaluator is ITSPDistanceMatrixEvaluator && DistanceMatrix != null)
|
---|
| 268 | SolutionCreator.LengthParameter.Value = new IntValue(DistanceMatrix.Rows);
|
---|
| 269 | else if (Evaluator is ITSPCoordinatesPathEvaluator && Coordinates != null)
|
---|
| 270 | SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
|
---|
[8660] | 271 | else {
|
---|
| 272 | SolutionCreator.LengthParameter.Value = null;
|
---|
| 273 | string error = "The given problem does not support the selected evaluator.";
|
---|
| 274 | if (Evaluator is ITSPDistanceMatrixEvaluator)
|
---|
| 275 | error += Environment.NewLine + "Please review that the " + DistanceMatrixParameter.Name + " parameter is defined or choose another evaluator.";
|
---|
| 276 | else error += Environment.NewLine + "Please review that the " + CoordinatesParameter.Name + " parameter is defined or choose another evaluator.";
|
---|
| 277 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(error, null);
|
---|
| 278 | }
|
---|
[7621] | 279 | SolutionCreator.LengthParameter.Hidden = SolutionCreator.LengthParameter.Value != null;
|
---|
[3231] | 280 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
|
---|
[6051] | 281 | SolutionCreator.PermutationTypeParameter.Hidden = true;
|
---|
[2865] | 282 | }
|
---|
[2975] | 283 | private void ParameterizeEvaluator() {
|
---|
[6051] | 284 | if (Evaluator is ITSPPathEvaluator) {
|
---|
| 285 | ITSPPathEvaluator evaluator = (ITSPPathEvaluator)Evaluator;
|
---|
| 286 | evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 287 | evaluator.PermutationParameter.Hidden = true;
|
---|
| 288 | }
|
---|
[3066] | 289 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
| 290 | ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
|
---|
| 291 | evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[6051] | 292 | evaluator.CoordinatesParameter.Hidden = true;
|
---|
[3066] | 293 | evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[6051] | 294 | evaluator.DistanceMatrixParameter.Hidden = true;
|
---|
[3066] | 295 | evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
[6051] | 296 | evaluator.UseDistanceMatrixParameter.Hidden = true;
|
---|
[3066] | 297 | }
|
---|
[7558] | 298 | if (Evaluator is ITSPDistanceMatrixEvaluator) {
|
---|
| 299 | var evaluator = (ITSPDistanceMatrixEvaluator)Evaluator;
|
---|
| 300 | evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
| 301 | evaluator.DistanceMatrixParameter.Hidden = true;
|
---|
| 302 | }
|
---|
[2865] | 303 | }
|
---|
[4623] | 304 | private void ParameterizeAnalyzers() {
|
---|
[4641] | 305 | if (BestTSPSolutionAnalyzer != null) {
|
---|
| 306 | BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 307 | BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
| 308 | BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 309 | BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 310 | BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
| 311 | BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 312 | BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 313 | }
|
---|
[4623] | 314 |
|
---|
[4641] | 315 | if (TSPAlleleFrequencyAnalyzer != null) {
|
---|
| 316 | TSPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 317 | TSPAlleleFrequencyAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[4866] | 318 | TSPAlleleFrequencyAnalyzer.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[4641] | 319 | TSPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 320 | TSPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 321 | TSPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 322 | TSPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 323 | }
|
---|
[4703] | 324 |
|
---|
| 325 | if (TSPPopulationDiversityAnalyzer != null) {
|
---|
| 326 | TSPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 327 | TSPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 328 | TSPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 329 | TSPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 330 | }
|
---|
[8660] | 331 |
|
---|
| 332 | if (SingleObjectivePopulationDiversityAnalyzer != null) {
|
---|
| 333 | SingleObjectivePopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 334 | SingleObjectivePopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 335 | SingleObjectivePopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 336 | SingleObjectivePopulationDiversityAnalyzer.SimilarityCalculator = Operators.OfType<TSPSimilarityCalculator>().SingleOrDefault();
|
---|
| 337 | }
|
---|
[3107] | 338 | }
|
---|
[2975] | 339 | private void ParameterizeOperators() {
|
---|
| 340 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
| 341 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 342 | op.ParentsParameter.Hidden = true;
|
---|
[2975] | 343 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 344 | op.ChildParameter.Hidden = true;
|
---|
[2975] | 345 | }
|
---|
| 346 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
| 347 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 348 | op.PermutationParameter.Hidden = true;
|
---|
[2975] | 349 | }
|
---|
[3074] | 350 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
| 351 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 352 | op.PermutationParameter.Hidden = true;
|
---|
[3074] | 353 | }
|
---|
| 354 | foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
|
---|
| 355 | op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[6051] | 356 | op.CoordinatesParameter.Hidden = true;
|
---|
[3074] | 357 | op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[6051] | 358 | op.DistanceMatrixParameter.Hidden = true;
|
---|
[3074] | 359 | op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
[6051] | 360 | op.UseDistanceMatrixParameter.Hidden = true;
|
---|
[3209] | 361 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 362 | op.QualityParameter.Hidden = true;
|
---|
[3209] | 363 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 364 | op.PermutationParameter.Hidden = true;
|
---|
[3074] | 365 | }
|
---|
[6051] | 366 | foreach (IPermutationMultiNeighborhoodShakingOperator op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
|
---|
[6042] | 367 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 368 | op.PermutationParameter.Hidden = true;
|
---|
| 369 | }
|
---|
[8660] | 370 | foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
|
---|
| 371 | op.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 372 | op.SolutionParameter.Hidden = true;
|
---|
| 373 | }
|
---|
| 374 | foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) {
|
---|
| 375 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 376 | op.ParentsParameter.Hidden = true;
|
---|
| 377 | }
|
---|
| 378 | foreach (TSPSimilarityCalculator op in Operators.OfType<TSPSimilarityCalculator>()) {
|
---|
| 379 | op.SolutionVariableName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 380 | op.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
| 381 | }
|
---|
[2975] | 382 | }
|
---|
[3074] | 383 |
|
---|
[3066] | 384 | private void ClearDistanceMatrix() {
|
---|
[8660] | 385 | DistanceMatrixParameter.Value = null;
|
---|
[3066] | 386 | }
|
---|
[2975] | 387 | #endregion
|
---|
[4098] | 388 |
|
---|
[7558] | 389 | public void Load(TSPData data) {
|
---|
| 390 | if (data.Coordinates == null && data.Distances == null)
|
---|
[7621] | 391 | throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
|
---|
[8085] | 392 | if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
|
---|
| 393 | || data.DistanceMeasure == DistanceMeasure.Manhattan
|
---|
| 394 | || data.DistanceMeasure == DistanceMeasure.Maximum
|
---|
| 395 | || data.DistanceMeasure == DistanceMeasure.UpperEuclidean))
|
---|
[7558] | 396 | throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
|
---|
| 397 | if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
|
---|
| 398 | throw new System.IO.InvalidDataException("The coordinates of the given instance are not in the right format, there need to be one row for each customer and two columns for the x and y coordinates.");
|
---|
| 399 |
|
---|
| 400 | Name = data.Name;
|
---|
| 401 | Description = data.Description;
|
---|
| 402 |
|
---|
[8660] | 403 | bool clearCoordinates = false, clearDistanceMatrix = false;
|
---|
[7558] | 404 | if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
|
---|
| 405 | Coordinates = new DoubleMatrix(data.Coordinates);
|
---|
[8660] | 406 | else clearCoordinates = true;
|
---|
[7558] | 407 |
|
---|
| 408 | TSPEvaluator evaluator;
|
---|
[8085] | 409 | if (data.DistanceMeasure == DistanceMeasure.Att
|
---|
| 410 | || data.DistanceMeasure == DistanceMeasure.Manhattan
|
---|
| 411 | || data.DistanceMeasure == DistanceMeasure.Maximum
|
---|
| 412 | || data.DistanceMeasure == DistanceMeasure.UpperEuclidean) {
|
---|
[7558] | 413 | evaluator = new TSPDistanceMatrixEvaluator();
|
---|
| 414 | UseDistanceMatrix = new BoolValue(true);
|
---|
| 415 | DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
|
---|
[8085] | 416 | } else if (data.DistanceMeasure == DistanceMeasure.Direct && data.Distances != null) {
|
---|
[7558] | 417 | evaluator = new TSPDistanceMatrixEvaluator();
|
---|
| 418 | UseDistanceMatrix = new BoolValue(true);
|
---|
| 419 | DistanceMatrix = new DistanceMatrix(data.Distances);
|
---|
| 420 | } else {
|
---|
[8660] | 421 | clearDistanceMatrix = true;
|
---|
[7558] | 422 | UseDistanceMatrix = new BoolValue(data.Dimension <= DistanceMatrixSizeLimit);
|
---|
| 423 | switch (data.DistanceMeasure) {
|
---|
[8085] | 424 | case DistanceMeasure.Euclidean:
|
---|
[7558] | 425 | evaluator = new TSPEuclideanPathEvaluator();
|
---|
| 426 | break;
|
---|
[8085] | 427 | case DistanceMeasure.RoundedEuclidean:
|
---|
[7558] | 428 | evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
| 429 | break;
|
---|
[8085] | 430 | case DistanceMeasure.Geo:
|
---|
[7558] | 431 | evaluator = new TSPGeoPathEvaluator();
|
---|
| 432 | break;
|
---|
| 433 | default:
|
---|
| 434 | throw new InvalidDataException("An unknown distance measure is given in the instance!");
|
---|
| 435 | }
|
---|
[4098] | 436 | }
|
---|
[7558] | 437 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
| 438 | Evaluator = evaluator;
|
---|
| 439 |
|
---|
[8660] | 440 | // reset them after assigning the evaluator
|
---|
| 441 | if (clearCoordinates) Coordinates = null;
|
---|
| 442 | if (clearDistanceMatrix) DistanceMatrix = null;
|
---|
| 443 |
|
---|
[7558] | 444 | BestKnownSolution = null;
|
---|
[4098] | 445 | BestKnownQuality = null;
|
---|
| 446 |
|
---|
[7558] | 447 | if (data.BestKnownTour != null) {
|
---|
| 448 | try {
|
---|
| 449 | EvaluateAndLoadTour(data.BestKnownTour);
|
---|
[8660] | 450 | } catch (InvalidOperationException) {
|
---|
[7558] | 451 | if (data.BestKnownQuality.HasValue)
|
---|
| 452 | BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
|
---|
| 453 | }
|
---|
| 454 | } else if (data.BestKnownQuality.HasValue) {
|
---|
| 455 | BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
|
---|
[4098] | 456 | }
|
---|
| 457 | OnReset();
|
---|
| 458 | }
|
---|
[7558] | 459 |
|
---|
| 460 | public void EvaluateAndLoadTour(int[] tour) {
|
---|
| 461 | var route = new Permutation(PermutationTypes.RelativeUndirected, tour);
|
---|
| 462 | BestKnownSolution = route;
|
---|
| 463 |
|
---|
| 464 | double quality;
|
---|
[7621] | 465 | if (Evaluator is ITSPDistanceMatrixEvaluator) {
|
---|
[7558] | 466 | quality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, route);
|
---|
[7621] | 467 | } else if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
[7558] | 468 | quality = TSPCoordinatesPathEvaluator.Apply((TSPCoordinatesPathEvaluator)Evaluator, Coordinates, route);
|
---|
| 469 | } else {
|
---|
| 470 | throw new InvalidOperationException("Cannot calculate solution quality, evaluator type is unknown.");
|
---|
| 471 | }
|
---|
| 472 | BestKnownQuality = new DoubleValue(quality);
|
---|
[4098] | 473 | }
|
---|
[2796] | 474 | }
|
---|
| 475 | }
|
---|