#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis.Symbolic;
namespace HeuristicLab.Encodings.ParameterConfigurationTreeEncoding {
[StorableClass]
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(bool deserializing) : base(deserializing) { }
public SymbolValueConfiguration() : 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);
}
}
}