Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/VRPSolution.cs @ 17714

Last change on this file since 17714 was 17710, checked in by abeham, 4 years ago

#2521: working on VRP

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Problems.VehicleRouting.Interfaces;
28using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
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  [StorableType("74CBBEDB-DE6E-4122-AC38-F49DC2B85730")]
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 IVRPEncodedSolution solution;
56    public IVRPEncodedSolution 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 VRPEvaluation evaluation;
69    public VRPEvaluation Evaluation {
70      get { return evaluation; }
71      set {
72        if (evaluation != value) {
73          //if (evaluation != null) DeregisterQualityEvents();
74          evaluation = value;
75          //if (evaluation != null) RegisterQualityEvents();
76          OnEvaluationChanged();
77        }
78      }
79    }
80
81    public VRPSolution() : base() { }
82
83    public VRPSolution(IVRPProblemInstance problemInstance, IVRPEncodedSolution solution, VRPEvaluation evaluation)
84      : base() {
85      this.problemInstance = problemInstance;
86      this.solution = solution;
87      this.evaluation = evaluation;
88
89      Initialize();
90    }
91    [StorableConstructor]
92    private VRPSolution(StorableConstructorFlag _) : base(_) { }
93
94    [StorableHook(HookType.AfterDeserialization)]
95    private void Initialize() {
96      if (problemInstance != null) RegisterProblemInstanceEvents();
97      if (solution != null) RegisterSolutionEvents();
98      // TODO if (evaluation != 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 = cloner.Clone(original.solution);
109      this.evaluation = cloner.Clone(original.evaluation);
110
111      // TODO: this seems very strange
112      if (original.ProblemInstance != null && cloner.ClonedObjectRegistered(original.ProblemInstance))
113        this.ProblemInstance = cloner.Clone(original.ProblemInstance);
114      else
115        this.ProblemInstance = original.ProblemInstance;
116
117      this.Initialize();
118    }
119
120    #region Events
121    public event EventHandler ProblemInstanceChanged;
122    private void OnProblemInstanceChanged() {
123      var changed = ProblemInstanceChanged;
124      if (changed != null)
125        changed(this, EventArgs.Empty);
126    }
127    public event EventHandler SolutionChanged;
128    private void OnSolutionChanged() {
129      var changed = SolutionChanged;
130      if (changed != null)
131        changed(this, EventArgs.Empty);
132    }
133    public event EventHandler EvaluationChanged;
134    private void OnEvaluationChanged() {
135      var changed = EvaluationChanged;
136      if (changed != null)
137        changed(this, EventArgs.Empty);
138    }
139
140    private void RegisterProblemInstanceEvents() {
141      ProblemInstance.ToStringChanged += new EventHandler(ProblemInstance_ToStringChanged);
142    }
143    private void DeregisterProblemInstanceEvents() {
144      ProblemInstance.ToStringChanged -= new EventHandler(ProblemInstance_ToStringChanged);
145    }
146    private void RegisterSolutionEvents() {
147      Solution.ToStringChanged += new EventHandler(Solution_ToStringChanged);
148    }
149    private void DeregisterSolutionEvents() {
150      Solution.ToStringChanged -= new EventHandler(Solution_ToStringChanged);
151    }
152    //private void RegisterQualityEvents() {
153    //  Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
154    //}
155    //private void DeregisterQualityEvents() {
156    //  Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
157    //}
158
159    private void ProblemInstance_ToStringChanged(object sender, EventArgs e) {
160      OnProblemInstanceChanged();
161    }
162    private void Solution_ToStringChanged(object sender, EventArgs e) {
163      OnSolutionChanged();
164    }
165    //private void Quality_ValueChanged(object sender, EventArgs e) {
166    //  OnQualityChanged();
167    //}
168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.