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