Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/SymbolicExpressionGrammar/SymbolValueConfiguration.cs @ 18242

Last change on this file since 18242 was 8535, checked in by jkarder, 12 years ago

#1853:

  • enhanced combinations count calculation
  • restructured code
  • minor code improvements
  • added license information
File size: 9.0 KB
Line 
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.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Symbolic;
31
32namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
33  [StorableClass]
34  public class SymbolValueConfiguration : ParameterizedValueConfiguration {
35    [Storable]
36    private IOptimizable parentOptimizable;
37    public IOptimizable ParentOptimizable {
38      get { return parentOptimizable; }
39      set { this.parentOptimizable = value; }
40    }
41
42    #region Constructors and Cloning
43    [StorableConstructor]
44    protected SymbolValueConfiguration(bool deserializing) : base(deserializing) { }
45    protected SymbolValueConfiguration(SymbolValueConfiguration original, Cloner cloner)
46      : base(original, cloner) {
47      RegisterInitialFrequencyEvents();
48      this.parentOptimizable = cloner.Clone(original.parentOptimizable);
49    }
50    public SymbolValueConfiguration(Symbol symbol)
51      : base() {
52      this.IsOptimizable = true;
53      this.Optimize = false;
54      this.Name = symbol.Name;
55      this.ActualValue = new ConstrainedValue(symbol, symbol.GetType(), new ItemSet<IItem> { symbol }, false);
56    }
57    [StorableHook(HookType.AfterDeserialization)]
58    private void AfterDeserialization() {
59      RegisterInitialFrequencyEvents();
60    }
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new SymbolValueConfiguration(this, cloner);
63    }
64    #endregion
65
66    protected override void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
67      this.ClearParameterConfigurations();
68      var symbol = (Symbol)item;
69
70      var initialFrequencyValueConfigurations = new List<IValueConfiguration>();
71      initialFrequencyValueConfigurations.Add(new RangeValueConfiguration(new DoubleValue(0), typeof(DoubleValue)));
72      initialFrequencyValueConfigurations.Add(new RangeValueConfiguration(new DoubleValue(1), typeof(DoubleValue)));
73      var initialFrequencyParameterConfiguration = new ParameterConfiguration("InitialFrequency", typeof(Symbol), new DoubleValue(symbol.InitialFrequency), initialFrequencyValueConfigurations);
74      this.parameterConfigurations.Add(initialFrequencyParameterConfiguration);
75      RegisterInitialFrequencyEvents();
76
77      var constant = symbol as Constant;
78      if (constant != null) {
79        var minValueParameterConfiguration = new ParameterConfiguration("MinValue", typeof(DoubleValue), new DoubleValue(constant.MinValue));
80        var maxValueParameterConfiguration = new ParameterConfiguration("MaxValue", typeof(DoubleValue), new DoubleValue(constant.MaxValue));
81        var manipulatorMuParameterConfiguration = new ParameterConfiguration("ManipulatorMu", typeof(DoubleValue), new DoubleValue(constant.ManipulatorMu));
82        var manipulatorSigmaParameterConfiguration = new ParameterConfiguration("ManipulatorSigma", typeof(DoubleValue), new DoubleValue(constant.ManipulatorSigma));
83        var multiplicativeManipulatorSigmaParameterConfiguration = new ParameterConfiguration("MultiplicativeManipulatorSigma", typeof(DoubleValue), new DoubleValue(constant.MultiplicativeManipulatorSigma));
84
85        this.parameterConfigurations.Add(minValueParameterConfiguration);
86        this.parameterConfigurations.Add(maxValueParameterConfiguration);
87        this.parameterConfigurations.Add(manipulatorMuParameterConfiguration);
88        this.parameterConfigurations.Add(manipulatorSigmaParameterConfiguration);
89        this.parameterConfigurations.Add(multiplicativeManipulatorSigmaParameterConfiguration);
90      }
91
92      var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
93      if (variable != null) {
94        var weightMuParameterConfiguration = new ParameterConfiguration("WeightMu", typeof(DoubleValue), new DoubleValue(variable.WeightMu));
95        var weightSigmaParameterConfiguration = new ParameterConfiguration("WeightSigma", typeof(DoubleValue), new DoubleValue(variable.WeightSigma));
96        var weightManipulatorMuParameterConfiguration = new ParameterConfiguration("WeightManipulatorMu", typeof(DoubleValue), new DoubleValue(variable.WeightManipulatorMu));
97        var weightManipulatorSigmaParameterConfiguration = new ParameterConfiguration("WeightManipulatorSigma", typeof(DoubleValue), new DoubleValue(variable.WeightManipulatorSigma));
98        var multiplicativeWeightManipulatorSigmaParameterConfiguration = new ParameterConfiguration("MultiplicativeWeightManipulatorSigma", typeof(DoubleValue), new DoubleValue(variable.MultiplicativeWeightManipulatorSigma));
99
100        this.parameterConfigurations.Add(weightMuParameterConfiguration);
101        this.parameterConfigurations.Add(weightSigmaParameterConfiguration);
102        this.parameterConfigurations.Add(weightManipulatorMuParameterConfiguration);
103        this.parameterConfigurations.Add(weightManipulatorSigmaParameterConfiguration);
104        this.parameterConfigurations.Add(multiplicativeWeightManipulatorSigmaParameterConfiguration);
105      }
106    }
107
108    public virtual void Parameterize(Symbol symbol) {
109      var actualValueSymbol = this.ActualValue.Value as Symbol;
110      symbol.InitialFrequency = parentOptimizable.Optimize ? GetDoubleValue("InitialFrequency") : actualValueSymbol.InitialFrequency;
111
112      var constant = symbol as Constant;
113      if (constant != null) {
114        var actualValueConstant = this.ActualValue.Value as Constant;
115        constant.MinValue = parentOptimizable.Optimize ? GetDoubleValue("MinValue") : actualValueConstant.MinValue;
116        constant.MaxValue = parentOptimizable.Optimize ? GetDoubleValue("MaxValue") : actualValueConstant.MaxValue;
117        constant.ManipulatorMu = parentOptimizable.Optimize ? GetDoubleValue("ManipulatorMu") : actualValueConstant.ManipulatorMu;
118        constant.ManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("ManipulatorSigma") : actualValueConstant.ManipulatorSigma;
119        constant.MultiplicativeManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("MultiplicativeManipulatorSigma") : actualValueConstant.MultiplicativeManipulatorSigma;
120      }
121
122      var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
123      if (variable != null) {
124        var actualValueVariable = this.ActualValue.Value as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
125        variable.WeightMu = parentOptimizable.Optimize ? GetDoubleValue("WeightMu") : actualValueVariable.WeightMu;
126        variable.WeightSigma = parentOptimizable.Optimize ? GetDoubleValue("WeightSigma") : actualValueVariable.WeightSigma;
127        variable.WeightManipulatorMu = parentOptimizable.Optimize ? GetDoubleValue("WeightManipulatorMu") : actualValueVariable.WeightManipulatorMu;
128        variable.WeightManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("WeightManipulatorSigma") : actualValueVariable.WeightManipulatorSigma;
129        variable.MultiplicativeWeightManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("MultiplicativeWeightManipulatorSigma") : actualValueVariable.MultiplicativeWeightManipulatorSigma;
130      }
131    }
132
133    private double GetDoubleValue(string name) {
134      return ((DoubleValue)ParameterConfigurations.Single(x => x.Name == name).ActualValue.Value).Value;
135    }
136
137    private void RegisterInitialFrequencyEvents() {
138      this.parameterConfigurations.Single(x => x.Name == "InitialFrequency").ToStringChanged += new EventHandler(initialFrequencyParameterConfiguration_ToStringChanged);
139    }
140    private void DeregisterInitialFrequencyEvents() {
141      this.parameterConfigurations.Single(x => x.Name == "InitialFrequency").ToStringChanged -= new EventHandler(initialFrequencyParameterConfiguration_ToStringChanged);
142    }
143
144    protected virtual void initialFrequencyParameterConfiguration_ToStringChanged(object sender, EventArgs e) {
145      OnToStringChanged();
146    }
147
148    public override string ToString() {
149      return string.Format("{0}: {1}", this.Name, parameterConfigurations.Single(pc => pc.Name == "InitialFrequency").ActualValue.Value);
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.