Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurationTree.cs @ 5313

Last change on this file since 5313 was 5313, checked in by cneumuel, 14 years ago

#1215

  • changed AlgorithType and ProblemType to actually be types not objects. this eliminates redundant views for MetaOptimizationProblem
  • import algorithm for MetaOptimizationProblem
  • nicer dialog for combination creation
  • fixed iconimage for ParameterConfigurations
  • fixed ValidValues
File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Common;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.Optimization;
9using HeuristicLab.Data;
10using System.Collections;
11
12namespace 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}
Note: See TracBrowser for help on using the repository browser.