Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/SymbolicExpressionGrammar/SymbolValueConfiguration.cs @ 17282

Last change on this file since 17282 was 16996, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.MetaOptimization for new persistence

File size: 8.6 KB
RevLine 
[6197]1using System;
2using System.Collections.Generic;
[6023]3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8using HeuristicLab.Problems.DataAnalysis.Symbolic;
[16574]9using HEAL.Attic;
[6023]10
11namespace HeuristicLab.Problems.MetaOptimization {
[16574]12  [StorableType("C046C7BB-5C90-460D-BD3F-56E3EA28990C")]
[6023]13  public class SymbolValueConfiguration : ParameterizedValueConfiguration {
[6197]14    [Storable]
15    private IOptimizable parentOptimizable;
16    public IOptimizable ParentOptimizable {
17      get { return parentOptimizable; }
18      set { this.parentOptimizable = value; }
19    }
[6023]20
21    #region Constructors and Cloning
22    [StorableConstructor]
[16574]23    protected SymbolValueConfiguration(StorableConstructorFlag _) : base(_) { }
[6023]24    public SymbolValueConfiguration(Symbol symbol)
25      : base() {
26      this.IsOptimizable = true;
27      this.Optimize = false;
28      this.Name = symbol.Name;
[6197]29      this.ActualValue = new ConstrainedValue(symbol, symbol.GetType(), new ItemSet<IItem> { symbol }, false);
[6023]30    }
31    protected SymbolValueConfiguration(SymbolValueConfiguration original, Cloner cloner)
32      : base(original, cloner) {
[6197]33      RegisterInitialFrequencyEvents();
34      this.parentOptimizable = cloner.Clone(original.parentOptimizable);
[6023]35    }
36    public override IDeepCloneable Clone(Cloner cloner) {
37      return new SymbolValueConfiguration(this, cloner);
38    }
[6197]39    [StorableHook(HookType.AfterDeserialization)]
40    private void AfterDeserialization() {
41      RegisterInitialFrequencyEvents();
42    }
[6023]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);
[6197]54      RegisterInitialFrequencyEvents();
[6023]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) {
[6197]88      var actualValueSymbol = this.ActualValue.Value as Symbol;
89      symbol.InitialFrequency = parentOptimizable.Optimize ? GetDoubleValue("InitialFrequency") : actualValueSymbol.InitialFrequency;
90
[6023]91      var constant = symbol as Constant;
92      if (constant != null) {
[6197]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;
[6023]99      }
[6197]100
[6023]101      var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable;
102      if (variable != null) {
[6197]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;
[6023]109      }
110    }
111
112    private double GetDoubleValue(string name) {
113      return ((DoubleValue)ParameterConfigurations.Single(x => x.Name == name).ActualValue.Value).Value;
114    }
115
[6197]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
[6023]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.