1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
13 | // todo: storable, name, descr, ...
|
---|
14 | [StorableClass]
|
---|
15 | public class ParameterConfigurationTree : ParameterizedValueConfiguration, IEnumerable {
|
---|
16 |
|
---|
17 | [Storable]
|
---|
18 | private DoubleValue quality;
|
---|
19 | public DoubleValue Quality {
|
---|
20 | get { return quality; }
|
---|
21 | set {
|
---|
22 | if (quality != value) {
|
---|
23 | quality = value;
|
---|
24 | OnQualityChanged();
|
---|
25 | }
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | [Storable]
|
---|
30 | private DoubleArray normalizedQualityAverages;
|
---|
31 | public DoubleArray NormalizedQualityAverages {
|
---|
32 | get { return normalizedQualityAverages; }
|
---|
33 | set {
|
---|
34 | if (normalizedQualityAverages != value) {
|
---|
35 | normalizedQualityAverages = value;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private DoubleArray normalizedQualityDeviations;
|
---|
42 | public DoubleArray NormalizedQualityDeviations {
|
---|
43 | get { return normalizedQualityDeviations; }
|
---|
44 | set {
|
---|
45 | if (normalizedQualityDeviations != value) {
|
---|
46 | normalizedQualityDeviations = value;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [Storable]
|
---|
52 | private DoubleArray normalizedEvaluatedSolutions;
|
---|
53 | public DoubleArray NormalizedEvaluatedSolutions {
|
---|
54 | get { return normalizedEvaluatedSolutions; }
|
---|
55 | set {
|
---|
56 | if (normalizedEvaluatedSolutions != value) {
|
---|
57 | normalizedEvaluatedSolutions = value;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [Storable]
|
---|
63 | private DoubleArray bestQualities;
|
---|
64 | public DoubleArray BestQualities {
|
---|
65 | get { return bestQualities; }
|
---|
66 | set {
|
---|
67 | if (bestQualities != value) {
|
---|
68 | bestQualities = value;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | [Storable]
|
---|
74 | private DoubleArray averageQualities;
|
---|
75 | public DoubleArray AverageQualities {
|
---|
76 | get { return averageQualities; }
|
---|
77 | set { averageQualities = value; }
|
---|
78 | }
|
---|
79 |
|
---|
80 | [Storable]
|
---|
81 | private DoubleArray worstQualities;
|
---|
82 | public DoubleArray WorstQualities {
|
---|
83 | get { return worstQualities; }
|
---|
84 | set { worstQualities = value; }
|
---|
85 | }
|
---|
86 |
|
---|
87 | [Storable]
|
---|
88 | private DoubleArray qualityVariances;
|
---|
89 | public DoubleArray QualityVariances {
|
---|
90 | get { return qualityVariances; }
|
---|
91 | set { qualityVariances = value; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | [Storable]
|
---|
95 | private DoubleArray qualityStandardDeviations;
|
---|
96 | public DoubleArray QualityStandardDeviations {
|
---|
97 | get { return qualityStandardDeviations; }
|
---|
98 | set { qualityStandardDeviations = value; }
|
---|
99 | }
|
---|
100 |
|
---|
101 | [Storable]
|
---|
102 | private ItemList<TimeSpanValue> averageExecutionTimes;
|
---|
103 | public ItemList<TimeSpanValue> AverageExecutionTimes {
|
---|
104 | get { return averageExecutionTimes; }
|
---|
105 | set { averageExecutionTimes = value; }
|
---|
106 | }
|
---|
107 |
|
---|
108 | [Storable]
|
---|
109 | private DoubleArray averageEvaluatedSolutions;
|
---|
110 | public DoubleArray AverageEvaluatedSolutions {
|
---|
111 | get { return averageEvaluatedSolutions; }
|
---|
112 | set { averageEvaluatedSolutions = value; }
|
---|
113 | }
|
---|
114 |
|
---|
115 | [Storable]
|
---|
116 | private IntValue repetitions;
|
---|
117 | public IntValue Repetitions {
|
---|
118 | get { return repetitions; }
|
---|
119 | set { repetitions = value; }
|
---|
120 | }
|
---|
121 |
|
---|
122 | [Storable]
|
---|
123 | protected RunCollection runs;
|
---|
124 | public RunCollection Runs {
|
---|
125 | get { return runs; }
|
---|
126 | set { runs = value; }
|
---|
127 | }
|
---|
128 |
|
---|
129 | [Storable]
|
---|
130 | protected IDictionary<string, IItem> parameters;
|
---|
131 | public IDictionary<string, IItem> Parameters {
|
---|
132 | get { return parameters; }
|
---|
133 | set { parameters = value; }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public ParameterizedValueConfiguration AlgorithmConfiguration {
|
---|
137 | get {
|
---|
138 | return this.ParameterConfigurations.ElementAt(0).ValueConfigurations.First() as ParameterizedValueConfiguration;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public ParameterizedValueConfiguration ProblemConfiguration {
|
---|
143 | get {
|
---|
144 | return this.ParameterConfigurations.ElementAt(1).ValueConfigurations.First() as ParameterizedValueConfiguration;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | #region constructors and cloning
|
---|
149 | public ParameterConfigurationTree(IAlgorithm algorithm, IProblem problem)
|
---|
150 | : base(null, algorithm.GetType(), false) {
|
---|
151 | this.Optimize = false;
|
---|
152 | this.IsOptimizable = false;
|
---|
153 | this.parameters = new Dictionary<string, IItem>();
|
---|
154 | this.Name = algorithm.ItemName;
|
---|
155 |
|
---|
156 | var algproblemitem = new AlgorithmProblemItem();
|
---|
157 | algproblemitem.AlgorithmParameter.Value = algorithm;
|
---|
158 | algproblemitem.ProblemParameter.Value = problem;
|
---|
159 | this.discoverValidValues = false;
|
---|
160 |
|
---|
161 | this.parameterConfigurations.Add(new SingleValuedParameterConfiguration("Algorithm", algproblemitem.AlgorithmParameter));
|
---|
162 | this.parameterConfigurations.Add(new SingleValuedParameterConfiguration("Problem", algproblemitem.ProblemParameter));
|
---|
163 | }
|
---|
164 | public ParameterConfigurationTree() { }
|
---|
165 | [StorableConstructor]
|
---|
166 | protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { }
|
---|
167 | protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
|
---|
168 | : base(original, cloner) {
|
---|
169 | this.quality = cloner.Clone(original.quality);
|
---|
170 | this.normalizedQualityAverages = cloner.Clone(original.normalizedQualityAverages);
|
---|
171 | this.normalizedQualityDeviations = cloner.Clone(original.normalizedQualityDeviations);
|
---|
172 | this.normalizedEvaluatedSolutions = cloner.Clone(original.normalizedEvaluatedSolutions);
|
---|
173 | this.bestQualities = cloner.Clone(original.BestQualities);
|
---|
174 | this.averageQualities = cloner.Clone(original.averageQualities);
|
---|
175 | this.worstQualities = cloner.Clone(original.worstQualities);
|
---|
176 | this.qualityStandardDeviations = cloner.Clone(original.qualityStandardDeviations);
|
---|
177 | this.qualityVariances = cloner.Clone(original.qualityVariances);
|
---|
178 | this.averageExecutionTimes = cloner.Clone(original.averageExecutionTimes);
|
---|
179 | this.averageEvaluatedSolutions = cloner.Clone(original.averageEvaluatedSolutions);
|
---|
180 | this.repetitions = cloner.Clone(original.repetitions);
|
---|
181 | this.runs = cloner.Clone(original.runs);
|
---|
182 | this.parameters = new Dictionary<string, IItem>();
|
---|
183 | foreach (var p in original.parameters) {
|
---|
184 | this.parameters.Add(p.Key, cloner.Clone(p.Value));
|
---|
185 | }
|
---|
186 | }
|
---|
187 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
188 | return new ParameterConfigurationTree(this, cloner);
|
---|
189 | }
|
---|
190 | [StorableHook(HookType.AfterDeserialization)]
|
---|
191 | private void AfterDeserialization() {
|
---|
192 | }
|
---|
193 | #endregion
|
---|
194 |
|
---|
195 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
196 | values.Add("RunsAverageExecutionTimes", AverageExecutionTimes);
|
---|
197 | values.Add("RunsAverageEvaluatedSolutions", AverageEvaluatedSolutions);
|
---|
198 | values.Add("Repetitions", Repetitions);
|
---|
199 | values.Add("RunsBestQualities", BestQualities);
|
---|
200 | values.Add("RunsAverageQualities", AverageQualities);
|
---|
201 | values.Add("RunsWorstQualities", WorstQualities);
|
---|
202 | values.Add("RunsQualityVariances", QualityVariances);
|
---|
203 | values.Add("RunsQualityStandardDeviations", QualityStandardDeviations);
|
---|
204 | values.Add("QualitiesNormalized", NormalizedQualityAverages);
|
---|
205 | values.Add("AverageQualityNormalized", Quality);
|
---|
206 | values.Add("Runs", Runs);
|
---|
207 | }
|
---|
208 |
|
---|
209 | public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
210 | foreach (var p in parameters) {
|
---|
211 | values.Add(p);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | #region Events
|
---|
216 | public event EventHandler QualityChanged;
|
---|
217 | private void OnQualityChanged() {
|
---|
218 | var handler = QualityChanged;
|
---|
219 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
220 | }
|
---|
221 |
|
---|
222 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
223 | OnQualityChanged();
|
---|
224 | }
|
---|
225 | #endregion
|
---|
226 |
|
---|
227 | public override void Parameterize(IParameterizedItem item) {
|
---|
228 | this.parameters.Clear();
|
---|
229 | var algorithm = item as IAlgorithm;
|
---|
230 | var problem = algorithm.Problem;
|
---|
231 |
|
---|
232 | ProblemConfiguration.Parameterize(problem);
|
---|
233 | AlgorithmConfiguration.Parameterize(algorithm);
|
---|
234 |
|
---|
235 | algorithm.CollectParameterValues(this.Parameters);
|
---|
236 | }
|
---|
237 |
|
---|
238 | public Experiment GenerateExperiment(IAlgorithm algorithm, bool createBatchRuns, int repetitions) {
|
---|
239 | Experiment experiment = new Experiment();
|
---|
240 | foreach (ParameterizedValueConfiguration combination in this) {
|
---|
241 | IAlgorithm clonedAlg = (IAlgorithm)algorithm.Clone();
|
---|
242 | clonedAlg.Name = combination.ParameterInfoString;
|
---|
243 | combination.Parameterize(clonedAlg);
|
---|
244 | clonedAlg.StoreAlgorithmInEachRun = false;
|
---|
245 | if (createBatchRuns) {
|
---|
246 | BatchRun batchRun = new BatchRun(string.Format("BatchRun: {0}", combination.ParameterInfoString));
|
---|
247 | batchRun.Optimizer = clonedAlg;
|
---|
248 | batchRun.Repetitions = repetitions;
|
---|
249 | experiment.Optimizers.Add(batchRun);
|
---|
250 | } else {
|
---|
251 | experiment.Optimizers.Add(clonedAlg);
|
---|
252 | }
|
---|
253 | }
|
---|
254 | return experiment;
|
---|
255 | }
|
---|
256 |
|
---|
257 | public Experiment GenerateExperiment(IAlgorithm algorithm) {
|
---|
258 | return GenerateExperiment(algorithm, false, 0);
|
---|
259 | }
|
---|
260 |
|
---|
261 | public IEnumerator GetEnumerator() {
|
---|
262 | IEnumerator enumerator = new ParameterCombinationsEnumerator(this);
|
---|
263 | enumerator.Reset();
|
---|
264 | return enumerator;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /// <summary>
|
---|
268 | /// returns the number of possible parameter combinations
|
---|
269 | /// </summary>
|
---|
270 | /// <param name="max">algorithm stops counting when max is reached. zero for infinite counting</param>
|
---|
271 | /// <returns></returns>
|
---|
272 | public long GetCombinationCount(long max) {
|
---|
273 | long cnt = 0;
|
---|
274 | foreach (var c in this) {
|
---|
275 | cnt++;
|
---|
276 | if (max > 0 && cnt >= max) {
|
---|
277 | return cnt;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | return cnt;
|
---|
281 | }
|
---|
282 |
|
---|
283 | public IOptimizable GetRandomOptimizable(IRandom random) {
|
---|
284 | List<IOptimizable> allOptimizables = GetAllOptimizables();
|
---|
285 | return allOptimizables[random.Next(allOptimizables.Count)];
|
---|
286 | }
|
---|
287 |
|
---|
288 | public override string ToString() {
|
---|
289 | return this.Name;
|
---|
290 | }
|
---|
291 |
|
---|
292 | public IRun ToRun(bool clearParameters) {
|
---|
293 | return ToRun(this.ParameterInfoString, clearParameters);
|
---|
294 | }
|
---|
295 |
|
---|
296 | public IRun ToRun(string name, bool clearParameters) {
|
---|
297 | IRun run = new Run();
|
---|
298 | run.Name = name;
|
---|
299 | this.CollectResultValues(run.Results);
|
---|
300 | this.CollectParameterValues(run.Parameters);
|
---|
301 | if(clearParameters) MetaOptimizationUtil.ClearParameters(run, this.GetOptimizedParameterNames());
|
---|
302 | return run;
|
---|
303 | }
|
---|
304 |
|
---|
305 | public override string ParameterInfoString {
|
---|
306 | get {
|
---|
307 | string algorithmInfo = this.AlgorithmConfiguration.ParameterInfoString;
|
---|
308 | string problemInfo = this.ProblemConfiguration.ParameterInfoString;
|
---|
309 | var sb = new StringBuilder();
|
---|
310 | if (!string.IsNullOrEmpty(algorithmInfo)) {
|
---|
311 | sb.Append("Algorithm (");
|
---|
312 | sb.Append(algorithmInfo);
|
---|
313 | sb.Append(")");
|
---|
314 | }
|
---|
315 | if (!string.IsNullOrEmpty(problemInfo)) {
|
---|
316 | if (sb.Length > 0)
|
---|
317 | sb.Append(", ");
|
---|
318 | sb.Append("Problem( ");
|
---|
319 | sb.Append(problemInfo);
|
---|
320 | sb.Append(")");
|
---|
321 | }
|
---|
322 | return sb.ToString();
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
|
---|
327 | AlgorithmConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty);
|
---|
328 | ProblemConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty);
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|