1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
41 | public string Filename { get; set; }
|
---|
42 |
|
---|
43 | public override Image ItemImage {
|
---|
44 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | #region Parameter Properties
|
---|
48 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
49 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
50 | }
|
---|
51 | IParameter ISingleObjectiveHeuristicOptimizationProblem.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 IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
67 | get { return SolutionCreatorParameter; }
|
---|
68 | }
|
---|
69 | public ValueParameter<ITSPEvaluator> EvaluatorParameter {
|
---|
70 | get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
|
---|
71 | }
|
---|
72 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
73 | get { return EvaluatorParameter; }
|
---|
74 | }
|
---|
75 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
76 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
77 | }
|
---|
78 | IParameter ISingleObjectiveHeuristicOptimizationProblem.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 IHeuristicOptimizationProblem.SolutionCreator {
|
---|
104 | get { return SolutionCreatorParameter.Value; }
|
---|
105 | }
|
---|
106 | public ITSPEvaluator Evaluator {
|
---|
107 | get { return EvaluatorParameter.Value; }
|
---|
108 | set { EvaluatorParameter.Value = value; }
|
---|
109 | }
|
---|
110 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
111 | get { return EvaluatorParameter.Value; }
|
---|
112 | }
|
---|
113 | IEvaluator IHeuristicOptimizationProblem.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 | MaximizationParameter.Hidden = true;
|
---|
166 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
167 |
|
---|
168 | Coordinates = new DoubleMatrix(new double[,] {
|
---|
169 | { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
|
---|
170 | { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
|
---|
171 | { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
|
---|
172 | { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
|
---|
173 | });
|
---|
174 |
|
---|
175 | creator.PermutationParameter.ActualName = "TSPTour";
|
---|
176 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
177 | ParameterizeSolutionCreator();
|
---|
178 | ParameterizeEvaluator();
|
---|
179 |
|
---|
180 | InitializeOperators();
|
---|
181 | AttachEventHandlers();
|
---|
182 | }
|
---|
183 |
|
---|
184 | #region Events
|
---|
185 | public event EventHandler SolutionCreatorChanged;
|
---|
186 | private void OnSolutionCreatorChanged() {
|
---|
187 | EventHandler handler = SolutionCreatorChanged;
|
---|
188 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
189 | }
|
---|
190 | public event EventHandler EvaluatorChanged;
|
---|
191 | private void OnEvaluatorChanged() {
|
---|
192 | EventHandler handler = EvaluatorChanged;
|
---|
193 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
194 | }
|
---|
195 | public event EventHandler OperatorsChanged;
|
---|
196 | private void OnOperatorsChanged() {
|
---|
197 | EventHandler handler = OperatorsChanged;
|
---|
198 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
199 | }
|
---|
200 | public event EventHandler Reset;
|
---|
201 | private void OnReset() {
|
---|
202 | EventHandler handler = Reset;
|
---|
203 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
204 | }
|
---|
205 |
|
---|
206 | private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
207 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
208 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
209 | ParameterizeSolutionCreator();
|
---|
210 | ClearDistanceMatrix();
|
---|
211 | }
|
---|
212 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
213 | ClearDistanceMatrix();
|
---|
214 | }
|
---|
215 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
216 | ParameterizeSolutionCreator();
|
---|
217 | ClearDistanceMatrix();
|
---|
218 | }
|
---|
219 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
220 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
221 | ParameterizeSolutionCreator();
|
---|
222 | ParameterizeEvaluator();
|
---|
223 | ParameterizeAnalyzers();
|
---|
224 | ParameterizeOperators();
|
---|
225 | OnSolutionCreatorChanged();
|
---|
226 | }
|
---|
227 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
228 | ParameterizeEvaluator();
|
---|
229 | ParameterizeAnalyzers();
|
---|
230 | ParameterizeOperators();
|
---|
231 | }
|
---|
232 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
233 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
234 | ParameterizeEvaluator();
|
---|
235 | UpdateMoveEvaluators();
|
---|
236 | ParameterizeAnalyzers();
|
---|
237 | ClearDistanceMatrix();
|
---|
238 | OnEvaluatorChanged();
|
---|
239 | }
|
---|
240 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
241 | ParameterizeAnalyzers();
|
---|
242 | }
|
---|
243 | private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
244 | string name = ((ILookupParameter<InversionMove>)sender).ActualName;
|
---|
245 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
|
---|
246 | op.InversionMoveParameter.ActualName = name;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | private void MoveGenerator_TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
250 | string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
|
---|
251 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
|
---|
252 | op.TranslocationMoveParameter.ActualName = name;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | #endregion
|
---|
256 |
|
---|
257 | #region Helpers
|
---|
258 | [StorableHook(HookType.AfterDeserialization)]
|
---|
259 | private void AfterDeserialization() {
|
---|
260 | // BackwardsCompatibility3.3
|
---|
261 | #region Backwards compatible code (remove with 3.4)
|
---|
262 | OptionalValueParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as OptionalValueParameter<DoubleMatrix>;
|
---|
263 | if (oldDistanceMatrixParameter != null) {
|
---|
264 | Parameters.Remove(oldDistanceMatrixParameter);
|
---|
265 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
266 | DistanceMatrixParameter.GetsCollected = oldDistanceMatrixParameter.GetsCollected;
|
---|
267 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
268 | if (oldDistanceMatrixParameter.Value != null) {
|
---|
269 | DoubleMatrix oldDM = oldDistanceMatrixParameter.Value;
|
---|
270 | DistanceMatrix newDM = new DistanceMatrix(oldDM.Rows, oldDM.Columns, oldDM.ColumnNames, oldDM.RowNames);
|
---|
271 | newDM.SortableView = oldDM.SortableView;
|
---|
272 | for (int i = 0; i < newDM.Rows; i++)
|
---|
273 | for (int j = 0; j < newDM.Columns; j++)
|
---|
274 | newDM[i, j] = oldDM[i, j];
|
---|
275 | DistanceMatrixParameter.Value = (DistanceMatrix)newDM.AsReadOnly();
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (operators == null) InitializeOperators();
|
---|
280 | #endregion
|
---|
281 | AttachEventHandlers();
|
---|
282 | }
|
---|
283 |
|
---|
284 | private void AttachEventHandlers() {
|
---|
285 | CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
|
---|
286 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
287 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
288 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
289 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
290 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
291 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
292 | }
|
---|
293 |
|
---|
294 | private void InitializeOperators() {
|
---|
295 | operators = new List<IOperator>();
|
---|
296 | operators.Add(new BestTSPSolutionAnalyzer());
|
---|
297 | operators.Add(new TSPAlleleFrequencyAnalyzer());
|
---|
298 | operators.Add(new TSPPopulationDiversityAnalyzer());
|
---|
299 | ParameterizeAnalyzers();
|
---|
300 | operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
|
---|
301 | ParameterizeOperators();
|
---|
302 | UpdateMoveEvaluators();
|
---|
303 | InitializeMoveGenerators();
|
---|
304 | }
|
---|
305 | private void InitializeMoveGenerators() {
|
---|
306 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
|
---|
307 | if (op is IMoveGenerator) {
|
---|
308 | op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);
|
---|
309 | }
|
---|
310 | }
|
---|
311 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
|
---|
312 | if (op is IMoveGenerator) {
|
---|
313 | op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);
|
---|
314 | }
|
---|
315 | }
|
---|
316 | }
|
---|
317 | private void UpdateMoveEvaluators() {
|
---|
318 | operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
319 | foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
|
---|
320 | if (op.EvaluatorType == Evaluator.GetType()) {
|
---|
321 | operators.Add(op);
|
---|
322 | }
|
---|
323 | ParameterizeOperators();
|
---|
324 | OnOperatorsChanged();
|
---|
325 | }
|
---|
326 | private void ParameterizeSolutionCreator() {
|
---|
327 | SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
|
---|
328 | SolutionCreator.LengthParameter.Hidden = true;
|
---|
329 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
|
---|
330 | SolutionCreator.PermutationTypeParameter.Hidden = true;
|
---|
331 | }
|
---|
332 | private void ParameterizeEvaluator() {
|
---|
333 | if (Evaluator is ITSPPathEvaluator) {
|
---|
334 | ITSPPathEvaluator evaluator = (ITSPPathEvaluator)Evaluator;
|
---|
335 | evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
336 | evaluator.PermutationParameter.Hidden = true;
|
---|
337 | }
|
---|
338 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
339 | ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
|
---|
340 | evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
341 | evaluator.CoordinatesParameter.Hidden = true;
|
---|
342 | evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
343 | evaluator.DistanceMatrixParameter.Hidden = true;
|
---|
344 | evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
345 | evaluator.UseDistanceMatrixParameter.Hidden = true;
|
---|
346 | }
|
---|
347 | }
|
---|
348 | private void ParameterizeAnalyzers() {
|
---|
349 | if (BestTSPSolutionAnalyzer != null) {
|
---|
350 | BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
351 | BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
352 | BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
353 | BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
354 | BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
355 | BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
356 | BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
357 | }
|
---|
358 |
|
---|
359 | if (TSPAlleleFrequencyAnalyzer != null) {
|
---|
360 | TSPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
361 | TSPAlleleFrequencyAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
362 | TSPAlleleFrequencyAnalyzer.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
363 | TSPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
364 | TSPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
365 | TSPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
366 | TSPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
367 | }
|
---|
368 |
|
---|
369 | if (TSPPopulationDiversityAnalyzer != null) {
|
---|
370 | TSPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
371 | TSPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
372 | TSPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
373 | TSPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
374 | }
|
---|
375 | }
|
---|
376 | private void ParameterizeOperators() {
|
---|
377 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
378 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
379 | op.ParentsParameter.Hidden = true;
|
---|
380 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
381 | op.ChildParameter.Hidden = true;
|
---|
382 | }
|
---|
383 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
384 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
385 | op.PermutationParameter.Hidden = true;
|
---|
386 | }
|
---|
387 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
388 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
389 | op.PermutationParameter.Hidden = true;
|
---|
390 | }
|
---|
391 | foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
|
---|
392 | op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
393 | op.CoordinatesParameter.Hidden = true;
|
---|
394 | op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
395 | op.DistanceMatrixParameter.Hidden = true;
|
---|
396 | op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
397 | op.UseDistanceMatrixParameter.Hidden = true;
|
---|
398 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
399 | op.QualityParameter.Hidden = true;
|
---|
400 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
401 | op.PermutationParameter.Hidden = true;
|
---|
402 | }
|
---|
403 | string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
|
---|
404 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
|
---|
405 | op.InversionMoveParameter.ActualName = inversionMove;
|
---|
406 | op.InversionMoveParameter.Hidden = true;
|
---|
407 | }
|
---|
408 | string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
|
---|
409 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
|
---|
410 | op.TranslocationMoveParameter.ActualName = translocationMove;
|
---|
411 | op.TranslocationMoveParameter.Hidden = true;
|
---|
412 | }
|
---|
413 | foreach (IPermutationMultiNeighborhoodShakingOperator op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
|
---|
414 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
415 | op.PermutationParameter.Hidden = true;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | private void ClearDistanceMatrix() {
|
---|
420 | DistanceMatrixParameter.Value = null;
|
---|
421 | }
|
---|
422 | #endregion
|
---|
423 |
|
---|
424 | public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName) {
|
---|
425 | TSPLIBParser tspParser = new TSPLIBParser(tspFileName);
|
---|
426 | tspParser.Parse();
|
---|
427 | Name = tspParser.Name + " TSP (imported from TSPLIB)";
|
---|
428 | if (!string.IsNullOrEmpty(tspParser.Comment)) Description = tspParser.Comment;
|
---|
429 | Coordinates = new DoubleMatrix(tspParser.Vertices);
|
---|
430 | if (tspParser.WeightType == TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D) {
|
---|
431 | TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
432 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
433 | Evaluator = evaluator;
|
---|
434 | } else if (tspParser.WeightType == TSPLIBParser.TSPLIBEdgeWeightType.GEO) {
|
---|
435 | TSPGeoPathEvaluator evaluator = new TSPGeoPathEvaluator();
|
---|
436 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
437 | Evaluator = evaluator;
|
---|
438 | }
|
---|
439 | BestKnownQuality = null;
|
---|
440 | BestKnownSolution = null;
|
---|
441 |
|
---|
442 | if (!string.IsNullOrEmpty(optimalTourFileName)) {
|
---|
443 | TSPLIBTourParser tourParser = new TSPLIBTourParser(optimalTourFileName);
|
---|
444 | tourParser.Parse();
|
---|
445 | if (tourParser.Tour.Length != Coordinates.Rows) throw new InvalidDataException("Length of optimal tour is not equal to number of cities.");
|
---|
446 | BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, tourParser.Tour);
|
---|
447 | }
|
---|
448 | OnReset();
|
---|
449 | }
|
---|
450 | public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName, double bestKnownQuality) {
|
---|
451 | ImportFromTSPLIB(tspFileName, optimalTourFileName);
|
---|
452 | BestKnownQuality = new DoubleValue(bestKnownQuality);
|
---|
453 | }
|
---|
454 | }
|
---|
455 | }
|
---|