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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.OneMax {
|
---|
31 | /// <summary>
|
---|
32 | /// Represents a OneMax solution.
|
---|
33 | /// </summary>
|
---|
34 | [Item("OneMaxSolution", "Represents a OneMax solution.")]
|
---|
35 | [StorableClass]
|
---|
36 | public class OneMaxSolution : Item {
|
---|
37 | public override Image ItemImage {
|
---|
38 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.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 | public OneMaxSolution() : base() { }
|
---|
70 | public OneMaxSolution(BinaryVector binaryVector, DoubleValue quality)
|
---|
71 | : base() {
|
---|
72 | this.binaryVector = binaryVector;
|
---|
73 | this.quality = quality;
|
---|
74 | Initialize();
|
---|
75 | }
|
---|
76 | [StorableConstructor]
|
---|
77 | private OneMaxSolution(bool deserializing) : base(deserializing) { }
|
---|
78 |
|
---|
79 | [StorableHook(HookType.AfterDeserialization)]
|
---|
80 | private void Initialize() {
|
---|
81 | if (binaryVector != null) RegisterBinaryVectorEvents();
|
---|
82 | if (quality != null) RegisterQualityEvents();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
86 | OneMaxSolution clone = new OneMaxSolution();
|
---|
87 | cloner.RegisterClonedObject(this, clone);
|
---|
88 | clone.binaryVector = (BinaryVector)cloner.Clone(binaryVector);
|
---|
89 | clone.quality = (DoubleValue)cloner.Clone(quality);
|
---|
90 | clone.Initialize();
|
---|
91 | return clone;
|
---|
92 | }
|
---|
93 |
|
---|
94 | #region Events
|
---|
95 | public event EventHandler BinaryVectorChanged;
|
---|
96 | private void OnBinaryVectorChanged() {
|
---|
97 | var changed = BinaryVectorChanged;
|
---|
98 | if (changed != null)
|
---|
99 | changed(this, EventArgs.Empty);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public event EventHandler QualityChanged;
|
---|
103 | private void OnQualityChanged() {
|
---|
104 | var changed = QualityChanged;
|
---|
105 | if (changed != null)
|
---|
106 | changed(this, EventArgs.Empty);
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void RegisterBinaryVectorEvents() {
|
---|
110 | BinaryVector.ItemChanged += new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
|
---|
111 | BinaryVector.Reset += new EventHandler(BinaryVector_Reset);
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void DeregisterBinaryVectorEvents() {
|
---|
115 | BinaryVector.ItemChanged -= new EventHandler<EventArgs<int>>(BinaryVector_ItemChanged);
|
---|
116 | BinaryVector.Reset -= new EventHandler(BinaryVector_Reset);
|
---|
117 | }
|
---|
118 | private void RegisterQualityEvents() {
|
---|
119 | Quality.ValueChanged += new EventHandler(Quality_ValueChanged);
|
---|
120 | }
|
---|
121 | private void DeregisterQualityEvents() {
|
---|
122 | Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void BinaryVector_ItemChanged(object sender, EventArgs<int> e) {
|
---|
126 | OnBinaryVectorChanged();
|
---|
127 | }
|
---|
128 | private void BinaryVector_Reset(object sender, EventArgs e) {
|
---|
129 | OnBinaryVectorChanged();
|
---|
130 | }
|
---|
131 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
132 | OnQualityChanged();
|
---|
133 | }
|
---|
134 | #endregion
|
---|
135 | }
|
---|
136 | }
|
---|