Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs @ 18242

Last change on this file since 18242 was 16996, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.MetaOptimization for new persistence

File size: 5.5 KB
RevLine 
[5112]1using System;
[5653]2using System.Collections.Generic;
3using System.Drawing;
[5112]4using HeuristicLab.Common;
5using HeuristicLab.Core;
[16574]6using HEAL.Attic;
[5112]7
8namespace HeuristicLab.Problems.MetaOptimization {
9  // TODO: ItemName/Descr, storability
[16574]10  [StorableType("A5710490-7B38-40B2-943B-5AA55181C816")]
[5653]11  public abstract class ValueConfiguration : NamedItem, IValueConfiguration {
[5112]12    [Storable]
13    protected bool isOptimizable;
[5927]14    public virtual bool IsOptimizable {
[5112]15      get { return isOptimizable; }
16      set {
17        if (this.isOptimizable != value) {
18          this.isOptimizable = value;
19          OnIsOptimizableChanged();
20        }
21      }
22    }
23
24    [Storable]
25    protected bool optimize;
[5653]26    public virtual bool Optimize {
[5112]27      get { return optimize; }
28      set {
29        if (optimize != value) {
[5927]30          if (value == true && !this.IsOptimizable)
31            throw new NotSupportedException("This value is not optimizable.");
32
[5112]33          optimize = value;
34          OnOptimizeChanged();
35          OnToStringChanged();
36        }
37      }
38    }
39
40    [Storable]
41    protected ConstrainedValue actualValue;
42    public virtual ConstrainedValue ActualValue {
43      get { return actualValue; }
44      set {
45        if (this.actualValue != value) {
[5207]46          DeregisterActualValueEvents();
[5112]47          this.actualValue = value;
48          OnValueChanged();
49          OnToStringChanged();
[5207]50          RegisterActualValueEvents();
[5112]51        }
52      }
53    }
54
55    [Storable]
[5361]56    protected int number = 0;
[5359]57    public int Number {
58      get { return number; }
59      set {
60        if (value != number) {
61          number = value;
62          OnToStringChanged();
63        }
64      }
65    }
66
[6489]67    [Storable]
68    protected bool valuesReadOnly = false;
69    public virtual bool ValuesReadOnly {
70      get { return valuesReadOnly; }
71      set {
72        if (value != this.valuesReadOnly) {
73          this.valuesReadOnly = value;
74        }
75      }
76    }
77
[5112]78    #region Constructors and Cloning
[9827]79    protected ValueConfiguration(IItem value, Type valueDataType) {
[5231]80      this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false);
[5112]81      this.IsOptimizable = true;
[9827]82      this.Name = value == null ? "ValueConfiguration" : value.ItemName;
[5112]83    }
84
[9827]85    protected ValueConfiguration() { }
[5112]86    [StorableConstructor]
[16574]87    protected ValueConfiguration(StorableConstructorFlag _) { }
[5112]88    protected ValueConfiguration(ValueConfiguration original, Cloner cloner)
89      : base(original, cloner) {
90      this.actualValue = cloner.Clone(original.ActualValue);
91      this.isOptimizable = original.isOptimizable;
92      this.optimize = original.optimize;
[5359]93      this.number = original.number;
[6489]94      this.valuesReadOnly = original.valuesReadOnly;
[5112]95      RegisterActualValueEvents();
96    }
97    #endregion
98
99    private void RegisterActualValueEvents() {
100      if (this.actualValue != null) this.actualValue.ToStringChanged += new EventHandler(actualValue_ToStringChanged);
101    }
102    private void DeregisterActualValueEvents() {
103      if (this.actualValue != null) this.actualValue.ToStringChanged -= new EventHandler(actualValue_ToStringChanged);
104    }
105
106    #region Events
[5653]107    private void actualValue_ToStringChanged(object sender, EventArgs e) {
[5112]108      OnToStringChanged();
109    }
110    #endregion
[5144]111
[5112]112    #region IItem Members
113    public override string ItemDescription {
114      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemDescription; }
115    }
116
117    public override Image ItemImage {
118      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemImage : base.ItemImage; }
119    }
120
121    public override string ItemName {
122      get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemName; }
123    }
124    #endregion
[5144]125
[5112]126    #region Event Handlers
127    public virtual event EventHandler ValueChanged;
128    protected virtual void OnValueChanged() {
129      var handler = ValueChanged;
130      if (handler != null) handler(this, EventArgs.Empty);
131    }
132
133    public virtual event EventHandler IsOptimizableChanged;
134    private void OnIsOptimizableChanged() {
135      var handler = IsOptimizableChanged;
136      if (handler != null) handler(this, EventArgs.Empty);
137    }
138
139    public virtual event EventHandler OptimizeChanged;
140    protected virtual void OnOptimizeChanged() {
141      var handler = OptimizeChanged;
142      if (handler != null) handler(this, EventArgs.Empty);
143    }
144    #endregion
145
[5359]146    public string NumberedName {
147      get {
148        if (this.number == 0) {
[5361]149          return (ActualValue != null && ActualValue.Value != null) ? ActualValue.Value.ItemName : base.ToString();
[5359]150        } else {
151          return string.Format("{0} {1}", ActualValue.Value.ItemName, number);
152        }
153      }
154    }
155
[5653]156    public abstract void Randomize(IRandom random);
157    public abstract void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator);
158    public abstract void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover);
159    public abstract double CalculateSimilarity(IOptimizable optimizable);
160    public abstract string ParameterInfoString { get; }
161    public abstract void CollectOptimizedParameterNames(List<string> parameterNames, string prefix);
162    public abstract List<IOptimizable> GetAllOptimizables();
[5112]163  }
164}
Note: See TracBrowser for help on using the repository browser.