[2796] | 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 |
|
---|
[2865] | 22 | using System;
|
---|
[2975] | 23 | using System.Collections.Generic;
|
---|
[2865] | 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[2975] | 26 | using HeuristicLab.Common;
|
---|
[2796] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
[3053] | 29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[2834] | 30 | using HeuristicLab.Optimization;
|
---|
[2805] | 31 | using HeuristicLab.Parameters;
|
---|
[2796] | 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2865] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[2796] | 34 |
|
---|
[2883] | 35 | namespace HeuristicLab.Problems.TSP {
|
---|
[2796] | 36 | [Item("TSP", "Represents a symmetric Traveling Salesman Problem.")]
|
---|
| 37 | [Creatable("Problems")]
|
---|
[3017] | 38 | [StorableClass]
|
---|
[2865] | 39 | public sealed class TSP : ParameterizedNamedItem, ISingleObjectiveProblem {
|
---|
| 40 | public override Image ItemImage {
|
---|
| 41 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
|
---|
[2805] | 42 | }
|
---|
| 43 |
|
---|
[2975] | 44 | #region Parameter Properties
|
---|
[3048] | 45 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
| 46 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[2805] | 47 | }
|
---|
[2975] | 48 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
| 49 | get { return MaximizationParameter; }
|
---|
[2865] | 50 | }
|
---|
[3048] | 51 | public ValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 52 | get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
[2865] | 53 | }
|
---|
[3066] | 54 | public OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter {
|
---|
| 55 | get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
|
---|
| 56 | }
|
---|
| 57 | public ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
| 58 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
| 59 | }
|
---|
[2975] | 60 | public ValueParameter<IPermutationCreator> SolutionCreatorParameter {
|
---|
| 61 | get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; }
|
---|
[2865] | 62 | }
|
---|
[2975] | 63 | IParameter IProblem.SolutionCreatorParameter {
|
---|
| 64 | get { return SolutionCreatorParameter; }
|
---|
[2865] | 65 | }
|
---|
[2975] | 66 | public ValueParameter<ITSPEvaluator> EvaluatorParameter {
|
---|
| 67 | get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
|
---|
| 68 | }
|
---|
| 69 | IParameter IProblem.EvaluatorParameter {
|
---|
| 70 | get { return EvaluatorParameter; }
|
---|
| 71 | }
|
---|
[3048] | 72 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 73 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
[2975] | 74 | }
|
---|
| 75 | #endregion
|
---|
[2805] | 76 |
|
---|
[2975] | 77 | #region Properties
|
---|
[3048] | 78 | public DoubleMatrix Coordinates {
|
---|
[2975] | 79 | get { return CoordinatesParameter.Value; }
|
---|
| 80 | set { CoordinatesParameter.Value = value; }
|
---|
[2865] | 81 | }
|
---|
[3066] | 82 | public DoubleMatrix DistanceMatrix {
|
---|
| 83 | get { return DistanceMatrixParameter.Value; }
|
---|
| 84 | set { DistanceMatrixParameter.Value = value; }
|
---|
| 85 | }
|
---|
| 86 | public BoolValue UseDistanceMatrix {
|
---|
| 87 | get { return UseDistanceMatrixParameter.Value; }
|
---|
| 88 | set { UseDistanceMatrixParameter.Value = value; }
|
---|
| 89 | }
|
---|
[2975] | 90 | public IPermutationCreator SolutionCreator {
|
---|
[2865] | 91 | get { return SolutionCreatorParameter.Value; }
|
---|
[2975] | 92 | set { SolutionCreatorParameter.Value = value; }
|
---|
[2865] | 93 | }
|
---|
[2975] | 94 | ISolutionCreator IProblem.SolutionCreator {
|
---|
| 95 | get { return SolutionCreatorParameter.Value; }
|
---|
| 96 | }
|
---|
| 97 | public ITSPEvaluator Evaluator {
|
---|
[2865] | 98 | get { return EvaluatorParameter.Value; }
|
---|
[2975] | 99 | set { EvaluatorParameter.Value = value; }
|
---|
[2865] | 100 | }
|
---|
[2975] | 101 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
| 102 | get { return EvaluatorParameter.Value; }
|
---|
| 103 | }
|
---|
[2865] | 104 | IEvaluator IProblem.Evaluator {
|
---|
| 105 | get { return EvaluatorParameter.Value; }
|
---|
| 106 | }
|
---|
[3048] | 107 | public DoubleValue BestKnownQuality {
|
---|
[2975] | 108 | get { return BestKnownQualityParameter.Value; }
|
---|
| 109 | set { BestKnownQualityParameter.Value = value; }
|
---|
| 110 | }
|
---|
[2986] | 111 | private List<IPermutationOperator> operators;
|
---|
[2975] | 112 | public IEnumerable<IOperator> Operators {
|
---|
[2986] | 113 | get { return operators.Cast<IOperator>(); }
|
---|
[2865] | 114 | }
|
---|
[2975] | 115 | #endregion
|
---|
[2865] | 116 |
|
---|
[2796] | 117 | public TSP()
|
---|
| 118 | : base() {
|
---|
[2891] | 119 | RandomPermutationCreator creator = new RandomPermutationCreator();
|
---|
| 120 | TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
| 121 |
|
---|
[3048] | 122 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
|
---|
| 123 | Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix(0, 0)));
|
---|
[3066] | 124 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
| 125 | Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
|
---|
[2891] | 126 | Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
|
---|
| 127 | Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
|
---|
[3048] | 128 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
|
---|
[2865] | 129 |
|
---|
| 130 | creator.PermutationParameter.ActualName = "TSPTour";
|
---|
| 131 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
[2975] | 132 | ParameterizeSolutionCreator();
|
---|
| 133 | ParameterizeEvaluator();
|
---|
[2865] | 134 |
|
---|
[2986] | 135 | Initialize();
|
---|
[2975] | 136 | }
|
---|
[2986] | 137 | [StorableConstructor]
|
---|
| 138 | private TSP(bool deserializing) : base() { }
|
---|
[2891] | 139 |
|
---|
[2975] | 140 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 141 | TSP clone = (TSP)base.Clone(cloner);
|
---|
[2986] | 142 | clone.Initialize();
|
---|
[2975] | 143 | return clone;
|
---|
[2805] | 144 | }
|
---|
[2796] | 145 |
|
---|
[2805] | 146 | public void ImportFromTSPLIB(string filename) {
|
---|
| 147 | TSPLIBParser parser = new TSPLIBParser(filename);
|
---|
| 148 | parser.Parse();
|
---|
[3048] | 149 | Coordinates = new DoubleMatrix(parser.Vertices);
|
---|
[2796] | 150 | }
|
---|
[2865] | 151 |
|
---|
[2975] | 152 | #region Events
|
---|
| 153 | public event EventHandler SolutionCreatorChanged;
|
---|
| 154 | private void OnSolutionCreatorChanged() {
|
---|
| 155 | if (SolutionCreatorChanged != null)
|
---|
| 156 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
[2865] | 157 | }
|
---|
[2975] | 158 | public event EventHandler EvaluatorChanged;
|
---|
| 159 | private void OnEvaluatorChanged() {
|
---|
| 160 | if (EvaluatorChanged != null)
|
---|
| 161 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
| 162 | }
|
---|
| 163 | public event EventHandler OperatorsChanged;
|
---|
| 164 | private void OnOperatorsChanged() {
|
---|
| 165 | if (OperatorsChanged != null)
|
---|
| 166 | OperatorsChanged(this, EventArgs.Empty);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 170 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
| 171 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
| 172 | ParameterizeSolutionCreator();
|
---|
[3066] | 173 | ClearDistanceMatrix();
|
---|
[2975] | 174 | }
|
---|
| 175 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[3066] | 176 | ClearDistanceMatrix();
|
---|
[2975] | 177 | }
|
---|
| 178 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
| 179 | ParameterizeSolutionCreator();
|
---|
[3066] | 180 | ClearDistanceMatrix();
|
---|
[2975] | 181 | }
|
---|
[2865] | 182 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[2975] | 183 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 184 | ParameterizeSolutionCreator();
|
---|
| 185 | ParameterizeEvaluator();
|
---|
| 186 | ParameterizeOperators();
|
---|
[2865] | 187 | OnSolutionCreatorChanged();
|
---|
| 188 | }
|
---|
[2975] | 189 | private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 190 | ParameterizeEvaluator();
|
---|
| 191 | ParameterizeOperators();
|
---|
| 192 | }
|
---|
[2865] | 193 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[2975] | 194 | ParameterizeEvaluator();
|
---|
[3066] | 195 | ClearDistanceMatrix();
|
---|
[2865] | 196 | OnEvaluatorChanged();
|
---|
| 197 | }
|
---|
[3074] | 198 | private void MoveGenerator_TwoOptMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 199 | string name = ((ILookupParameter<TwoOptMove>)sender).ActualName;
|
---|
| 200 | foreach (ITwoOptPermutationMoveOperator op in Operators.OfType<ITwoOptPermutationMoveOperator>()) {
|
---|
| 201 | if (!(op is IMoveGenerator)) op.TwoOptMoveParameter.ActualName = name;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | private void MoveGenerator_ThreeOptMoveParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 205 | string name = ((ILookupParameter<ThreeOptMove>)sender).ActualName;
|
---|
| 206 | foreach (IThreeOptPermutationMoveOperator op in Operators.OfType<IThreeOptPermutationMoveOperator>()) {
|
---|
| 207 | if (!(op is IMoveGenerator)) op.ThreeOptMoveParameter.ActualName = name;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
[2975] | 210 | #endregion
|
---|
[2865] | 211 |
|
---|
[2975] | 212 | #region Helpers
|
---|
[2986] | 213 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 214 | private void Initialize() {
|
---|
| 215 | InitializeOperators();
|
---|
[2975] | 216 | CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
|
---|
| 217 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
| 218 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
| 219 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 220 | SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
|
---|
| 221 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
[2865] | 222 | }
|
---|
[2975] | 223 | private void ParameterizeSolutionCreator() {
|
---|
[3048] | 224 | SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
|
---|
[2865] | 225 | }
|
---|
[2975] | 226 | private void ParameterizeEvaluator() {
|
---|
| 227 | if (Evaluator is ITSPPathEvaluator)
|
---|
| 228 | ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
[3066] | 229 | if (Evaluator is ITSPCoordinatesPathEvaluator) {
|
---|
| 230 | ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
|
---|
| 231 | evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
| 232 | evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
| 233 | evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
| 234 | }
|
---|
[2865] | 235 | }
|
---|
[2975] | 236 | private void InitializeOperators() {
|
---|
[2986] | 237 | operators = new List<IPermutationOperator>();
|
---|
[2975] | 238 | if (ApplicationManager.Manager != null) {
|
---|
[2986] | 239 | operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
|
---|
[2975] | 240 | ParameterizeOperators();
|
---|
| 241 | }
|
---|
[3074] | 242 | InitializeMoveGenerators();
|
---|
[2975] | 243 | }
|
---|
| 244 | private void ParameterizeOperators() {
|
---|
| 245 | foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
|
---|
| 246 | op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 247 | op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 248 | }
|
---|
| 249 | foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
|
---|
| 250 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 251 | }
|
---|
[3074] | 252 | foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
|
---|
| 253 | op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
|
---|
| 254 | }
|
---|
| 255 | foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
|
---|
| 256 | op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
| 257 | op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
|
---|
| 258 | op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
|
---|
| 259 | }
|
---|
[2975] | 260 | }
|
---|
[3074] | 261 | private void InitializeMoveGenerators() {
|
---|
| 262 | foreach (ITwoOptPermutationMoveOperator op in Operators.OfType<ITwoOptPermutationMoveOperator>()) {
|
---|
| 263 | if (op is IMoveGenerator) {
|
---|
| 264 | op.TwoOptMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TwoOptMoveParameter_ActualNameChanged);
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | foreach (IThreeOptPermutationMoveOperator op in Operators.OfType<IThreeOptPermutationMoveOperator>()) {
|
---|
| 268 | if (op is IMoveGenerator) {
|
---|
| 269 | op.ThreeOptMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_ThreeOptMoveParameter_ActualNameChanged);
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[3066] | 274 | private void ClearDistanceMatrix() {
|
---|
| 275 | DistanceMatrixParameter.Value = null;
|
---|
| 276 | }
|
---|
[2975] | 277 | #endregion
|
---|
[2796] | 278 | }
|
---|
| 279 | }
|
---|