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.Problems.DataAnalysis.Symbolic;
|
---|
9 | using HEAL.Attic;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
12 | [StorableType("C046C7BB-5C90-460D-BD3F-56E3EA28990C")]
|
---|
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(StorableConstructorFlag _) : base(_) { }
|
---|
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 | }
|
---|