Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9827 was 8087, checked in by mkommend, 12 years ago

#1877: Corrected plugin dependencies and ToString methods in MetaOpt.

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