Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4374 was 4374, checked in by svonolfe, 14 years ago

Added analyzers and views (#1177)

File size: 5.5 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 override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.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    public override IDeepCloneable Clone(Cloner cloner) {
102      VRPSolution clone = new VRPSolution();
103      cloner.RegisterClonedObject(this, clone);
104      clone.problemInstance = (IVRPProblemInstance)cloner.Clone(problemInstance);
105      clone.solution = (IVRPEncoding)cloner.Clone(solution);
106      clone.quality = (DoubleValue)cloner.Clone(quality);
107     
108      clone.Initialize();
109      return clone;
110    }
111
112    #region Events
113    public event EventHandler ProblemInstanceChanged;
114    private void OnProblemInstanceChanged() {
115      var changed = ProblemInstanceChanged;
116      if (changed != null)
117        changed(this, EventArgs.Empty);
118    }
119    public event EventHandler SolutionChanged;
120    private void OnSolutionChanged() {
121      var changed = SolutionChanged;
122      if (changed != null)
123        changed(this, EventArgs.Empty);
124    }
125    public event EventHandler QualityChanged;
126    private void OnQualityChanged() {
127      var changed = QualityChanged;
128      if (changed != null)
129        changed(this, EventArgs.Empty);
130    }
131   
132    private void RegisterProblemInstanceEvents() {
133      ProblemInstance.ToStringChanged += new EventHandler(ProblemInstance_ToStringChanged);
134    }
135    private void DeregisterProblemInstanceEvents() {
136      ProblemInstance.ToStringChanged -= new EventHandler(ProblemInstance_ToStringChanged);
137    }
138    private void RegisterSolutionEvents() {
139      Solution.ToStringChanged += new EventHandler(Solution_ToStringChanged);
140    }
141    private void DeregisterSolutionEvents() {
142      Solution.ToStringChanged -= new EventHandler(Solution_ToStringChanged);
143    }
144    private void RegisterQualityEvents() {
145      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
146    }
147    private void DeregisterQualityEvents() {
148      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
149    }
150
151    private void ProblemInstance_ToStringChanged(object sender, EventArgs e) {
152      OnProblemInstanceChanged();
153    }
154    private void Solution_ToStringChanged(object sender, EventArgs e) {
155      OnSolutionChanged();
156    }
157    private void Quality_ValueChanged(object sender, EventArgs e) {
158      OnQualityChanged();
159    }
160    #endregion
161  }
162}
Note: See TracBrowser for help on using the repository browser.