Changeset 5207 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding
- Timestamp:
- 01/04/11 02:18:27 (14 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/Crossovers/ParameterConfigurationCrossover.cs
r5184 r5207 8 8 using HeuristicLab.Parameters; 9 9 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 10 using System; 10 11 11 12 namespace HeuristicLab.Problems.MetaOptimization { … … 57 58 ParameterConfigurationTree child2 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1]; 58 59 59 //child1.Cross(child2, RandomParameter.ActualValue);60 //this.ChildParameter.ActualValue = child1;61 62 60 child1.Cross(RandomParameter.ActualValue, child2, Cross, this); 63 61 this.ChildParameter.ActualValue = child1; -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurationTree.cs
r5184 r5207 177 177 178 178 public override void Parameterize(IParameterizedItem item) { 179 this.parameters.Clear(); 179 180 base.Parameterize(item); 180 this.parameters.Clear();181 181 ((IAlgorithm)item).CollectParameterValues(this.Parameters); 182 182 } -
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 -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/ConstrainedValue.cs
r5112 r5207 33 33 34 34 [Storable] 35 protected IItemSet<IItem> validValues;36 public IItemSet<IItem> ValidValues {37 get { return valid Values; }35 protected Type[] validTypes; 36 public Type[] ValidTypes { 37 get { return validTypes; } 38 38 protected set { 39 if (this.valid Values != value) {40 this.valid Values = value;39 if (this.validTypes != value) { 40 this.validTypes = value; 41 41 } 42 42 } … … 65 65 [StorableConstructor] 66 66 protected ConstrainedValue(bool deserializing) : base(deserializing) { } 67 public ConstrainedValue(IItem value, Type valueDataType, IItemSet<IItem> validValues, bool isNullable) {67 public ConstrainedValue(IItem value, Type valueDataType, Type[] validTypes, bool isNullable) { 68 68 this.Value = value; 69 69 this.ValueDataType = valueDataType; 70 this.Valid Values = validValues;70 this.ValidTypes = validTypes; 71 71 this.isNullable = isNullable; 72 72 } … … 74 74 this.valueDataType = original.valueDataType; 75 75 this.Value = cloner.Clone(original.value); 76 this.ValidValues = cloner.Clone(original.ValidValues);76 if(original.ValidTypes != null) this.ValidTypes = original.ValidTypes.ToArray(); 77 77 this.isNullable = original.isNullable; 78 78 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/DoubleValueRange.cs
r5184 r5207 25 25 do { 26 26 val = Math.Round((random.NextDouble() * (UpperBound.Value - LowerBound.Value) + LowerBound.Value) / StepSize.Value, 0) * StepSize.Value; 27 } while ( val < LowerBound.Value || val > UpperBound.Value);27 } while (!IsInRange(val)); 28 28 return new DoubleValue(val); 29 29 } 30 30 31 public void Fix(DoubleValue value) { 32 // apply stepsize 31 public void ApplyStepSize(DoubleValue value) { 33 32 value.Value = ((int)Math.Round(value.Value / this.StepSize.Value, 0)) * this.StepSize.Value; 33 } 34 34 35 // repair bounds 36 if (value.Value > this.UpperBound.Value) value.Value = this.UpperBound.Value; 37 if (value.Value < this.LowerBound.Value) value.Value = this.LowerBound.Value; 35 public bool IsInRange(double value) { 36 return value <= this.UpperBound.Value && value >= this.LowerBound.Value; 38 37 } 39 38 40 39 public override IEnumerable<DoubleValue> GetCombinations() { 41 40 var solutions = new List<DoubleValue>(); 42 double value = ((int)Math.Round(LowerBound.Value / StepSize.Value, 0)) * StepSize.Value; 43 if (value < LowerBound.Value) value += StepSize.Value; 41 double value = LowerBound.Value; 44 42 45 43 while (value <= UpperBound.Value) { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/IntValueRange.cs
r5184 r5207 25 25 do { 26 26 val = random.Next(LowerBound.Value / StepSize.Value, UpperBound.Value / StepSize.Value + 1) * StepSize.Value; 27 } while ( val < LowerBound.Value || val > UpperBound.Value);27 } while (!IsInRange(val)); 28 28 return new IntValue(val); 29 29 } 30 30 31 public void Fix(IntValue value) { 32 // apply stepsize 31 public void ApplyStepSize(IntValue value) { 33 32 value.Value = ((int)Math.Round(value.Value / (double)this.StepSize.Value, 0)) * this.StepSize.Value; 33 } 34 34 35 // repair bounds 36 if (value.Value > this.UpperBound.Value) value.Value = this.UpperBound.Value; 37 if (value.Value < this.LowerBound.Value) value.Value = this.LowerBound.Value; 35 public bool IsInRange(int value) { 36 return value <= this.UpperBound.Value && value >= this.LowerBound.Value; 38 37 } 39 38 40 39 public override IEnumerable<IntValue> GetCombinations() { 41 40 var solutions = new List<IntValue>(); 42 int value = (this.LowerBound.Value / StepSize.Value) * StepSize.Value; 43 if (value < this.LowerBound.Value) value += StepSize.Value; 41 int value = this.LowerBound.Value; 44 42 45 43 while (value <= this.UpperBound.Value) { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/PercentValueRange.cs
r5184 r5207 25 25 do { 26 26 val = Math.Round((random.NextDouble() * (UpperBound.Value - LowerBound.Value) + LowerBound.Value) / StepSize.Value, 0) * StepSize.Value; 27 } while ( val < LowerBound.Value || val > UpperBound.Value);27 } while (!IsInRange(val)); 28 28 return new PercentValue(val); 29 29 } … … 33 33 } 34 34 35 public void ApplyStepSize(PercentValue value) { 36 value.Value = ((int)Math.Round(value.Value / this.StepSize.Value, 0)) * this.StepSize.Value; 37 } 38 39 public bool IsInRange(double value) { 40 return value <= this.UpperBound.Value && value >= this.LowerBound.Value; 41 } 42 35 43 public override IEnumerable<PercentValue> GetCombinations() { 36 44 var solutions = new List<PercentValue>(); 37 double value = ((int)Math.Round(LowerBound.Value / StepSize.Value, 0)) * StepSize.Value; 38 if (value < LowerBound.Value) value += StepSize.Value; 45 double value = LowerBound.Value; 39 46 40 47 while (value <= UpperBound.Value) { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/CheckedValueConfigurationCollection.cs
r5112 r5207 24 24 } 25 25 26 public CheckedValueConfigurationCollection(IItemSet<IItem> validValues) { 27 this.validValues = validValues; 26 public CheckedValueConfigurationCollection(IEnumerable<Type> validTypes) { 27 this.validValues = new ItemSet<IItem>(); 28 foreach (Type t in validTypes) { 29 this.validValues.Add((IItem)Activator.CreateInstance(t)); 30 } 28 31 RegisterEvents(); 29 32 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs
r5184 r5207 60 60 set { 61 61 if (this.actualValue != value) { 62 RegisterActualValueEvents();62 DeregisterActualValueEvents(); 63 63 ClearParameterConfigurations(); 64 64 this.actualValue = value; … … 66 66 OnValueChanged(); 67 67 OnToStringChanged(); 68 DeregisterActualValueEvents();68 RegisterActualValueEvents(); 69 69 } 70 70 } … … 87 87 public ValueConfiguration(IItem value, Type valueDataType) { 88 88 this.ParameterConfigurations = new ItemList<IParameterConfiguration>(); 89 var valid Values = new ItemSet<IItem>(ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName));90 this.ActualValue = new ConstrainedValue(value, valueDataType, valid Values, false);89 var validTypes = ApplicationManager.Manager.GetTypes(valueDataType).OrderBy(x => x.Name).ToArray(); 90 this.ActualValue = new ConstrainedValue(value, valueDataType, validTypes, false); 91 91 this.IsOptimizable = true; 92 92 if (actualValue.ValueDataType == typeof(IntValue)) {
Note: See TracChangeset
for help on using the changeset viewer.