Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/SymbolicExpressionGrammer/SymbolValueConfiguration.cs @ 12394

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

#1853:

  • included files from metaopt branch
  • ignored mutation and crossover
  • updated plugin frame
File size: 9.0 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.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.ParameterConfigurationTreeEncoding {
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    public SymbolValueConfiguration() : base() { }
46    public SymbolValueConfiguration(Symbol symbol)
47      : base() {
48      this.IsOptimizable = true;
49      this.Optimize = false;
50      this.Name = symbol.Name;
51      this.ActualValue = new ConstrainedValue(symbol, symbol.GetType(), new ItemSet<IItem> { symbol }, false);
52    }
53    protected SymbolValueConfiguration(SymbolValueConfiguration original, Cloner cloner)
54      : base(original, cloner) {
55      RegisterInitialFrequencyEvents();
56      this.parentOptimizable = cloner.Clone(original.parentOptimizable);
57    }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new SymbolValueConfiguration(this, cloner);
60    }
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      RegisterInitialFrequencyEvents();
64    }
65    #endregion
66
67    protected override void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
68      this.ClearParameterConfigurations();
69      var symbol = (Symbol)item;
70
71      var initialFrequencyValueConfigurations = new List<IValueConfiguration>();
72      initialFrequencyValueConfigurations.Add(new RangeValueConfiguration(new DoubleValue(0), typeof(DoubleValue)));
73      initialFrequencyValueConfigurations.Add(new RangeValueConfiguration(new DoubleValue(1), typeof(DoubleValue)));
74      var initialFrequencyParameterConfiguration = new ParameterConfiguration("InitialFrequency", typeof(Symbol), new DoubleValue(symbol.InitialFrequency), initialFrequencyValueConfigurations);
75      this.parameterConfigurations.Add(initialFrequencyParameterConfiguration);
76      RegisterInitialFrequencyEvents();
77
78      var constant = symbol as Constant;
79      if (constant != null) {
80        var minValueParameterConfiguration = new ParameterConfiguration("MinValue", typeof(DoubleValue), new DoubleValue(constant.MinValue));
81        var maxValueParameterConfiguration = new ParameterConfiguration("MaxValue", typeof(DoubleValue), new DoubleValue(constant.MaxValue));
82        var manipulatorMuParameterConfiguration = new ParameterConfiguration("ManipulatorMu", typeof(DoubleValue), new DoubleValue(constant.ManipulatorMu));
83        var manipulatorSigmaParameterConfiguration = new ParameterConfiguration("ManipulatorSigma", typeof(DoubleValue), new DoubleValue(constant.ManipulatorSigma));
84        var multiplicativeManipulatorSigmaParameterConfiguration = new ParameterConfiguration("MultiplicativeManipulatorSigma", typeof(DoubleValue), new DoubleValue(constant.MultiplicativeManipulatorSigma));
85
86        this.parameterConfigurations.Add(minValueParameterConfiguration);
87        this.parameterConfigurations.Add(maxValueParameterConfiguration);
88        this.parameterConfigurations.Add(manipulatorMuParameterConfiguration);
89        this.parameterConfigurations.Add(manipulatorSigmaParameterConfiguration);
90        this.parameterConfigurations.Add(multiplicativeManipulatorSigmaParameterConfiguration);
91      }
92
93      var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
94      if (variable != null) {
95        var weightMuParameterConfiguration = new ParameterConfiguration("WeightMu", typeof(DoubleValue), new DoubleValue(variable.WeightMu));
96        var weightSigmaParameterConfiguration = new ParameterConfiguration("WeightSigma", typeof(DoubleValue), new DoubleValue(variable.WeightSigma));
97        var weightManipulatorMuParameterConfiguration = new ParameterConfiguration("WeightManipulatorMu", typeof(DoubleValue), new DoubleValue(variable.WeightManipulatorMu));
98        var weightManipulatorSigmaParameterConfiguration = new ParameterConfiguration("WeightManipulatorSigma", typeof(DoubleValue), new DoubleValue(variable.WeightManipulatorSigma));
99        var multiplicativeWeightManipulatorSigmaParameterConfiguration = new ParameterConfiguration("MultiplicativeWeightManipulatorSigma", typeof(DoubleValue), new DoubleValue(variable.MultiplicativeWeightManipulatorSigma));
100
101        this.parameterConfigurations.Add(weightMuParameterConfiguration);
102        this.parameterConfigurations.Add(weightSigmaParameterConfiguration);
103        this.parameterConfigurations.Add(weightManipulatorMuParameterConfiguration);
104        this.parameterConfigurations.Add(weightManipulatorSigmaParameterConfiguration);
105        this.parameterConfigurations.Add(multiplicativeWeightManipulatorSigmaParameterConfiguration);
106      }
107    }
108
109    public virtual void Parameterize(Symbol symbol) {
110      var actualValueSymbol = this.ActualValue.Value as Symbol;
111      symbol.InitialFrequency = parentOptimizable.Optimize ? GetDoubleValue("InitialFrequency") : actualValueSymbol.InitialFrequency;
112
113      var constant = symbol as Constant;
114      if (constant != null) {
115        var actualValueConstant = this.ActualValue.Value as Constant;
116        constant.MinValue = parentOptimizable.Optimize ? GetDoubleValue("MinValue") : actualValueConstant.MinValue;
117        constant.MaxValue = parentOptimizable.Optimize ? GetDoubleValue("MaxValue") : actualValueConstant.MaxValue;
118        constant.ManipulatorMu = parentOptimizable.Optimize ? GetDoubleValue("ManipulatorMu") : actualValueConstant.ManipulatorMu;
119        constant.ManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("ManipulatorSigma") : actualValueConstant.ManipulatorSigma;
120        constant.MultiplicativeManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("MultiplicativeManipulatorSigma") : actualValueConstant.MultiplicativeManipulatorSigma;
121      }
122
123      var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
124      if (variable != null) {
125        var actualValueVariable = this.ActualValue.Value as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
126        variable.WeightMu = parentOptimizable.Optimize ? GetDoubleValue("WeightMu") : actualValueVariable.WeightMu;
127        variable.WeightSigma = parentOptimizable.Optimize ? GetDoubleValue("WeightSigma") : actualValueVariable.WeightSigma;
128        variable.WeightManipulatorMu = parentOptimizable.Optimize ? GetDoubleValue("WeightManipulatorMu") : actualValueVariable.WeightManipulatorMu;
129        variable.WeightManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("WeightManipulatorSigma") : actualValueVariable.WeightManipulatorSigma;
130        variable.MultiplicativeWeightManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("MultiplicativeWeightManipulatorSigma") : actualValueVariable.MultiplicativeWeightManipulatorSigma;
131      }
132    }
133
134    private double GetDoubleValue(string name) {
135      return ((DoubleValue)ParameterConfigurations.Single(x => x.Name == name).ActualValue.Value).Value;
136    }
137
138    private void RegisterInitialFrequencyEvents() {
139      this.parameterConfigurations.Single(x => x.Name == "InitialFrequency").ToStringChanged += new EventHandler(initialFrequencyParameterConfiguration_ToStringChanged);
140    }
141    private void DeregisterInitialFrequencyEvents() {
142      this.parameterConfigurations.Single(x => x.Name == "InitialFrequency").ToStringChanged -= new EventHandler(initialFrequencyParameterConfiguration_ToStringChanged);
143    }
144
145    protected virtual void initialFrequencyParameterConfiguration_ToStringChanged(object sender, EventArgs e) {
146      OnToStringChanged();
147    }
148
149    public override string ToString() {
150      return string.Format("{0}: {1}", this.Name, parameterConfigurations.Single(pc => pc.Name == "InitialFrequency").ActualValue.Value);
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.