Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/PathTSPTour.cs @ 3306

Last change on this file since 3306 was 3306, checked in by swagner, 14 years ago

Implemented reviewers' comments (#893).

File size: 4.7 KB
Line 
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
22using System;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.TravelingSalesman {
31  /// <summary>
32  /// Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.
33  /// </summary>
34  [Item("PathTSPTour", "Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.")]
35  [StorableClass]
36  public sealed class PathTSPTour : Item {
37    public override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Image; }
39    }
40
41    private DoubleMatrix coordinates;
42    [Storable]
43    public DoubleMatrix Coordinates {
44      get { return coordinates; }
45      set {
46        if (coordinates != value) {
47          if (coordinates != null) DeregisterCoordinatesEvents();
48          coordinates = value;
49          if (coordinates != null) RegisterCoordinatesEvents();
50          OnCoordinatesChanged();
51        }
52      }
53    }
54    private Permutation permutation;
55    [Storable]
56    public Permutation Permutation {
57      get { return permutation; }
58      set {
59        if (permutation != value) {
60          if (permutation != null) DeregisterPermutationEvents();
61          permutation = value;
62          if (permutation != null) RegisterPermutationEvents();
63          OnPermutationChanged();
64        }
65      }
66    }
67
68    public PathTSPTour() : base() { }
69    public PathTSPTour(DoubleMatrix coordinates)
70      : base() {
71      Coordinates = coordinates;
72    }
73    public PathTSPTour(DoubleMatrix coordinates, Permutation permutation)
74      : this(coordinates) {
75      Permutation = permutation;
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      PathTSPTour clone = new PathTSPTour();
80      cloner.RegisterClonedObject(this, clone);
81      clone.Coordinates = (DoubleMatrix)cloner.Clone(coordinates);
82      clone.Permutation = (Permutation)cloner.Clone(permutation);
83      return clone;
84    }
85
86    #region Events
87    public event EventHandler CoordinatesChanged;
88    private void OnCoordinatesChanged() {
89      var changed = CoordinatesChanged;
90      if (changed != null)
91        changed(this, EventArgs.Empty);
92    }
93    public event EventHandler PermutationChanged;
94    private void OnPermutationChanged() {
95      var changed = PermutationChanged;
96      if (changed != null)
97        changed(this, EventArgs.Empty);
98    }
99
100    private void RegisterCoordinatesEvents() {
101      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
102      Coordinates.Reset += new EventHandler(Coordinates_Reset);
103    }
104    private void DeregisterCoordinatesEvents() {
105      Coordinates.ItemChanged -= new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
106      Coordinates.Reset -= new EventHandler(Coordinates_Reset);
107    }
108    private void RegisterPermutationEvents() {
109      Permutation.ItemChanged += new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
110      Permutation.Reset += new EventHandler(Permutation_Reset);
111    }
112    private void DeregisterPermutationEvents() {
113      Permutation.ItemChanged -= new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
114      Permutation.Reset -= new EventHandler(Permutation_Reset);
115    }
116
117    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
118      OnCoordinatesChanged();
119    }
120    private void Coordinates_Reset(object sender, EventArgs e) {
121      OnCoordinatesChanged();
122    }
123    private void Permutation_ItemChanged(object sender, EventArgs<int> e) {
124      OnPermutationChanged();
125    }
126    private void Permutation_Reset(object sender, EventArgs e) {
127      OnPermutationChanged();
128    }
129    #endregion
130  }
131}
Note: See TracBrowser for help on using the repository browser.