using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Problems.DataAnalysis.Symbolic; using HEAL.Attic; namespace HeuristicLab.Problems.MetaOptimization { [StorableType("C046C7BB-5C90-460D-BD3F-56E3EA28990C")] public class SymbolValueConfiguration : ParameterizedValueConfiguration { [Storable] private IOptimizable parentOptimizable; public IOptimizable ParentOptimizable { get { return parentOptimizable; } set { this.parentOptimizable = value; } } #region Constructors and Cloning [StorableConstructor] protected SymbolValueConfiguration(StorableConstructorFlag _) : base(_) { } public SymbolValueConfiguration(Symbol symbol) : base() { this.IsOptimizable = true; this.Optimize = false; this.Name = symbol.Name; this.ActualValue = new ConstrainedValue(symbol, symbol.GetType(), new ItemSet { symbol }, false); } protected SymbolValueConfiguration(SymbolValueConfiguration original, Cloner cloner) : base(original, cloner) { RegisterInitialFrequencyEvents(); this.parentOptimizable = cloner.Clone(original.parentOptimizable); } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolValueConfiguration(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterInitialFrequencyEvents(); } #endregion protected override void PopulateParameterConfigurations(IItem item, bool discoverValidValues) { this.ClearParameterConfigurations(); var symbol = (Symbol)item; var initialFrequencyValueConfigurations = new List(); initialFrequencyValueConfigurations.Add(new RangeValueConfiguration(new DoubleValue(0), typeof(DoubleValue))); initialFrequencyValueConfigurations.Add(new RangeValueConfiguration(new DoubleValue(1), typeof(DoubleValue))); var initialFrequencyParameterConfiguration = new ParameterConfiguration("InitialFrequency", typeof(Symbol), new DoubleValue(symbol.InitialFrequency), initialFrequencyValueConfigurations); this.parameterConfigurations.Add(initialFrequencyParameterConfiguration); RegisterInitialFrequencyEvents(); var constant = symbol as Constant; if (constant != null) { var minValueParameterConfiguration = new ParameterConfiguration("MinValue", typeof(DoubleValue), new DoubleValue(constant.MinValue)); var maxValueParameterConfiguration = new ParameterConfiguration("MaxValue", typeof(DoubleValue), new DoubleValue(constant.MaxValue)); var manipulatorMuParameterConfiguration = new ParameterConfiguration("ManipulatorMu", typeof(DoubleValue), new DoubleValue(constant.ManipulatorMu)); var manipulatorSigmaParameterConfiguration = new ParameterConfiguration("ManipulatorSigma", typeof(DoubleValue), new DoubleValue(constant.ManipulatorSigma)); var multiplicativeManipulatorSigmaParameterConfiguration = new ParameterConfiguration("MultiplicativeManipulatorSigma", typeof(DoubleValue), new DoubleValue(constant.MultiplicativeManipulatorSigma)); this.parameterConfigurations.Add(minValueParameterConfiguration); this.parameterConfigurations.Add(maxValueParameterConfiguration); this.parameterConfigurations.Add(manipulatorMuParameterConfiguration); this.parameterConfigurations.Add(manipulatorSigmaParameterConfiguration); this.parameterConfigurations.Add(multiplicativeManipulatorSigmaParameterConfiguration); } var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable; if (variable != null) { var weightMuParameterConfiguration = new ParameterConfiguration("WeightMu", typeof(DoubleValue), new DoubleValue(variable.WeightMu)); var weightSigmaParameterConfiguration = new ParameterConfiguration("WeightSigma", typeof(DoubleValue), new DoubleValue(variable.WeightSigma)); var weightManipulatorMuParameterConfiguration = new ParameterConfiguration("WeightManipulatorMu", typeof(DoubleValue), new DoubleValue(variable.WeightManipulatorMu)); var weightManipulatorSigmaParameterConfiguration = new ParameterConfiguration("WeightManipulatorSigma", typeof(DoubleValue), new DoubleValue(variable.WeightManipulatorSigma)); var multiplicativeWeightManipulatorSigmaParameterConfiguration = new ParameterConfiguration("MultiplicativeWeightManipulatorSigma", typeof(DoubleValue), new DoubleValue(variable.MultiplicativeWeightManipulatorSigma)); this.parameterConfigurations.Add(weightMuParameterConfiguration); this.parameterConfigurations.Add(weightSigmaParameterConfiguration); this.parameterConfigurations.Add(weightManipulatorMuParameterConfiguration); this.parameterConfigurations.Add(weightManipulatorSigmaParameterConfiguration); this.parameterConfigurations.Add(multiplicativeWeightManipulatorSigmaParameterConfiguration); } } public virtual void Parameterize(Symbol symbol) { var actualValueSymbol = this.ActualValue.Value as Symbol; symbol.InitialFrequency = parentOptimizable.Optimize ? GetDoubleValue("InitialFrequency") : actualValueSymbol.InitialFrequency; var constant = symbol as Constant; if (constant != null) { var actualValueConstant = this.ActualValue.Value as Constant; constant.MinValue = parentOptimizable.Optimize ? GetDoubleValue("MinValue") : actualValueConstant.MinValue; constant.MaxValue = parentOptimizable.Optimize ? GetDoubleValue("MaxValue") : actualValueConstant.MaxValue; constant.ManipulatorMu = parentOptimizable.Optimize ? GetDoubleValue("ManipulatorMu") : actualValueConstant.ManipulatorMu; constant.ManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("ManipulatorSigma") : actualValueConstant.ManipulatorSigma; constant.MultiplicativeManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("MultiplicativeManipulatorSigma") : actualValueConstant.MultiplicativeManipulatorSigma; } var variable = symbol as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable; if (variable != null) { var actualValueVariable = this.ActualValue.Value as HeuristicLab.Problems.DataAnalysis.Symbolic.Variable; variable.WeightMu = parentOptimizable.Optimize ? GetDoubleValue("WeightMu") : actualValueVariable.WeightMu; variable.WeightSigma = parentOptimizable.Optimize ? GetDoubleValue("WeightSigma") : actualValueVariable.WeightSigma; variable.WeightManipulatorMu = parentOptimizable.Optimize ? GetDoubleValue("WeightManipulatorMu") : actualValueVariable.WeightManipulatorMu; variable.WeightManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("WeightManipulatorSigma") : actualValueVariable.WeightManipulatorSigma; variable.MultiplicativeWeightManipulatorSigma = parentOptimizable.Optimize ? GetDoubleValue("MultiplicativeWeightManipulatorSigma") : actualValueVariable.MultiplicativeWeightManipulatorSigma; } } private double GetDoubleValue(string name) { return ((DoubleValue)ParameterConfigurations.Single(x => x.Name == name).ActualValue.Value).Value; } private void RegisterInitialFrequencyEvents() { this.parameterConfigurations.Single(x => x.Name == "InitialFrequency").ToStringChanged += new EventHandler(initialFrequencyParameterConfiguration_ToStringChanged); } private void DeregisterInitialFrequencyEvents() { this.parameterConfigurations.Single(x => x.Name == "InitialFrequency").ToStringChanged -= new EventHandler(initialFrequencyParameterConfiguration_ToStringChanged); } protected virtual void initialFrequencyParameterConfiguration_ToStringChanged(object sender, EventArgs e) { OnToStringChanged(); } public override string ToString() { return string.Format("{0}: {1}", this.Name, parameterConfigurations.Single(pc => pc.Name == "InitialFrequency").ActualValue.Value); } public override void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { base.Mutate(random, mutate, intValueManipulator, doubleValueManipulator); } public override void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) { base.Cross(random, other, cross, intValueCrossover, doubleValueCrossover); } } }