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