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 |
|
---|
17 | [Storable]
|
---|
18 | private DoubleValue averageQualityNormalized;
|
---|
19 | public DoubleValue AverageQualityNormalized {
|
---|
20 | get { return averageQualityNormalized; }
|
---|
21 | set {
|
---|
22 | if (averageQualityNormalized != value) {
|
---|
23 | averageQualityNormalized = value;
|
---|
24 | OnQualityChanged();
|
---|
25 | }
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | [Storable]
|
---|
30 | private DoubleArray qualitiesNormalized;
|
---|
31 | public DoubleArray QualitiesNormalized {
|
---|
32 | get { return qualitiesNormalized; }
|
---|
33 | set {
|
---|
34 | if (qualitiesNormalized != value) {
|
---|
35 | qualitiesNormalized = value;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private DoubleArray bestQualities;
|
---|
42 | public DoubleArray BestQualities {
|
---|
43 | get { return bestQualities; }
|
---|
44 | set {
|
---|
45 | if (bestQualities != value) {
|
---|
46 | bestQualities = value;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [Storable]
|
---|
52 | private DoubleArray averageQualities;
|
---|
53 | public DoubleArray AverageQualities {
|
---|
54 | get { return averageQualities; }
|
---|
55 | set { averageQualities = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [Storable]
|
---|
59 | private DoubleArray worstQualities;
|
---|
60 | public DoubleArray WorstQualities {
|
---|
61 | get { return worstQualities; }
|
---|
62 | set { worstQualities = value; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | [Storable]
|
---|
66 | private DoubleArray qualityVariances;
|
---|
67 | public DoubleArray QualityVariances {
|
---|
68 | get { return qualityVariances; }
|
---|
69 | set { qualityVariances = value; }
|
---|
70 | }
|
---|
71 |
|
---|
72 | [Storable]
|
---|
73 | private DoubleArray qualityStandardDeviations;
|
---|
74 | public DoubleArray QualityStandardDeviations {
|
---|
75 | get { return qualityStandardDeviations; }
|
---|
76 | set { qualityStandardDeviations = value; }
|
---|
77 | }
|
---|
78 |
|
---|
79 | [Storable]
|
---|
80 | private ItemList<TimeSpanValue> averageExecutionTimes;
|
---|
81 | public ItemList<TimeSpanValue> AverageExecutionTimes {
|
---|
82 | get { return averageExecutionTimes; }
|
---|
83 | set { averageExecutionTimes = value; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | [Storable]
|
---|
87 | private IntValue repetitions;
|
---|
88 | public IntValue Repetitions {
|
---|
89 | get { return repetitions; }
|
---|
90 | set { repetitions = value; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | [Storable]
|
---|
94 | protected RunCollection runs;
|
---|
95 | public RunCollection Runs {
|
---|
96 | get { return runs; }
|
---|
97 | set { runs = value; }
|
---|
98 | }
|
---|
99 |
|
---|
100 | [Storable]
|
---|
101 | protected IDictionary<string, IItem> parameters;
|
---|
102 | public IDictionary<string, IItem> Parameters {
|
---|
103 | get { return parameters; }
|
---|
104 | set { parameters = value; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | [Storable]
|
---|
108 | protected string name;
|
---|
109 | public string Name {
|
---|
110 | get { return name; }
|
---|
111 | set { name = value; }
|
---|
112 | }
|
---|
113 |
|
---|
114 | #region constructors and cloning
|
---|
115 | public ParameterConfigurationTree(EngineAlgorithm algorithm)
|
---|
116 | : base(null, algorithm.GetType()) {
|
---|
117 | this.Optimize = true; // root must always be optimized
|
---|
118 | this.parameters = new Dictionary<string, IItem>();
|
---|
119 | this.Name = algorithm.ItemName;
|
---|
120 | PopulateParameterConfigurations(algorithm);
|
---|
121 | }
|
---|
122 | public ParameterConfigurationTree() {
|
---|
123 | }
|
---|
124 | [StorableConstructor]
|
---|
125 | protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { }
|
---|
126 | protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
|
---|
127 | : base(original, cloner) {
|
---|
128 | this.name = original.name;
|
---|
129 | this.bestQualities = cloner.Clone(original.BestQualities);
|
---|
130 | this.averageQualities = cloner.Clone(original.averageQualities);
|
---|
131 | this.worstQualities = cloner.Clone(original.worstQualities);
|
---|
132 | this.qualityStandardDeviations = cloner.Clone(original.qualityStandardDeviations);
|
---|
133 | this.qualityVariances = cloner.Clone(original.qualityVariances);
|
---|
134 | this.averageExecutionTimes = cloner.Clone(original.averageExecutionTimes);
|
---|
135 | this.repetitions = cloner.Clone(original.repetitions);
|
---|
136 | this.runs = cloner.Clone(original.runs);
|
---|
137 | this.parameters = new Dictionary<string, IItem>();
|
---|
138 | foreach (var p in original.parameters) {
|
---|
139 | this.parameters.Add(p.Key, cloner.Clone(p.Value));
|
---|
140 | }
|
---|
141 | }
|
---|
142 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
143 | return new ParameterConfigurationTree(this, cloner);
|
---|
144 | }
|
---|
145 | [StorableHook(HookType.AfterDeserialization)]
|
---|
146 | private void AfterDeserialization() {
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 |
|
---|
150 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
151 | values.Add("RunsAverageExecutionTimes", AverageExecutionTimes);
|
---|
152 | values.Add("Repetitions", Repetitions);
|
---|
153 | values.Add("RunsBestQualities", BestQualities);
|
---|
154 | values.Add("RunsAverageQualities", AverageQualities);
|
---|
155 | values.Add("RunsWorstQualities", WorstQualities);
|
---|
156 | values.Add("RunsQualityVariances", QualityVariances);
|
---|
157 | values.Add("RunsQualityStandardDeviations", QualityStandardDeviations);
|
---|
158 | values.Add("QualitiesNormalized", QualitiesNormalized);
|
---|
159 | values.Add("AverageQualityNormalized", AverageQualityNormalized);
|
---|
160 | values.Add("Runs", Runs);
|
---|
161 | }
|
---|
162 |
|
---|
163 | public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
164 | foreach (var p in parameters) {
|
---|
165 | values.Add(p);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | #region Events
|
---|
170 | public event EventHandler QualityChanged;
|
---|
171 | private void OnQualityChanged() {
|
---|
172 | var handler = QualityChanged;
|
---|
173 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
174 | }
|
---|
175 |
|
---|
176 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
177 | OnQualityChanged();
|
---|
178 | }
|
---|
179 | #endregion
|
---|
180 |
|
---|
181 | public override void Parameterize(IParameterizedItem item) {
|
---|
182 | this.parameters.Clear();
|
---|
183 | base.Parameterize(item);
|
---|
184 | ((IAlgorithm)item).CollectParameterValues(this.Parameters);
|
---|
185 | }
|
---|
186 |
|
---|
187 | public Experiment GenerateExperiment(IAlgorithm algorithm, bool createBatchRuns, int repetitions) {
|
---|
188 | Experiment experiment = new Experiment();
|
---|
189 | foreach (IValueConfiguration combination in this) {
|
---|
190 | IAlgorithm clonedAlg = (IAlgorithm)algorithm.Clone();
|
---|
191 | clonedAlg.Name = combination.ParameterInfoString;
|
---|
192 | combination.Parameterize(clonedAlg);
|
---|
193 | clonedAlg.StoreAlgorithmInEachRun = false;
|
---|
194 | if (createBatchRuns) {
|
---|
195 | BatchRun batchRun = new BatchRun(string.Format("BatchRun: {0}", combination.ParameterInfoString));
|
---|
196 | batchRun.Optimizer = clonedAlg;
|
---|
197 | batchRun.Repetitions = repetitions;
|
---|
198 | experiment.Optimizers.Add(batchRun);
|
---|
199 | } else {
|
---|
200 | experiment.Optimizers.Add(clonedAlg);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | return experiment;
|
---|
204 | }
|
---|
205 |
|
---|
206 | public Experiment GenerateExperiment(IAlgorithm algorithm) {
|
---|
207 | return GenerateExperiment(algorithm, false, 0);
|
---|
208 | }
|
---|
209 |
|
---|
210 | public IEnumerator GetEnumerator() {
|
---|
211 | IEnumerator enumerator = new ParameterCombinationsEnumerator(this);
|
---|
212 | enumerator.Reset();
|
---|
213 | return enumerator;
|
---|
214 | }
|
---|
215 |
|
---|
216 | public long GetCombinationCount() {
|
---|
217 | long cnt = 0;
|
---|
218 | foreach (var c in this) {
|
---|
219 | cnt++;
|
---|
220 | }
|
---|
221 | return cnt;
|
---|
222 | }
|
---|
223 |
|
---|
224 | public IOptimizable GetRandomOptimizable(IRandom random) {
|
---|
225 | List<IOptimizable> allOptimizables = GetAllOptimizables();
|
---|
226 | return allOptimizables[random.Next(allOptimizables.Count)];
|
---|
227 | }
|
---|
228 |
|
---|
229 | public override string ToString() {
|
---|
230 | return this.Name;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|