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