- Timestamp:
- 12/04/10 18:08:43 (14 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/BestParameterConfigurationAnalyzer.cs
r5009 r5023 17 17 [StorableClass] 18 18 public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer { 19 // Wagner: Spezielle View für die Lösungen (ParameterConfigurations): So wie bei Runs: die zu Optimierenden Parameter(-werte) der besten solution anzeigen20 19 21 20 public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter { … … 62 61 ResultCollection results = ResultsParameter.ActualValue; 63 62 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 64 ItemArray<ParameterConfigurationTree> parameter Configurations = ParameterConfigurationParameter.ActualValue;63 ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue; 65 64 66 65 int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; 67 66 68 EngineAlgorithm bestAlg = ((EngineAlgorithm)((ParameterConfigurationTree)parameter Configurations[i]).ActualValue.Value);67 EngineAlgorithm bestAlg = ((EngineAlgorithm)((ParameterConfigurationTree)parameterTrees[i]).ActualValue.Value); 69 68 Run bestRun = new Run(bestAlg); 69 ((ParameterConfigurationTree)parameterTrees[i]).CollectResultValues(bestRun.Results); 70 70 71 if (bestKnownQuality == null || qualities[i].Value < bestKnownQuality.Value) { 71 if (bestKnownQuality == null || qualities[i].Value < bestKnownQuality.Value) { // todo: respect Maximization:true/false 72 72 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 73 73 BestKnownSolutionParameter.ActualValue = bestRun; … … 83 83 } 84 84 85 // population 85 86 RunCollection rc = new RunCollection(); 86 87 foreach (ParameterConfigurationTree pc in parameterConfigurations.OrderBy(x => x.Quality.Value*-1)) { 88 IAlgorithm alg = (IAlgorithm)pc.ActualValue.Value; 87 foreach (ParameterConfigurationTree pt in parameterTrees.OrderByDescending(x => x.BestQuality.Value)) { // todo: respect Maximization:true/false 88 IAlgorithm alg = (IAlgorithm)pt.ActualValue.Value; 89 89 alg.StoreAlgorithmInEachRun = false; 90 90 IRun run = new Run(alg); 91 pt.CollectResultValues(run.Results); 91 92 rc.Add(run); 92 93 } … … 98 99 results["Population"].Value = rc; 99 100 } 100 GC.Collect();101 101 102 102 return base.Apply(); -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ParameterConfigurationTree.cs
r5009 r5023 13 13 [StorableClass] 14 14 public class ParameterConfigurationTree : ValueConfiguration { 15 //[Storable] 16 //protected EngineAlgorithm algorithm; 17 //public EngineAlgorithm Algorithm { 18 // get { return algorithm; } 19 // set { 20 // if (algorithm != value) { 21 // if (algorithm != null) DeregisterAlgorithmEvents(); 22 // algorithm = value; 23 // if (algorithm != null) RegisterAlgorithmEvents(); 24 // OnAlgorithmChanged(); 25 // } 26 // } 27 //} 15 [Storable] 16 public EngineAlgorithm Algorithm { 17 get { return (EngineAlgorithm)base.ActualValue.Value; } 18 set { 19 base.ActualValue.Value = value; 20 } 21 } 28 22 29 23 [Storable] 30 private DoubleValue quality;31 public DoubleValue Quality {32 get { return quality; }24 private DoubleValue bestQuality; 25 public DoubleValue BestQuality { 26 get { return bestQuality; } 33 27 set { 34 if ( quality != value) {35 if ( quality != null) DeregisterQualityEvents();36 quality = value;37 if ( quality != null) RegisterQualityEvents();28 if (bestQuality != value) { 29 if (bestQuality != null) DeregisterQualityEvents(); 30 bestQuality = value; 31 if (bestQuality != null) RegisterQualityEvents(); 38 32 OnQualityChanged(); 39 33 } … … 41 35 } 42 36 37 [Storable] 38 private DoubleValue averageQuality; 39 public DoubleValue AverageQuality { 40 get { return averageQuality; } 41 set { averageQuality = value; } 42 } 43 44 [Storable] 45 private DoubleValue worstQuality; 46 public DoubleValue WorstQuality { 47 get { return worstQuality; } 48 set { worstQuality = value; } 49 } 50 51 [Storable] 52 private DoubleValue qualityVariance; 53 public DoubleValue QualityVariance { 54 get { return qualityVariance; } 55 set { qualityVariance = value; } 56 } 57 58 [Storable] 59 private DoubleValue qualityStandardDeviation; 60 public DoubleValue QualityStandardDeviation { 61 get { return qualityStandardDeviation; } 62 set { qualityStandardDeviation = value; } 63 } 64 65 [Storable] 66 private TimeSpanValue averageExecutionTime; 67 public TimeSpanValue AverageExecutionTime { 68 get { return averageExecutionTime; } 69 set { averageExecutionTime = value; } 70 } 71 72 [Storable] 73 private IntValue repetitions; 74 public IntValue Repetitions { 75 get { return repetitions; } 76 set { repetitions = value; } 77 } 78 79 [Storable] 80 public RunCollection Runs { get; set; } 81 43 82 #region constructors and cloning 44 83 public ParameterConfigurationTree(EngineAlgorithm algorithm) : base(algorithm, algorithm.GetType()) { 45 84 this.Optimize = true; // root must always be optimized 46 this.Quality = new DoubleValue(); 47 //this.algorithm = algorithm; 85 this.BestQuality = new DoubleValue(); 48 86 } 49 87 public ParameterConfigurationTree() { … … 53 91 protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { } 54 92 protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner) : base(original, cloner) { 55 this.quality = cloner.Clone(original.Quality); 93 this.bestQuality = cloner.Clone(original.BestQuality); 94 this.Algorithm.Prepare(); 95 this.Algorithm.Runs.Clear(); 56 96 Initialize(); 57 97 } … … 67 107 private void Initialize() { 68 108 //if (algorithm != null) RegisterAlgorithmEvents(); 69 if (quality != null) RegisterQualityEvents(); 109 if (bestQuality != null) RegisterQualityEvents(); 110 } 111 112 public virtual void CollectResultValues(IDictionary<string, IItem> values) { 113 values.Add("RunsAverageExecutionTime", AverageExecutionTime); 114 values.Add("Repetitions", Repetitions); 115 values.Add("RunsBestQuality", BestQuality); 116 values.Add("RunsAverageQuality", AverageQuality); 117 values.Add("RunsWorstQuality", WorstQuality); 118 values.Add("RunsQualityVariance", QualityVariance); 119 values.Add("RunsQualityStandardDeviation", QualityStandardDeviation); 120 values.Add("Runs", Runs); 70 121 } 71 122 … … 89 140 //} 90 141 private void RegisterQualityEvents() { 91 Quality.ValueChanged += new EventHandler(Quality_ValueChanged);142 BestQuality.ValueChanged += new EventHandler(Quality_ValueChanged); 92 143 } 93 144 private void DeregisterQualityEvents() { 94 Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);145 BestQuality.ValueChanged -= new EventHandler(Quality_ValueChanged); 95 146 } 96 147 … … 99 150 } 100 151 #endregion 152 101 153 } 102 154 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ParameterConfigurations/ParameterConfiguration.cs
r5009 r5023 127 127 this.ActualValue = cloner.Clone(original.ActualValue); 128 128 this.optimize = original.optimize; 129 this.actualValueConfigurationIndex = original.actualValueConfigurationIndex; 129 130 if (this.valueConfigurations != null) RegisterValueConfigurationEvents(); 130 131 } … … 270 271 271 272 public void Parameterize(IValueParameter parameter) { 272 parameter.ActualValue = this.ActualValue.Value; 273 if (Optimize) { 274 if (this.ActualValue.Value is IParameterizedItem) { 275 this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).Parameterize((IParameterizedItem)this.ActualValue.Value); 276 } 277 } 278 parameter.Value = this.ActualValue.Value; 273 279 } 274 280 … … 298 304 this.ValueConfigurations.ElementAt(i).Cross(otherPc.ValueConfigurations.ElementAt(i), random); 299 305 } 306 if (random.NextDouble() > 0.5) { 307 actualValueConfigurationIndex = ((ParameterConfiguration)other).actualValueConfigurationIndex; 308 } 300 309 this.ActualValue = this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).ActualValue; 301 310 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/RangeConstraints/DoubleValueRange.cs
r5009 r5023 21 21 } 22 22 23 p ublic override DoubleValue GetRandomValue(IRandom random) {23 protected override DoubleValue GetRandomSample(IRandom random) { 24 24 double val; 25 25 do { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/RangeConstraints/IntValueRange.cs
r5009 r5023 21 21 } 22 22 23 p ublic override IntValue GetRandomValue(IRandom random) {23 protected override IntValue GetRandomSample(IRandom random) { 24 24 int val; 25 25 do { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/RangeConstraints/PercentValueRange.cs
r5009 r5023 21 21 } 22 22 23 p ublic override PercentValue GetRandomValue(IRandom random) {23 protected override PercentValue GetRandomSample(IRandom random) { 24 24 double val; 25 25 do { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/RangeConstraints/Range.cs
r5009 r5023 191 191 #endregion 192 192 193 public abstract T GetRandomValue(IRandom random); 193 public T GetRandomValue(IRandom random) { 194 // by a chance return the extreme values of this range to intensify search in those regions 195 if (random.NextDouble() < 0.1) { 196 if (random.NextDouble() < 0.5) { 197 return LowerBound; 198 } else { 199 return UpperBound; 200 } 201 } 202 203 // otherwise sample a random value from the range 204 return GetRandomSample(random); 205 } 206 207 protected abstract T GetRandomSample(IRandom random); 194 208 195 209 IItem IRange.GetRandomValue(IRandom random) { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ValueConfigurations/ValueConfiguration.cs
r5009 r5023 6 6 using HeuristicLab.Data; 7 7 using System.Drawing; 8 using HeuristicLab.Encodings.RealVectorEncoding; 9 using HeuristicLab.Encodings.IntegerVectorEncoding; 8 10 9 11 namespace HeuristicLab.Problems.MetaOptimization { … … 92 94 [StorableConstructor] 93 95 protected ValueConfiguration(bool deserializing) { } 94 protected ValueConfiguration(ValueConfiguration original, Cloner cloner) : base(original, cloner) { 96 protected ValueConfiguration(ValueConfiguration original, Cloner cloner) 97 : base(original, cloner) { 95 98 this.ParameterConfigurations = cloner.Clone(original.parameterConfigurations); 96 99 this.actualValue = cloner.Clone(original.ActualValue); … … 228 231 if (rangeConstraint != null) { 229 232 if (this.actualValue.ValueDataType == typeof(IntValue)) { 230 this.actualValue.Value = new IntValue((((IntValue)this.actualValue.Value).Value + ((IntValue)other.ActualValue.Value).Value) / 2); 233 //this.actualValue.Value = new IntValue((((IntValue)this.actualValue.Value).Value + ((IntValue)other.ActualValue.Value).Value) / 2); 234 235 IntegerVector[] parents = new IntegerVector[2]; 236 parents[0] = new IntegerVector(new int[] { ((IntValue)this.actualValue.Value).Value }); 237 parents[1] = new IntegerVector(new int[] { ((IntValue)other.ActualValue.Value).Value }); 238 239 this.actualValue.Value = new IntValue(HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(random, parents[0], parents[1]).First()); 240 231 241 } else if (this.actualValue.ValueDataType == typeof(DoubleValue)) { 232 this.actualValue.Value = new DoubleValue((((DoubleValue)this.actualValue.Value).Value + ((DoubleValue)other.ActualValue.Value).Value) / 2); 242 //this.actualValue.Value = new DoubleValue((((DoubleValue)this.actualValue.Value).Value + ((DoubleValue)other.ActualValue.Value).Value) / 2); 243 RealVector[] parents = new RealVector[2]; 244 parents[0] = new RealVector( new double[] {((DoubleValue)this.actualValue.Value).Value} ); 245 parents[1] = new RealVector( new double[] {((DoubleValue)other.ActualValue.Value).Value} ); 246 247 if (random.NextDouble() < 0.5) { 248 this.actualValue.Value = new DoubleValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First()); 249 } else { 250 this.actualValue.Value = new DoubleValue(HeuristicLab.Encodings.RealVectorEncoding.DiscreteCrossover.Apply(random, new ItemArray<RealVector>(parents)).First()); 251 } 252 //this.actualValue.Value = new DoubleValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First()); 253 233 254 } else if (this.actualValue.ValueDataType == typeof(PercentValue)) { 234 this.actualValue.Value = new PercentValue((((PercentValue)this.actualValue.Value).Value + ((PercentValue)other.ActualValue.Value).Value) / 2); 255 //this.actualValue.Value = new PercentValue((((PercentValue)this.actualValue.Value).Value + ((PercentValue)other.ActualValue.Value).Value) / 2); 256 257 RealVector[] parents = new RealVector[2]; 258 parents[0] = new RealVector(new double[] { ((PercentValue)this.actualValue.Value).Value }); 259 parents[1] = new RealVector(new double[] { ((PercentValue)other.ActualValue.Value).Value }); 260 261 if (random.NextDouble() < 0.5) { 262 this.actualValue.Value = new PercentValue(AverageCrossover.Apply(random, new ItemArray<RealVector>(parents)).First()); 263 } else { 264 this.actualValue.Value = new PercentValue(HeuristicLab.Encodings.RealVectorEncoding.DiscreteCrossover.Apply(random, new ItemArray<RealVector>(parents)).First()); 265 } 266 235 267 } else if (this.actualValue.ValueDataType == typeof(BoolValue)) { 236 bool first = ((BoolValue)this.actualValue.Value).Value; 237 bool second = ((BoolValue)other.ActualValue.Value).Value; 238 if (first && second) { 239 this.actualValue.Value = new BoolValue(true); 240 } else if (!first && !second) { 241 this.actualValue.Value = new BoolValue(false); 242 } else { 243 if (random.NextDouble() > 0.5) 244 this.actualValue.Value = new BoolValue(true); 245 else 246 this.actualValue.Value = new BoolValue(false); 247 } 268 if (random.NextDouble() > 0.5) 269 this.actualValue.Value = this.actualValue.Value; 270 else 271 this.actualValue.Value = other.ActualValue.Value; 248 272 } else { 249 273 throw new NotImplementedException(); -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/MetaOptimizationEvaluator.cs
r5009 r5023 1 1 using System; 2 using System.Linq; 2 3 using System.Threading; 3 4 using HeuristicLab.Common; … … 8 9 using HeuristicLab.Parameters; 9 10 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 11 using System.Collections.Generic; 10 12 11 13 namespace HeuristicLab.Problems.MetaOptimization { … … 69 71 algorithm.Stopped += new EventHandler(ActualValue_Stopped); 70 72 71 double qualitySum = 0; 73 List<double> qualities = new List<double>(); 74 List<TimeSpan> executionTimes = new List<TimeSpan>(); 75 algorithm.Prepare(true); 72 76 73 //foreach (ISingleObjectiveProblem problem in ProblemsParameter.ActualValue) { 74 algorithm.Engine = new SequentialEngine.SequentialEngine(); 75 //algorithm.Problem = problem; 76 algorithm.Prepare(); 77 algorithm.Start(); 78 while (!algorithmStopped) { 79 Thread.Sleep(1000); // 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) 77 for (int i = 0; i < Repetitions.Value; i++) { 78 algorithm.Engine = new SequentialEngine.SequentialEngine(); 79 algorithm.Prepare(); 80 algorithm.Start(); 81 while (!algorithmStopped) { 82 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) 83 } 84 qualities.Add(((DoubleValue)algorithm.Results["BestQuality"].Value).Value); 85 executionTimes.Add(algorithm.ExecutionTime); 86 87 algorithmStopped = false; 80 88 } 81 double quality = ((DoubleValue)algorithm.Results["BestQuality"].Value).Value;82 ParameterConfigurationParameter.ActualValue.Quality.Value = quality;83 qualitySum += quality;84 //}85 86 89 algorithm.Stopped -= new EventHandler(ActualValue_Stopped); 87 90 88 //double qualityAvg = qualitySum / ProblemsParameter.ActualValue.Count; 89 //this.QualityParameter.ActualValue = new DoubleValue(qualityAvg); 91 qualities = qualities.OrderBy(x => x).ToList(); // todo: respect Maximization:true/false 92 93 ParameterConfigurationParameter.ActualValue.AverageExecutionTime = new TimeSpanValue(TimeSpan.FromMilliseconds(executionTimes.Average(t => t.TotalMilliseconds))); 94 ParameterConfigurationParameter.ActualValue.Repetitions = Repetitions; 95 ParameterConfigurationParameter.ActualValue.BestQuality = new DoubleValue(qualities.First()); 96 ParameterConfigurationParameter.ActualValue.AverageQuality = new DoubleValue(qualities.Average()); 97 ParameterConfigurationParameter.ActualValue.WorstQuality = new DoubleValue(qualities.Last()); 98 ParameterConfigurationParameter.ActualValue.QualityVariance = new DoubleValue(qualities.Variance()); 99 ParameterConfigurationParameter.ActualValue.QualityStandardDeviation = new DoubleValue(qualities.StandardDeviation()); 100 ParameterConfigurationParameter.ActualValue.Runs = algorithm.Runs; 101 102 double quality = ParameterConfigurationParameter.ActualValue.AverageQuality.Value; // todo: also include other measures (executiontime, variance) 90 103 this.QualityParameter.ActualValue = new DoubleValue(quality); 104 105 return base.Apply(); 106 } 91 107 92 return base.Apply(); 108 public static double Variance(IEnumerable<double> source) { 109 double avg = source.Average(); 110 double d = source.Aggregate(0.0, (total, next) => total += Math.Pow(next - avg, 2)); 111 return d / (source.Count() - 1); 112 } 113 114 public static double StandardDeviation(IEnumerable<double> source) { 115 return Math.Sqrt(source.Variance()); 93 116 } 94 117 … … 96 119 algorithmStopped = true; 97 120 } 98 99 //private void ParameterizeAlgorithm() {100 // foreach (IParameterConfiguration parameter in ParameterVectorParameter.ActualValue) {101 // if (typeof(IAlgorithm).IsAssignableFrom(parameter.OperatorType)) {102 // this.AlgorithmParameter.ActualValue.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;103 // } else if (typeof(IProblem).IsAssignableFrom(parameter.OperatorType)) {104 // this.AlgorithmParameter.ActualValue.Problem.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;105 // }106 // }107 //}108 121 } 109 122 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj
r5009 r5023 91 91 <ItemGroup> 92 92 <Compile Include="Analyzers\BestParameterConfigurationAnalyzer.cs" /> 93 <None Include="ClassDiagram1.cd" /> 93 94 <None Include="Properties\AssemblyInfo.cs.frame" /> 94 95 <None Include="HeuristicLabProblemsMetaOptimizationPlugin.cs.frame" /> -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IOptimizable.cs
r5009 r5023 15 15 void Mutate(IRandom random); 16 16 void Cross(IOptimizable other, IRandom random); 17 17 18 18 event EventHandler IsOptimizableChanged; 19 19 event EventHandler OptimizeChanged;
Note: See TracChangeset
for help on using the changeset viewer.