Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/MetaOptimizationEvaluator.cs @ 5023

Last change on this file since 5023 was 5023, checked in by cneumuel, 14 years ago

#1215 worked on metaoptimization

File size: 5.8 KB
Line 
1using System;
2using System.Linq;
3using System.Threading;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Operators;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using System.Collections.Generic;
12
13namespace HeuristicLab.Problems.MetaOptimization {
14  /// <summary>
15  /// A base class for operators which evaluate TSP solutions.
16  /// </summary>
17  [Item("MetaOptimizationEvaluator", "A base class for operators which evaluate Meta Optimization solutions.")]
18  [StorableClass]
19  public class MetaOptimizationEvaluator : SingleSuccessorOperator, IMetaOptimizationEvaluator {
20    private const string RepetitionsParameterName = "Repetitions";
21
22    private bool algorithmStopped;
23
24    public ILookupParameter<DoubleValue> QualityParameter {
25      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
26    }
27    public ILookupParameter<EngineAlgorithm> AlgorithmParameter {
28      get { return (ILookupParameter<EngineAlgorithm>)Parameters["Algorithm"]; }
29    }
30    public ILookupParameter<IItemList<ISingleObjectiveProblem>> ProblemsParameter {
31      get { return (ILookupParameter<IItemList<ISingleObjectiveProblem>>)Parameters["Problems"]; }
32    }
33    public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
34      get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
35    }
36    public ValueParameter<IntValue> RepetitionsParameter {
37      get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
38    }
39
40    public IntValue Repetitions {
41      get { return RepetitionsParameter.Value; }
42      set { RepetitionsParameter.Value = value; }
43    }
44
45    public MetaOptimizationEvaluator()
46      : base() {
47      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the ParameterVector."));
48      Parameters.Add(new LookupParameter<EngineAlgorithm>("Algorithm", "Missing description."));
49      Parameters.Add(new LookupParameter<IItemList<ISingleObjectiveProblem>>("Problems", "Missing description."));
50      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", "Missing description."));
51      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "Number of evaluations for one individual.", new IntValue(3)));
52    }
53
54    [StorableConstructor]
55    protected MetaOptimizationEvaluator(bool deserializing) : base(deserializing) { }
56    protected MetaOptimizationEvaluator(MetaOptimizationEvaluator original, Cloner cloner)
57      : base(original, cloner) {
58      this.algorithmStopped = original.algorithmStopped;
59    }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new MetaOptimizationEvaluator(this, cloner);
62    }
63
64    public override IOperation Apply() {
65      EngineAlgorithm algorithm = (EngineAlgorithm)ParameterConfigurationParameter.ActualValue.ActualValue.Value;
66
67      // set parameters
68      ParameterConfigurationParameter.ActualValue.Parameterize(algorithm);
69
70      algorithmStopped = false;
71      algorithm.Stopped += new EventHandler(ActualValue_Stopped);
72
73      List<double> qualities = new List<double>();
74      List<TimeSpan> executionTimes = new List<TimeSpan>();
75      algorithm.Prepare(true);
76
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;
88      }
89      algorithm.Stopped -= new EventHandler(ActualValue_Stopped);
90
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)
103      this.QualityParameter.ActualValue = new DoubleValue(quality);
104     
105      return base.Apply();
106    }
107
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());
116    }
117
118    void ActualValue_Stopped(object sender, EventArgs e) {
119      algorithmStopped = true;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.