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