Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.PTSP/3.3/PathPTSPTour.cs @ 14636

Last change on this file since 14636 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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