Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Problems.TravelingSalesman/3.3/PathTSPTour.cs @ 9286

Last change on this file since 9286 was 9286, checked in by sforsten, 11 years ago

#2018:

  • added new methods to the interface IStringConvertibleMatrix as well as two structs. Also the event ItemChanged has been changed to ItemsChanged
  • class ValueTypeMatrix now implements IStringConvertibleMatrix instead of the classes which inherit from it
  • small changes have been applied to a lot of classes to correctly implement the changed interface IStringConvertibleMatrix
  • solution file, Build.cmd and PreBuildEvent.cmd have been added
File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
24using System.Drawing;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.TravelingSalesman {
32  /// <summary>
33  /// Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.
34  /// </summary>
35  [Item("PathTSPTour", "Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.")]
36  [StorableClass]
37  public sealed class PathTSPTour : Item {
38    public static new Image StaticItemImage {
39      get { return HeuristicLab.Common.Resources.VSImageLibrary.Image; }
40    }
41
42    [Storable]
43    private DoubleMatrix coordinates;
44    public DoubleMatrix Coordinates {
45      get { return coordinates; }
46      set {
47        if (coordinates != value) {
48          if (coordinates != null) DeregisterCoordinatesEvents();
49          coordinates = value;
50          if (coordinates != null) RegisterCoordinatesEvents();
51          OnCoordinatesChanged();
52        }
53      }
54    }
55    [Storable]
56    private Permutation permutation;
57    public Permutation Permutation {
58      get { return permutation; }
59      set {
60        if (permutation != value) {
61          if (permutation != null) DeregisterPermutationEvents();
62          permutation = value;
63          if (permutation != null) RegisterPermutationEvents();
64          OnPermutationChanged();
65        }
66      }
67    }
68    [Storable]
69    private DoubleValue quality;
70    public DoubleValue Quality {
71      get { return quality; }
72      set {
73        if (quality != value) {
74          if (quality != null) DeregisterQualityEvents();
75          quality = value;
76          if (quality != null) RegisterQualityEvents();
77          OnQualityChanged();
78        }
79      }
80    }
81
82    [StorableConstructor]
83    private PathTSPTour(bool deserializing) : base(deserializing) { }
84    private PathTSPTour(PathTSPTour original, Cloner cloner)
85      : base(original, cloner) {
86      this.coordinates = cloner.Clone(original.coordinates);
87      this.permutation = cloner.Clone(original.permutation);
88      this.quality = cloner.Clone(original.quality);
89      Initialize();
90    }
91    public PathTSPTour() : base() { }
92    public PathTSPTour(DoubleMatrix coordinates)
93      : base() {
94      this.coordinates = coordinates;
95      Initialize();
96    }
97    public PathTSPTour(DoubleMatrix coordinates, Permutation permutation)
98      : base() {
99      this.coordinates = coordinates;
100      this.permutation = permutation;
101      Initialize();
102    }
103    public PathTSPTour(DoubleMatrix coordinates, Permutation permutation, DoubleValue quality)
104      : base() {
105      this.coordinates = coordinates;
106      this.permutation = permutation;
107      this.quality = quality;
108      Initialize();
109    }
110
111    public override IDeepCloneable Clone(Cloner cloner) {
112      return new PathTSPTour(this, cloner);
113    }
114
115    [StorableHook(HookType.AfterDeserialization)]
116    private void AfterDeserialization() {
117      Initialize();
118    }
119
120    private void Initialize() {
121      if (coordinates != null) RegisterCoordinatesEvents();
122      if (permutation != null) RegisterPermutationEvents();
123      if (quality != null) RegisterQualityEvents();
124    }
125
126    #region Events
127    public event EventHandler CoordinatesChanged;
128    private void OnCoordinatesChanged() {
129      var changed = CoordinatesChanged;
130      if (changed != null)
131        changed(this, EventArgs.Empty);
132    }
133    public event EventHandler PermutationChanged;
134    private void OnPermutationChanged() {
135      var changed = PermutationChanged;
136      if (changed != null)
137        changed(this, EventArgs.Empty);
138    }
139    public event EventHandler QualityChanged;
140    private void OnQualityChanged() {
141      var changed = QualityChanged;
142      if (changed != null)
143        changed(this, EventArgs.Empty);
144    }
145
146    private void RegisterCoordinatesEvents() {
147      Coordinates.ItemsChanged += new EventHandler<EventArgs<IEnumerable<Position>>>(Coordinates_ItemChanged);
148      Coordinates.Reset += new EventHandler(Coordinates_Reset);
149    }
150    private void DeregisterCoordinatesEvents() {
151      Coordinates.ItemsChanged -= new EventHandler<EventArgs<IEnumerable<Position>>>(Coordinates_ItemChanged);
152      Coordinates.Reset -= new EventHandler(Coordinates_Reset);
153    }
154    private void RegisterPermutationEvents() {
155      Permutation.ItemChanged += new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
156      Permutation.Reset += new EventHandler(Permutation_Reset);
157    }
158    private void DeregisterPermutationEvents() {
159      Permutation.ItemChanged -= new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
160      Permutation.Reset -= new EventHandler(Permutation_Reset);
161    }
162    private void RegisterQualityEvents() {
163      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
164    }
165    private void DeregisterQualityEvents() {
166      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
167    }
168
169    private void Coordinates_ItemChanged(object sender, EventArgs<IEnumerable<Position>> e) {
170      OnCoordinatesChanged();
171    }
172    private void Coordinates_Reset(object sender, EventArgs e) {
173      OnCoordinatesChanged();
174    }
175    private void Permutation_ItemChanged(object sender, EventArgs<int> e) {
176      OnPermutationChanged();
177    }
178    private void Permutation_Reset(object sender, EventArgs e) {
179      OnPermutationChanged();
180    }
181    private void Quality_ValueChanged(object sender, EventArgs e) {
182      OnQualityChanged();
183    }
184    #endregion
185  }
186}
Note: See TracBrowser for help on using the repository browser.