Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1853:

  • added problem instance selection to CreateExperimentDialog
  • adopted experiment creation
  • minor code improvements
File size: 6.2 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          OnToStringChanged();
64        }
65      }
66    }
67
68    [Storable]
69    protected ConstrainedValue actualValue;
70    public virtual ConstrainedValue ActualValue {
71      get { return actualValue; }
72      set {
73        if (this.actualValue != value) {
74          DeregisterActualValueEvents();
75          this.actualValue = value;
76          OnValueChanged();
77          OnToStringChanged();
78          RegisterActualValueEvents();
79        }
80      }
81    }
82
83    [Storable]
84    protected int number = 0;
85    public int Number {
86      get { return number; }
87      set {
88        if (value != number) {
89          number = value;
90          OnToStringChanged();
91        }
92      }
93    }
94
95    [Storable]
96    protected bool valuesReadOnly = false;
97    public virtual bool ValuesReadOnly {
98      get { return valuesReadOnly; }
99      set {
100        if (value != this.valuesReadOnly) {
101          this.valuesReadOnly = value;
102        }
103      }
104    }
105
106    #region Constructors and Cloning
107    [StorableConstructor]
108    protected ValueConfiguration(bool deserializing) : base(deserializing) { }
109    protected ValueConfiguration(ValueConfiguration original, Cloner cloner)
110      : base(original, cloner) {
111      this.actualValue = cloner.Clone(original.ActualValue);
112      this.isOptimizable = original.isOptimizable;
113      this.optimize = original.optimize;
114      this.number = original.number;
115      this.valuesReadOnly = original.valuesReadOnly;
116      RegisterActualValueEvents();
117    }
118    protected ValueConfiguration() : base() { }
119    protected ValueConfiguration(IItem value, Type valueDataType) {
120      this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false);
121      this.IsOptimizable = true;
122    }
123    #endregion
124
125    private void RegisterActualValueEvents() {
126      if (this.actualValue != null) this.actualValue.ToStringChanged += new EventHandler(actualValue_ToStringChanged);
127    }
128    private void DeregisterActualValueEvents() {
129      if (this.actualValue != null) this.actualValue.ToStringChanged -= new EventHandler(actualValue_ToStringChanged);
130    }
131
132    #region Events
133    private void actualValue_ToStringChanged(object sender, EventArgs e) {
134      OnToStringChanged();
135    }
136    #endregion
137
138    #region IItem Members
139    public override string ItemDescription {
140      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemDescription; }
141    }
142
143    public override Image ItemImage {
144      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemImage : base.ItemImage; }
145    }
146
147    public override string ItemName {
148      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemName; }
149    }
150    #endregion
151
152    #region Event Handlers
153    public virtual event EventHandler ValueChanged;
154    protected virtual void OnValueChanged() {
155      var handler = ValueChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
158
159    public virtual event EventHandler IsOptimizableChanged;
160    private void OnIsOptimizableChanged() {
161      var handler = IsOptimizableChanged;
162      if (handler != null) handler(this, EventArgs.Empty);
163    }
164
165    public virtual event EventHandler OptimizeChanged;
166    protected virtual void OnOptimizeChanged() {
167      var handler = OptimizeChanged;
168      if (handler != null) handler(this, EventArgs.Empty);
169    }
170    #endregion
171
172    public string NumberedName {
173      get {
174        if (this.number == 0) {
175          return (ActualValue != null && ActualValue.Value != null) ? ActualValue.Value.ItemName : base.ToString();
176        } else {
177          return string.Format("{0} {1}", ActualValue.Value.ItemName, number);
178        }
179      }
180    }
181
182    public abstract void Randomize(IRandom random);
183    public abstract double CalculateSimilarity(IOptimizable optimizable);
184    public abstract string ParameterInfoString { get; }
185    public abstract void CollectOptimizedParameterNames(List<string> parameterNames, string prefix);
186    public abstract List<IOptimizable> GetAllOptimizables();
187  }
188}
Note: See TracBrowser for help on using the repository browser.