[2796] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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;
|
---|
[2865] | 24 | using System.Drawing;
|
---|
[3153] | 25 | using System.IO;
|
---|
[2865] | 26 | using System.Linq;
|
---|
[2975] | 27 | using HeuristicLab.Common;
|
---|
[2796] | 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
[3053] | 30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[2834] | 31 | using HeuristicLab.Optimization;
|
---|
[2805] | 32 | using HeuristicLab.Parameters;
|
---|
[2796] | 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2865] | 34 | using HeuristicLab.PluginInfrastructure;
|
---|
[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]
|
---|
[4419] | 40 | public sealed class TravelingSalesmanProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
|
---|
| 41 | public string Filename { get; set; }
|
---|
| 42 |
|
---|
[2865] | 43 | public override Image ItemImage {
|
---|
[5287] | 44 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
[2805] | 45 | }
|
---|
| 46 |
|
---|
[2975] | 47 | #region Parameter Properties
|
---|
[3048] | 48 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
| 49 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[2805] | 50 | }
|
---|
[2975] | 51 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
| 52 | get { return MaximizationParameter; }
|
---|
[2865] | 53 | }
|
---|
[3048] | 54 | public ValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 55 | get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
[2865] | 56 | }
|
---|
[4825] | 57 | public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
| 58 | get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
[3066] | 59 | }
|
---|
| 60 | public ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
| 61 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
| 62 | }
|
---|
[2975] | 63 | public ValueParameter<IPermutationCreator> SolutionCreatorParameter {
|
---|
| 64 | get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; }
|
---|
[2865] | 65 | }
|
---|
[2975] | 66 | IParameter IProblem.SolutionCreatorParameter {
|
---|
| 67 | get { return SolutionCreatorParameter; }
|
---|
[2865] | 68 | }
|
---|
[2975] | 69 | public ValueParameter<ITSPEvaluator> EvaluatorParameter {
|
---|
| 70 | get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
|
---|
| 71 | }
|
---|
| 72 | IParameter IProblem.EvaluatorParameter {
|
---|
| 73 | get { return EvaluatorParameter; }
|
---|
| 74 | }
|
---|
[3048] | 75 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 76 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
[2975] | 77 | }
|
---|
[3080] | 78 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
| 79 | get { return BestKnownQualityParameter; }
|
---|
| 80 | }
|
---|
[3151] | 81 | public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
|
---|
| 82 | get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
|
---|
| 83 | }
|
---|
[2975] | 84 | #endregion
|
---|
[2805] | 85 |
|
---|
[2975] | 86 | #region Properties
|
---|
[3048] | 87 | public DoubleMatrix Coordinates {
|
---|
[2975] | 88 | get { return CoordinatesParameter.Value; }
|
---|
| 89 | set { CoordinatesParameter.Value = value; }
|
---|
[2865] | 90 | }
|
---|
[4825] | 91 | public DistanceMatrix DistanceMatrix {
|
---|
[3066] | 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 | }
|
---|
[2975] | 99 | public IPermutationCreator SolutionCreator {
|
---|
[2865] | 100 | get { return SolutionCreatorParameter.Value; }
|
---|
[2975] | 101 | set { SolutionCreatorParameter.Value = value; }
|
---|
[2865] | 102 | }
|
---|
[2975] | 103 | ISolutionCreator IProblem.SolutionCreator {
|
---|
| 104 | get { return SolutionCreatorParameter.Value; }
|
---|
| 105 | }
|
---|
| 106 | public ITSPEvaluator Evaluator {
|
---|
[2865] | 107 | get { return EvaluatorParameter.Value; }
|
---|
[2975] | 108 | set { EvaluatorParameter.Value = value; }
|
---|
[2865] | 109 | }
|
---|
[2975] | 110 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
| 111 | get { return EvaluatorParameter.Value; }
|
---|
| 112 | }
|
---|
[2865] | 113 | IEvaluator IProblem.Evaluator {
|
---|
| 114 | get { return EvaluatorParameter.Value; }
|
---|
| 115 | }
|
---|
[3048] | 116 | public DoubleValue BestKnownQuality {
|
---|
[2975] | 117 | get { return BestKnownQualityParameter.Value; }
|
---|
| 118 | set { BestKnownQualityParameter.Value = value; }
|
---|
| 119 | }
|
---|
[3151] | 120 | public Permutation BestKnownSolution {
|
---|
| 121 | get { return BestKnownSolutionParameter.Value; }
|
---|
| 122 | set { BestKnownSolutionParameter.Value = value; }
|
---|
| 123 | }
|
---|
[2975] | 124 | public IEnumerable<IOperator> Operators {
|
---|
[3616] | 125 | get { return operators; }
|
---|
[2865] | 126 | }
|
---|
[3663] | 127 | private BestTSPSolutionAnalyzer BestTSPSolutionAnalyzer {
|
---|
| 128 | get { return operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
|
---|
[3616] | 129 | }
|
---|
[4623] | 130 | private TSPAlleleFrequencyAnalyzer TSPAlleleFrequencyAnalyzer {
|
---|
| 131 | get { return operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
|
---|
| 132 | }
|
---|
[4703] | 133 | private TSPPopulationDiversityAnalyzer TSPPopulationDiversityAnalyzer {
|
---|
| 134 | get { return operators.OfType<TSPPopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
| 135 | }
|
---|
[2975] | 136 | #endregion
|
---|
[2865] | 137 |
|
---|
[4098] | 138 | [Storable]
|
---|
| 139 | private List<IOperator> operators;
|
---|
| 140 |
|
---|
| 141 | [StorableConstructor]
|
---|
[4118] | 142 | private TravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 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 | }
|
---|
[3159] | 151 | public TravelingSalesmanProblem()
|
---|
[2796] | 152 | : base() {
|
---|
[2891] | 153 | RandomPermutationCreator creator = new RandomPermutationCreator();
|
---|
| 154 | TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
| 155 |
|
---|
[3048] | 156 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
|
---|
[3504] | 157 | Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
[4825] | 158 | Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
[3066] | 159 | Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
|
---|
[2891] | 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));
|
---|
[3048] | 162 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
|
---|
[3151] | 163 | Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
|
---|
[2865] | 164 |
|
---|
[4825] | 165 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
| 166 |
|
---|
[3504] | 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 |
|
---|
[2865] | 174 | creator.PermutationParameter.ActualName = "TSPTour";
|
---|
| 175 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
[2975] | 176 | ParameterizeSolutionCreator();
|
---|
| 177 | ParameterizeEvaluator();
|
---|
[2865] | 178 |
|
---|
[4098] | 179 | InitializeOperators();
|
---|
| 180 | AttachEventHandlers();
|
---|
[2975] | 181 | }
|
---|
[2891] | 182 |
|
---|
[2975] | 183 | #region Events
|
---|
| 184 | public event EventHandler SolutionCreatorChanged;
|
---|
| 185 | private void OnSolutionCreatorChanged() {
|
---|
[3739] | 186 | EventHandler handler = SolutionCreatorChanged;
|
---|
| 187 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2865] | 188 | }
|
---|
[2975] | 189 | public event EventHandler EvaluatorChanged;
|
---|
| 190 | private void OnEvaluatorChanged() {
|
---|
[3739] | 191 | EventHandler handler = EvaluatorChanged;
|
---|
| 192 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2975] | 193 | }
|
---|
| 194 | public event EventHandler OperatorsChanged;
|
---|
| 195 | private void OnOperatorsChanged() {
|
---|
[3739] | 196 | EventHandler handler = OperatorsChanged;
|
---|
| 197 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2975] | 198 | }
|
---|
[3739] | 199 | public event EventHandler Reset;
|
---|
| 200 | private void OnReset() {
|
---|
| 201 | EventHandler handler = Reset;
|
---|
| 202 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 203 | }
|
---|
[2975] | 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();
|
---|
[3066] | 209 | ClearDistanceMatrix();
|
---|
[2975] | 210 | }
|
---|
| 211 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[3066] | 212 | ClearDistanceMatrix();
|
---|
[2975] | 213 | }
|
---|
| 214 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
| 215 | ParameterizeSolutionCreator();
|
---|
[3066] | 216 | ClearDistanceMatrix();
|
---|
[2975] | 217 | }
|
---|
[2865] | 218 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[2975] | 219 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 220 | ParameterizeSolutionCreator();
|
---|
| 221 | ParameterizeEvaluator();
|
---|
[4623] | 222 | ParameterizeAnalyzers();
|
---|
[2975] | 223 | ParameterizeOperators();
|
---|
[2865] | 224 | OnSolutionCreatorChanged();
|
---|
| 225 | }
|
---|
[2975] | 226 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 227 | ParameterizeEvaluator();
|
---|
[4623] | 228 | ParameterizeAnalyzers();
|
---|
[2975] | 229 | ParameterizeOperators();
|
---|
| 230 | }
|
---|
[2865] | 231 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[3139] | 232 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[2975] | 233 | ParameterizeEvaluator();
|
---|
[3209] | 234 | UpdateMoveEvaluators();
|
---|
[4623] | 235 | ParameterizeAnalyzers();
|
---|
[3066] | 236 | ClearDistanceMatrix();
|
---|
[2865] | 237 | OnEvaluatorChanged();
|
---|
| 238 | }
|
---|
[3139] | 239 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[4623] | 240 | ParameterizeAnalyzers();
|
---|
[3139] | 241 | }
|
---|
[3232] | 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;
|
---|
[3074] | 246 | }
|
---|
| 247 | }
|
---|
[3232] | 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;
|
---|
[3074] | 252 | }
|
---|
| 253 | }
|
---|
[2975] | 254 | #endregion
|
---|
[2865] | 255 |
|
---|
[2975] | 256 | #region Helpers
|
---|
[2986] | 257 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 258 | private void AfterDeserialization() {
|
---|
[4118] | 259 | // BackwardsCompatibility3.3
|
---|
| 260 | #region Backwards compatible code (remove with 3.4)
|
---|
[4825] | 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 |
|
---|
[4118] | 278 | if (operators == null) InitializeOperators();
|
---|
| 279 | #endregion
|
---|
| 280 | AttachEventHandlers();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[4098] | 283 | private void AttachEventHandlers() {
|
---|
[2975] | 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);
|
---|
[3139] | 290 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[2865] | 291 | }
|
---|
[3139] | 292 |
|
---|
[3099] | 293 | private void InitializeOperators() {
|
---|
[3616] | 294 | operators = new List<IOperator>();
|
---|
[3663] | 295 | operators.Add(new BestTSPSolutionAnalyzer());
|
---|
[4623] | 296 | operators.Add(new TSPAlleleFrequencyAnalyzer());
|
---|
[4703] | 297 | operators.Add(new TSPPopulationDiversityAnalyzer());
|
---|
[4623] | 298 | ParameterizeAnalyzers();
|
---|
[3616] | 299 | operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
|
---|
[3199] | 300 | ParameterizeOperators();
|
---|
[3209] | 301 | UpdateMoveEvaluators();
|
---|
[3099] | 302 | InitializeMoveGenerators();
|
---|
| 303 | }
|
---|
| 304 | private void InitializeMoveGenerators() {
|
---|
[3232] | 305 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
|
---|
[3099] | 306 | if (op is IMoveGenerator) {
|
---|
[3232] | 307 | op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);
|
---|
[3099] | 308 | }
|
---|
| 309 | }
|
---|
[3232] | 310 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
|
---|
[3099] | 311 | if (op is IMoveGenerator) {
|
---|
[3232] | 312 | op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);
|
---|
[3099] | 313 | }
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
[3209] | 316 | private void UpdateMoveEvaluators() {
|
---|
[4047] | 317 | operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
[3303] | 318 | foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
|
---|
| 319 | if (op.EvaluatorType == Evaluator.GetType()) {
|
---|
| 320 | operators.Add(op);
|
---|
| 321 | }
|
---|
| 322 | ParameterizeOperators();
|
---|
| 323 | OnOperatorsChanged();
|
---|
[3209] | 324 | }
|
---|
[2975] | 325 | private void ParameterizeSolutionCreator() {
|
---|
[3048] | 326 | SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
|
---|
[3231] | 327 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
|
---|
[2865] | 328 | }
|
---|
[2975] | 329 | private void ParameterizeEvaluator() {
|
---|
| 330 | if (Evaluator is ITSPPathEvaluator)
|
---|
| 331 | ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[3066] | 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 | }
|
---|
[2865] | 338 | }
|
---|
[4623] | 339 | private void ParameterizeAnalyzers() {
|
---|
[4641] | 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 | }
|
---|
[4623] | 349 |
|
---|
[4641] | 350 | if (TSPAlleleFrequencyAnalyzer != null) {
|
---|
| 351 | TSPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 352 | TSPAlleleFrequencyAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[4866] | 353 | TSPAlleleFrequencyAnalyzer.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[4641] | 354 | TSPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 355 | TSPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 356 | TSPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 357 | TSPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 358 | }
|
---|
[4703] | 359 |
|
---|
| 360 | if (TSPPopulationDiversityAnalyzer != null) {
|
---|
| 361 | TSPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 362 | TSPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 363 | TSPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 364 | TSPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 365 | }
|
---|
[3107] | 366 | }
|
---|
[2975] | 367 | private void ParameterizeOperators() {
|
---|
| 368 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
| 369 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 370 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 371 | }
|
---|
| 372 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
| 373 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 374 | }
|
---|
[3074] | 375 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
| 376 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 377 | }
|
---|
| 378 | foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
|
---|
| 379 | op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
| 380 | op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
| 381 | op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
[3209] | 382 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 383 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[3074] | 384 | }
|
---|
[3340] | 385 | string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
|
---|
| 386 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
|
---|
| 387 | op.InversionMoveParameter.ActualName = inversionMove;
|
---|
| 388 | string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
|
---|
| 389 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
|
---|
| 390 | op.TranslocationMoveParameter.ActualName = translocationMove;
|
---|
[2975] | 391 | }
|
---|
[3074] | 392 |
|
---|
[3066] | 393 | private void ClearDistanceMatrix() {
|
---|
| 394 | DistanceMatrixParameter.Value = null;
|
---|
| 395 | }
|
---|
[2975] | 396 | #endregion
|
---|
[4098] | 397 |
|
---|
| 398 | public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName) {
|
---|
| 399 | TSPLIBParser tspParser = new TSPLIBParser(tspFileName);
|
---|
| 400 | tspParser.Parse();
|
---|
| 401 | Name = tspParser.Name + " TSP (imported from TSPLIB)";
|
---|
| 402 | if (!string.IsNullOrEmpty(tspParser.Comment)) Description = tspParser.Comment;
|
---|
| 403 | Coordinates = new DoubleMatrix(tspParser.Vertices);
|
---|
| 404 | if (tspParser.WeightType == TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D) {
|
---|
| 405 | TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
| 406 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
| 407 | Evaluator = evaluator;
|
---|
| 408 | } else if (tspParser.WeightType == TSPLIBParser.TSPLIBEdgeWeightType.GEO) {
|
---|
| 409 | TSPGeoPathEvaluator evaluator = new TSPGeoPathEvaluator();
|
---|
| 410 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
| 411 | Evaluator = evaluator;
|
---|
| 412 | }
|
---|
| 413 | BestKnownQuality = null;
|
---|
| 414 | BestKnownSolution = null;
|
---|
| 415 |
|
---|
| 416 | if (!string.IsNullOrEmpty(optimalTourFileName)) {
|
---|
| 417 | TSPLIBTourParser tourParser = new TSPLIBTourParser(optimalTourFileName);
|
---|
| 418 | tourParser.Parse();
|
---|
| 419 | if (tourParser.Tour.Length != Coordinates.Rows) throw new InvalidDataException("Length of optimal tour is not equal to number of cities.");
|
---|
| 420 | BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, tourParser.Tour);
|
---|
| 421 | }
|
---|
| 422 | OnReset();
|
---|
| 423 | }
|
---|
| 424 | public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName, double bestKnownQuality) {
|
---|
| 425 | ImportFromTSPLIB(tspFileName, optimalTourFileName);
|
---|
| 426 | BestKnownQuality = new DoubleValue(bestKnownQuality);
|
---|
| 427 | }
|
---|
[2796] | 428 | }
|
---|
| 429 | }
|
---|