1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Text;
|
---|
27 | using System.Threading;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
|
---|
35 | // todo: storable, name, descr, ...
|
---|
36 | [StorableClass]
|
---|
37 | public class ParameterConfigurationTree : ParameterizedValueConfiguration, IEnumerable {
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | private DoubleValue quality;
|
---|
41 | public DoubleValue Quality {
|
---|
42 | get { return quality; }
|
---|
43 | set {
|
---|
44 | if (quality != value) {
|
---|
45 | quality = value;
|
---|
46 | OnQualityChanged();
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [Storable]
|
---|
52 | private DoubleArray normalizedQualityAverages;
|
---|
53 | public DoubleArray NormalizedQualityAverages {
|
---|
54 | get { return normalizedQualityAverages; }
|
---|
55 | set {
|
---|
56 | if (normalizedQualityAverages != value) {
|
---|
57 | normalizedQualityAverages = value;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [Storable]
|
---|
63 | private DoubleArray normalizedQualityDeviations;
|
---|
64 | public DoubleArray NormalizedQualityDeviations {
|
---|
65 | get { return normalizedQualityDeviations; }
|
---|
66 | set {
|
---|
67 | if (normalizedQualityDeviations != value) {
|
---|
68 | normalizedQualityDeviations = value;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | [Storable]
|
---|
74 | private DoubleArray normalizedEvaluatedSolutions;
|
---|
75 | public DoubleArray NormalizedEvaluatedSolutions {
|
---|
76 | get { return normalizedEvaluatedSolutions; }
|
---|
77 | set {
|
---|
78 | if (normalizedEvaluatedSolutions != value) {
|
---|
79 | normalizedEvaluatedSolutions = value;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | [Storable]
|
---|
85 | private DoubleArray bestQualities;
|
---|
86 | public DoubleArray BestQualities {
|
---|
87 | get { return bestQualities; }
|
---|
88 | set {
|
---|
89 | if (bestQualities != value) {
|
---|
90 | bestQualities = value;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | [Storable]
|
---|
96 | private DoubleArray averageQualities;
|
---|
97 | public DoubleArray AverageQualities {
|
---|
98 | get { return averageQualities; }
|
---|
99 | set { averageQualities = value; }
|
---|
100 | }
|
---|
101 |
|
---|
102 | [Storable]
|
---|
103 | private DoubleArray worstQualities;
|
---|
104 | public DoubleArray WorstQualities {
|
---|
105 | get { return worstQualities; }
|
---|
106 | set { worstQualities = value; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | [Storable]
|
---|
110 | private DoubleArray qualityVariances;
|
---|
111 | public DoubleArray QualityVariances {
|
---|
112 | get { return qualityVariances; }
|
---|
113 | set { qualityVariances = value; }
|
---|
114 | }
|
---|
115 |
|
---|
116 | [Storable]
|
---|
117 | private DoubleArray qualityStandardDeviations;
|
---|
118 | public DoubleArray QualityStandardDeviations {
|
---|
119 | get { return qualityStandardDeviations; }
|
---|
120 | set { qualityStandardDeviations = value; }
|
---|
121 | }
|
---|
122 |
|
---|
123 | [Storable]
|
---|
124 | private ItemList<TimeSpanValue> averageExecutionTimes;
|
---|
125 | public ItemList<TimeSpanValue> AverageExecutionTimes {
|
---|
126 | get { return averageExecutionTimes; }
|
---|
127 | set { averageExecutionTimes = value; }
|
---|
128 | }
|
---|
129 |
|
---|
130 | [Storable]
|
---|
131 | private DoubleArray averageEvaluatedSolutions;
|
---|
132 | public DoubleArray AverageEvaluatedSolutions {
|
---|
133 | get { return averageEvaluatedSolutions; }
|
---|
134 | set { averageEvaluatedSolutions = value; }
|
---|
135 | }
|
---|
136 |
|
---|
137 | [Storable]
|
---|
138 | private IntValue repetitions;
|
---|
139 | public IntValue Repetitions {
|
---|
140 | get { return repetitions; }
|
---|
141 | set { repetitions = value; }
|
---|
142 | }
|
---|
143 |
|
---|
144 | [Storable]
|
---|
145 | protected RunCollection runs;
|
---|
146 | public RunCollection Runs {
|
---|
147 | get { return runs; }
|
---|
148 | set { runs = value; }
|
---|
149 | }
|
---|
150 |
|
---|
151 | [Storable]
|
---|
152 | protected IDictionary<string, IItem> parameters;
|
---|
153 | public IDictionary<string, IItem> Parameters {
|
---|
154 | get { return parameters; }
|
---|
155 | set { parameters = value; }
|
---|
156 | }
|
---|
157 |
|
---|
158 | [Storable]
|
---|
159 | private double experimentGenerationProgress;
|
---|
160 | public double ExperimentGenerationProgress {
|
---|
161 | get { return experimentGenerationProgress; }
|
---|
162 | private set {
|
---|
163 | if (experimentGenerationProgress != value) {
|
---|
164 | experimentGenerationProgress = value;
|
---|
165 | OnExperimentGenerationProgressChanged();
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public ParameterizedValueConfiguration AlgorithmConfiguration {
|
---|
171 | get {
|
---|
172 | return this.ParameterConfigurations.ElementAt(0).ValueConfigurations.First() as ParameterizedValueConfiguration;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | public ParameterizedValueConfiguration ProblemConfiguration {
|
---|
177 | get {
|
---|
178 | return this.ParameterConfigurations.ElementAt(1).ValueConfigurations.First() as ParameterizedValueConfiguration;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | #region constructors and cloning
|
---|
183 | public ParameterConfigurationTree(IAlgorithm algorithm, IProblem problem)
|
---|
184 | : base(null, algorithm.GetType(), false) {
|
---|
185 | this.Optimize = false;
|
---|
186 | this.IsOptimizable = false;
|
---|
187 | this.parameters = new Dictionary<string, IItem>();
|
---|
188 | this.Name = algorithm.ItemName;
|
---|
189 |
|
---|
190 | var algproblemitem = new AlgorithmProblemItem();
|
---|
191 | algproblemitem.AlgorithmParameter.Value = algorithm;
|
---|
192 | algproblemitem.ProblemParameter.Value = problem;
|
---|
193 | this.discoverValidValues = false;
|
---|
194 |
|
---|
195 | this.parameterConfigurations.Add(new SingleValuedParameterConfiguration("Algorithm", algproblemitem.AlgorithmParameter));
|
---|
196 | this.parameterConfigurations.Add(new SingleValuedParameterConfiguration("Problem", algproblemitem.ProblemParameter));
|
---|
197 |
|
---|
198 | // problems can be modified in the list of problem instances, so the parameters which are not Optimize=true,
|
---|
199 | // must not be modifiable in the parameter configuration tree. otherwise the parameter values would be ambiguous
|
---|
200 | ProblemConfiguration.ValuesReadOnly = true;
|
---|
201 | }
|
---|
202 | public ParameterConfigurationTree() { }
|
---|
203 | [StorableConstructor]
|
---|
204 | protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { }
|
---|
205 | protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
|
---|
206 | : base(original, cloner) {
|
---|
207 | this.quality = cloner.Clone(original.quality);
|
---|
208 | this.normalizedQualityAverages = cloner.Clone(original.normalizedQualityAverages);
|
---|
209 | this.normalizedQualityDeviations = cloner.Clone(original.normalizedQualityDeviations);
|
---|
210 | this.normalizedEvaluatedSolutions = cloner.Clone(original.normalizedEvaluatedSolutions);
|
---|
211 | this.bestQualities = cloner.Clone(original.BestQualities);
|
---|
212 | this.averageQualities = cloner.Clone(original.averageQualities);
|
---|
213 | this.worstQualities = cloner.Clone(original.worstQualities);
|
---|
214 | this.qualityStandardDeviations = cloner.Clone(original.qualityStandardDeviations);
|
---|
215 | this.qualityVariances = cloner.Clone(original.qualityVariances);
|
---|
216 | this.averageExecutionTimes = cloner.Clone(original.averageExecutionTimes);
|
---|
217 | this.averageEvaluatedSolutions = cloner.Clone(original.averageEvaluatedSolutions);
|
---|
218 | this.repetitions = cloner.Clone(original.repetitions);
|
---|
219 | this.runs = cloner.Clone(original.runs);
|
---|
220 | this.parameters = new Dictionary<string, IItem>();
|
---|
221 | if (original.parameters != null) {
|
---|
222 | foreach (var p in original.parameters) {
|
---|
223 | this.parameters.Add(p.Key, cloner.Clone(p.Value));
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
228 | return new ParameterConfigurationTree(this, cloner);
|
---|
229 | }
|
---|
230 | [StorableHook(HookType.AfterDeserialization)]
|
---|
231 | private void AfterDeserialization() {
|
---|
232 | if (ProblemConfiguration != null) ProblemConfiguration.ValuesReadOnly = true;
|
---|
233 | }
|
---|
234 | #endregion
|
---|
235 |
|
---|
236 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
237 | values.Add("RunsAverageExecutionTimes", AverageExecutionTimes);
|
---|
238 | values.Add("RunsAverageEvaluatedSolutions", AverageEvaluatedSolutions);
|
---|
239 | values.Add("Repetitions", Repetitions);
|
---|
240 | values.Add("RunsBestQualities", BestQualities);
|
---|
241 | values.Add("RunsAverageQualities", AverageQualities);
|
---|
242 | values.Add("RunsWorstQualities", WorstQualities);
|
---|
243 | values.Add("RunsQualityVariances", QualityVariances);
|
---|
244 | values.Add("RunsQualityStandardDeviations", QualityStandardDeviations);
|
---|
245 | values.Add("QualitiesNormalized", NormalizedQualityAverages);
|
---|
246 | values.Add("AverageQualityNormalized", Quality);
|
---|
247 | values.Add("Runs", Runs);
|
---|
248 | }
|
---|
249 |
|
---|
250 | public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
251 | foreach (var p in parameters) {
|
---|
252 | values.Add(p);
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | #region Events
|
---|
257 | public event EventHandler QualityChanged;
|
---|
258 | private void OnQualityChanged() {
|
---|
259 | var handler = QualityChanged;
|
---|
260 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
261 | }
|
---|
262 |
|
---|
263 | private void Quality_ValueChanged(object sender, EventArgs e) {
|
---|
264 | OnQualityChanged();
|
---|
265 | }
|
---|
266 |
|
---|
267 | public event EventHandler ExperimentGenerationProgressChanged;
|
---|
268 | private void OnExperimentGenerationProgressChanged() {
|
---|
269 | var handler = ExperimentGenerationProgressChanged;
|
---|
270 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
271 | }
|
---|
272 | #endregion
|
---|
273 |
|
---|
274 | public override void Parameterize(IParameterizedItem item) {
|
---|
275 | this.parameters.Clear();
|
---|
276 | var algorithm = item as IAlgorithm;
|
---|
277 | var problem = algorithm.Problem;
|
---|
278 |
|
---|
279 | ProblemConfiguration.Parameterize(problem);
|
---|
280 | AlgorithmConfiguration.Parameterize(algorithm);
|
---|
281 |
|
---|
282 | algorithm.CollectParameterValues(this.Parameters);
|
---|
283 | }
|
---|
284 |
|
---|
285 | public Experiment GenerateExperiment(IAlgorithm algorithm, bool createBatchRuns, int repetitions, CancellationToken ct) {
|
---|
286 | Experiment experiment = new Experiment();
|
---|
287 | ExperimentGenerationProgress = 0;
|
---|
288 | foreach (ParameterizedValueConfiguration combination in this) {
|
---|
289 | ct.ThrowIfCancellationRequested();
|
---|
290 | IAlgorithm clonedAlg = (IAlgorithm)algorithm.Clone();
|
---|
291 | clonedAlg.Name = combination.ParameterInfoString;
|
---|
292 | combination.Parameterize(clonedAlg);
|
---|
293 | clonedAlg.StoreAlgorithmInEachRun = false;
|
---|
294 | if (createBatchRuns) {
|
---|
295 | BatchRun batchRun = new BatchRun(string.Format("BatchRun: {0}", combination.ParameterInfoString));
|
---|
296 | batchRun.Optimizer = clonedAlg;
|
---|
297 | batchRun.Repetitions = repetitions;
|
---|
298 | experiment.Optimizers.Add(batchRun);
|
---|
299 | } else {
|
---|
300 | experiment.Optimizers.Add(clonedAlg);
|
---|
301 | }
|
---|
302 | ExperimentGenerationProgress = (double)experiment.Optimizers.Count / this.GetCombinationCount(0);
|
---|
303 | }
|
---|
304 | return experiment;
|
---|
305 | }
|
---|
306 |
|
---|
307 | public Experiment GenerateExperiment(IAlgorithm algorithm) {
|
---|
308 | return GenerateExperiment(algorithm, false, 0, CancellationToken.None);
|
---|
309 | }
|
---|
310 |
|
---|
311 | public Experiment GenerateExperiment(IAlgorithm algorithm, bool createBatchRuns, int repetitions) {
|
---|
312 | return GenerateExperiment(algorithm, createBatchRuns, repetitions, CancellationToken.None);
|
---|
313 | }
|
---|
314 |
|
---|
315 | public IEnumerator GetEnumerator() {
|
---|
316 | IEnumerator enumerator = new ParameterCombinationsEnumerator(this);
|
---|
317 | enumerator.Reset();
|
---|
318 | return enumerator;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /// <summary>
|
---|
322 | /// returns the number of possible parameter combinations
|
---|
323 | /// </summary>
|
---|
324 | /// <param name="max">algorithm stops counting when max is reached. zero for infinite counting</param>
|
---|
325 | /// <returns></returns>
|
---|
326 | public long GetCombinationCount(long max) {
|
---|
327 | long cnt = 0;
|
---|
328 | foreach (var c in this) {
|
---|
329 | cnt++;
|
---|
330 | if (max > 0 && cnt >= max) {
|
---|
331 | return cnt;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | return cnt;
|
---|
335 | }
|
---|
336 |
|
---|
337 | public IOptimizable GetRandomOptimizable(IRandom random) {
|
---|
338 | List<IOptimizable> allOptimizables = GetAllOptimizables();
|
---|
339 | return allOptimizables[random.Next(allOptimizables.Count)];
|
---|
340 | }
|
---|
341 |
|
---|
342 | public override string ToString() {
|
---|
343 | return this.Name;
|
---|
344 | }
|
---|
345 |
|
---|
346 | public IRun ToRun(bool clearParameters) {
|
---|
347 | return ToRun(this.ParameterInfoString, clearParameters);
|
---|
348 | }
|
---|
349 |
|
---|
350 | public IRun ToRun(string name, bool clearParameters) {
|
---|
351 | IRun run = new Run();
|
---|
352 | run.Name = name;
|
---|
353 | this.CollectResultValues(run.Results);
|
---|
354 | this.CollectParameterValues(run.Parameters);
|
---|
355 | if (clearParameters) ClearParameters(run, this.GetOptimizedParameterNames());
|
---|
356 | return run;
|
---|
357 | }
|
---|
358 |
|
---|
359 | public override string ParameterInfoString {
|
---|
360 | get {
|
---|
361 | string algorithmInfo = this.AlgorithmConfiguration.ParameterInfoString;
|
---|
362 | string problemInfo = this.ProblemConfiguration.ParameterInfoString;
|
---|
363 | var sb = new StringBuilder();
|
---|
364 | if (!string.IsNullOrEmpty(algorithmInfo)) {
|
---|
365 | sb.Append("Algorithm (");
|
---|
366 | sb.Append(algorithmInfo);
|
---|
367 | sb.Append(")");
|
---|
368 | }
|
---|
369 | if (!string.IsNullOrEmpty(problemInfo)) {
|
---|
370 | if (sb.Length > 0)
|
---|
371 | sb.Append(", ");
|
---|
372 | sb.Append("Problem( ");
|
---|
373 | sb.Append(problemInfo);
|
---|
374 | sb.Append(")");
|
---|
375 | }
|
---|
376 | return sb.ToString();
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
|
---|
381 | AlgorithmConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty);
|
---|
382 | ProblemConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty);
|
---|
383 | }
|
---|
384 |
|
---|
385 | #region Helpers
|
---|
386 | /// <summary>
|
---|
387 | /// Removes those parameters from the run which are not declared in parametersToKeep
|
---|
388 | /// </summary>
|
---|
389 | private void ClearParameters(IRun run, IEnumerable<string> parametersToKeep) {
|
---|
390 | var parametersToRemove = new List<string>();
|
---|
391 | foreach (var parameter in run.Parameters) {
|
---|
392 | if (!parametersToKeep.Contains(parameter.Key))
|
---|
393 | parametersToRemove.Add(parameter.Key);
|
---|
394 | }
|
---|
395 | foreach (var parameter in parametersToRemove)
|
---|
396 | run.Parameters.Remove(parameter);
|
---|
397 | }
|
---|
398 | #endregion
|
---|
399 | }
|
---|
400 | }
|
---|