Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/ValueConfigurations/ValueConfiguration.cs @ 7840

Last change on this file since 7840 was 7840, checked in by spimming, 12 years ago

#1853:

  • included files from metaopt branch
  • ignored mutation and crossover
  • updated plugin frame
File size: 6.1 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.ParameterConfigurationTreeEncoding {
30  // TODO: ItemName/Descr, storability
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
62          optimize = value;
63          OnOptimizeChanged();
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    public ValueConfiguration(IItem value, Type valueDataType) {
109      this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false);
110      this.IsOptimizable = true;
111    }
112
113    public ValueConfiguration() { }
114    [StorableConstructor]
115    protected ValueConfiguration(bool deserializing) { }
116    protected ValueConfiguration(ValueConfiguration original, Cloner cloner)
117      : base(original, cloner) {
118      this.actualValue = cloner.Clone(original.ActualValue);
119      this.isOptimizable = original.isOptimizable;
120      this.optimize = original.optimize;
121      this.number = original.number;
122      this.valuesReadOnly = original.valuesReadOnly;
123      RegisterActualValueEvents();
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    #endregion
173
174    public string NumberedName {
175      get {
176        if (this.number == 0) {
177          return (ActualValue != null && ActualValue.Value != null) ? ActualValue.Value.ItemName : base.ToString();
178        } else {
179          return string.Format("{0} {1}", ActualValue.Value.ItemName, number);
180        }
181      }
182    }
183
184    public abstract void Randomize(IRandom random);
185    public abstract double CalculateSimilarity(IOptimizable optimizable);
186    public abstract string ParameterInfoString { get; }
187    public abstract void CollectOptimizedParameterNames(List<string> parameterNames, string prefix);
188    public abstract List<IOptimizable> GetAllOptimizables();
189  }
190}
Note: See TracBrowser for help on using the repository browser.