Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/VRPSolution.cs @ 12515

Last change on this file since 12515 was 7907, checked in by svonolfe, 13 years ago

Merged changes from trunk into branch (#1177)

File size: 5.8 KB
Line 
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
22using System;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting {
31  /// <summary>
32  /// Represents a VRP solution which can be visualized in the GUI.
33  /// </summary>
34  [Item("VRPSolution", "Represents a VRP solution which can be visualized in the GUI.")]
35  [StorableClass]
36  public sealed class VRPSolution : Item {
37    public static new Image StaticItemImage {
38      get { return HeuristicLab.Common.Resources.VSImageLibrary.Image; }
39    }
40
41    [Storable]
42    private IVRPProblemInstance problemInstance;
43    public IVRPProblemInstance ProblemInstance {
44      get { return problemInstance; }
45      set {
46        if (problemInstance != value) {
47          if (problemInstance != null) DeregisterProblemInstanceEvents();
48          problemInstance = value;
49          if (problemInstance != null) RegisterProblemInstanceEvents();
50          OnProblemInstanceChanged();
51        }
52      }
53    }
54    [Storable]
55    private IVRPEncoding solution;
56    public IVRPEncoding Solution {
57      get { return solution; }
58      set {
59        if (solution != value) {
60          if (solution != null) DeregisterSolutionEvents();
61          solution = value;
62          if (solution != null) RegisterSolutionEvents();
63          OnSolutionChanged();
64        }
65      }
66    }
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    }
80
81    public VRPSolution() : base() { }
82
83    public VRPSolution(IVRPProblemInstance problemInstance, IVRPEncoding solution, DoubleValue quality)
84      : base() {
85      this.problemInstance = problemInstance;
86      this.solution = solution;
87      this.quality = quality;
88     
89      Initialize();
90    }
91    [StorableConstructor]
92    private VRPSolution(bool deserializing) : base(deserializing) { }
93
94    [StorableHook(HookType.AfterDeserialization)]
95    private void Initialize() {
96      if (problemInstance != null) RegisterProblemInstanceEvents();
97      if (solution != null) RegisterSolutionEvents();
98      if (quality != null) RegisterQualityEvents();
99    }
100
101
102    public override IDeepCloneable Clone(Cloner cloner) {
103      return new VRPSolution(this, cloner);
104    }
105
106    private VRPSolution(VRPSolution original, Cloner cloner)
107      : base(original, cloner) {
108      this.solution = (IVRPEncoding)cloner.Clone(original.solution);
109      this.quality = (DoubleValue)cloner.Clone(original.quality);
110
111      if (original.ProblemInstance != null && cloner.ClonedObjectRegistered(original.ProblemInstance))
112        this.ProblemInstance = (IVRPProblemInstance)cloner.Clone(original.ProblemInstance);
113      else
114        this.ProblemInstance = original.ProblemInstance;
115
116      this.Initialize();
117    }
118
119    #region Events
120    public event EventHandler ProblemInstanceChanged;
121    private void OnProblemInstanceChanged() {
122      var changed = ProblemInstanceChanged;
123      if (changed != null)
124        changed(this, EventArgs.Empty);
125    }
126    public event EventHandler SolutionChanged;
127    private void OnSolutionChanged() {
128      var changed = SolutionChanged;
129      if (changed != null)
130        changed(this, EventArgs.Empty);
131    }
132    public event EventHandler QualityChanged;
133    private void OnQualityChanged() {
134      var changed = QualityChanged;
135      if (changed != null)
136        changed(this, EventArgs.Empty);
137    }
138   
139    private void RegisterProblemInstanceEvents() {
140      ProblemInstance.ToStringChanged += new EventHandler(ProblemInstance_ToStringChanged);
141    }
142    private void DeregisterProblemInstanceEvents() {
143      ProblemInstance.ToStringChanged -= new EventHandler(ProblemInstance_ToStringChanged);
144    }
145    private void RegisterSolutionEvents() {
146      Solution.ToStringChanged += new EventHandler(Solution_ToStringChanged);
147    }
148    private void DeregisterSolutionEvents() {
149      Solution.ToStringChanged -= new EventHandler(Solution_ToStringChanged);
150    }
151    private void RegisterQualityEvents() {
152      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
153    }
154    private void DeregisterQualityEvents() {
155      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
156    }
157
158    private void ProblemInstance_ToStringChanged(object sender, EventArgs e) {
159      OnProblemInstanceChanged();
160    }
161    private void Solution_ToStringChanged(object sender, EventArgs e) {
162      OnSolutionChanged();
163    }
164    private void Quality_ValueChanged(object sender, EventArgs e) {
165      OnQualityChanged();
166    }
167    #endregion
168  }
169}
Note: See TracBrowser for help on using the repository browser.