Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Routing.TSP/3.2/TSPTour.cs @ 2716

Last change on this file since 2716 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Permutation;
29
30namespace HeuristicLab.Routing.TSP {
31  /// <summary>
32  /// Represent the tour of a TSP.
33  /// </summary>
34  public class TSPTour : ItemBase, IVisualizationItem {
35    private DoubleMatrixData myCoordinates;
36    /// <summary>
37    /// Gets or sets the coordinates of the current instance.
38    /// </summary>
39    public DoubleMatrixData Coordinates {
40      get { return myCoordinates; }
41      set { myCoordinates = value; }
42    }
43    private Permutation.Permutation myTour;
44    /// <summary>
45    /// Gets or sets the current permutation/tour of the current instance.
46    /// </summary>
47    public Permutation.Permutation Tour {
48      get { return myTour; }
49      set { myTour = value; }
50    }
51
52
53    /// <summary>
54    /// Initializes a new instance of <see cref="TSPTour"/>.
55    /// </summary>
56    public TSPTour() { }
57    /// <summary>
58    /// Initializes a new instance of <see cref="TSPTour"/> with the given <paramref name="coordinates"/>
59    /// and the given <paramref name="tour"/>.
60    /// </summary>
61    /// <param name="coordinates">The coordinates of the TSP.</param>
62    /// <param name="tour">The tour the current instance should represent.</param>
63    public TSPTour(DoubleMatrixData coordinates, Permutation.Permutation tour) {
64      myCoordinates = coordinates;
65      myTour = tour;
66    }
67
68    /// <summary>
69    /// Clones the current instance (deep clone).
70    /// </summary>
71    /// <remarks>Uses <see cref="Auxiliary.Clone"/> method of class <see cref="Auxiliary"/> to clone
72    /// the coordinates.</remarks>
73    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
74    /// <returns>The cloned object as <see cref="TSPTour"/>.</returns>
75    public override object Clone(IDictionary<Guid, object> clonedObjects) {
76      TSPTour clone = (TSPTour)base.Clone(clonedObjects);
77      clone.myCoordinates = (DoubleMatrixData)Auxiliary.Clone(Coordinates, clonedObjects);
78      clone.myTour = Tour;
79      return clone;
80    }
81
82    /// <summary>
83    /// Creates a new instance of <see cref="TSPTourView"/> to display the current instance.
84    /// </summary>
85    /// <returns>The created view as <see cref="TSPTourView"/>.</returns>
86    public override IView CreateView() {
87      return new TSPTourView(this);
88    }
89
90    /// <summary>
91    /// Occurs when the coordinates of the current instance have been changed.
92    /// </summary>
93    public event EventHandler CoordinatesChanged;
94    /// <summary>
95    /// Fires a new <c>CoordinatesChanged</c> event.
96    /// </summary>
97    protected virtual void OnCoordinatesChanged() {
98      if (CoordinatesChanged != null)
99        CoordinatesChanged(this, new EventArgs());
100    }
101    /// <summary>
102    /// Occurs when the tour of the current instance has been changed.
103    /// </summary>
104    public event EventHandler TourChanged;
105    /// <summary>
106    /// Fires a new <c>TourChanged</c> event.
107    /// </summary>
108    protected virtual void OnTourChanged() {
109      if (TourChanged != null)
110        TourChanged(this, new EventArgs());
111    }
112
113    #region Persistence Methods
114    /// <summary>
115    /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
116    /// </summary>
117    /// <remarks>Calls <see cref="StorableBase.GetXmlNode"/> of base class <see cref="ItemBase"/>. <br/>
118    /// The coordinates and the tour are saved as a child node with the tag names <c>Coordinates</c> and
119    /// <c>Tour</c>.</remarks>
120    /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
121    /// <param name="document">The <see cref="XmlDocument"/> where to save the data.</param>
122    /// <param name="persistedObjects">The dictionary of all already persisted objects.
123    /// (Needed to avoid cycles.)</param>
124    /// <returns>The saved <see cref="XmlNode"/>.</returns>
125    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
126      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
127      node.AppendChild(PersistenceManager.Persist("Coordinates", Coordinates, document, persistedObjects));
128      node.AppendChild(PersistenceManager.Persist("Tour", Tour, document, persistedObjects));
129      return node;
130    }
131    /// <summary>
132    /// Loads the persisted TSP tour from the specified <paramref name="node"/>.
133    /// </summary>
134    /// <remarks>Calls <see cref="StorableBase.Populate"/> of base class
135    /// <see cref="ItemBase"/>.<br/>
136    /// The coordinates and the tour must be saved as child nodes with the tag names <c>Coordinates</c>
137    /// and <c>Tour</c> (see <see cref="GetXmlNode"/>).</remarks>
138    /// <param name="node">The <see cref="XmlNode"/> where the TSP tour is saved.</param>
139    /// <param name="restoredObjects">A dictionary of all already restored objects. (Needed to avoid cycles.)</param>
140    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
141      base.Populate(node, restoredObjects);
142      myCoordinates = (DoubleMatrixData)PersistenceManager.Restore(node.SelectSingleNode("Coordinates"), restoredObjects);
143      myTour = (Permutation.Permutation)PersistenceManager.Restore(node.SelectSingleNode("Tour"), restoredObjects);
144    }
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.