[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;
|
---|
[2975] | 26 | using HeuristicLab.Common;
|
---|
[2796] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
[3053] | 29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[2834] | 30 | using HeuristicLab.Optimization;
|
---|
[2805] | 31 | using HeuristicLab.Parameters;
|
---|
[2796] | 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2865] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[7558] | 34 | using HeuristicLab.Problems.Instances;
|
---|
[2796] | 35 |
|
---|
[3158] | 36 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
[3159] | 37 | [Item("Traveling Salesman Problem", "Represents a symmetric Traveling Salesman Problem.")]
|
---|
[2796] | 38 | [Creatable("Problems")]
|
---|
[3017] | 39 | [StorableClass]
|
---|
[7558] | 40 | public sealed class TravelingSalesmanProblem : SingleObjectiveHeuristicOptimizationProblem<ITSPEvaluator, IPermutationCreator>, IStorableContent,
|
---|
| 41 | IProblemInstanceConsumer<TSPData> {
|
---|
| 42 | private static readonly int DistanceMatrixSizeLimit = 1000;
|
---|
[4419] | 43 | public string Filename { get; set; }
|
---|
| 44 |
|
---|
[2975] | 45 | #region Parameter Properties
|
---|
[7621] | 46 | public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 47 | get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
[2865] | 48 | }
|
---|
[4825] | 49 | public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
| 50 | get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
[3066] | 51 | }
|
---|
| 52 | public ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
| 53 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
| 54 | }
|
---|
[3151] | 55 | public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
| 56 | get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
| 57 | }
|
---|
[2975] | 58 | #endregion
|
---|
[2805] | 59 |
|
---|
[2975] | 60 | #region Properties
|
---|
[3048] | 61 | public DoubleMatrix Coordinates {
|
---|
[2975] | 62 | get { return CoordinatesParameter.Value; }
|
---|
| 63 | set { CoordinatesParameter.Value = value; }
|
---|
[2865] | 64 | }
|
---|
[4825] | 65 | public DistanceMatrix DistanceMatrix {
|
---|
[3066] | 66 | get { return DistanceMatrixParameter.Value; }
|
---|
| 67 | set { DistanceMatrixParameter.Value = value; }
|
---|
| 68 | }
|
---|
| 69 | public BoolValue UseDistanceMatrix {
|
---|
| 70 | get { return UseDistanceMatrixParameter.Value; }
|
---|
| 71 | set { UseDistanceMatrixParameter.Value = value; }
|
---|
| 72 | }
|
---|
[3151] | 73 | public Permutation BestKnownSolution {
|
---|
| 74 | get { return BestKnownSolutionParameter.Value; }
|
---|
| 75 | set { BestKnownSolutionParameter.Value = value; }
|
---|
| 76 | }
|
---|
[3663] | 77 | private BestTSPSolutionAnalyzer BestTSPSolutionAnalyzer {
|
---|
[6938] | 78 | get { return Operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
|
---|
[3616] | 79 | }
|
---|
[4623] | 80 | private TSPAlleleFrequencyAnalyzer TSPAlleleFrequencyAnalyzer {
|
---|
[6938] | 81 | get { return Operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
|
---|
[4623] | 82 | }
|
---|
[4703] | 83 | private TSPPopulationDiversityAnalyzer TSPPopulationDiversityAnalyzer {
|
---|
[6938] | 84 | get { return Operators.OfType<TSPPopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
[4703] | 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]
|
---|
[4118] | 102 | private TravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
|
---|
[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."));
|
---|
[3066] | 114 | Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, 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;
|
---|
[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();
|
---|
| 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 | }
|
---|
[2975] | 160 | ParameterizeSolutionCreator();
|
---|
[3066] | 161 | ClearDistanceMatrix();
|
---|
[2975] | 162 | }
|
---|
| 163 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[3066] | 164 | ClearDistanceMatrix();
|
---|
[2975] | 165 | }
|
---|
| 166 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
| 167 | ParameterizeSolutionCreator();
|
---|
[3066] | 168 | ClearDistanceMatrix();
|
---|
[2975] | 169 | }
|
---|
| 170 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 171 | ParameterizeEvaluator();
|
---|
[4623] | 172 | ParameterizeAnalyzers();
|
---|
[2975] | 173 | ParameterizeOperators();
|
---|
| 174 | }
|
---|
[3139] | 175 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[4623] | 176 | ParameterizeAnalyzers();
|
---|
[3139] | 177 | }
|
---|
[2975] | 178 | #endregion
|
---|
[2865] | 179 |
|
---|
[2975] | 180 | #region Helpers
|
---|
[2986] | 181 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 182 | private void AfterDeserialization() {
|
---|
[4118] | 183 | // BackwardsCompatibility3.3
|
---|
| 184 | #region Backwards compatible code (remove with 3.4)
|
---|
[4825] | 185 | OptionalValueParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as OptionalValueParameter<DoubleMatrix>;
|
---|
| 186 | if (oldDistanceMatrixParameter != null) {
|
---|
| 187 | Parameters.Remove(oldDistanceMatrixParameter);
|
---|
| 188 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
| 189 | DistanceMatrixParameter.GetsCollected = oldDistanceMatrixParameter.GetsCollected;
|
---|
| 190 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
| 191 | if (oldDistanceMatrixParameter.Value != null) {
|
---|
| 192 | DoubleMatrix oldDM = oldDistanceMatrixParameter.Value;
|
---|
| 193 | DistanceMatrix newDM = new DistanceMatrix(oldDM.Rows, oldDM.Columns, oldDM.ColumnNames, oldDM.RowNames);
|
---|
| 194 | newDM.SortableView = oldDM.SortableView;
|
---|
| 195 | for (int i = 0; i < newDM.Rows; i++)
|
---|
| 196 | for (int j = 0; j < newDM.Columns; j++)
|
---|
| 197 | newDM[i, j] = oldDM[i, j];
|
---|
| 198 | DistanceMatrixParameter.Value = (DistanceMatrix)newDM.AsReadOnly();
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[7621] | 202 | ValueParameter<DoubleMatrix> oldCoordinates = (Parameters["Coordinates"] as ValueParameter<DoubleMatrix>);
|
---|
| 203 | if (oldCoordinates != null) {
|
---|
| 204 | Parameters.Remove(oldCoordinates);
|
---|
| 205 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", oldCoordinates.Value, oldCoordinates.GetsCollected));
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[6938] | 208 | if (Operators.Count == 0) InitializeOperators();
|
---|
[4118] | 209 | #endregion
|
---|
[7351] | 210 | RegisterEventHandlers();
|
---|
[4118] | 211 | }
|
---|
| 212 |
|
---|
[7351] | 213 | private void RegisterEventHandlers() {
|
---|
[2975] | 214 | CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
|
---|
[7621] | 215 | if (Coordinates != null) {
|
---|
| 216 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
| 217 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
| 218 | }
|
---|
[2975] | 219 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
[3139] | 220 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[2865] | 221 | }
|
---|
[3139] | 222 |
|
---|
[3099] | 223 | private void InitializeOperators() {
|
---|
[6938] | 224 | Operators.Add(new BestTSPSolutionAnalyzer());
|
---|
| 225 | Operators.Add(new TSPAlleleFrequencyAnalyzer());
|
---|
| 226 | Operators.Add(new TSPPopulationDiversityAnalyzer());
|
---|
[4623] | 227 | ParameterizeAnalyzers();
|
---|
[7658] | 228 | var operators = new HashSet<IPermutationOperator>(new IPermutationOperator[] {
|
---|
[7626] | 229 | new OrderCrossover2(),
|
---|
| 230 | new InversionManipulator(),
|
---|
| 231 | new StochasticInversionMultiMoveGenerator()
|
---|
[7658] | 232 | }, new TypeEqualityComparer<IPermutationOperator>());
|
---|
| 233 | foreach (var op in ApplicationManager.Manager.GetInstances<IPermutationOperator>())
|
---|
| 234 | operators.Add(op);
|
---|
| 235 | Operators.AddRange(operators);
|
---|
[3199] | 236 | ParameterizeOperators();
|
---|
[3209] | 237 | UpdateMoveEvaluators();
|
---|
[3099] | 238 | }
|
---|
[3209] | 239 | private void UpdateMoveEvaluators() {
|
---|
[6938] | 240 | Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
[3303] | 241 | foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
|
---|
| 242 | if (op.EvaluatorType == Evaluator.GetType()) {
|
---|
[6938] | 243 | Operators.Add(op);
|
---|
[3303] | 244 | }
|
---|
| 245 | ParameterizeOperators();
|
---|
| 246 | OnOperatorsChanged();
|
---|
[3209] | 247 | }
|
---|
[2975] | 248 | private void ParameterizeSolutionCreator() {
|
---|
[7621] | 249 | if (Evaluator is ITSPDistanceMatrixEvaluator && DistanceMatrix != null)
|
---|
| 250 | SolutionCreator.LengthParameter.Value = new IntValue(DistanceMatrix.Rows);
|
---|
| 251 | else if (Evaluator is ITSPCoordinatesPathEvaluator && Coordinates != null)
|
---|
| 252 | SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
|
---|
| 253 | else SolutionCreator.LengthParameter.Value = null;
|
---|
| 254 | SolutionCreator.LengthParameter.Hidden = SolutionCreator.LengthParameter.Value != null;
|
---|
[3231] | 255 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
|
---|
[6051] | 256 | SolutionCreator.PermutationTypeParameter.Hidden = true;
|
---|
[2865] | 257 | }
|
---|
[2975] | 258 | private void ParameterizeEvaluator() {
|
---|
[6051] | 259 | if (Evaluator is ITSPPathEvaluator) {
|
---|
| 260 | ITSPPathEvaluator evaluator = (ITSPPathEvaluator)Evaluator;
|
---|
| 261 | evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 262 | evaluator.PermutationParameter.Hidden = true;
|
---|
| 263 | }
|
---|
[3066] | 264 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
| 265 | ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
|
---|
| 266 | evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[6051] | 267 | evaluator.CoordinatesParameter.Hidden = true;
|
---|
[3066] | 268 | evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[6051] | 269 | evaluator.DistanceMatrixParameter.Hidden = true;
|
---|
[3066] | 270 | evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
[6051] | 271 | evaluator.UseDistanceMatrixParameter.Hidden = true;
|
---|
[3066] | 272 | }
|
---|
[7558] | 273 | if (Evaluator is ITSPDistanceMatrixEvaluator) {
|
---|
| 274 | var evaluator = (ITSPDistanceMatrixEvaluator)Evaluator;
|
---|
| 275 | evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
| 276 | evaluator.DistanceMatrixParameter.Hidden = true;
|
---|
| 277 | }
|
---|
[2865] | 278 | }
|
---|
[4623] | 279 | private void ParameterizeAnalyzers() {
|
---|
[4641] | 280 | if (BestTSPSolutionAnalyzer != null) {
|
---|
| 281 | BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 282 | BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
| 283 | BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 284 | BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 285 | BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
| 286 | BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 287 | BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 288 | }
|
---|
[4623] | 289 |
|
---|
[4641] | 290 | if (TSPAlleleFrequencyAnalyzer != null) {
|
---|
| 291 | TSPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 292 | TSPAlleleFrequencyAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[4866] | 293 | TSPAlleleFrequencyAnalyzer.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[4641] | 294 | TSPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 295 | TSPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 296 | TSPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 297 | TSPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 298 | }
|
---|
[4703] | 299 |
|
---|
| 300 | if (TSPPopulationDiversityAnalyzer != null) {
|
---|
| 301 | TSPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 302 | TSPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 303 | TSPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 304 | TSPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 305 | }
|
---|
[3107] | 306 | }
|
---|
[2975] | 307 | private void ParameterizeOperators() {
|
---|
| 308 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
| 309 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 310 | op.ParentsParameter.Hidden = true;
|
---|
[2975] | 311 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 312 | op.ChildParameter.Hidden = true;
|
---|
[2975] | 313 | }
|
---|
| 314 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
| 315 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 316 | op.PermutationParameter.Hidden = true;
|
---|
[2975] | 317 | }
|
---|
[3074] | 318 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
| 319 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 320 | op.PermutationParameter.Hidden = true;
|
---|
[3074] | 321 | }
|
---|
| 322 | foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
|
---|
| 323 | op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[6051] | 324 | op.CoordinatesParameter.Hidden = true;
|
---|
[3074] | 325 | op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[6051] | 326 | op.DistanceMatrixParameter.Hidden = true;
|
---|
[3074] | 327 | op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
[6051] | 328 | op.UseDistanceMatrixParameter.Hidden = true;
|
---|
[3209] | 329 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 330 | op.QualityParameter.Hidden = true;
|
---|
[3209] | 331 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 332 | op.PermutationParameter.Hidden = true;
|
---|
[3074] | 333 | }
|
---|
[6051] | 334 | foreach (IPermutationMultiNeighborhoodShakingOperator op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
|
---|
[6042] | 335 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 336 | op.PermutationParameter.Hidden = true;
|
---|
| 337 | }
|
---|
[2975] | 338 | }
|
---|
[3074] | 339 |
|
---|
[3066] | 340 | private void ClearDistanceMatrix() {
|
---|
[7558] | 341 | if (!(Evaluator is ITSPDistanceMatrixEvaluator))
|
---|
| 342 | DistanceMatrixParameter.Value = null;
|
---|
[3066] | 343 | }
|
---|
[2975] | 344 | #endregion
|
---|
[4098] | 345 |
|
---|
[7558] | 346 | public void Load(TSPData data) {
|
---|
| 347 | if (data.Coordinates == null && data.Distances == null)
|
---|
[7621] | 348 | throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
|
---|
[7872] | 349 | if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
|
---|
| 350 | || data.DistanceMeasure == DistanceMeasure.Manhattan
|
---|
| 351 | || data.DistanceMeasure == DistanceMeasure.Maximum
|
---|
| 352 | || data.DistanceMeasure == DistanceMeasure.UpperEuclidean))
|
---|
[7558] | 353 | throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
|
---|
| 354 | if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
|
---|
| 355 | 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.");
|
---|
| 356 |
|
---|
| 357 | Name = data.Name;
|
---|
| 358 | Description = data.Description;
|
---|
| 359 |
|
---|
| 360 | if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
|
---|
| 361 | Coordinates = new DoubleMatrix(data.Coordinates);
|
---|
[7621] | 362 | else Coordinates = null;
|
---|
[7558] | 363 |
|
---|
| 364 | TSPEvaluator evaluator;
|
---|
[7872] | 365 | if (data.DistanceMeasure == DistanceMeasure.Att
|
---|
| 366 | || data.DistanceMeasure == DistanceMeasure.Manhattan
|
---|
| 367 | || data.DistanceMeasure == DistanceMeasure.Maximum
|
---|
| 368 | || data.DistanceMeasure == DistanceMeasure.UpperEuclidean) {
|
---|
[7558] | 369 | evaluator = new TSPDistanceMatrixEvaluator();
|
---|
| 370 | UseDistanceMatrix = new BoolValue(true);
|
---|
| 371 | DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
|
---|
[7872] | 372 | } else if (data.DistanceMeasure == DistanceMeasure.Direct && data.Distances != null) {
|
---|
[7558] | 373 | evaluator = new TSPDistanceMatrixEvaluator();
|
---|
| 374 | UseDistanceMatrix = new BoolValue(true);
|
---|
| 375 | DistanceMatrix = new DistanceMatrix(data.Distances);
|
---|
| 376 | } else {
|
---|
| 377 | DistanceMatrix = null;
|
---|
| 378 | UseDistanceMatrix = new BoolValue(data.Dimension <= DistanceMatrixSizeLimit);
|
---|
| 379 | switch (data.DistanceMeasure) {
|
---|
[7872] | 380 | case DistanceMeasure.Euclidean:
|
---|
[7558] | 381 | evaluator = new TSPEuclideanPathEvaluator();
|
---|
| 382 | break;
|
---|
[7872] | 383 | case DistanceMeasure.RoundedEuclidean:
|
---|
[7558] | 384 | evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
| 385 | break;
|
---|
[7872] | 386 | case DistanceMeasure.Geo:
|
---|
[7558] | 387 | evaluator = new TSPGeoPathEvaluator();
|
---|
| 388 | break;
|
---|
| 389 | default:
|
---|
| 390 | throw new InvalidDataException("An unknown distance measure is given in the instance!");
|
---|
| 391 | }
|
---|
[4098] | 392 | }
|
---|
[7558] | 393 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
| 394 | Evaluator = evaluator;
|
---|
| 395 |
|
---|
| 396 | BestKnownSolution = null;
|
---|
[4098] | 397 | BestKnownQuality = null;
|
---|
| 398 |
|
---|
[7558] | 399 | if (data.BestKnownTour != null) {
|
---|
| 400 | try {
|
---|
| 401 | EvaluateAndLoadTour(data.BestKnownTour);
|
---|
[7658] | 402 | }
|
---|
| 403 | catch (InvalidOperationException) {
|
---|
[7558] | 404 | if (data.BestKnownQuality.HasValue)
|
---|
| 405 | BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
|
---|
| 406 | }
|
---|
| 407 | } else if (data.BestKnownQuality.HasValue) {
|
---|
| 408 | BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
|
---|
[4098] | 409 | }
|
---|
| 410 | OnReset();
|
---|
| 411 | }
|
---|
[7558] | 412 |
|
---|
| 413 | public void EvaluateAndLoadTour(int[] tour) {
|
---|
| 414 | var route = new Permutation(PermutationTypes.RelativeUndirected, tour);
|
---|
| 415 | BestKnownSolution = route;
|
---|
| 416 |
|
---|
| 417 | double quality;
|
---|
[7621] | 418 | if (Evaluator is ITSPDistanceMatrixEvaluator) {
|
---|
[7558] | 419 | quality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, route);
|
---|
[7621] | 420 | } else if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
[7558] | 421 | quality = TSPCoordinatesPathEvaluator.Apply((TSPCoordinatesPathEvaluator)Evaluator, Coordinates, route);
|
---|
| 422 | } else {
|
---|
| 423 | throw new InvalidOperationException("Cannot calculate solution quality, evaluator type is unknown.");
|
---|
| 424 | }
|
---|
| 425 | BestKnownQuality = new DoubleValue(quality);
|
---|
[4098] | 426 | }
|
---|
[2796] | 427 | }
|
---|
| 428 | }
|
---|