1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
12 | // todo: storable, name, descr, ...
|
---|
13 | [StorableClass]
|
---|
14 | public class ParameterConfigurationTree : ValueConfiguration {
|
---|
15 | //[Storable]
|
---|
16 | //public EngineAlgorithm Algorithm {
|
---|
17 | // get { return (EngineAlgorithm)base.ActualValue.Value; }
|
---|
18 | // set {
|
---|
19 | // base.ActualValue.Value = value;
|
---|
20 | // }
|
---|
21 | //}
|
---|
22 |
|
---|
23 | [Storable]
|
---|
24 | private DoubleValue bestQuality;
|
---|
25 | public DoubleValue BestQuality {
|
---|
26 | get { return bestQuality; }
|
---|
27 | set {
|
---|
28 | if (bestQuality != value) {
|
---|
29 | if (bestQuality != null) DeregisterQualityEvents();
|
---|
30 | bestQuality = value;
|
---|
31 | if (bestQuality != null) RegisterQualityEvents();
|
---|
32 | OnQualityChanged();
|
---|
33 | }
|
---|
34 | }
|
---|
35 | }
|
---|
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 | protected RunCollection runs;
|
---|
81 | public RunCollection Runs {
|
---|
82 | get { return runs; }
|
---|
83 | set { runs = value; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected IDictionary<string, IItem> parameters;
|
---|
87 | public IDictionary<string, IItem> Parameters {
|
---|
88 | get { return parameters; }
|
---|
89 | set { parameters = value; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | #region constructors and cloning
|
---|
93 | public ParameterConfigurationTree(EngineAlgorithm algorithm)
|
---|
94 | : base(null, algorithm.GetType()) {
|
---|
95 | this.Optimize = true; // root must always be optimized
|
---|
96 | this.BestQuality = new DoubleValue();
|
---|
97 | this.parameters = new Dictionary<string, IItem>();
|
---|
98 |
|
---|
99 | PopulateParameterConfigurations(algorithm);
|
---|
100 | Initialize();
|
---|
101 | }
|
---|
102 | public ParameterConfigurationTree() {
|
---|
103 | Initialize();
|
---|
104 | }
|
---|
105 | [StorableConstructor]
|
---|
106 | protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { }
|
---|
107 | protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
|
---|
108 | : base(original, cloner) {
|
---|
109 | this.bestQuality = cloner.Clone(original.BestQuality);
|
---|
110 | this.parameters = new Dictionary<string, IItem>();
|
---|
111 | foreach (var p in original.parameters) {
|
---|
112 | this.parameters.Add(p.Key, cloner.Clone(p.Value));
|
---|
113 | }
|
---|
114 | Initialize();
|
---|
115 | }
|
---|
116 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
117 | return new ParameterConfigurationTree(this, cloner);
|
---|
118 | }
|
---|
119 | [StorableHook(HookType.AfterDeserialization)]
|
---|
120 | private void AfterDeserialization() {
|
---|
121 | Initialize();
|
---|
122 | }
|
---|
123 | #endregion
|
---|
124 |
|
---|
125 | private void Initialize() {
|
---|
126 | //if (algorithm != null) RegisterAlgorithmEvents();
|
---|
127 | if (bestQuality != null) RegisterQualityEvents();
|
---|
128 | }
|
---|
129 |
|
---|
130 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
131 | values.Add("RunsAverageExecutionTime", AverageExecutionTime);
|
---|
132 | values.Add("Repetitions", Repetitions);
|
---|
133 | values.Add("RunsBestQuality", BestQuality);
|
---|
134 | values.Add("RunsAverageQuality", AverageQuality);
|
---|
135 | values.Add("RunsWorstQuality", WorstQuality);
|
---|
136 | values.Add("RunsQualityVariance", QualityVariance);
|
---|
137 | values.Add("RunsQualityStandardDeviation", QualityStandardDeviation);
|
---|
138 | values.Add("Runs", Runs);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
142 | foreach (var p in parameters) {
|
---|
143 | values.Add(p);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | #region Events
|
---|
148 | //public event EventHandler AlgorithmChanged;
|
---|
149 | //private void OnAlgorithmChanged() {
|
---|
150 | // var handler = AlgorithmChanged;
|
---|
151 | // if (handler != null) handler(this, EventArgs.Empty);
|
---|
152 | //}
|
---|
153 | public event EventHandler QualityChanged;
|
---|
154 | private void OnQualityChanged() {
|
---|
155 | var handler = QualityChanged;
|
---|
156 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
157 | }
|
---|
158 |
|
---|
159 | //private void RegisterAlgorithmEvents() {
|
---|
160 |
|
---|
161 | //}
|
---|
162 | //private void DeregisterAlgorithmEvents() {
|
---|
163 |
|
---|
164 | //}
|
---|
165 | private void RegisterQualityEvents() {
|
---|
166 | BestQuality.ValueChanged += new EventHandler(Quality_ValueChanged);
|
---|
167 | }
|
---|
168 | private void DeregisterQualityEvents() {
|
---|
169 | BestQuality.ValueChanged -= new EventHandler(Quality_ValueChanged);
|
---|
170 | }
|
---|
171 |
|
---|
172 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
173 | OnQualityChanged();
|
---|
174 | }
|
---|
175 | #endregion
|
---|
176 |
|
---|
177 | public override void Parameterize(IParameterizedItem item) {
|
---|
178 | base.Parameterize(item);
|
---|
179 | this.parameters.Clear();
|
---|
180 | ((IAlgorithm)item).CollectParameterValues(this.Parameters);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|