- Timestamp:
- 01/04/11 02:18:27 (14 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
- Files:
-
- 14 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)) { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs
r5184 r5207 10 10 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 11 11 using System.Collections.Generic; 12 using HeuristicLab.Algorithms.GeneticAlgorithm; 13 using System.Threading.Tasks; 14 using System.Diagnostics; 15 using System.Reflection; 12 16 13 17 namespace HeuristicLab.Problems.MetaOptimization { … … 18 22 [StorableClass] 19 23 public class ParameterConfigurationEvaluator : SingleSuccessorOperator, IParameterConfigurationEvaluator { 20 private bool algorithmStopped; 21 private bool algorithmExceptionOccured; 24 private const double PenaltyQuality = 100000.0; // todo: use something better 22 25 23 26 public ILookupParameter<DoubleValue> QualityParameter { … … 54 57 protected ParameterConfigurationEvaluator(ParameterConfigurationEvaluator original, Cloner cloner) 55 58 : base(original, cloner) { 56 this.algorithmStopped = original.algorithmStopped;57 59 } 58 60 public override IDeepCloneable Clone(Cloner cloner) { … … 61 63 62 64 public override IOperation Apply() { 63 EngineAlgorithm algorithm = AlgorithmParameter.ActualValue;65 EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue.Clone(); 64 66 IItemList<ISingleObjectiveProblem> problems = ProblemsParameter.ActualValue; 65 67 66 68 // set parameters 67 69 ParameterConfigurationParameter.ActualValue.Parameterize(algorithm); 68 algorithm.Problem = problems.First();69 70 algorithm.StoreAlgorithmInEachRun = false; 70 71 algorithmStopped = false;72 algorithmExceptionOccured = false;73 algorithm.Stopped += new EventHandler(algorithm_Stopped);74 algorithm.Paused += new EventHandler(algorithm_Paused);75 algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);76 71 77 72 List<double> qualities = new List<double>(); … … 81 76 82 77 foreach (ISingleObjectiveProblem problem in problems) { 83 algorithm.Problem = problem;78 algorithm.Problem = (IProblem)problem.Clone(); 84 79 85 80 for (int i = 0; i < Repetitions.Value; i++) { 86 81 algorithm.Prepare(); 87 algorithm.Start();88 while (!algorithmStopped) {89 Thread.Sleep(200); // wait for algorithm to complete; do not rely on Algorithm.ExecutionState here, because change of ExecutionState happens before Run is added (which causes problems because Algorithm might get cloned when its started already)90 }91 82 92 if (algorithmExceptionOccured) { 83 AlgorithmExecutor executor = new AlgorithmExecutor(algorithm); 84 executor.StartSync(); 85 86 if (algorithm.ExecutionState == ExecutionState.Paused) { 93 87 // this parametercombination was bad. set penalty for this solution 94 qualities.Add( double.MaxValue); // todo: respect Maximization88 qualities.Add(PenaltyQuality); // todo: respect Maximization; problem: this messes up average value; solution: use currently worst quality*2 95 89 executionTimes.Add(algorithm.ExecutionTime); 96 90 } else { … … 103 97 algorithm.Runs.Last().Parameters.Add("Problem", problem); 104 98 } 105 algorithmStopped = false;106 algorithmExceptionOccured = false;107 99 } 108 100 } 109 110 algorithm.Stopped -= new EventHandler(algorithm_Stopped);111 algorithm.Paused -= new EventHandler(algorithm_Paused);112 algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);113 101 algorithm.Prepare(); 114 102 … … 130 118 } 131 119 132 private void algorithm_Paused(object sender, EventArgs e) {133 algorithmStopped = true;134 }135 136 private void algorithm_Stopped(object sender, EventArgs e) {137 algorithmStopped = true;138 }139 140 void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {141 algorithmExceptionOccured = true;142 }143 144 120 public static double Variance(IEnumerable<double> source) { 145 121 double avg = source.Average(); … … 151 127 return Math.Sqrt(source.Variance()); 152 128 } 129 } 153 130 131 public class AlgorithmExecutor { 132 private EngineAlgorithm algorithm; 133 private AutoResetEvent waitHandle = new AutoResetEvent(false); 154 134 135 public AlgorithmExecutor(EngineAlgorithm algorithm) { 136 this.algorithm = algorithm; 137 } 138 139 public void StartSync() { 140 algorithm.Stopped += new EventHandler(algorithm_Stopped); 141 algorithm.Paused += new EventHandler(algorithm_Paused); 142 algorithm.Start(); 143 waitHandle.WaitOne(); 144 waitHandle.Dispose(); 145 algorithm.Stopped -= new EventHandler(algorithm_Stopped); 146 algorithm.Paused -= new EventHandler(algorithm_Paused); 147 } 148 149 void algorithm_Paused(object sender, EventArgs e) { 150 waitHandle.Set(); 151 } 152 153 void algorithm_Stopped(object sender, EventArgs e) { 154 waitHandle.Set(); 155 } 155 156 } 156 157 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IParameterConfiguration.cs
r5144 r5207 10 10 Type ParameterDataType { get; } 11 11 ICheckedValueConfigurationCollection ValueConfigurations { get; } 12 IItemSet<IItem> ValidValues { get; }12 Type[] ValidTypes { get; } 13 13 int ActualValueConfigurationIndex { get; set; } 14 14 -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Operators/Manipulators/NormalDoubleValueManipulator.cs
r5111 r5207 36 36 37 37 public static void ApplyStatic(IRandom random, DoubleValue value, DoubleValueRange range) { 38 var vector = new RealVector(new double[] { value.Value }); 39 var strategy = new RealVector(new double[] { (range.UpperBound.Value - range.LowerBound.Value) / 5}); 40 NormalAllPositionsManipulator.Apply(random, vector, strategy); 41 value.Value = vector[0]; 42 range.Fix(value); 38 bool ok = false; 39 RealVector strategy = new RealVector(new double[] { (range.UpperBound.Value - range.LowerBound.Value) / 15}); 40 RealVector vector = new RealVector(1); 41 double val = value.Value; 42 43 while (!ok) { 44 vector[0] = val; 45 NormalAllPositionsManipulator.Apply(random, vector, strategy); 46 value.Value = vector[0]; 47 range.ApplyStepSize(value); 48 ok = range.IsInRange(value.Value); 49 } 43 50 } 44 51 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Operators/Manipulators/UniformDoubleValueManipulator.cs
r5111 r5207 36 36 37 37 public static void ApplyStatic(IRandom random, DoubleValue value, DoubleValueRange range) { 38 var vector = new RealVector(new double[] { value.Value }); 38 bool ok = false; 39 var vector = new RealVector(1); 39 40 var bounds = new DoubleMatrix(1, 2); 40 41 bounds[0, 0] = range.LowerBound.Value; 41 42 bounds[0, 1] = range.UpperBound.Value; 42 UniformOnePositionManipulator.Apply(random, vector, bounds); 43 value.Value = vector[0]; 44 range.Fix(value); 43 double val = value.Value; 44 45 while (!ok) { 46 vector[0] = val; 47 UniformOnePositionManipulator.Apply(random, vector, bounds); 48 value.Value = vector[0]; 49 range.ApplyStepSize(value); 50 ok = range.IsInRange(value.Value); 51 } 45 52 } 46 53 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Operators/Manipulators/UniformIntValueManipulator.cs
r5111 r5207 37 37 38 38 public static void ApplyStatic(IRandom random, IntValue value, IntValueRange range) { 39 bool ok = false; 39 40 var vector = new IntegerVector(new int[] { value.Value }); 40 UniformOnePositionManipulator.Apply(random, vector, range.LowerBound, new IntValue(range.UpperBound.Value + 1)); 41 value.Value = vector[0]; 42 range.Fix(value); 41 int val = value.Value; 42 43 while (!ok) { 44 vector[0] = val; 45 UniformOnePositionManipulator.Apply(random, vector, range.LowerBound, new IntValue(range.UpperBound.Value + 1)); 46 value.Value = vector[0]; 47 range.ApplyStepSize(value); 48 ok = range.IsInRange(value.Value); 49 } 43 50 } 44 51 }
Note: See TracChangeset
for help on using the changeset viewer.