Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5927 was 5927, checked in by cneumuel, 12 years ago

#1215

  • worked on configurability of SymbolicExpressionGrammar
File size: 10.9 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Optimization;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.Problems.MetaOptimization {
13  // todo: storable, name, descr, ...
14  [StorableClass]
15  public class ParameterConfigurationTree : ParameterizedValueConfiguration, 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    public ParameterizedValueConfiguration AlgorithmConfiguration {
108      get {
109        return this.ParameterConfigurations.ElementAt(0).ValueConfigurations.First() as ParameterizedValueConfiguration;
110      }
111    }
112
113    public ParameterizedValueConfiguration ProblemConfiguration {
114      get {
115        return this.ParameterConfigurations.ElementAt(1).ValueConfigurations.First() as ParameterizedValueConfiguration;
116      }
117    }
118
119    //[Storable]
120    //protected string name;
121    //public string Name {
122    //  get { return name; }
123    //  set { name = value; }
124    //}
125
126    #region constructors and cloning
127    public ParameterConfigurationTree(IAlgorithm algorithm, IProblem problem)
128      : base(null, algorithm.GetType(), false) {
129      this.Optimize = false;
130      this.IsOptimizable = false;
131      this.parameters = new Dictionary<string, IItem>();
132      this.Name = algorithm.ItemName;
133
134      var algproblemitem = new AlgorithmProblemItem();
135      algproblemitem.AlgorithmParameter.Value = algorithm;
136      algproblemitem.ProblemParameter.Value = problem;
137      this.discoverValidValues = false;
138     
139      this.parameterConfigurations.Add(new SingleValuedParameterConfiguration("Algorithm", algproblemitem.AlgorithmParameter));
140      this.parameterConfigurations.Add(new SingleValuedParameterConfiguration("Problem", algproblemitem.ProblemParameter));
141    }
142    public ParameterConfigurationTree() { }
143    [StorableConstructor]
144    protected ParameterConfigurationTree(bool deserializing) : base(deserializing) { }
145    protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
146      : base(original, cloner) {
147      this.averageQualityNormalized = cloner.Clone(original.averageQualityNormalized);
148      this.qualitiesNormalized = cloner.Clone(original.qualitiesNormalized);
149      this.bestQualities = cloner.Clone(original.BestQualities);
150      this.averageQualities = cloner.Clone(original.averageQualities);
151      this.worstQualities = cloner.Clone(original.worstQualities);
152      this.qualityStandardDeviations = cloner.Clone(original.qualityStandardDeviations);
153      this.qualityVariances = cloner.Clone(original.qualityVariances);
154      this.averageExecutionTimes = cloner.Clone(original.averageExecutionTimes);
155      this.repetitions = cloner.Clone(original.repetitions);
156      this.runs = cloner.Clone(original.runs);
157      this.parameters = new Dictionary<string, IItem>();
158      foreach (var p in original.parameters) {
159        this.parameters.Add(p.Key, cloner.Clone(p.Value));
160      }
161      //this.name = original.name;
162    }
163    public override IDeepCloneable Clone(Cloner cloner) {
164      return new ParameterConfigurationTree(this, cloner);
165    }
166    [StorableHook(HookType.AfterDeserialization)]
167    private void AfterDeserialization() {
168    }
169    #endregion
170
171    public virtual void CollectResultValues(IDictionary<string, IItem> values) {
172      values.Add("RunsAverageExecutionTimes", AverageExecutionTimes);
173      values.Add("Repetitions", Repetitions);
174      values.Add("RunsBestQualities", BestQualities);
175      values.Add("RunsAverageQualities", AverageQualities);
176      values.Add("RunsWorstQualities", WorstQualities);
177      values.Add("RunsQualityVariances", QualityVariances);
178      values.Add("RunsQualityStandardDeviations", QualityStandardDeviations);
179      values.Add("QualitiesNormalized", QualitiesNormalized);
180      values.Add("AverageQualityNormalized", AverageQualityNormalized);
181      values.Add("Runs", Runs);
182    }
183
184    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
185      foreach (var p in parameters) {
186        values.Add(p);
187      }
188    }
189
190    #region Events
191    public event EventHandler QualityChanged;
192    private void OnQualityChanged() {
193      var handler = QualityChanged;
194      if (handler != null) handler(this, EventArgs.Empty);
195    }
196
197    private void Quality_ValueChanged(object sender, EventArgs e) {
198      OnQualityChanged();
199    }
200    #endregion
201
202    public override void Parameterize(IParameterizedItem item) {
203      this.parameters.Clear();
204      var algorithm = item as IAlgorithm;
205      var problem = algorithm.Problem;
206
207      AlgorithmConfiguration.Parameterize(algorithm);
208      ProblemConfiguration.Parameterize(problem);
209
210      algorithm.CollectParameterValues(this.Parameters);
211      //((IProblem)ProblemConfiguration.ActualValue.Value).CollectParameterValues(this.Parameters);
212      //CollectAlgorithmParameterValues((IAlgorithm)AlgorithmConfiguration.ActualValue.Value, parameters);
213    }
214
215    //protected virtual void CollectAlgorithmParameterValues(IAlgorithm algorithm, IDictionary<string, IItem> values) {
216    //  var algorithmValues = new Dictionary<string, IItem>();
217    //  algorithm.CollectParameterValues(algorithmValues);
218    //  foreach (var value in algorithmValues) {
219    //    values.Add("Algorithm." + value.Key, value.Value);
220    //  }
221    //}
222
223    public Experiment GenerateExperiment(IAlgorithm algorithm, bool createBatchRuns, int repetitions) {
224      Experiment experiment = new Experiment();
225      foreach (ParameterizedValueConfiguration combination in this) {
226        IAlgorithm clonedAlg = (IAlgorithm)algorithm.Clone();
227        clonedAlg.Name = combination.ParameterInfoString;
228        combination.Parameterize(clonedAlg);
229        clonedAlg.StoreAlgorithmInEachRun = false;
230        if (createBatchRuns) {
231          BatchRun batchRun = new BatchRun(string.Format("BatchRun: {0}", combination.ParameterInfoString));
232          batchRun.Optimizer = clonedAlg;
233          batchRun.Repetitions = repetitions;
234          experiment.Optimizers.Add(batchRun);
235        } else {
236          experiment.Optimizers.Add(clonedAlg);
237        }
238      }
239      return experiment;
240    }
241
242    public Experiment GenerateExperiment(IAlgorithm algorithm) {
243      return GenerateExperiment(algorithm, false, 0);
244    }
245
246    public IEnumerator GetEnumerator() {
247      IEnumerator enumerator = new ParameterCombinationsEnumerator(this);
248      enumerator.Reset();
249      return enumerator;
250    }
251
252    /// <summary>
253    /// returns the number of possible parameter combinations
254    /// </summary>
255    /// <param name="max">algorithm stops counting when max is reached. zero for infinite counting</param>
256    /// <returns></returns>
257    public long GetCombinationCount(long max) {
258      long cnt = 0;
259      foreach (var c in this) {
260        cnt++;
261        if (max > 0 && cnt >= max) {
262          return cnt;
263        }
264      }
265      return cnt;
266    }
267
268    public IOptimizable GetRandomOptimizable(IRandom random) {
269      List<IOptimizable> allOptimizables = GetAllOptimizables();
270      return allOptimizables[random.Next(allOptimizables.Count)];
271    }
272
273    public override string ToString() {
274      return this.Name;
275    }
276
277    public IRun ToRun() {
278      return ToRun(this.ParameterInfoString);
279    }
280
281    public IRun ToRun(string name) {
282      IRun run = new Run();
283      run.Name = name;
284      this.CollectResultValues(run.Results);
285      this.CollectParameterValues(run.Parameters);
286      MetaOptimizationUtil.ClearParameters(run, this.GetOptimizedParameterNames());
287      return run;
288    }
289
290    public override string ParameterInfoString {
291      get {
292        string algorithmInfo = this.AlgorithmConfiguration.ParameterInfoString;
293        string problemInfo = this.ProblemConfiguration.ParameterInfoString;
294        var sb = new StringBuilder();
295        if (!string.IsNullOrEmpty(algorithmInfo)) {
296          sb.Append("Algorithm: ");
297          sb.Append(algorithmInfo);
298        }
299        if (!string.IsNullOrEmpty(problemInfo)) {
300          if (sb.Length > 0)
301            sb.Append(", ");
302          sb.Append("Problem: ");
303          sb.Append(problemInfo);
304        }
305        return sb.ToString();
306      }
307    }
308
309    public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
310      AlgorithmConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty);
311      ProblemConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty);
312    }
313
314   
315  }
316}
Note: See TracBrowser for help on using the repository browser.