Free cookie consent management tool by TermsFeed Policy Generator

source: branches/QAPAlgorithms/HeuristicLab.Problems.OneMax/3.3/OneMaxSolution.cs @ 6377

Last change on this file since 6377 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.OneMax {
31  /// <summary>
32  /// Represents a OneMax solution.
33  /// </summary>
34  [Item("OneMaxSolution", "Represents a OneMax solution.")]
35  [StorableClass]
36  public sealed class OneMaxSolution : Item {
37    public override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VSImageLibrary.Image; }
39    }
40
41    [Storable]
42    private BinaryVector binaryVector;
43    public BinaryVector BinaryVector {
44      get { return binaryVector; }
45      set {
46        if (binaryVector != value) {
47          if (binaryVector != null) DeregisterBinaryVectorEvents();
48          binaryVector = value;
49          if (binaryVector != null) RegisterBinaryVectorEvents();
50          OnBinaryVectorChanged();
51        }
52      }
53    }
54
55    [Storable]
56    private DoubleValue quality;
57    public DoubleValue Quality {
58      get { return quality; }
59      set {
60        if (quality != value) {
61          if (quality != null) DeregisterQualityEvents();
62          quality = value;
63          if (quality != null) RegisterQualityEvents();
64          OnQualityChanged();
65        }
66      }
67    }
68
69    [StorableConstructor]
70    private OneMaxSolution(bool deserializing) : base(deserializing) { }
71    private OneMaxSolution(OneMaxSolution original, Cloner cloner)
72      : base(original, cloner) {
73      binaryVector = cloner.Clone(original.binaryVector);
74      quality = cloner.Clone(original.quality);
75      Initialize();
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new OneMaxSolution(this, cloner);
79    }
80    public OneMaxSolution() : base() { }
81    public OneMaxSolution(BinaryVector binaryVector, DoubleValue quality)
82      : base() {
83      this.binaryVector = binaryVector;
84      this.quality = quality;
85      Initialize();
86    }
87
88    [StorableHook(HookType.AfterDeserialization)]
89    private void AfterDeserialization() {
90      Initialize();
91    }
92
93    private void Initialize() {
94      if (binaryVector != null) RegisterBinaryVectorEvents();
95      if (quality != null) RegisterQualityEvents();
96    }
97
98    #region Events
99    public event EventHandler BinaryVectorChanged;
100    private void OnBinaryVectorChanged() {
101      var changed = BinaryVectorChanged;
102      if (changed != null)
103        changed(this, EventArgs.Empty);
104    }
105
106    public event EventHandler QualityChanged;
107    private void OnQualityChanged() {
108      var changed = QualityChanged;
109      if (changed != null)
110        changed(this, EventArgs.Empty);
111    }
112
113    private void RegisterBinaryVectorEvents() {
114      BinaryVector.ItemChanged += new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
115      BinaryVector.Reset += new EventHandler(BinaryVector_Reset);
116    }
117
118    private void DeregisterBinaryVectorEvents() {
119      BinaryVector.ItemChanged -= new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
120      BinaryVector.Reset -= new EventHandler(BinaryVector_Reset);
121    }
122    private void RegisterQualityEvents() {
123      Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
124    }
125    private void DeregisterQualityEvents() {
126      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
127    }
128
129    private void BinaryVector_ItemChanged(object sender, EventArgs<int> e) {
130      OnBinaryVectorChanged();
131    }
132    private void BinaryVector_Reset(object sender, EventArgs e) {
133      OnBinaryVectorChanged();
134    }
135    private void Quality_ValueChanged(object sender, EventArgs e) {
136      OnQualityChanged();
137    }
138    #endregion
139  }
140}
Note: See TracBrowser for help on using the repository browser.