Changeset 5267 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding
- Timestamp:
- 01/10/11 15:48:06 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs
r5262 r5267 121 121 } 122 122 123 [Storable] 123 124 protected IItemSet<IItem> validValues; 124 125 … … 134 135 validTypes = new List<Type>(validTypes) { typeof(NullValue) }.ToArray(); 135 136 } 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); 137 this.ValueConfigurations = new CheckedValueConfigurationCollection(); 138 this.ActualValue = new ConstrainedValue( 139 valueParameter.Value != null ? (IItem)valueParameter.Value.Clone() : null, 140 valueParameter.DataType, 141 this.validValues != null ? new ItemSet<IItem>(this.validValues) : CreateValidValues(), 142 this.IsNullable); 138 143 if (Optimize) { 139 144 PopulateValueConfigurations(); … … 191 196 this.ValueConfigurations.Add(new NullValueConfiguration()); 192 197 } else { 193 IItem val = CreateItem(t); // (IItem)Activator.CreateInstance(t); 198 IItem val; 199 if (ActualValue.Value != null && ActualValue.ValueDataType == t) { 200 val = (IItem)ActualValue.Value.Clone(); // use existing value for that type (if available) 201 } else { 202 val = CreateItem(t); 203 } 194 204 this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true); 195 205 } … … 198 208 199 209 private IEnumerable<Type> GetValidTypes(IValueParameter parameter) { 210 // in case of IOperator return empty list, otherwise hundreds of types would be returned. this mostly occurs for Successor which will never be optimized 211 if (parameter.DataType == typeof(IOperator)) 212 return new List<Type>(); 213 200 214 if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) { 201 215 var parameterValidValues = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { }); … … 209 223 if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) { 210 224 var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { }); 211 return new ItemSet<IItem>(x.Cast<IItem>().Select(y => (IItem)y.Clone())); 225 return new ItemSet<IItem>(x.Cast<IItem>().Select(y => (IItem)y.Clone())); // cloning actually saves memory, because references to event-subscribers are removed 212 226 } else { 213 227 return null; … … 226 240 IItem value = this.validValues.Where(v => v.GetType() == type).SingleOrDefault(); 227 241 if (value != null) 228 return (IItem)value.Clone();242 return value; 229 243 230 244 return null; … … 234 248 var validValues = new ItemSet<IItem>(); 235 249 foreach (Type t in this.validTypes) { 236 validValues.Add(CreateItem(t)); 250 try { 251 var val = CreateItem(t); 252 validValues.Add(val); 253 } 254 catch (MissingMethodException) { /* Constructor is missing, don't use those types */ } 237 255 } 238 256 return validValues;
Note: See TracChangeset
for help on using the changeset viewer.