Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5184 was 5184, checked in by cneumuel, 13 years ago

#1215

  • enhanced combinations generator (now with batchruns!)
  • fixed ActualNames for metaopt-alg
  • added penalty for invalid solution-candidates (algs which throw exceptions)
  • migrated to .NET 4.0
File size: 7.2 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    [Storable]
17    private DoubleValue bestQuality;
18    public DoubleValue BestQuality {
19      get { return bestQuality; }
20      set {
21        if (bestQuality != value) {
22          if (bestQuality != null) DeregisterQualityEvents();
23          bestQuality = value;
24          if (bestQuality != null) RegisterQualityEvents();
25          OnQualityChanged();
26        }
27      }
28    }
29
30    [Storable]
31    private DoubleValue averageQuality;
32    public DoubleValue AverageQuality {
33      get { return averageQuality; }
34      set { averageQuality = value; }
35    }
36
37    [Storable]
38    private DoubleValue worstQuality;
39    public DoubleValue WorstQuality {
40      get { return worstQuality; }
41      set { worstQuality = value; }
42    }
43
44    [Storable]
45    private DoubleValue qualityVariance;
46    public DoubleValue QualityVariance {
47      get { return qualityVariance; }
48      set { qualityVariance = value; }
49    }
50
51    [Storable]
52    private DoubleValue qualityStandardDeviation;
53    public DoubleValue QualityStandardDeviation {
54      get { return qualityStandardDeviation; }
55      set { qualityStandardDeviation = value; }
56    }
57
58    [Storable]
59    private TimeSpanValue averageExecutionTime;
60    public TimeSpanValue AverageExecutionTime {
61      get { return averageExecutionTime; }
62      set { averageExecutionTime = value; }
63    }
64
65    [Storable]
66    private IntValue repetitions;
67    public IntValue Repetitions {
68      get { return repetitions; }
69      set { repetitions = value; }
70    }
71
72    [Storable]
73    protected RunCollection runs;
74    public RunCollection Runs {
75      get { return runs; }
76      set { runs = value; }
77    }
78
79    [Storable]
80    protected IDictionary<string, IItem> parameters;
81    public IDictionary<string, IItem> Parameters {
82      get { return parameters; }
83      set { parameters = value; }
84    }
85
86    #region constructors and cloning
87    public ParameterConfigurationTree(EngineAlgorithm algorithm)
88      : base(null, algorithm.GetType()) {
89      this.Optimize = true; // root must always be optimized
90      this.BestQuality = new DoubleValue();
91      this.parameters = new Dictionary<string, IItem>();
92
93      PopulateParameterConfigurations(algorithm);
94      Initialize();
95    }
96    public ParameterConfigurationTree() {
97      Initialize();
98    }
99    [StorableConstructor]
100    protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { }
101    protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
102      : base(original, cloner) {
103      this.bestQuality = cloner.Clone(original.BestQuality);
104      this.averageQuality = cloner.Clone(original.averageQuality);
105      this.worstQuality = cloner.Clone(original.worstQuality);
106      this.qualityStandardDeviation = cloner.Clone(original.qualityStandardDeviation);
107      this.qualityVariance = cloner.Clone(original.qualityVariance);
108      this.averageExecutionTime = cloner.Clone(original.averageExecutionTime);
109      this.repetitions = cloner.Clone(original.repetitions);
110      this.runs = cloner.Clone(original.runs);
111      this.parameters = new Dictionary<string, IItem>();
112      foreach (var p in original.parameters) {
113        this.parameters.Add(p.Key, cloner.Clone(p.Value));
114      }
115      Initialize();
116    }
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new ParameterConfigurationTree(this, cloner);
119    }
120    [StorableHook(HookType.AfterDeserialization)]
121    private void AfterDeserialization() {
122      Initialize();
123    }
124    #endregion
125
126    private void Initialize() {
127      //if (algorithm != null) RegisterAlgorithmEvents();
128      if (bestQuality != null) RegisterQualityEvents();
129    }
130
131    public virtual void CollectResultValues(IDictionary<string, IItem> values) {
132      values.Add("RunsAverageExecutionTime", AverageExecutionTime);
133      values.Add("Repetitions", Repetitions);
134      values.Add("RunsBestQuality", BestQuality);
135      values.Add("RunsAverageQuality", AverageQuality);
136      values.Add("RunsWorstQuality", WorstQuality);
137      values.Add("RunsQualityVariance", QualityVariance);
138      values.Add("RunsQualityStandardDeviation", QualityStandardDeviation);
139      values.Add("Runs", Runs);
140    }
141
142    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
143      foreach (var p in parameters) {
144        values.Add(p);
145      }
146    }
147
148    #region Events
149    //public event EventHandler AlgorithmChanged;
150    //private void OnAlgorithmChanged() {
151    //  var handler = AlgorithmChanged;
152    //  if (handler != null) handler(this, EventArgs.Empty);
153    //}
154    public event EventHandler QualityChanged;
155    private void OnQualityChanged() {
156      var handler = QualityChanged;
157      if (handler != null) handler(this, EventArgs.Empty);
158    }
159
160    //private void RegisterAlgorithmEvents() {
161
162    //}
163    //private void DeregisterAlgorithmEvents() {
164
165    //}
166    private void RegisterQualityEvents() {
167      BestQuality.ValueChanged += new EventHandler(Quality_ValueChanged);
168    }
169    private void DeregisterQualityEvents() {
170      BestQuality.ValueChanged -= new EventHandler(Quality_ValueChanged);
171    }
172
173    private void Quality_ValueChanged(object sender, EventArgs e) {
174      OnQualityChanged();
175    }
176    #endregion
177
178    public override void Parameterize(IParameterizedItem item) {
179      base.Parameterize(item);
180      this.parameters.Clear();
181      ((IAlgorithm)item).CollectParameterValues(this.Parameters);
182    }
183
184    public Experiment GenerateExperiment(IAlgorithm algorithm, bool createBatchRuns, int repetitions) {
185      Experiment experiment = new Experiment();
186      foreach (IValueConfiguration combination in this) {
187        IAlgorithm clonedAlg = (IAlgorithm)algorithm.Clone();
188        clonedAlg.Name = combination.ParameterInfoString;
189        combination.Parameterize(clonedAlg);
190        clonedAlg.StoreAlgorithmInEachRun = false;
191        if (createBatchRuns) {
192          BatchRun batchRun = new BatchRun(string.Format("BatchRun: {0}", combination.ParameterInfoString));
193          batchRun.Algorithm = clonedAlg;
194          batchRun.Repetitions = repetitions;
195          experiment.Optimizers.Add(batchRun);
196        } else {
197          experiment.Optimizers.Add(clonedAlg);
198        }
199      }
200      return experiment;
201    }
202
203    public Experiment GenerateExperiment(IAlgorithm algorithm) {
204      return GenerateExperiment(algorithm, false, 0);
205    }
206
207    public IEnumerator GetEnumerator() {
208      IEnumerator enumerator = new ParameterCombinationsEnumerator(this);
209      enumerator.Reset();
210      return enumerator;
211    }
212
213    public long GetCombinationCount() {
214      long cnt = 0;
215      foreach (var c in this) {
216        cnt++;
217      }
218      return cnt;
219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.