Changeset 5231 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations
- Timestamp:
- 01/07/11 17:06:40 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs
r5207 r5231 11 11 using System.Text; 12 12 using System.Reflection; 13 using HeuristicLab.Optimization; 13 14 14 15 namespace HeuristicLab.Problems.MetaOptimization { … … 120 121 } 121 122 123 protected IItemSet<IItem> validValues; 124 122 125 #region Constructors and Cloning 123 126 public ParameterConfiguration(string parameterName, IValueParameter valueParameter) { … … 125 128 this.parameterDataType = valueParameter.GetType(); 126 129 this.valueDataType = valueParameter.DataType; 130 this.validValues = GetValidValues(valueParameter); 127 131 this.validTypes = GetValidTypes(valueParameter).ToArray(); 128 132 this.IsNullable = valueParameter.ItemName.StartsWith("Optional"); … … 130 134 validTypes = new List<Type>(validTypes) { typeof(NullValue) }.ToArray(); 131 135 } 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);136 this.ValueConfigurations = new CheckedValueConfigurationCollection(this.CreateValidValues()); 137 this.ActualValue = new ConstrainedValue(valueParameter.Value != null ? (IItem)valueParameter.Value.Clone() : null, valueParameter.DataType, this.CreateValidValues(), this.IsNullable); 134 138 if (Optimize) { 135 139 PopulateValueConfigurations(); … … 145 149 this.parameterDataType = original.parameterDataType; 146 150 this.valueDataType = original.ValueDataType; 151 this.validValues = cloner.Clone(original.validValues); 147 152 this.validTypes = original.validTypes.ToArray(); 148 153 this.valueConfigurations = cloner.Clone(original.ValueConfigurations); … … 186 191 this.ValueConfigurations.Add(new NullValueConfiguration()); 187 192 } else { 188 IItem val; 189 if (ActualValue.Value != null && ActualValue.ValueDataType == t) { 190 val = ActualValue.Value; // use existing value for that type (if available) 191 } else { 192 val = (IItem)Activator.CreateInstance(t); 193 } 193 IItem val = CreateItem(t); // (IItem)Activator.CreateInstance(t); 194 194 this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true); 195 195 } … … 198 198 199 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); 200 if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) { 201 var parameterValidValues = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { }); 202 return parameterValidValues.Cast<object>().Select(x => x.GetType()); 203 } else { 204 return ApplicationManager.Manager.GetTypes(valueDataType, true); 205 } 206 } 207 208 private IItemSet<IItem> GetValidValues(IValueParameter parameter) { 209 if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) { 210 var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { }); 211 return new ItemSet<IItem>(x.Cast<IItem>()); 212 } else { 213 return null; 214 } 215 } 216 217 public IItem CreateItem(Type type) { 218 // no valid values; just instantiate 219 if (validValues == null) 220 return (IItem)Activator.CreateInstance(type); 221 222 if (type == typeof(NullValue)) 223 return new NullValue(); 224 225 // copy value from ValidValues; this ensures that previously set ActualNames for a type are kept 226 IItem value = this.validValues.Where(v => v.GetType() == type).SingleOrDefault(); 227 if (value != null) 228 return (IItem)value.Clone(); 229 230 return null; 231 } 232 233 private ItemSet<IItem> CreateValidValues() { 234 var validValues = new ItemSet<IItem>(); 235 foreach (Type t in this.validTypes) { 236 validValues.Add(CreateItem(t)); 237 } 238 return validValues; 207 239 } 208 240
Note: See TracChangeset
for help on using the changeset viewer.