Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/PathTSPTour.cs @ 3924

Last change on this file since 3924 was 3616, checked in by swagner, 15 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

  • adapted GA and TSP
  • removed stuff related to visualizers
File size: 6.4 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[3097]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[3306]23using System.Drawing;
[3107]24using HeuristicLab.Common;
[2]25using HeuristicLab.Core;
26using HeuristicLab.Data;
[3097]27using HeuristicLab.Encodings.PermutationEncoding;
[1823]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]29
[3158]30namespace HeuristicLab.Problems.TravelingSalesman {
[884]31  /// <summary>
[3107]32  /// Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.
[884]33  /// </summary>
[3107]34  [Item("PathTSPTour", "Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.")]
[3097]35  [StorableClass]
[3107]36  public sealed class PathTSPTour : Item {
[3306]37    public override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Image; }
39    }
40
[3317]41    [Storable]
[3097]42    private DoubleMatrix coordinates;
43    public DoubleMatrix Coordinates {
44      get { return coordinates; }
45      set {
46        if (coordinates != value) {
[3107]47          if (coordinates != null) DeregisterCoordinatesEvents();
[3097]48          coordinates = value;
[3107]49          if (coordinates != null) RegisterCoordinatesEvents();
[3097]50          OnCoordinatesChanged();
51        }
52      }
[2]53    }
[3317]54    [Storable]
[3097]55    private Permutation permutation;
56    public Permutation Permutation {
57      get { return permutation; }
58      set {
59        if (permutation != value) {
[3107]60          if (permutation != null) DeregisterPermutationEvents();
[3097]61          permutation = value;
[3133]62          if (permutation != null) RegisterPermutationEvents();
[3097]63          OnPermutationChanged();
64        }
65      }
[2]66    }
[3616]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    }
[2]80
[3139]81    public PathTSPTour() : base() { }
82    public PathTSPTour(DoubleMatrix coordinates)
[3097]83      : base() {
[3317]84      this.coordinates = coordinates;
85      Initialize();
[3139]86    }
87    public PathTSPTour(DoubleMatrix coordinates, Permutation permutation)
[3317]88      : base() {
89      this.coordinates = coordinates;
90      this.permutation = permutation;
91      Initialize();
[2]92    }
[3616]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    }
[3317]100    [StorableConstructor]
101    private PathTSPTour(bool deserializing) : base(deserializing) { }
[2]102
[3317]103    [StorableHook(HookType.AfterDeserialization)]
104    private void Initialize() {
105      if (coordinates != null) RegisterCoordinatesEvents();
106      if (permutation != null) RegisterPermutationEvents();
[3616]107      if (quality != null) RegisterQualityEvents();
[3317]108    }
109
[3097]110    public override IDeepCloneable Clone(Cloner cloner) {
[3107]111      PathTSPTour clone = new PathTSPTour();
[3097]112      cloner.RegisterClonedObject(this, clone);
[3317]113      clone.coordinates = (DoubleMatrix)cloner.Clone(coordinates);
114      clone.permutation = (Permutation)cloner.Clone(permutation);
[3616]115      clone.quality = (DoubleValue)cloner.Clone(quality);
[3317]116      clone.Initialize();
[2]117      return clone;
118    }
119
[3107]120    #region Events
[2]121    public event EventHandler CoordinatesChanged;
[3097]122    private void OnCoordinatesChanged() {
[3239]123      var changed = CoordinatesChanged;
124      if (changed != null)
125        changed(this, EventArgs.Empty);
[2]126    }
[3097]127    public event EventHandler PermutationChanged;
128    private void OnPermutationChanged() {
[3239]129      var changed = PermutationChanged;
130      if (changed != null)
131        changed(this, EventArgs.Empty);
[3097]132    }
[3616]133    public event EventHandler QualityChanged;
134    private void OnQualityChanged() {
135      var changed = QualityChanged;
136      if (changed != null)
137        changed(this, EventArgs.Empty);
138    }
[3107]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    }
[3616]156    private void RegisterQualityEvents() {
157      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
158    }
159    private void DeregisterQualityEvents() {
160      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
161    }
[3107]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    }
[3616]175    private void Quality_ValueChanged(object sender, EventArgs e) {
176      OnQualityChanged();
177    }
[3107]178    #endregion
[2]179  }
180}
Note: See TracBrowser for help on using the repository browser.