1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
31 | /// <summary>
|
---|
32 | /// Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.
|
---|
33 | /// </summary>
|
---|
34 | [Item("PathTSPTour", "Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.")]
|
---|
35 | [StorableClass]
|
---|
36 | public sealed class PathTSPTour : Item {
|
---|
37 | public override Image ItemImage {
|
---|
38 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.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 |
|
---|
68 | public PathTSPTour() : base() { }
|
---|
69 | public PathTSPTour(DoubleMatrix coordinates)
|
---|
70 | : base() {
|
---|
71 | this.coordinates = coordinates;
|
---|
72 | Initialize();
|
---|
73 | }
|
---|
74 | public PathTSPTour(DoubleMatrix coordinates, Permutation permutation)
|
---|
75 | : base() {
|
---|
76 | this.coordinates = coordinates;
|
---|
77 | this.permutation = permutation;
|
---|
78 | Initialize();
|
---|
79 | }
|
---|
80 | [StorableConstructor]
|
---|
81 | private PathTSPTour(bool deserializing) : base(deserializing) { }
|
---|
82 |
|
---|
83 | [StorableHook(HookType.AfterDeserialization)]
|
---|
84 | private void Initialize() {
|
---|
85 | if (coordinates != null) RegisterCoordinatesEvents();
|
---|
86 | if (permutation != null) RegisterPermutationEvents();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
90 | PathTSPTour clone = new PathTSPTour();
|
---|
91 | cloner.RegisterClonedObject(this, clone);
|
---|
92 | clone.coordinates = (DoubleMatrix)cloner.Clone(coordinates);
|
---|
93 | clone.permutation = (Permutation)cloner.Clone(permutation);
|
---|
94 | clone.Initialize();
|
---|
95 | return clone;
|
---|
96 | }
|
---|
97 |
|
---|
98 | #region Events
|
---|
99 | public event EventHandler CoordinatesChanged;
|
---|
100 | private void OnCoordinatesChanged() {
|
---|
101 | var changed = CoordinatesChanged;
|
---|
102 | if (changed != null)
|
---|
103 | changed(this, EventArgs.Empty);
|
---|
104 | }
|
---|
105 | public event EventHandler PermutationChanged;
|
---|
106 | private void OnPermutationChanged() {
|
---|
107 | var changed = PermutationChanged;
|
---|
108 | if (changed != null)
|
---|
109 | changed(this, EventArgs.Empty);
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void RegisterCoordinatesEvents() {
|
---|
113 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
114 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
115 | }
|
---|
116 | private void DeregisterCoordinatesEvents() {
|
---|
117 | Coordinates.ItemChanged -= new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
118 | Coordinates.Reset -= new EventHandler(Coordinates_Reset);
|
---|
119 | }
|
---|
120 | private void RegisterPermutationEvents() {
|
---|
121 | Permutation.ItemChanged += new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
|
---|
122 | Permutation.Reset += new EventHandler(Permutation_Reset);
|
---|
123 | }
|
---|
124 | private void DeregisterPermutationEvents() {
|
---|
125 | Permutation.ItemChanged -= new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
|
---|
126 | Permutation.Reset -= new EventHandler(Permutation_Reset);
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
130 | OnCoordinatesChanged();
|
---|
131 | }
|
---|
132 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
133 | OnCoordinatesChanged();
|
---|
134 | }
|
---|
135 | private void Permutation_ItemChanged(object sender, EventArgs<int> e) {
|
---|
136 | OnPermutationChanged();
|
---|
137 | }
|
---|
138 | private void Permutation_Reset(object sender, EventArgs e) {
|
---|
139 | OnPermutationChanged();
|
---|
140 | }
|
---|
141 | #endregion
|
---|
142 | }
|
---|
143 | }
|
---|