Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/ParameterConfigurationTree.cs @ 15286

Last change on this file since 15286 was 7840, checked in by spimming, 13 years ago

#1853:

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