Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.Problems.PTSP/3.3/PathPTSPTour.cs @ 17328

Last change on this file since 17328 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.PTSP {
31  /// <summary>
32  /// Represents a tour of a Probabilistic Traveling Salesman Problem given in path representation which can be visualized in the GUI.
33  /// </summary>
34  [Item("PathPTSPTour", "Represents a tour of a Probabilistic Traveling Salesman Problem given in path representation which can be visualized in the GUI.")]
35  [StorableClass]
36  public sealed class PathPTSPTour : Item {
37    public static new Image StaticItemImage {
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    [Storable]
81    private DoubleArray probabilities;
82    public DoubleArray Probabilities {
83      get { return probabilities; }
84      set {
85        if (probabilities != value) {
86          if (probabilities != null) DeregisterProbabilitiesEvents();
87          probabilities = value;
88          if (probabilities != null) RegisterProbabilitiesEvents();
89          OnQualityChanged();
90        }
91      }
92    }
93
94    [StorableConstructor]
95    private PathPTSPTour(bool deserializing) : base(deserializing) { }
96    private PathPTSPTour(PathPTSPTour original, Cloner cloner)
97      : base(original, cloner) {
98      this.coordinates = cloner.Clone(original.coordinates);
99      this.probabilities = cloner.Clone(original.probabilities);
100      this.permutation = cloner.Clone(original.permutation);
101      this.quality = cloner.Clone(original.quality);
102      Initialize();
103    }
104    public PathPTSPTour() : base() { }
105    public PathPTSPTour(DoubleMatrix coordinates, DoubleArray probabilities)
106      : base() {
107      this.coordinates = coordinates;
108      this.probabilities = probabilities;
109      Initialize();
110    }
111    public PathPTSPTour(DoubleMatrix coordinates, DoubleArray probabilities, Permutation permutation)
112      : base() {
113      this.coordinates = coordinates;
114      this.probabilities = probabilities;
115      this.permutation = permutation;
116      Initialize();
117    }
118    public PathPTSPTour(DoubleMatrix coordinates, DoubleArray probabilities, Permutation permutation, DoubleValue quality)
119      : base() {
120      this.coordinates = coordinates;
121      this.probabilities = probabilities;
122      this.permutation = permutation;
123      this.quality = quality;
124      Initialize();
125    }
126
127    public override IDeepCloneable Clone(Cloner cloner) {
128      return new PathPTSPTour(this, cloner);
129    }
130
131    [StorableHook(HookType.AfterDeserialization)]
132    private void AfterDeserialization() {
133      Initialize();
134    }
135
136    private void Initialize() {
137      if (coordinates != null) RegisterCoordinatesEvents();
138      if (probabilities != null) RegisterProbabilitiesEvents();
139      if (permutation != null) RegisterPermutationEvents();
140      if (quality != null) RegisterQualityEvents();
141    }
142
143    #region Events
144    public event EventHandler CoordinatesChanged;
145    private void OnCoordinatesChanged() {
146      var changed = CoordinatesChanged;
147      if (changed != null)
148        changed(this, EventArgs.Empty);
149    }
150    public event EventHandler ProbabilitiesChanged;
151    private void OnProbabilitiesChanged() {
152      var changed = ProbabilitiesChanged;
153      if (changed != null)
154        changed(this, EventArgs.Empty);
155    }
156    public event EventHandler PermutationChanged;
157    private void OnPermutationChanged() {
158      var changed = PermutationChanged;
159      if (changed != null)
160        changed(this, EventArgs.Empty);
161    }
162    public event EventHandler QualityChanged;
163    private void OnQualityChanged() {
164      var changed = QualityChanged;
165      if (changed != null)
166        changed(this, EventArgs.Empty);
167    }
168
169    private void RegisterCoordinatesEvents() {
170      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
171      Coordinates.Reset += new EventHandler(Coordinates_Reset);
172    }
173    private void DeregisterCoordinatesEvents() {
174      Coordinates.ItemChanged -= new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
175      Coordinates.Reset -= new EventHandler(Coordinates_Reset);
176    }
177    private void RegisterProbabilitiesEvents() {
178      Probabilities.ItemChanged += new EventHandler<EventArgs<int>>(Probabilities_ItemChanged);
179      Probabilities.Reset += new EventHandler(Probabilities_Reset);
180    }
181    private void DeregisterProbabilitiesEvents() {
182      Probabilities.ItemChanged -= new EventHandler<EventArgs<int>>(Probabilities_ItemChanged);
183      Probabilities.Reset -= new EventHandler(Probabilities_Reset);
184    }
185    private void RegisterPermutationEvents() {
186      Permutation.ItemChanged += new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
187      Permutation.Reset += new EventHandler(Permutation_Reset);
188    }
189    private void DeregisterPermutationEvents() {
190      Permutation.ItemChanged -= new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
191      Permutation.Reset -= new EventHandler(Permutation_Reset);
192    }
193    private void RegisterQualityEvents() {
194      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
195    }
196    private void DeregisterQualityEvents() {
197      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
198    }
199
200    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
201      OnCoordinatesChanged();
202    }
203    private void Coordinates_Reset(object sender, EventArgs e) {
204      OnCoordinatesChanged();
205    }
206    private void Probabilities_ItemChanged(object sender, EventArgs<int> e) {
207      OnProbabilitiesChanged();
208    }
209    private void Probabilities_Reset(object sender, EventArgs e) {
210      OnProbabilitiesChanged();
211    }
212    private void Permutation_ItemChanged(object sender, EventArgs<int> e) {
213      OnPermutationChanged();
214    }
215    private void Permutation_Reset(object sender, EventArgs e) {
216      OnPermutationChanged();
217    }
218    private void Quality_ValueChanged(object sender, EventArgs e) {
219      OnQualityChanged();
220    }
221    #endregion
222  }
223}
Note: See TracBrowser for help on using the repository browser.