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 | using System.Collections;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
13 | // todo: storable, name, descr, ...
|
---|
14 | [StorableClass]
|
---|
15 | public class ParameterConfigurationTree : ValueConfiguration, IEnumerable {
|
---|
16 | //[Storable]
|
---|
17 | //public EngineAlgorithm Algorithm {
|
---|
18 | // get { return (EngineAlgorithm)base.ActualValue.Value; }
|
---|
19 | // set {
|
---|
20 | // base.ActualValue.Value = value;
|
---|
21 | // }
|
---|
22 | //}
|
---|
23 |
|
---|
24 | [Storable]
|
---|
25 | private DoubleValue bestQuality;
|
---|
26 | public DoubleValue BestQuality {
|
---|
27 | get { return bestQuality; }
|
---|
28 | set {
|
---|
29 | if (bestQuality != value) {
|
---|
30 | if (bestQuality != null) DeregisterQualityEvents();
|
---|
31 | bestQuality = value;
|
---|
32 | if (bestQuality != null) RegisterQualityEvents();
|
---|
33 | OnQualityChanged();
|
---|
34 | }
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | private DoubleValue averageQuality;
|
---|
40 | public DoubleValue AverageQuality {
|
---|
41 | get { return averageQuality; }
|
---|
42 | set { averageQuality = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [Storable]
|
---|
46 | private DoubleValue worstQuality;
|
---|
47 | public DoubleValue WorstQuality {
|
---|
48 | get { return worstQuality; }
|
---|
49 | set { worstQuality = value; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [Storable]
|
---|
53 | private DoubleValue qualityVariance;
|
---|
54 | public DoubleValue QualityVariance {
|
---|
55 | get { return qualityVariance; }
|
---|
56 | set { qualityVariance = value; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | [Storable]
|
---|
60 | private DoubleValue qualityStandardDeviation;
|
---|
61 | public DoubleValue QualityStandardDeviation {
|
---|
62 | get { return qualityStandardDeviation; }
|
---|
63 | set { qualityStandardDeviation = value; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | [Storable]
|
---|
67 | private TimeSpanValue averageExecutionTime;
|
---|
68 | public TimeSpanValue AverageExecutionTime {
|
---|
69 | get { return averageExecutionTime; }
|
---|
70 | set { averageExecutionTime = value; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | [Storable]
|
---|
74 | private IntValue repetitions;
|
---|
75 | public IntValue Repetitions {
|
---|
76 | get { return repetitions; }
|
---|
77 | set { repetitions = value; }
|
---|
78 | }
|
---|
79 |
|
---|
80 | [Storable]
|
---|
81 | protected RunCollection runs;
|
---|
82 | public RunCollection Runs {
|
---|
83 | get { return runs; }
|
---|
84 | set { runs = value; }
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected IDictionary<string, IItem> parameters;
|
---|
88 | public IDictionary<string, IItem> Parameters {
|
---|
89 | get { return parameters; }
|
---|
90 | set { parameters = value; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | #region constructors and cloning
|
---|
94 | public ParameterConfigurationTree(EngineAlgorithm algorithm)
|
---|
95 | : base(null, algorithm.GetType()) {
|
---|
96 | this.Optimize = true; // root must always be optimized
|
---|
97 | this.BestQuality = new DoubleValue();
|
---|
98 | this.parameters = new Dictionary<string, IItem>();
|
---|
99 |
|
---|
100 | PopulateParameterConfigurations(algorithm);
|
---|
101 | Initialize();
|
---|
102 | }
|
---|
103 | public ParameterConfigurationTree() {
|
---|
104 | Initialize();
|
---|
105 | }
|
---|
106 | [StorableConstructor]
|
---|
107 | protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { }
|
---|
108 | protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
|
---|
109 | : base(original, cloner) {
|
---|
110 | this.bestQuality = cloner.Clone(original.BestQuality);
|
---|
111 | this.parameters = new Dictionary<string, IItem>();
|
---|
112 | foreach (var p in original.parameters) {
|
---|
113 | this.parameters.Add(p.Key, cloner.Clone(p.Value));
|
---|
114 | }
|
---|
115 | Initialize();
|
---|
116 | }
|
---|
117 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
118 | return new ParameterConfigurationTree(this, cloner);
|
---|
119 | }
|
---|
120 | [StorableHook(HookType.AfterDeserialization)]
|
---|
121 | private void AfterDeserialization() {
|
---|
122 | Initialize();
|
---|
123 | }
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | private void Initialize() {
|
---|
127 | //if (algorithm != null) RegisterAlgorithmEvents();
|
---|
128 | if (bestQuality != null) RegisterQualityEvents();
|
---|
129 | }
|
---|
130 |
|
---|
131 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
132 | values.Add("RunsAverageExecutionTime", AverageExecutionTime);
|
---|
133 | values.Add("Repetitions", Repetitions);
|
---|
134 | values.Add("RunsBestQuality", BestQuality);
|
---|
135 | values.Add("RunsAverageQuality", AverageQuality);
|
---|
136 | values.Add("RunsWorstQuality", WorstQuality);
|
---|
137 | values.Add("RunsQualityVariance", QualityVariance);
|
---|
138 | values.Add("RunsQualityStandardDeviation", QualityStandardDeviation);
|
---|
139 | values.Add("Runs", Runs);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
143 | foreach (var p in parameters) {
|
---|
144 | values.Add(p);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | #region Events
|
---|
149 | //public event EventHandler AlgorithmChanged;
|
---|
150 | //private void OnAlgorithmChanged() {
|
---|
151 | // var handler = AlgorithmChanged;
|
---|
152 | // if (handler != null) handler(this, EventArgs.Empty);
|
---|
153 | //}
|
---|
154 | public event EventHandler QualityChanged;
|
---|
155 | private void OnQualityChanged() {
|
---|
156 | var handler = QualityChanged;
|
---|
157 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
158 | }
|
---|
159 |
|
---|
160 | //private void RegisterAlgorithmEvents() {
|
---|
161 |
|
---|
162 | //}
|
---|
163 | //private void DeregisterAlgorithmEvents() {
|
---|
164 |
|
---|
165 | //}
|
---|
166 | private void RegisterQualityEvents() {
|
---|
167 | BestQuality.ValueChanged += new EventHandler(Quality_ValueChanged);
|
---|
168 | }
|
---|
169 | private void DeregisterQualityEvents() {
|
---|
170 | BestQuality.ValueChanged -= new EventHandler(Quality_ValueChanged);
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
174 | OnQualityChanged();
|
---|
175 | }
|
---|
176 | #endregion
|
---|
177 |
|
---|
178 | public override void Parameterize(IParameterizedItem item) {
|
---|
179 | base.Parameterize(item);
|
---|
180 | this.parameters.Clear();
|
---|
181 | ((IAlgorithm)item).CollectParameterValues(this.Parameters);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public Experiment GenerateExperiment(IAlgorithm algorithm) {
|
---|
185 | Experiment experiment = new Experiment();
|
---|
186 | foreach (IValueConfiguration combination in this) {
|
---|
187 | IAlgorithm clonedAlg = (IAlgorithm)algorithm.Clone();
|
---|
188 | clonedAlg.Name = combination.ToParameterInfoString();
|
---|
189 | combination.Parameterize(clonedAlg);
|
---|
190 | experiment.Optimizers.Add(clonedAlg);
|
---|
191 | }
|
---|
192 | return experiment;
|
---|
193 | }
|
---|
194 |
|
---|
195 | public IEnumerator GetEnumerator() {
|
---|
196 | IEnumerator enumerator = new ParameterCombinationsEnumerator(this);
|
---|
197 | enumerator.Reset();
|
---|
198 | return enumerator;
|
---|
199 | }
|
---|
200 |
|
---|
201 | public long GetCombinationCount() {
|
---|
202 | long cnt = 0;
|
---|
203 | foreach (var c in this) {
|
---|
204 | cnt++;
|
---|
205 | }
|
---|
206 | return cnt;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|