Changeset 5207 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations
- Timestamp:
- 01/04/11 02:18:27 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs
r5184 r5207 10 10 using HeuristicLab.PluginInfrastructure; 11 11 using System.Text; 12 using System.Reflection; 12 13 13 14 namespace HeuristicLab.Problems.MetaOptimization { 14 15 [StorableClass] 15 16 public class ParameterConfiguration : Item, IParameterConfiguration, IStorableContent { 16 public bool IsOptimizable { 17 get { return true; } 17 public bool IsOptimizable { 18 get { return true; } 18 19 } 19 20 … … 58 59 59 60 [Storable] 60 protected IItemSet<IItem> validValues;61 public IItemSet<IItem> ValidValues {62 get { return valid Values; }61 protected Type[] validTypes; 62 public Type[] ValidTypes { 63 get { return validTypes; } 63 64 protected set { 64 if (this.valid Values != value) {65 this.valid Values = value;65 if (this.validTypes != value) { 66 this.validTypes = value; 66 67 } 67 68 } … … 124 125 this.parameterDataType = valueParameter.GetType(); 125 126 this.valueDataType = valueParameter.DataType; 126 this.valid Values = GetValidValues(valueParameter);127 this.validTypes = GetValidTypes(valueParameter).ToArray(); 127 128 this.IsNullable = valueParameter.ItemName.StartsWith("Optional"); 128 129 if (IsNullable) { 129 valid Values.Add(new NullValue());130 } 131 this.ValueConfigurations = new CheckedValueConfigurationCollection(this.valid Values);132 this.ActualValue = new ConstrainedValue(valueParameter.Value , valueParameter.DataType, this.ValidValues, this.IsNullable);130 validTypes = new List<Type>(validTypes) { typeof(NullValue) }.ToArray(); 131 } 132 this.ValueConfigurations = new CheckedValueConfigurationCollection(this.validTypes); 133 this.ActualValue = new ConstrainedValue(valueParameter.Value != null ? (IItem)valueParameter.Value.Clone() : null, valueParameter.DataType, this.ValidTypes, this.IsNullable); 133 134 if (Optimize) { 134 135 PopulateValueConfigurations(); … … 139 140 [StorableConstructor] 140 141 protected ParameterConfiguration(bool deserializing) { } 141 protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) : base(original, cloner) { 142 protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) 143 : base(original, cloner) { 142 144 this.parameterName = original.ParameterName; 143 145 this.parameterDataType = original.parameterDataType; 144 146 this.valueDataType = original.ValueDataType; 145 this.valid Values = cloner.Clone(original.ValidValues);147 this.validTypes = original.validTypes.ToArray(); 146 148 this.valueConfigurations = cloner.Clone(original.ValueConfigurations); 147 149 this.ActualValue = cloner.Clone(original.ActualValue); … … 180 182 181 183 private void PopulateValueConfigurations() { 182 foreach ( IItem validValue in this.validValues) {183 if ( validValue is NullValue) {184 foreach (Type t in this.validTypes) { 185 if (t == typeof(NullValue)) { 184 186 this.ValueConfigurations.Add(new NullValueConfiguration()); 185 187 } else { 186 188 IItem val; 187 if (ActualValue.Value != null && ActualValue.ValueDataType == validValue.GetType()) {189 if (ActualValue.Value != null && ActualValue.ValueDataType == t) { 188 190 val = ActualValue.Value; // use existing value for that type (if available) 189 191 } else { 190 val = validValue;192 val = (IItem)Activator.CreateInstance(t); 191 193 } 192 194 this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true); … … 195 197 } 196 198 197 private IItemSet<IItem> GetValidValues(IValueParameter parameter) { 198 if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) { 199 var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { }); 200 return new ItemSet<IItem>(x.Cast<IItem>()); 201 } else { 202 return new ItemSet<IItem>(ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName)); 203 } 199 private IEnumerable<Type> GetValidTypes(IValueParameter parameter) { 200 //if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) { 201 // var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { }); 202 // return new ItemSet<IItem>(x.Cast<IItem>()); 203 //} else { 204 // return new ItemSet<IItem>(ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName)); 205 //} 206 return ApplicationManager.Manager.GetTypes(valueDataType, true); 204 207 } 205 208 … … 278 281 if (handler != null) handler(sender, e); 279 282 } 280 283 281 284 public virtual event EventHandler IsOptimizableChanged; 282 285 private void OnIsOptimizableChanged() { … … 333 336 } 334 337 } 335 parameter.Value = this.ActualValue.Value; 338 var clonedValue = this.ActualValue.Value != null ? (IItem)this.ActualValue.Value.Clone() : null; 339 AdaptValidValues(parameter, clonedValue); 340 parameter.Value = clonedValue; 341 } 342 343 /// <summary> 344 /// Adds value to the ValidValues of the parameter if they don't contain the value 345 /// </summary> 346 private void AdaptValidValues(IValueParameter parameter, IItem value) { 347 // this requires some tricky reflection, because the type is unknown at runtime so parameter.ValidValues cannot be casted to Collection<?> 348 if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) { 349 var validValues = parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { }); 350 Type validValuesType = validValues.GetType(); 351 352 var containsMethod = validValuesType.GetMethod("Contains"); 353 if (!(bool)containsMethod.Invoke(validValues, new object[] { value })) { 354 var addMethod = validValuesType.GetMethod("Add"); 355 addMethod.Invoke(validValues, new object[] { value }); 356 } 357 } 336 358 } 337 359
Note: See TracChangeset
for help on using the changeset viewer.