Changeset 4830 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings
- Timestamp:
- 11/18/10 00:53:15 (14 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings
- Files:
-
- 4 added
- 6 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ParameterConfiguration.cs
r4525 r4830 6 6 using HeuristicLab.Common; 7 7 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 8 using HeuristicLab.Data; 9 using HeuristicLab.Parameters; 8 10 9 11 namespace HeuristicLab.Problems.MetaOptimization { 10 12 [StorableClass] 11 public abstract class ParameterConfiguration : IParameterConfiguration { 12 [Storable] 13 public IParameter Parameter { get; set; } 14 15 [Storable] 16 public string Category { get; set; } 13 public class ParameterConfiguration : DeepCloneable, IParameterConfiguration, IStorableContent { 14 protected ParameterConfiguration parentConfiguration; 15 16 [Storable] 17 public string Filename { get; set; } 18 19 [Storable] 20 protected bool optimizationEnabled; 21 public bool OptimizationEnabled { 22 get { return optimizationEnabled; } 23 set { 24 if (optimizationEnabled != value) { 25 optimizationEnabled = value; 26 OnOptimizationEnabledChanged(); 27 } 28 } 29 } 30 31 [Storable] 32 protected string parameterName; 33 public string ParameterName { 34 get { return parameterName; } 35 set { 36 if (parameterName != value) { 37 parameterName = value; 38 } 39 } 40 } 41 42 [Storable] 43 protected IItemList<IParameterConfiguration> childParameterConfigurations = new ItemList<IParameterConfiguration>(); 44 public IItemList<IParameterConfiguration> ChildParameterConfigurations { 45 get { return childParameterConfigurations; } 46 } 47 48 public virtual IParameter Parameter { 49 get { 50 return parentConfiguration.GetChildParameter(this.parameterName); 51 } 52 } 53 54 protected IItem value; 55 public IItem Value { 56 get { return value; } 57 set { 58 if (this.value != value) { 59 ClearChildParameterConfigurations(); 60 OnValueChanging(); 61 this.value = value; 62 if(this.value is IParameterizedNamedItem) AddChildParameterConfigurations(this.value as IParameterizedNamedItem); 63 OnValueChanged(); 64 } 65 } 66 } 67 68 public ParameterConfiguration(ParameterConfiguration parentConfiguration, string parameterName, IItem value) { 69 this.parentConfiguration = parentConfiguration; 70 this.ParameterName = parameterName; 71 this.Value = value; 72 } 17 73 18 74 public ParameterConfiguration() { } 19 20 75 [StorableConstructor] 21 76 protected ParameterConfiguration(bool deserializing) { } 22 23 public ParameterConfiguration(IParameter parameter, string category) { 24 this.Parameter = parameter; 25 this.Category = category; 26 Parameter.NameChanged += this.NameChanged; 27 Parameter.NameChanging += this.NameChanging; 28 Parameter.DescriptionChanged += this.DescriptionChanged; 29 Parameter.ItemImageChanged += this.ItemImageChanged; 30 parameter.ToStringChanged += this.ToStringChanged; 31 } 32 33 public bool CanChangeDescription { 34 get { return false; } 35 } 36 37 public bool CanChangeName { 38 get { return false; } 39 } 40 41 public string Description { 77 protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) 78 : base(original, cloner) { 79 this.OptimizationEnabled = original.optimizationEnabled; 80 this.ParameterName = original.parameterName; 81 this.Value = cloner.Clone(original.Value); 82 this.childParameterConfigurations = cloner.Clone(original.childParameterConfigurations); 83 } 84 public override IDeepCloneable Clone(Cloner cloner) { 85 return new ParameterConfiguration(this, cloner); 86 } 87 88 protected virtual IValueParameter GetChildParameter(string childParameterName) { 89 return (IValueParameter)((IParameterizedItem)Value).Parameters[childParameterName]; 90 } 91 92 protected virtual void AddChildParameterConfigurations(IParameterizedNamedItem parameterizedItem) { 93 foreach (var childParameter in parameterizedItem.Parameters) { 94 var pc = ParameterConfiguration.Create(this, parameterizedItem, childParameter); 95 if (pc != null) this.childParameterConfigurations.Add(pc); 96 } 97 } 98 protected virtual void ClearChildParameterConfigurations() { 99 childParameterConfigurations.Clear(); 100 } 101 102 #region INamedItem Properties 103 public virtual string Name { 104 get { return Parameter.Name; } 105 set { throw new NotSupportedException(); } 106 } 107 public virtual string Description { 42 108 get { return Parameter.Description; } 43 109 set { throw new NotSupportedException(); } 44 110 } 45 public string Name { 46 get { return Parameter.Name; } 47 set { throw new NotSupportedException(); } 48 } 49 50 public string ItemDescription { 111 public virtual bool CanChangeDescription { 112 get { return false; } 113 } 114 public virtual bool CanChangeName { 115 get { return false; } 116 } 117 public virtual string ItemDescription { 51 118 get { return Parameter.ItemDescription; } 52 119 } 53 54 public System.Drawing.Image ItemImage { 120 public virtual System.Drawing.Image ItemImage { 55 121 get { return Parameter.ItemImage; } 56 122 } 57 58 public string ItemName { 123 public virtual string ItemName { 59 124 get { return Parameter.ItemName; } 60 125 } 61 62 public Version ItemVersion { 126 public virtual Version ItemVersion { 63 127 get { return Parameter.ItemVersion; } 64 128 } 129 #endregion 65 130 66 131 #region Events 67 132 public virtual event EventHandler NameChanged; 133 protected virtual void OnNameChanged(object sender, EventArgs e) { 134 var handler = NameChanged; 135 if (handler != null) handler(sender, e); 136 } 137 68 138 public virtual event EventHandler<CancelEventArgs<string>> NameChanging; 139 protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) { 140 var handler = NameChanging; 141 if (handler != null) handler(sender, e); 142 } 143 69 144 public virtual event EventHandler DescriptionChanged; 145 protected virtual void OnDescriptionChanged(object sender, EventArgs e) { 146 var handler = DescriptionChanged; 147 if (handler != null) handler(sender, e); 148 } 149 70 150 public virtual event EventHandler ItemImageChanged; 151 protected virtual void OnItemImageChanged(object sender, EventArgs e) { 152 var handler = ItemImageChanged; 153 if (handler != null) handler(sender, e); 154 } 155 71 156 public virtual event EventHandler ToStringChanged; 72 73 protected virtual void OnToStringChanged() { 157 protected virtual void OnStringChanged(object sender, EventArgs e) { 74 158 var handler = ToStringChanged; 159 if (handler != null) handler(this, e); // important to set 'this' as sender 160 } 161 162 public virtual event EventHandler OptimizationEnabledChanged; 163 protected virtual void OnOptimizationEnabledChanged() { 164 var handler = OptimizationEnabledChanged; 75 165 if (handler != null) handler(this, EventArgs.Empty); 76 166 } 167 168 public virtual event EventHandler ValueChanging; 169 protected virtual void OnValueChanging() { 170 var handler = ValueChanging; 171 if (handler != null) handler(this, EventArgs.Empty); 172 } 173 174 public virtual event EventHandler ValueChanged; 175 protected virtual void OnValueChanged() { 176 var handler = ValueChanged; 177 if (handler != null) handler(this, EventArgs.Empty); 178 } 77 179 #endregion 78 180 79 #region Cloning80 public virtual IDeepCloneable Clone(Cloner cloner) {81 ParameterConfiguration clone = (ParameterConfiguration)Activator.CreateInstance(this.GetType(), true);82 cloner.RegisterClonedObject(this, clone);83 clone.Parameter = (IParameter)cloner.Clone(this.Parameter);84 clone.Category = this.Category;85 return clone;86 }87 88 public object Clone() {89 return Clone(new Cloner());90 }91 #endregion92 93 181 public override string ToString() { 94 return string.Format("{0}.{1}", Category, Parameter.Name); 95 } 96 97 public abstract void SetParameterWithRandomValue(IRandom random); 182 return string.Format("{0} ({1})", ParameterName, Value); 183 } 184 185 public static IParameterConfiguration Create(IParameterizedNamedItem parent) { 186 return new RootParameterConfiguration(parent); 187 } 188 189 public static IParameterConfiguration Create(ParameterConfiguration parentConfiguration, IParameterizedNamedItem parent, IParameter parameter) { 190 if (parameter is IValueParameter) { 191 IValueParameter valueParameter = parameter as IValueParameter; 192 193 if (typeof(IntValue).IsAssignableFrom(parameter.DataType)) { 194 return new IntValueParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value); 195 } else if (typeof(IParameterizedItem).IsAssignableFrom(parameter.DataType)) { 196 return new ParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value); 197 } else { 198 // todo 199 return new IntValueParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value); 200 } 201 } 202 return null; 203 } 204 205 public void Parameterize(IParameterizedItem parameterizedItem) { 206 throw new NotImplementedException(); 207 } 98 208 99 209 }
Note: See TracChangeset
for help on using the changeset viewer.