[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]
|
---|
[5809] | 40 | public sealed class TravelingSalesmanProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
[4419] | 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 | }
|
---|
[5809] | 51 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
[2975] | 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 | }
|
---|
[5809] | 66 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
[2975] | 67 | get { return SolutionCreatorParameter; }
|
---|
[2865] | 68 | }
|
---|
[2975] | 69 | public ValueParameter<ITSPEvaluator> EvaluatorParameter {
|
---|
| 70 | get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
|
---|
| 71 | }
|
---|
[5809] | 72 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
[2975] | 73 | get { return EvaluatorParameter; }
|
---|
| 74 | }
|
---|
[3048] | 75 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 76 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
[2975] | 77 | }
|
---|
[5809] | 78 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
[3080] | 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 | }
|
---|
[5809] | 103 | ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
|
---|
[2975] | 104 | get { return SolutionCreatorParameter.Value; }
|
---|
| 105 | }
|
---|
| 106 | public ITSPEvaluator Evaluator {
|
---|
[2865] | 107 | get { return EvaluatorParameter.Value; }
|
---|
[2975] | 108 | set { EvaluatorParameter.Value = value; }
|
---|
[2865] | 109 | }
|
---|
[5809] | 110 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
[2975] | 111 | get { return EvaluatorParameter.Value; }
|
---|
| 112 | }
|
---|
[5809] | 113 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
[2865] | 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 |
|
---|
[6051] | 165 | MaximizationParameter.Hidden = true;
|
---|
[4825] | 166 | DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
| 167 |
|
---|
[3504] | 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 |
|
---|
[2865] | 175 | creator.PermutationParameter.ActualName = "TSPTour";
|
---|
| 176 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
[2975] | 177 | ParameterizeSolutionCreator();
|
---|
| 178 | ParameterizeEvaluator();
|
---|
[2865] | 179 |
|
---|
[4098] | 180 | InitializeOperators();
|
---|
| 181 | AttachEventHandlers();
|
---|
[2975] | 182 | }
|
---|
[2891] | 183 |
|
---|
[2975] | 184 | #region Events
|
---|
| 185 | public event EventHandler SolutionCreatorChanged;
|
---|
| 186 | private void OnSolutionCreatorChanged() {
|
---|
[3739] | 187 | EventHandler handler = SolutionCreatorChanged;
|
---|
| 188 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2865] | 189 | }
|
---|
[2975] | 190 | public event EventHandler EvaluatorChanged;
|
---|
| 191 | private void OnEvaluatorChanged() {
|
---|
[3739] | 192 | EventHandler handler = EvaluatorChanged;
|
---|
| 193 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2975] | 194 | }
|
---|
| 195 | public event EventHandler OperatorsChanged;
|
---|
| 196 | private void OnOperatorsChanged() {
|
---|
[3739] | 197 | EventHandler handler = OperatorsChanged;
|
---|
| 198 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2975] | 199 | }
|
---|
[3739] | 200 | public event EventHandler Reset;
|
---|
| 201 | private void OnReset() {
|
---|
| 202 | EventHandler handler = Reset;
|
---|
| 203 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 204 | }
|
---|
[2975] | 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();
|
---|
[3066] | 210 | ClearDistanceMatrix();
|
---|
[2975] | 211 | }
|
---|
| 212 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[3066] | 213 | ClearDistanceMatrix();
|
---|
[2975] | 214 | }
|
---|
| 215 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
| 216 | ParameterizeSolutionCreator();
|
---|
[3066] | 217 | ClearDistanceMatrix();
|
---|
[2975] | 218 | }
|
---|
[2865] | 219 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[2975] | 220 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 221 | ParameterizeSolutionCreator();
|
---|
| 222 | ParameterizeEvaluator();
|
---|
[4623] | 223 | ParameterizeAnalyzers();
|
---|
[2975] | 224 | ParameterizeOperators();
|
---|
[2865] | 225 | OnSolutionCreatorChanged();
|
---|
| 226 | }
|
---|
[2975] | 227 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 228 | ParameterizeEvaluator();
|
---|
[4623] | 229 | ParameterizeAnalyzers();
|
---|
[2975] | 230 | ParameterizeOperators();
|
---|
| 231 | }
|
---|
[2865] | 232 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[3139] | 233 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[2975] | 234 | ParameterizeEvaluator();
|
---|
[3209] | 235 | UpdateMoveEvaluators();
|
---|
[4623] | 236 | ParameterizeAnalyzers();
|
---|
[3066] | 237 | ClearDistanceMatrix();
|
---|
[2865] | 238 | OnEvaluatorChanged();
|
---|
| 239 | }
|
---|
[3139] | 240 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[4623] | 241 | ParameterizeAnalyzers();
|
---|
[3139] | 242 | }
|
---|
[3232] | 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;
|
---|
[3074] | 247 | }
|
---|
| 248 | }
|
---|
[3232] | 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;
|
---|
[3074] | 253 | }
|
---|
| 254 | }
|
---|
[2975] | 255 | #endregion
|
---|
[2865] | 256 |
|
---|
[2975] | 257 | #region Helpers
|
---|
[2986] | 258 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 259 | private void AfterDeserialization() {
|
---|
[4118] | 260 | // BackwardsCompatibility3.3
|
---|
| 261 | #region Backwards compatible code (remove with 3.4)
|
---|
[4825] | 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 |
|
---|
[4118] | 279 | if (operators == null) InitializeOperators();
|
---|
| 280 | #endregion
|
---|
| 281 | AttachEventHandlers();
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[4098] | 284 | private void AttachEventHandlers() {
|
---|
[2975] | 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);
|
---|
[3139] | 291 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[2865] | 292 | }
|
---|
[3139] | 293 |
|
---|
[3099] | 294 | private void InitializeOperators() {
|
---|
[3616] | 295 | operators = new List<IOperator>();
|
---|
[3663] | 296 | operators.Add(new BestTSPSolutionAnalyzer());
|
---|
[4623] | 297 | operators.Add(new TSPAlleleFrequencyAnalyzer());
|
---|
[4703] | 298 | operators.Add(new TSPPopulationDiversityAnalyzer());
|
---|
[4623] | 299 | ParameterizeAnalyzers();
|
---|
[3616] | 300 | operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
|
---|
[3199] | 301 | ParameterizeOperators();
|
---|
[3209] | 302 | UpdateMoveEvaluators();
|
---|
[3099] | 303 | InitializeMoveGenerators();
|
---|
| 304 | }
|
---|
| 305 | private void InitializeMoveGenerators() {
|
---|
[3232] | 306 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
|
---|
[3099] | 307 | if (op is IMoveGenerator) {
|
---|
[3232] | 308 | op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);
|
---|
[3099] | 309 | }
|
---|
| 310 | }
|
---|
[3232] | 311 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
|
---|
[3099] | 312 | if (op is IMoveGenerator) {
|
---|
[3232] | 313 | op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);
|
---|
[3099] | 314 | }
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
[3209] | 317 | private void UpdateMoveEvaluators() {
|
---|
[4047] | 318 | operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
|
---|
[3303] | 319 | foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
|
---|
| 320 | if (op.EvaluatorType == Evaluator.GetType()) {
|
---|
| 321 | operators.Add(op);
|
---|
| 322 | }
|
---|
| 323 | ParameterizeOperators();
|
---|
| 324 | OnOperatorsChanged();
|
---|
[3209] | 325 | }
|
---|
[2975] | 326 | private void ParameterizeSolutionCreator() {
|
---|
[3048] | 327 | SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
|
---|
[6051] | 328 | SolutionCreator.LengthParameter.Hidden = true;
|
---|
[3231] | 329 | SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
|
---|
[6051] | 330 | SolutionCreator.PermutationTypeParameter.Hidden = true;
|
---|
[2865] | 331 | }
|
---|
[2975] | 332 | private void ParameterizeEvaluator() {
|
---|
[6051] | 333 | if (Evaluator is ITSPPathEvaluator) {
|
---|
| 334 | ITSPPathEvaluator evaluator = (ITSPPathEvaluator)Evaluator;
|
---|
| 335 | evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 336 | evaluator.PermutationParameter.Hidden = true;
|
---|
| 337 | }
|
---|
[3066] | 338 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
| 339 | ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
|
---|
| 340 | evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[6051] | 341 | evaluator.CoordinatesParameter.Hidden = true;
|
---|
[3066] | 342 | evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[6051] | 343 | evaluator.DistanceMatrixParameter.Hidden = true;
|
---|
[3066] | 344 | evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
[6051] | 345 | evaluator.UseDistanceMatrixParameter.Hidden = true;
|
---|
[3066] | 346 | }
|
---|
[2865] | 347 | }
|
---|
[4623] | 348 | private void ParameterizeAnalyzers() {
|
---|
[4641] | 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 | }
|
---|
[4623] | 358 |
|
---|
[4641] | 359 | if (TSPAlleleFrequencyAnalyzer != null) {
|
---|
| 360 | TSPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 361 | TSPAlleleFrequencyAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[4866] | 362 | TSPAlleleFrequencyAnalyzer.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[4641] | 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 | }
|
---|
[4703] | 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 | }
|
---|
[3107] | 375 | }
|
---|
[2975] | 376 | private void ParameterizeOperators() {
|
---|
| 377 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
| 378 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 379 | op.ParentsParameter.Hidden = true;
|
---|
[2975] | 380 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 381 | op.ChildParameter.Hidden = true;
|
---|
[2975] | 382 | }
|
---|
| 383 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
| 384 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 385 | op.PermutationParameter.Hidden = true;
|
---|
[2975] | 386 | }
|
---|
[3074] | 387 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
| 388 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 389 | op.PermutationParameter.Hidden = true;
|
---|
[3074] | 390 | }
|
---|
| 391 | foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
|
---|
| 392 | op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
[6051] | 393 | op.CoordinatesParameter.Hidden = true;
|
---|
[3074] | 394 | op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
[6051] | 395 | op.DistanceMatrixParameter.Hidden = true;
|
---|
[3074] | 396 | op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
[6051] | 397 | op.UseDistanceMatrixParameter.Hidden = true;
|
---|
[3209] | 398 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 399 | op.QualityParameter.Hidden = true;
|
---|
[3209] | 400 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 401 | op.PermutationParameter.Hidden = true;
|
---|
[3074] | 402 | }
|
---|
[3340] | 403 | string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
|
---|
[6051] | 404 | foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
|
---|
[3340] | 405 | op.InversionMoveParameter.ActualName = inversionMove;
|
---|
[6051] | 406 | op.InversionMoveParameter.Hidden = true;
|
---|
| 407 | }
|
---|
[3340] | 408 | string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
|
---|
[6051] | 409 | foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
|
---|
[3340] | 410 | op.TranslocationMoveParameter.ActualName = translocationMove;
|
---|
[6051] | 411 | op.TranslocationMoveParameter.Hidden = true;
|
---|
| 412 | }
|
---|
| 413 | foreach (IPermutationMultiNeighborhoodShakingOperator op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
|
---|
[6042] | 414 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[6051] | 415 | op.PermutationParameter.Hidden = true;
|
---|
| 416 | }
|
---|
[2975] | 417 | }
|
---|
[3074] | 418 |
|
---|
[3066] | 419 | private void ClearDistanceMatrix() {
|
---|
| 420 | DistanceMatrixParameter.Value = null;
|
---|
| 421 | }
|
---|
[2975] | 422 | #endregion
|
---|
[4098] | 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 | }
|
---|
[2796] | 454 | }
|
---|
| 455 | }
|
---|