1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Drawing;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
37 | [Item("Traveling Salesman Problem", "Represents a symmetric Traveling Salesman Problem.")]
|
---|
38 | [Creatable("Problems")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class TravelingSalesmanProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
|
---|
41 | public string Filename { get; set; }
|
---|
42 |
|
---|
43 | public override Image ItemImage {
|
---|
44 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | #region Parameter Properties
|
---|
48 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
49 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
50 | }
|
---|
51 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
52 | get { return MaximizationParameter; }
|
---|
53 | }
|
---|
54 | public ValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
55 | get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
56 | }
|
---|
57 | public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
58 | get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
59 | }
|
---|
60 | public ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
61 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
62 | }
|
---|
63 | public ValueParameter<IPermutationCreator> SolutionCreatorParameter {
|
---|
64 | get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; }
|
---|
65 | }
|
---|
66 | IParameter IProblem.SolutionCreatorParameter {
|
---|
67 | get { return SolutionCreatorParameter; }
|
---|
68 | }
|
---|
69 | public ValueParameter<ITSPEvaluator> EvaluatorParameter {
|
---|
70 | get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
|
---|
71 | }
|
---|
72 | IParameter IProblem.EvaluatorParameter {
|
---|
73 | get { return EvaluatorParameter; }
|
---|
74 | }
|
---|
75 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
76 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
77 | }
|
---|
78 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
79 | get { return BestKnownQualityParameter; }
|
---|
80 | }
|
---|
81 | public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
82 | get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
83 | }
|
---|
84 | #endregion
|
---|
85 |
|
---|
86 | #region Properties
|
---|
87 | public DoubleMatrix Coordinates {
|
---|
88 | get { return CoordinatesParameter.Value; }
|
---|
89 | set { CoordinatesParameter.Value = value; }
|
---|
90 | }
|
---|
91 | public DistanceMatrix DistanceMatrix {
|
---|
92 | get { return DistanceMatrixParameter.Value; }
|
---|
93 | set { DistanceMatrixParameter.Value = value; }
|
---|
94 | }
|
---|
95 | public BoolValue UseDistanceMatrix {
|
---|
96 | get { return UseDistanceMatrixParameter.Value; }
|
---|
97 | set { UseDistanceMatrixParameter.Value = value; }
|
---|
98 | }
|
---|
99 | public IPermutationCreator SolutionCreator {
|
---|
100 | get { return SolutionCreatorParameter.Value; }
|
---|
101 | set { SolutionCreatorParameter.Value = value; }
|
---|
102 | }
|
---|
103 | ISolutionCreator IProblem.SolutionCreator {
|
---|
104 | get { return SolutionCreatorParameter.Value; }
|
---|
105 | }
|
---|
106 | public ITSPEvaluator Evaluator {
|
---|
107 | get { return EvaluatorParameter.Value; }
|
---|
108 | set { EvaluatorParameter.Value = value; }
|
---|
109 | }
|
---|
110 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
111 | get { return EvaluatorParameter.Value; }
|
---|
112 | }
|
---|
113 | IEvaluator IProblem.Evaluator {
|
---|
114 | get { return EvaluatorParameter.Value; }
|
---|
115 | }
|
---|
116 | public DoubleValue BestKnownQuality {
|
---|
117 | get { return BestKnownQualityParameter.Value; }
|
---|
118 | set { BestKnownQualityParameter.Value = value; }
|
---|
119 | }
|
---|
120 | public Permutation BestKnownSolution {
|
---|
121 | get { return BestKnownSolutionParameter.Value; }
|
---|
122 | set { BestKnownSolutionParameter.Value = value; }
|
---|
123 | }
|
---|
124 | public IEnumerable<IOperator> Operators {
|
---|
125 | get { return operators; }
|
---|
126 | }
|
---|
127 | private BestTSPSolutionAnalyzer BestTSPSolutionAnalyzer {
|
---|
128 | get { return operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
|
---|
129 | }
|
---|
130 | private TSPAlleleFrequencyAnalyzer TSPAlleleFrequencyAnalyzer {
|
---|
131 | get { return operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
|
---|
132 | }
|
---|
133 | private TSPPopulationDiversityAnalyzer TSPPopulationDiversityAnalyzer {
|
---|
134 | get { return operators.OfType<TSPPopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
135 | }
|
---|
136 | #endregion
|
---|
137 |
|
---|
138 | [Storable]
|
---|
139 | private List<IOperator> operators;
|
---|
140 |
|
---|
141 | [StorableConstructor]
|
---|
142 | private TravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
|
---|
143 | private TravelingSalesmanProblem(TravelingSalesmanProblem original, Cloner cloner)
|
---|
144 | : base(original, cloner) {
|
---|
145 | this.operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
|
---|
146 | AttachEventHandlers();
|
---|
147 | }
|
---|
148 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
149 | return new TravelingSalesmanProblem(this, cloner);
|
---|
150 | }
|
---|
151 | public TravelingSalesmanProblem()
|
---|
152 | : base() {
|
---|
153 | RandomPermutationCreator creator = new RandomPermutationCreator();
|
---|
154 | TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
155 |
|
---|
156 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
|
---|
157 | Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
158 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
159 | Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
|
---|
160 | Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
|
---|
161 | Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
|
---|
162 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
|
---|
163 | Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
|
---|
164 |
|
---|
165 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
166 |
|
---|
167 | Coordinates = new DoubleMatrix(new double[,] {
|
---|
168 | { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
|
---|
169 | { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
|
---|
170 | { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
|
---|
171 | { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
|
---|
172 | });
|
---|
173 |
|
---|
174 | creator.PermutationParameter.ActualName = "TSPTour";
|
---|
175 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
176 | ParameterizeSolutionCreator();
|
---|
177 | ParameterizeEvaluator();
|
---|
178 |
|
---|
179 | InitializeOperators();
|
---|
180 | AttachEventHandlers();
|
---|
181 | }
|
---|
182 |
|
---|
183 | #region Events
|
---|
184 | public event EventHandler SolutionCreatorChanged;
|
---|
185 | private void OnSolutionCreatorChanged() {
|
---|
186 | EventHandler handler = SolutionCreatorChanged;
|
---|
187 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
188 | }
|
---|
189 | public event EventHandler EvaluatorChanged;
|
---|
190 | private void OnEvaluatorChanged() {
|
---|
191 | EventHandler handler = EvaluatorChanged;
|
---|
192 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
193 | }
|
---|
194 | public event EventHandler OperatorsChanged;
|
---|
195 | private void OnOperatorsChanged() {
|
---|
196 | EventHandler handler = OperatorsChanged;
|
---|
197 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
198 | }
|
---|
199 | public event EventHandler Reset;
|
---|
200 | private void OnReset() {
|
---|
201 | EventHandler handler = Reset;
|
---|
202 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
206 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
207 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
208 | ParameterizeSolutionCreator();
|
---|
209 | ClearDistanceMatrix();
|
---|
210 | }
|
---|
211 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
212 | ClearDistanceMatrix();
|
---|
213 | }
|
---|
214 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
215 | ParameterizeSolutionCreator();
|
---|
216 | ClearDistanceMatrix();
|
---|
217 | }
|
---|
218 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
219 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
220 | ParameterizeSolutionCreator();
|
---|
221 | ParameterizeEvaluator();
|
---|
222 | ParameterizeAnalyzers();
|
---|
223 | ParameterizeOperators();
|
---|
224 | OnSolutionCreatorChanged();
|
---|
225 | }
|
---|
226 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
227 | ParameterizeEvaluator();
|
---|
228 | ParameterizeAnalyzers();
|
---|
229 | ParameterizeOperators();
|
---|
230 | }
|
---|
231 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
232 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
233 | ParameterizeEvaluator();
|
---|
234 | UpdateMoveEvaluators();
|
---|
235 | ParameterizeAnalyzers();
|
---|
236 | ClearDistanceMatrix();
|
---|
237 | OnEvaluatorChanged();
|
---|
238 | }
|
---|
239 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
240 | ParameterizeAnalyzers();
|
---|
241 | }
|
---|
242 | private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
243 | string name = ((ILookupParameter<InversionMove>)sender).ActualName;
|
---|
244 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
|
---|
245 | op.InversionMoveParameter.ActualName = name;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | private void MoveGenerator_TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
249 | string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
|
---|
250 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
|
---|
251 | op.TranslocationMoveParameter.ActualName = name;
|
---|
252 | }
|
---|
253 | }
|
---|
254 | #endregion
|
---|
255 |
|
---|
256 | #region Helpers
|
---|
257 | [StorableHook(HookType.AfterDeserialization)]
|
---|
258 | private void AfterDeserialization() {
|
---|
259 | // BackwardsCompatibility3.3
|
---|
260 | #region Backwards compatible code (remove with 3.4)
|
---|
261 | OptionalValueParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as OptionalValueParameter<DoubleMatrix>;
|
---|
262 | if (oldDistanceMatrixParameter != null) {
|
---|
263 | Parameters.Remove(oldDistanceMatrixParameter);
|
---|
264 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
265 | DistanceMatrixParameter.GetsCollected = oldDistanceMatrixParameter.GetsCollected;
|
---|
266 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
267 | if (oldDistanceMatrixParameter.Value != null) {
|
---|
268 | DoubleMatrix oldDM = oldDistanceMatrixParameter.Value;
|
---|
269 | DistanceMatrix newDM = new DistanceMatrix(oldDM.Rows, oldDM.Columns, oldDM.ColumnNames, oldDM.RowNames);
|
---|
270 | newDM.SortableView = oldDM.SortableView;
|
---|
271 | for (int i = 0; i < newDM.Rows; i++)
|
---|
272 | for (int j = 0; j < newDM.Columns; j++)
|
---|
273 | newDM[i, j] = oldDM[i, j];
|
---|
274 | DistanceMatrixParameter.Value = (DistanceMatrix)newDM.AsReadOnly();
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | if (operators == null) InitializeOperators();
|
---|
279 | #endregion
|
---|
280 | AttachEventHandlers();
|
---|
281 | }
|
---|
282 |
|
---|
283 | private void AttachEventHandlers() {
|
---|
284 | CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
|
---|
285 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
286 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
287 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
288 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
289 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
290 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
291 | }
|
---|
292 |
|
---|
293 | private void InitializeOperators() {
|
---|
294 | operators = new List<IOperator>();
|
---|
295 | operators.Add(new BestTSPSolutionAnalyzer());
|
---|
296 | operators.Add(new TSPAlleleFrequencyAnalyzer());
|
---|
297 | operators.Add(new TSPPopulationDiversityAnalyzer());
|
---|
298 | ParameterizeAnalyzers();
|
---|
299 | operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
|
---|
300 | ParameterizeOperators();
|
---|
301 | UpdateMoveEvaluators();
|
---|
302 | InitializeMoveGenerators();
|
---|
303 | }
|
---|
304 | private void InitializeMoveGenerators() {
|
---|
305 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
|
---|
306 | if (op is IMoveGenerator) {
|
---|
307 | op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);
|
---|
308 | }
|
---|
309 | }
|
---|
310 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
|
---|
311 | if (op is IMoveGenerator) {
|
---|
312 | op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 | private void UpdateMoveEvaluators() {
|
---|
317 | operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
318 | foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
|
---|
319 | if (op.EvaluatorType == Evaluator.GetType()) {
|
---|
320 | operators.Add(op);
|
---|
321 | }
|
---|
322 | ParameterizeOperators();
|
---|
323 | OnOperatorsChanged();
|
---|
324 | }
|
---|
325 | private void ParameterizeSolutionCreator() {
|
---|
326 | SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
|
---|
327 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
|
---|
328 | }
|
---|
329 | private void ParameterizeEvaluator() {
|
---|
330 | if (Evaluator is ITSPPathEvaluator)
|
---|
331 | ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
332 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
333 | ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
|
---|
334 | evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
335 | evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
336 | evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
337 | }
|
---|
338 | }
|
---|
339 | private void ParameterizeAnalyzers() {
|
---|
340 | if (BestTSPSolutionAnalyzer != null) {
|
---|
341 | BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
342 | BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
343 | BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
344 | BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
345 | BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
346 | BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
347 | BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (TSPAlleleFrequencyAnalyzer != null) {
|
---|
351 | TSPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
352 | TSPAlleleFrequencyAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
353 | TSPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
354 | TSPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
355 | TSPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
356 | TSPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
357 | }
|
---|
358 |
|
---|
359 | if (TSPPopulationDiversityAnalyzer != null) {
|
---|
360 | TSPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
361 | TSPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
362 | TSPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
363 | TSPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
364 | }
|
---|
365 | }
|
---|
366 | private void ParameterizeOperators() {
|
---|
367 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
368 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
369 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
370 | }
|
---|
371 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
372 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
373 | }
|
---|
374 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
375 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
376 | }
|
---|
377 | foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
|
---|
378 | op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
379 | op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
380 | op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
381 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
382 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
383 | }
|
---|
384 | string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
|
---|
385 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
|
---|
386 | op.InversionMoveParameter.ActualName = inversionMove;
|
---|
387 | string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
|
---|
388 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
|
---|
389 | op.TranslocationMoveParameter.ActualName = translocationMove;
|
---|
390 | }
|
---|
391 |
|
---|
392 | private void ClearDistanceMatrix() {
|
---|
393 | DistanceMatrixParameter.Value = null;
|
---|
394 | }
|
---|
395 | #endregion
|
---|
396 |
|
---|
397 | public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName) {
|
---|
398 | TSPLIBParser tspParser = new TSPLIBParser(tspFileName);
|
---|
399 | tspParser.Parse();
|
---|
400 | Name = tspParser.Name + " TSP (imported from TSPLIB)";
|
---|
401 | if (!string.IsNullOrEmpty(tspParser.Comment)) Description = tspParser.Comment;
|
---|
402 | Coordinates = new DoubleMatrix(tspParser.Vertices);
|
---|
403 | if (tspParser.WeightType == TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D) {
|
---|
404 | TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
405 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
406 | Evaluator = evaluator;
|
---|
407 | } else if (tspParser.WeightType == TSPLIBParser.TSPLIBEdgeWeightType.GEO) {
|
---|
408 | TSPGeoPathEvaluator evaluator = new TSPGeoPathEvaluator();
|
---|
409 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
410 | Evaluator = evaluator;
|
---|
411 | }
|
---|
412 | BestKnownQuality = null;
|
---|
413 | BestKnownSolution = null;
|
---|
414 |
|
---|
415 | if (!string.IsNullOrEmpty(optimalTourFileName)) {
|
---|
416 | TSPLIBTourParser tourParser = new TSPLIBTourParser(optimalTourFileName);
|
---|
417 | tourParser.Parse();
|
---|
418 | if (tourParser.Tour.Length != Coordinates.Rows) throw new InvalidDataException("Length of optimal tour is not equal to number of cities.");
|
---|
419 | BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, tourParser.Tour);
|
---|
420 | }
|
---|
421 | OnReset();
|
---|
422 | }
|
---|
423 | public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName, double bestKnownQuality) {
|
---|
424 | ImportFromTSPLIB(tspFileName, optimalTourFileName);
|
---|
425 | BestKnownQuality = new DoubleValue(bestKnownQuality);
|
---|
426 | }
|
---|
427 | }
|
---|
428 | }
|
---|