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