Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/PathTSPTour.cs @ 4656

Last change on this file since 4656 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

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