Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/ValueConfigurations/ValueConfiguration.cs @ 14853

Last change on this file since 14853 was 8535, checked in by jkarder, 12 years ago

#1853:

  • enhanced combinations count calculation
  • restructured code
  • minor code improvements
  • added license information
File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
30  [Item("ValueConfiguration", "Represents a value configuration.")]
31  [StorableClass]
32  public abstract class ValueConfiguration : NamedItem, IValueConfiguration {
33    public override bool CanChangeName {
34      get { return true; }
35    }
36
37    public override bool CanChangeDescription {
38      get { return true; }
39    }
40
41    [Storable]
42    protected bool isOptimizable;
43    public virtual bool IsOptimizable {
44      get { return isOptimizable; }
45      set {
46        if (this.isOptimizable != value) {
47          this.isOptimizable = value;
48          OnIsOptimizableChanged();
49        }
50      }
51    }
52
53    [Storable]
54    protected bool optimize;
55    public virtual bool Optimize {
56      get { return optimize; }
57      set {
58        if (optimize != value) {
59          if (value == true && !this.IsOptimizable)
60            throw new NotSupportedException("This value is not optimizable.");
61          optimize = value;
62          OnOptimizeChanged();
63          OnCombinationsCountChanged();
64          OnToStringChanged();
65        }
66      }
67    }
68
69    [Storable]
70    protected ConstrainedValue actualValue;
71    public virtual ConstrainedValue ActualValue {
72      get { return actualValue; }
73      set {
74        if (this.actualValue != value) {
75          DeregisterActualValueEvents();
76          this.actualValue = value;
77          OnValueChanged();
78          OnToStringChanged();
79          RegisterActualValueEvents();
80        }
81      }
82    }
83
84    [Storable]
85    protected int number = 0;
86    public int Number {
87      get { return number; }
88      set {
89        if (value != number) {
90          number = value;
91          OnToStringChanged();
92        }
93      }
94    }
95
96    [Storable]
97    protected bool valuesReadOnly = false;
98    public virtual bool ValuesReadOnly {
99      get { return valuesReadOnly; }
100      set {
101        if (value != this.valuesReadOnly) {
102          this.valuesReadOnly = value;
103        }
104      }
105    }
106
107    #region Constructors and Cloning
108    [StorableConstructor]
109    protected ValueConfiguration(bool deserializing) : base(deserializing) { }
110    protected ValueConfiguration(ValueConfiguration original, Cloner cloner)
111      : base(original, cloner) {
112      this.actualValue = cloner.Clone(original.ActualValue);
113      this.isOptimizable = original.isOptimizable;
114      this.optimize = original.optimize;
115      this.number = original.number;
116      this.valuesReadOnly = original.valuesReadOnly;
117      RegisterActualValueEvents();
118    }
119    protected ValueConfiguration() : base() { }
120    protected ValueConfiguration(IItem value, Type valueDataType)
121      : base() {
122      this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false);
123      this.IsOptimizable = true;
124    }
125    #endregion
126
127    private void RegisterActualValueEvents() {
128      if (this.actualValue != null) this.actualValue.ToStringChanged += new EventHandler(actualValue_ToStringChanged);
129    }
130    private void DeregisterActualValueEvents() {
131      if (this.actualValue != null) this.actualValue.ToStringChanged -= new EventHandler(actualValue_ToStringChanged);
132    }
133
134    #region Events
135    private void actualValue_ToStringChanged(object sender, EventArgs e) {
136      OnToStringChanged();
137    }
138    #endregion
139
140    #region IItem Members
141    public override string ItemDescription {
142      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemDescription; }
143    }
144
145    public override Image ItemImage {
146      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemImage : base.ItemImage; }
147    }
148
149    public override string ItemName {
150      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemName; }
151    }
152    #endregion
153
154    #region Event Handlers
155    public virtual event EventHandler ValueChanged;
156    protected virtual void OnValueChanged() {
157      var handler = ValueChanged;
158      if (handler != null) handler(this, EventArgs.Empty);
159    }
160
161    public virtual event EventHandler IsOptimizableChanged;
162    private void OnIsOptimizableChanged() {
163      var handler = IsOptimizableChanged;
164      if (handler != null) handler(this, EventArgs.Empty);
165    }
166
167    public virtual event EventHandler OptimizeChanged;
168    protected virtual void OnOptimizeChanged() {
169      var handler = OptimizeChanged;
170      if (handler != null) handler(this, EventArgs.Empty);
171    }
172
173    public event EventHandler CombinationsCountChanged;
174    protected void OnCombinationsCountChanged() {
175      var handler = CombinationsCountChanged;
176      if (handler != null) handler(this, EventArgs.Empty);
177    }
178    #endregion
179
180    public string NumberedName {
181      get {
182        if (this.number == 0) {
183          return (ActualValue != null && ActualValue.Value != null) ? ActualValue.Value.ItemName : base.ToString();
184        } else {
185          return string.Format("{0} {1}", ActualValue.Value.ItemName, number);
186        }
187      }
188    }
189
190    public abstract void Randomize(IRandom random);
191    public abstract double CalculateSimilarity(IOptimizable optimizable);
192    public abstract string ParameterInfoString { get; }
193    public abstract void CollectOptimizedParameterNames(List<string> parameterNames, string prefix);
194    public abstract List<IOptimizable> GetAllOptimizables();
195  }
196}
Note: See TracBrowser for help on using the repository browser.