1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Threading;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 | using System.Collections.Generic;
|
---|
12 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
13 | using System.Threading.Tasks;
|
---|
14 | using System.Diagnostics;
|
---|
15 | using System.Reflection;
|
---|
16 |
|
---|
17 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
18 | /// <summary>
|
---|
19 | /// A base class for operators which evaluate TSP solutions.
|
---|
20 | /// </summary>
|
---|
21 | [Item("ParameterConfigurationEvaluator", "A base class for operators which evaluate Meta Optimization solutions.")]
|
---|
22 | [StorableClass]
|
---|
23 | public class ParameterConfigurationEvaluator : SingleSuccessorOperator, IParameterConfigurationEvaluator {
|
---|
24 | private const double PenaltyQuality = 100000.0; // todo: use something better
|
---|
25 |
|
---|
26 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
27 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
28 | }
|
---|
29 | public ILookupParameter<EngineAlgorithm> AlgorithmParameter {
|
---|
30 | get { return (ILookupParameter<EngineAlgorithm>)Parameters[MetaOptimizationProblem.AlgorithmTypeParameterName]; }
|
---|
31 | }
|
---|
32 | public ILookupParameter<IItemList<ISingleObjectiveProblem>> ProblemsParameter {
|
---|
33 | get { return (ILookupParameter<IItemList<ISingleObjectiveProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
34 | }
|
---|
35 | public ILookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
36 | get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
37 | }
|
---|
38 | public LookupParameter<IntValue> RepetitionsParameter {
|
---|
39 | get { return (LookupParameter<IntValue>)Parameters[MetaOptimizationProblem.RepetitionsParameterName]; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public IntValue Repetitions {
|
---|
43 | get { return RepetitionsParameter.ActualValue; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public ParameterConfigurationEvaluator()
|
---|
47 | : base() {
|
---|
48 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the ParameterVector."));
|
---|
49 | Parameters.Add(new LookupParameter<EngineAlgorithm>(MetaOptimizationProblem.AlgorithmTypeParameterName, "Missing description."));
|
---|
50 | Parameters.Add(new LookupParameter<IItemList<ISingleObjectiveProblem>>(MetaOptimizationProblem.ProblemsParameterName, "Missing description."));
|
---|
51 | Parameters.Add(new LookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", "Missing description."));
|
---|
52 | Parameters.Add(new LookupParameter<IntValue>(MetaOptimizationProblem.RepetitionsParameterName, "Number of evaluations on one problem."));
|
---|
53 | }
|
---|
54 |
|
---|
55 | [StorableConstructor]
|
---|
56 | protected ParameterConfigurationEvaluator(bool deserializing) : base(deserializing) { }
|
---|
57 | protected ParameterConfigurationEvaluator(ParameterConfigurationEvaluator original, Cloner cloner)
|
---|
58 | : base(original, cloner) {
|
---|
59 | }
|
---|
60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
61 | return new ParameterConfigurationEvaluator(this, cloner);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IOperation Apply() {
|
---|
65 | EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue.Clone();
|
---|
66 | IItemList<ISingleObjectiveProblem> problems = ProblemsParameter.ActualValue;
|
---|
67 |
|
---|
68 | // set parameters
|
---|
69 | ParameterConfigurationParameter.ActualValue.Parameterize(algorithm);
|
---|
70 | algorithm.StoreAlgorithmInEachRun = false;
|
---|
71 |
|
---|
72 | List<double> qualities = new List<double>();
|
---|
73 | List<TimeSpan> executionTimes = new List<TimeSpan>();
|
---|
74 | algorithm.Engine = new SequentialEngine.SequentialEngine();
|
---|
75 | algorithm.Prepare(true);
|
---|
76 |
|
---|
77 | foreach (ISingleObjectiveProblem problem in problems) {
|
---|
78 | algorithm.Problem = (IProblem)problem.Clone();
|
---|
79 |
|
---|
80 | for (int i = 0; i < Repetitions.Value; i++) {
|
---|
81 | algorithm.Prepare();
|
---|
82 |
|
---|
83 | AlgorithmExecutor executor = new AlgorithmExecutor(algorithm);
|
---|
84 | executor.StartSync();
|
---|
85 |
|
---|
86 | if (algorithm.ExecutionState == ExecutionState.Paused) {
|
---|
87 | // this parametercombination was bad. set penalty for this solution
|
---|
88 | qualities.Add(PenaltyQuality); // todo: respect Maximization; problem: this messes up average value; solution: use currently worst quality*2
|
---|
89 | executionTimes.Add(algorithm.ExecutionTime);
|
---|
90 | } else {
|
---|
91 | qualities.Add(((DoubleValue)algorithm.Results["BestQuality"].Value).Value);
|
---|
92 | executionTimes.Add(algorithm.ExecutionTime);
|
---|
93 |
|
---|
94 | // parameters will be stored in ParameterConfigurationTree anyway. they would be redundant in runs
|
---|
95 | algorithm.Runs.Last().Parameters.Clear();
|
---|
96 | // but keep the problem, since this differs in runs
|
---|
97 | algorithm.Runs.Last().Parameters.Add("Problem", problem);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | algorithm.Prepare();
|
---|
102 |
|
---|
103 | qualities = qualities.OrderBy(x => x).ToList(); // todo: respect Maximization:true/false
|
---|
104 |
|
---|
105 | ParameterConfigurationParameter.ActualValue.AverageExecutionTime = new TimeSpanValue(TimeSpan.FromMilliseconds(executionTimes.Average(t => t.TotalMilliseconds)));
|
---|
106 | ParameterConfigurationParameter.ActualValue.Repetitions = (IntValue)Repetitions.Clone();
|
---|
107 | ParameterConfigurationParameter.ActualValue.BestQuality = new DoubleValue(qualities.First());
|
---|
108 | ParameterConfigurationParameter.ActualValue.AverageQuality = new DoubleValue(qualities.Average());
|
---|
109 | ParameterConfigurationParameter.ActualValue.WorstQuality = new DoubleValue(qualities.Last());
|
---|
110 | ParameterConfigurationParameter.ActualValue.QualityVariance = new DoubleValue(qualities.Variance());
|
---|
111 | ParameterConfigurationParameter.ActualValue.QualityStandardDeviation = new DoubleValue(qualities.StandardDeviation());
|
---|
112 | ParameterConfigurationParameter.ActualValue.Runs = (RunCollection)algorithm.Runs.Clone();
|
---|
113 |
|
---|
114 | double quality = ParameterConfigurationParameter.ActualValue.AverageQuality.Value; // todo: also include other measures (executiontime, variance)
|
---|
115 | this.QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
116 |
|
---|
117 | return base.Apply();
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static double Variance(IEnumerable<double> source) {
|
---|
121 | double avg = source.Average();
|
---|
122 | double d = source.Aggregate(0.0, (total, next) => total += Math.Pow(next - avg, 2));
|
---|
123 | return d / (source.Count() - 1);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public static double StandardDeviation(IEnumerable<double> source) {
|
---|
127 | return Math.Sqrt(source.Variance());
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public class AlgorithmExecutor {
|
---|
132 | private EngineAlgorithm algorithm;
|
---|
133 | private AutoResetEvent waitHandle = new AutoResetEvent(false);
|
---|
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 | }
|
---|
156 | }
|
---|
157 | }
|
---|