Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/04/10 18:08:43 (13 years ago)
Author:
cneumuel
Message:

#1215 worked on metaoptimization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/MetaOptimizationEvaluator.cs

    r5009 r5023  
    11using System;
     2using System.Linq;
    23using System.Threading;
    34using HeuristicLab.Common;
     
    89using HeuristicLab.Parameters;
    910using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     11using System.Collections.Generic;
    1012
    1113namespace HeuristicLab.Problems.MetaOptimization {
     
    6971      algorithm.Stopped += new EventHandler(ActualValue_Stopped);
    7072
    71       double qualitySum = 0;
     73      List<double> qualities = new List<double>();
     74      List<TimeSpan> executionTimes = new List<TimeSpan>();
     75      algorithm.Prepare(true);
    7276
    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;
    8088      }
    81       double quality = ((DoubleValue)algorithm.Results["BestQuality"].Value).Value;
    82       ParameterConfigurationParameter.ActualValue.Quality.Value = quality;
    83       qualitySum += quality;
    84       //}
    85 
    8689      algorithm.Stopped -= new EventHandler(ActualValue_Stopped);
    8790
    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)
    90103      this.QualityParameter.ActualValue = new DoubleValue(quality);
     104     
     105      return base.Apply();
     106    }
    91107
    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());
    93116    }
    94117
     
    96119      algorithmStopped = true;
    97120    }
    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     //}
    108121  }
    109122}
Note: See TracChangeset for help on using the changeset viewer.