[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
| 22 | using System;
|
---|
[3306] | 23 | using System.Drawing;
|
---|
[3107] | 24 | using HeuristicLab.Common;
|
---|
[2] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[3097] | 27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[1823] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 29 |
|
---|
[3158] | 30 | namespace 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 {
|
---|
[7201] | 37 | public static new Image StaticItemImage {
|
---|
[5287] | 38 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Image; }
|
---|
[3306] | 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 |
|
---|
[4722] | 81 | [StorableConstructor]
|
---|
| 82 | private PathTSPTour(bool deserializing) : base(deserializing) { }
|
---|
| 83 | private PathTSPTour(PathTSPTour original, Cloner cloner)
|
---|
| 84 | : base(original, cloner) {
|
---|
| 85 | this.coordinates = cloner.Clone(original.coordinates);
|
---|
| 86 | this.permutation = cloner.Clone(original.permutation);
|
---|
| 87 | this.quality = cloner.Clone(original.quality);
|
---|
| 88 | Initialize();
|
---|
| 89 | }
|
---|
[3139] | 90 | public PathTSPTour() : base() { }
|
---|
| 91 | public PathTSPTour(DoubleMatrix coordinates)
|
---|
[3097] | 92 | : base() {
|
---|
[3317] | 93 | this.coordinates = coordinates;
|
---|
| 94 | Initialize();
|
---|
[3139] | 95 | }
|
---|
| 96 | public PathTSPTour(DoubleMatrix coordinates, Permutation permutation)
|
---|
[3317] | 97 | : base() {
|
---|
| 98 | this.coordinates = coordinates;
|
---|
| 99 | this.permutation = permutation;
|
---|
| 100 | Initialize();
|
---|
[2] | 101 | }
|
---|
[3616] | 102 | public PathTSPTour(DoubleMatrix coordinates, Permutation permutation, DoubleValue quality)
|
---|
| 103 | : base() {
|
---|
| 104 | this.coordinates = coordinates;
|
---|
| 105 | this.permutation = permutation;
|
---|
| 106 | this.quality = quality;
|
---|
| 107 | Initialize();
|
---|
| 108 | }
|
---|
[2] | 109 |
|
---|
[4722] | 110 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 111 | return new PathTSPTour(this, cloner);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[3317] | 114 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 115 | private void AfterDeserialization() {
|
---|
| 116 | Initialize();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[3317] | 119 | private void Initialize() {
|
---|
| 120 | if (coordinates != null) RegisterCoordinatesEvents();
|
---|
| 121 | if (permutation != null) RegisterPermutationEvents();
|
---|
[3616] | 122 | if (quality != null) RegisterQualityEvents();
|
---|
[3317] | 123 | }
|
---|
| 124 |
|
---|
[3107] | 125 | #region Events
|
---|
[2] | 126 | public event EventHandler CoordinatesChanged;
|
---|
[3097] | 127 | private void OnCoordinatesChanged() {
|
---|
[3239] | 128 | var changed = CoordinatesChanged;
|
---|
| 129 | if (changed != null)
|
---|
| 130 | changed(this, EventArgs.Empty);
|
---|
[2] | 131 | }
|
---|
[3097] | 132 | public event EventHandler PermutationChanged;
|
---|
| 133 | private void OnPermutationChanged() {
|
---|
[3239] | 134 | var changed = PermutationChanged;
|
---|
| 135 | if (changed != null)
|
---|
| 136 | changed(this, EventArgs.Empty);
|
---|
[3097] | 137 | }
|
---|
[3616] | 138 | public event EventHandler QualityChanged;
|
---|
| 139 | private void OnQualityChanged() {
|
---|
| 140 | var changed = QualityChanged;
|
---|
| 141 | if (changed != null)
|
---|
| 142 | changed(this, EventArgs.Empty);
|
---|
| 143 | }
|
---|
[3107] | 144 |
|
---|
| 145 | private void RegisterCoordinatesEvents() {
|
---|
| 146 | Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
| 147 | Coordinates.Reset += new EventHandler(Coordinates_Reset);
|
---|
| 148 | }
|
---|
| 149 | private void DeregisterCoordinatesEvents() {
|
---|
| 150 | Coordinates.ItemChanged -= new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
|
---|
| 151 | Coordinates.Reset -= new EventHandler(Coordinates_Reset);
|
---|
| 152 | }
|
---|
| 153 | private void RegisterPermutationEvents() {
|
---|
| 154 | Permutation.ItemChanged += new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
|
---|
| 155 | Permutation.Reset += new EventHandler(Permutation_Reset);
|
---|
| 156 | }
|
---|
| 157 | private void DeregisterPermutationEvents() {
|
---|
| 158 | Permutation.ItemChanged -= new EventHandler<EventArgs<int>>(Permutation_ItemChanged);
|
---|
| 159 | Permutation.Reset -= new EventHandler(Permutation_Reset);
|
---|
| 160 | }
|
---|
[3616] | 161 | private void RegisterQualityEvents() {
|
---|
| 162 | Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
|
---|
| 163 | }
|
---|
| 164 | private void DeregisterQualityEvents() {
|
---|
| 165 | Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
|
---|
| 166 | }
|
---|
[3107] | 167 |
|
---|
| 168 | private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
| 169 | OnCoordinatesChanged();
|
---|
| 170 | }
|
---|
| 171 | private void Coordinates_Reset(object sender, EventArgs e) {
|
---|
| 172 | OnCoordinatesChanged();
|
---|
| 173 | }
|
---|
| 174 | private void Permutation_ItemChanged(object sender, EventArgs<int> e) {
|
---|
| 175 | OnPermutationChanged();
|
---|
| 176 | }
|
---|
| 177 | private void Permutation_Reset(object sender, EventArgs e) {
|
---|
| 178 | OnPermutationChanged();
|
---|
| 179 | }
|
---|
[3616] | 180 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
| 181 | OnQualityChanged();
|
---|
| 182 | }
|
---|
[3107] | 183 | #endregion
|
---|
[2] | 184 | }
|
---|
| 185 | }
|
---|