source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/SymbolicExpressionGrammar/SymbolValueConfiguration.cs @ 6023

Last change on this file since 6023 was 6023, checked in by cneumuel, 9 years ago

#1215

  • added missing files
File size: 6.6 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.Problems.DataAnalysis.Symbolic;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  [StorableClass]
12  public class SymbolValueConfiguration : ParameterizedValueConfiguration {
13
14    #region Constructors and Cloning
15    [StorableConstructor]
16    protected SymbolValueConfiguration(bool deserializing) : base(deserializing) { }
17    public SymbolValueConfiguration() : base() { }
18    public SymbolValueConfiguration(Symbol symbol)
19      : base() {
20      this.IsOptimizable = true;
21      this.Optimize = false;
22      this.Name = symbol.Name;
23      this.actualValue = new ConstrainedValue(symbol, symbol.GetType(), new ItemSet<IItem> { symbol }, false);
24    }
25    protected SymbolValueConfiguration(SymbolValueConfiguration original, Cloner cloner)
26      : base(original, cloner) {
27    }
28    public override IDeepCloneable Clone(Cloner cloner) {
29      return new SymbolValueConfiguration(this, cloner);
30    }
31    #endregion
32
33    protected override void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
34      this.ClearParameterConfigurations();
35
36      var symbol = (Symbol)item;
37
38      var initialFrequencyValueConfigurations = new List<IValueConfiguration>();
39      initialFrequencyValueConfigurations.Add(new RangeValueConfiguration(new DoubleValue(0), typeof(DoubleValue)));
40      initialFrequencyValueConfigurations.Add(new RangeValueConfiguration(new DoubleValue(1), typeof(DoubleValue)));
41      var initialFrequencyParameterConfiguration = new ParameterConfiguration("InitialFrequency", typeof(Symbol), new DoubleValue(symbol.InitialFrequency), initialFrequencyValueConfigurations);
42      this.parameterConfigurations.Add(initialFrequencyParameterConfiguration);
43
44      var constant = symbol as Constant;
45      if (constant != null) {
46        var minValueParameterConfiguration = new ParameterConfiguration("MinValue", typeof(DoubleValue), new DoubleValue(constant.MinValue));
47        var maxValueParameterConfiguration = new ParameterConfiguration("MaxValue", typeof(DoubleValue), new DoubleValue(constant.MaxValue));
48        var manipulatorMuParameterConfiguration = new ParameterConfiguration("ManipulatorMu", typeof(DoubleValue), new DoubleValue(constant.ManipulatorMu));
49        var manipulatorSigmaParameterConfiguration = new ParameterConfiguration("ManipulatorSigma", typeof(DoubleValue), new DoubleValue(constant.ManipulatorSigma));
50        var multiplicativeManipulatorSigmaParameterConfiguration = new ParameterConfiguration("MultiplicativeManipulatorSigma", typeof(DoubleValue), new DoubleValue(constant.MultiplicativeManipulatorSigma));
51
52        this.parameterConfigurations.Add(minValueParameterConfiguration);
53        this.parameterConfigurations.Add(maxValueParameterConfiguration);
54        this.parameterConfigurations.Add(manipulatorMuParameterConfiguration);
55        this.parameterConfigurations.Add(manipulatorSigmaParameterConfiguration);
56        this.parameterConfigurations.Add(multiplicativeManipulatorSigmaParameterConfiguration);
57      }
58
59      var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
60      if (variable != null) {
61        var weightMuParameterConfiguration = new ParameterConfiguration("WeightMu", typeof(DoubleValue), new DoubleValue(variable.WeightMu));
62        var weightSigmaParameterConfiguration = new ParameterConfiguration("WeightSigma", typeof(DoubleValue), new DoubleValue(variable.WeightSigma));
63        var weightManipulatorMuParameterConfiguration = new ParameterConfiguration("WeightManipulatorMu", typeof(DoubleValue), new DoubleValue(variable.WeightManipulatorMu));
64        var weightManipulatorSigmaParameterConfiguration = new ParameterConfiguration("WeightManipulatorSigma", typeof(DoubleValue), new DoubleValue(variable.WeightManipulatorSigma));
65        var multiplicativeWeightManipulatorSigmaParameterConfiguration = new ParameterConfiguration("MultiplicativeWeightManipulatorSigma", typeof(DoubleValue), new DoubleValue(variable.MultiplicativeWeightManipulatorSigma));
66
67        this.parameterConfigurations.Add(weightMuParameterConfiguration);
68        this.parameterConfigurations.Add(weightSigmaParameterConfiguration);
69        this.parameterConfigurations.Add(weightManipulatorMuParameterConfiguration);
70        this.parameterConfigurations.Add(weightManipulatorSigmaParameterConfiguration);
71        this.parameterConfigurations.Add(multiplicativeWeightManipulatorSigmaParameterConfiguration);
72      }
73    }
74
75    public virtual void Parameterize(Symbol symbol) {
76      symbol.InitialFrequency = GetDoubleValue("InitialFrequency");
77     
78      var constant = symbol as Constant;
79      if (constant != null) {
80        constant.MinValue = GetDoubleValue("MinValue");
81        constant.MaxValue = GetDoubleValue("MaxValue");
82        constant.ManipulatorMu = GetDoubleValue("ManipulatorMu");
83        constant.ManipulatorSigma = GetDoubleValue("ManipulatorSigma");
84        constant.MultiplicativeManipulatorSigma = GetDoubleValue("MultiplicativeManipulatorSigma");
85      }
86     
87      var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
88      if (variable != null) {
89        variable.WeightMu = GetDoubleValue("WeightMu");
90        variable.WeightSigma = GetDoubleValue("WeightSigma");
91        variable.WeightManipulatorMu = GetDoubleValue("WeightManipulatorMu");
92        variable.WeightManipulatorSigma = GetDoubleValue("WeightManipulatorSigma");
93        variable.MultiplicativeWeightManipulatorSigma = GetDoubleValue("MultiplicativeWeightManipulatorSigma");
94      }
95    }
96
97    private double GetDoubleValue(string name) {
98      return ((DoubleValue)ParameterConfigurations.Single(x => x.Name == name).ActualValue.Value).Value;
99    }
100
101    public override string ToString() {
102      return string.Format("{0}: {1}", this.Name, parameterConfigurations.Single(pc => pc.Name == "InitialFrequency").ActualValue.Value);
103    }
104
105    public override void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
106      base.Mutate(random, mutate, intValueManipulator, doubleValueManipulator);
107    }
108
109    public override void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
110      base.Cross(random, other, cross, intValueCrossover, doubleValueCrossover);
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.