Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7461 was 7203, checked in by svonolfe, 13 years ago

Added static item image according to #1651

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