1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 | using HeuristicLab.Optimization;
|
---|
11 | using System.Threading;
|
---|
12 | using HeuristicLab.Common;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
15 | /// <summary>
|
---|
16 | /// A base class for operators which evaluate TSP solutions.
|
---|
17 | /// </summary>
|
---|
18 | [Item("MetaOptimizationEvaluator", "A base class for operators which evaluate Meta Optimization solutions.")]
|
---|
19 | [StorableClass]
|
---|
20 | public class MetaOptimizationEvaluator : SingleSuccessorOperator, IMetaOptimizationEvaluator {
|
---|
21 | private const string RepetitionsParameterName = "Repetitions";
|
---|
22 |
|
---|
23 | private bool algorithmStopped;
|
---|
24 |
|
---|
25 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
26 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
27 | }
|
---|
28 | public ILookupParameter<IAlgorithm> AlgorithmParameter {
|
---|
29 | get { return (ILookupParameter<IAlgorithm>)Parameters["Algorithm"]; }
|
---|
30 | }
|
---|
31 | public ILookupParameter<IItemList<IProblem>> ProblemsParameter {
|
---|
32 | get { return (ILookupParameter<IItemList<IProblem>>)Parameters["Problems"]; }
|
---|
33 | }
|
---|
34 | public ILookupParameter<IParameterConfiguration> ParameterConfigurationParameter {
|
---|
35 | get { return (ILookupParameter<IParameterConfiguration>)Parameters["ParameterConfiguration"]; }
|
---|
36 | }
|
---|
37 | public ValueParameter<IntValue> RepetitionsParameter {
|
---|
38 | get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public IntValue Repetitions {
|
---|
42 | get { return RepetitionsParameter.Value; }
|
---|
43 | set { RepetitionsParameter.Value = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public MetaOptimizationEvaluator() : base() {
|
---|
47 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the ParameterVector."));
|
---|
48 | Parameters.Add(new LookupParameter<IAlgorithm>("Algorithm", "Missing description."));
|
---|
49 | Parameters.Add(new LookupParameter<IItemList<IProblem>>("Problems", "Missing description."));
|
---|
50 | Parameters.Add(new LookupParameter<IParameterConfiguration>("ParameterConfiguration", "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 | AlgorithmParameter.ActualValue.Prepare();
|
---|
66 | //ParameterizeAlgorithm();
|
---|
67 | ParameterConfigurationParameter.ActualValue.Parameterize(AlgorithmParameter.ActualValue);
|
---|
68 | algorithmStopped = false;
|
---|
69 | AlgorithmParameter.ActualValue.Stopped += new EventHandler(ActualValue_Stopped);
|
---|
70 |
|
---|
71 | double qualitySum = 0;
|
---|
72 |
|
---|
73 | foreach (IProblem problem in ProblemsParameter.ActualValue) {
|
---|
74 | AlgorithmParameter.ActualValue.Problem = problem;
|
---|
75 | AlgorithmParameter.ActualValue.Start();
|
---|
76 | while (!algorithmStopped) {
|
---|
77 | 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)
|
---|
78 | }
|
---|
79 | AlgorithmParameter.ActualValue.Stopped -= new EventHandler(ActualValue_Stopped);
|
---|
80 | qualitySum += ((DoubleValue)AlgorithmParameter.ActualValue.Results["BestQuality"].Value).Value;
|
---|
81 | }
|
---|
82 |
|
---|
83 | double qualityAvg = qualitySum / ProblemsParameter.ActualValue.Count;
|
---|
84 | this.QualityParameter.ActualValue = new DoubleValue(qualityAvg);
|
---|
85 |
|
---|
86 | return base.Apply();
|
---|
87 | }
|
---|
88 |
|
---|
89 | void ActualValue_Stopped(object sender, EventArgs e) {
|
---|
90 | algorithmStopped = true;
|
---|
91 | }
|
---|
92 |
|
---|
93 | //private void ParameterizeAlgorithm() {
|
---|
94 | // foreach (IParameterConfiguration parameter in ParameterVectorParameter.ActualValue) {
|
---|
95 | // if (typeof(IAlgorithm).IsAssignableFrom(parameter.OperatorType)) {
|
---|
96 | // this.AlgorithmParameter.ActualValue.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;
|
---|
97 | // } else if (typeof(IProblem).IsAssignableFrom(parameter.OperatorType)) {
|
---|
98 | // this.AlgorithmParameter.ActualValue.Problem.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;
|
---|
99 | // }
|
---|
100 | // }
|
---|
101 | //}
|
---|
102 | }
|
---|
103 | }
|
---|