Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.TravelingSalesman/3.3/PathTSPTour.cs @ 17578

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

#2582 created branch for Hive Web Job Manager

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