Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/VRPSolution.cs @ 17687

Last change on this file since 17687 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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