Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs @ 3135

Last change on this file since 3135 was 3107, checked in by swagner, 15 years ago

Continued to implement TSP tour visualization (#924)

File size: 14.5 KB
RevLine 
[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]22using System;
[2975]23using System.Collections.Generic;
[2865]24using System.Drawing;
25using System.Linq;
[2975]26using HeuristicLab.Common;
[2796]27using HeuristicLab.Core;
28using HeuristicLab.Data;
[3053]29using HeuristicLab.Encodings.PermutationEncoding;
[2834]30using HeuristicLab.Optimization;
[2805]31using HeuristicLab.Parameters;
[2796]32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2865]33using HeuristicLab.PluginInfrastructure;
[2796]34
[2883]35namespace 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    }
[3107]72    public OptionalValueParameter<ITSPSolutionsVisualizer> VisualizerParameter {
73      get { return (OptionalValueParameter<ITSPSolutionsVisualizer>)Parameters["Visualizer"]; }
74    }
75    IParameter IProblem.VisualizerParameter {
76      get { return VisualizerParameter; }
77    }
[3048]78    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
79      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
[2975]80    }
[3080]81    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
82      get { return BestKnownQualityParameter; }
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    }
[3066]91    public DoubleMatrix 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    }
[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    }
[3107]116    public ITSPSolutionsVisualizer Visualizer {
117      get { return VisualizerParameter.Value; }
118      set { VisualizerParameter.Value = value; }
119    }
120    ISolutionsVisualizer IProblem.Visualizer {
121      get { return VisualizerParameter.Value; }
122    }
[3048]123    public DoubleValue BestKnownQuality {
[2975]124      get { return BestKnownQualityParameter.Value; }
125      set { BestKnownQualityParameter.Value = value; }
126    }
[2986]127    private List<IPermutationOperator> operators;
[2975]128    public IEnumerable<IOperator> Operators {
[2986]129      get { return operators.Cast<IOperator>(); }
[2865]130    }
[2975]131    #endregion
[2865]132
[2796]133    public TSP()
134      : base() {
[2891]135      RandomPermutationCreator creator = new RandomPermutationCreator();
136      TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
[3107]137      BestPathTSPTourVisualizer visualizer = new BestPathTSPTourVisualizer();
[2891]138
[3048]139      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
140      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix(0, 0)));
[3066]141      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
142      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
[2891]143      Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
144      Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
[3107]145      Parameters.Add(new OptionalValueParameter<ITSPSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize TSP solutions.", visualizer));
[3048]146      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
[2865]147
148      creator.PermutationParameter.ActualName = "TSPTour";
149      evaluator.QualityParameter.ActualName = "TSPTourLength";
[2975]150      ParameterizeSolutionCreator();
151      ParameterizeEvaluator();
[3107]152      ParameterizeVisualizer();
[2865]153
[2986]154      Initialize();
[2975]155    }
[2986]156    [StorableConstructor]
157    private TSP(bool deserializing) : base() { }
[2891]158
[2975]159    public override IDeepCloneable Clone(Cloner cloner) {
160      TSP clone = (TSP)base.Clone(cloner);
[2986]161      clone.Initialize();
[2975]162      return clone;
[2805]163    }
[2796]164
[2805]165    public void ImportFromTSPLIB(string filename) {
166      TSPLIBParser parser = new TSPLIBParser(filename);
167      parser.Parse();
[3048]168      Coordinates = new DoubleMatrix(parser.Vertices);
[2796]169    }
[2865]170
[2975]171    #region Events
172    public event EventHandler SolutionCreatorChanged;
173    private void OnSolutionCreatorChanged() {
174      if (SolutionCreatorChanged != null)
175        SolutionCreatorChanged(this, EventArgs.Empty);
[2865]176    }
[2975]177    public event EventHandler EvaluatorChanged;
178    private void OnEvaluatorChanged() {
179      if (EvaluatorChanged != null)
180        EvaluatorChanged(this, EventArgs.Empty);
181    }
[3107]182    public event EventHandler VisualizerChanged;
183    private void OnVisualizerChanged() {
184      if (VisualizerChanged != null)
185        VisualizerChanged(this, EventArgs.Empty);
186    }
[2975]187    public event EventHandler OperatorsChanged;
188    private void OnOperatorsChanged() {
189      if (OperatorsChanged != null)
190        OperatorsChanged(this, EventArgs.Empty);
191    }
192
193    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
194      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
195      Coordinates.Reset += new EventHandler(Coordinates_Reset);
196      ParameterizeSolutionCreator();
[3066]197      ClearDistanceMatrix();
[2975]198    }
199    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
[3066]200      ClearDistanceMatrix();
[2975]201    }
202    private void Coordinates_Reset(object sender, EventArgs e) {
203      ParameterizeSolutionCreator();
[3066]204      ClearDistanceMatrix();
[2975]205    }
[2865]206    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
[2975]207      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
208      ParameterizeSolutionCreator();
209      ParameterizeEvaluator();
[3107]210      ParameterizeVisualizer();
[2975]211      ParameterizeOperators();
[2865]212      OnSolutionCreatorChanged();
213    }
[2975]214    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
215      ParameterizeEvaluator();
[3107]216      ParameterizeVisualizer();
[2975]217      ParameterizeOperators();
218    }
[2865]219    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
[2975]220      ParameterizeEvaluator();
[3107]221      ParameterizeVisualizer();
[3066]222      ClearDistanceMatrix();
[2865]223      OnEvaluatorChanged();
224    }
[3107]225    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
226      ParameterizeVisualizer();
227      OnVisualizerChanged();
228    }
[3074]229    private void MoveGenerator_TwoOptMoveParameter_ActualNameChanged(object sender, EventArgs e) {
230      string name = ((ILookupParameter<TwoOptMove>)sender).ActualName;
231      foreach (ITwoOptPermutationMoveOperator op in Operators.OfType<ITwoOptPermutationMoveOperator>()) {
[3099]232        op.TwoOptMoveParameter.ActualName = name;
[3074]233      }
234    }
235    private void MoveGenerator_ThreeOptMoveParameter_ActualNameChanged(object sender, EventArgs e) {
236      string name = ((ILookupParameter<ThreeOptMove>)sender).ActualName;
237      foreach (IThreeOptPermutationMoveOperator op in Operators.OfType<IThreeOptPermutationMoveOperator>()) {
[3099]238        op.ThreeOptMoveParameter.ActualName = name;
[3074]239      }
240    }
[2975]241    #endregion
[2865]242
[2975]243    #region Helpers
[2986]244    [StorableHook(HookType.AfterDeserialization)]
245    private void Initialize() {
246      InitializeOperators();
[2975]247      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
248      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
249      Coordinates.Reset += new EventHandler(Coordinates_Reset);
250      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
251      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
252      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
[3107]253      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
[2865]254    }
[3099]255    private void InitializeOperators() {
256      operators = new List<IPermutationOperator>();
257      if (ApplicationManager.Manager != null) {
258        operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
259        ParameterizeOperators();
260      }
261      InitializeMoveGenerators();
262    }
263    private void InitializeMoveGenerators() {
264      foreach (ITwoOptPermutationMoveOperator op in Operators.OfType<ITwoOptPermutationMoveOperator>()) {
265        if (op is IMoveGenerator) {
266          op.TwoOptMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TwoOptMoveParameter_ActualNameChanged);
267        }
268      }
269      foreach (IThreeOptPermutationMoveOperator op in Operators.OfType<IThreeOptPermutationMoveOperator>()) {
270        if (op is IMoveGenerator) {
271          op.ThreeOptMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_ThreeOptMoveParameter_ActualNameChanged);
272        }
273      }
274    }
[2975]275    private void ParameterizeSolutionCreator() {
[3048]276      SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
[2865]277    }
[2975]278    private void ParameterizeEvaluator() {
279      if (Evaluator is ITSPPathEvaluator)
280        ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
[3066]281      if (Evaluator is ITSPCoordinatesPathEvaluator) {
282        ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
283        evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
284        evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
285        evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
286      }
[2865]287    }
[3107]288    private void ParameterizeVisualizer() {
289      Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
290      if (Visualizer is ICoordinatesTSPSolutionsVisualizer)
291        ((ICoordinatesTSPSolutionsVisualizer)Visualizer).CoordinatesParameter.ActualName = CoordinatesParameter.Name;
292      if (Visualizer is IPathCoordinatesTSPSolutionsVisualizer)
293        ((IPathCoordinatesTSPSolutionsVisualizer)Visualizer).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
294    }
[2975]295    private void ParameterizeOperators() {
296      foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
297        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
298        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
299      }
300      foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
301        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
302      }
[3074]303      foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
304        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
305      }
306      foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
307        op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
308        op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
309        op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
310      }
[2975]311    }
[3074]312
[3066]313    private void ClearDistanceMatrix() {
314      DistanceMatrixParameter.Value = null;
315    }
[2975]316    #endregion
[2796]317  }
318}
Note: See TracBrowser for help on using the repository browser.