Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.TravelingSalesman/3.3/PathTSPTour.cs @ 9420

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

Updated year of copyrights (#1406)

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.VSImageLibrary.Image; }
39    }
40
41    [Storable]
42    private DoubleMatrix coordinates;
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    [Storable]
55    private Permutation permutation;
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    [Storable]
68    private DoubleValue quality;
69    public DoubleValue Quality {
70      get { return quality; }
71      set {
72        if (quality != value) {
73          if (quality != null) DeregisterQualityEvents();
74          quality = value;
75          if (quality != null) RegisterQualityEvents();
76          OnQualityChanged();
77        }
78      }
79    }
80
81    [StorableConstructor]
82    private PathTSPTour(bool deserializing) : base(deserializing) { }
83    private PathTSPTour(PathTSPTour original, Cloner cloner)
84      : base(original, cloner) {
85      this.coordinates = cloner.Clone(original.coordinates);
86      this.permutation = cloner.Clone(original.permutation);
87      this.quality = cloner.Clone(original.quality);
88      Initialize();
89    }
90    public PathTSPTour() : base() { }
91    public PathTSPTour(DoubleMatrix coordinates)
92      : base() {
93      this.coordinates = coordinates;
94      Initialize();
95    }
96    public PathTSPTour(DoubleMatrix coordinates, Permutation permutation)
97      : base() {
98      this.coordinates = coordinates;
99      this.permutation = permutation;
100      Initialize();
101    }
102    public PathTSPTour(DoubleMatrix coordinates, Permutation permutation, DoubleValue quality)
103      : base() {
104      this.coordinates = coordinates;
105      this.permutation = permutation;
106      this.quality = quality;
107      Initialize();
108    }
109
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new PathTSPTour(this, cloner);
112    }
113
114    [StorableHook(HookType.AfterDeserialization)]
115    private void AfterDeserialization() {
116      Initialize();
117    }
118
119    private void Initialize() {
120      if (coordinates != null) RegisterCoordinatesEvents();
121      if (permutation != null) RegisterPermutationEvents();
122      if (quality != null) RegisterQualityEvents();
123    }
124
125    #region Events
126    public event EventHandler CoordinatesChanged;
127    private void OnCoordinatesChanged() {
128      var changed = CoordinatesChanged;
129      if (changed != null)
130        changed(this, EventArgs.Empty);
131    }
132    public event EventHandler PermutationChanged;
133    private void OnPermutationChanged() {
134      var changed = PermutationChanged;
135      if (changed != null)
136        changed(this, EventArgs.Empty);
137    }
138    public event EventHandler QualityChanged;
139    private void OnQualityChanged() {
140      var changed = QualityChanged;
141      if (changed != null)
142        changed(this, EventArgs.Empty);
143    }
144
145    private void RegisterCoordinatesEvents() {
146      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
147      Coordinates.Reset += new EventHandler(Coordinates_Reset);
148    }
149    private void DeregisterCoordinatesEvents() {
150      Coordinates.ItemChanged -= new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
151      Coordinates.Reset -= new EventHandler(Coordinates_Reset);
152    }
153    private void RegisterPermutationEvents() {
154      Permutation.ItemChanged += new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
155      Permutation.Reset += new EventHandler(Permutation_Reset);
156    }
157    private void DeregisterPermutationEvents() {
158      Permutation.ItemChanged -= new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
159      Permutation.Reset -= new EventHandler(Permutation_Reset);
160    }
161    private void RegisterQualityEvents() {
162      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
163    }
164    private void DeregisterQualityEvents() {
165      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
166    }
167
168    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
169      OnCoordinatesChanged();
170    }
171    private void Coordinates_Reset(object sender, EventArgs e) {
172      OnCoordinatesChanged();
173    }
174    private void Permutation_ItemChanged(object sender, EventArgs<int> e) {
175      OnPermutationChanged();
176    }
177    private void Permutation_Reset(object sender, EventArgs e) {
178      OnPermutationChanged();
179    }
180    private void Quality_ValueChanged(object sender, EventArgs e) {
181      OnQualityChanged();
182    }
183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.