Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.OneMax/3.3/OneMaxSolution.cs @ 3732

Last change on this file since 3732 was 3642, checked in by svonolfe, 15 years ago

Added anaylzers for the onemax problem (#999)

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