using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { [StorableClass] public class SymbolicExpressionGrammarValueConfiguration : ParameterizedValueConfiguration { #region Constructors and Cloning [StorableConstructor] protected SymbolicExpressionGrammarValueConfiguration(bool deserializing) : base(deserializing) { } public SymbolicExpressionGrammarValueConfiguration() : base() { } public SymbolicExpressionGrammarValueConfiguration(ISymbolicExpressionGrammar grammar) { this.IsOptimizable = true; this.ActualValue = new ConstrainedValue(grammar, grammar.GetType(), new ItemSet { grammar }, false); } protected SymbolicExpressionGrammarValueConfiguration(SymbolicExpressionGrammarValueConfiguration original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionGrammarValueConfiguration(this, cloner); } #endregion protected override void PopulateParameterConfigurations(IItem item, bool discoverValidValues) { var grammar = (ISymbolicExpressionGrammar)item; foreach (Symbol symbol in grammar.Symbols) { var svc = new SymbolValueConfiguration(symbol); var pc = new SingleValuedParameterConfiguration(symbol.Name, svc); svc.ParentOptimizable = pc; SetOptimizeIfOnlyOneValueConfiguration(pc); this.parameterConfigurations.Add(pc); } } public virtual void Parameterize(ISymbolicExpressionGrammar grammar) { foreach (Symbol symbol in grammar.Symbols) { var symbolValueConfiguration = (SymbolValueConfiguration)this.parameterConfigurations.Single(pc => pc.Name == symbol.Name).ValueConfigurations.First(); symbolValueConfiguration.Parameterize(symbol); } } /// /// If only 1 parameter exists (usually this is InitialFrequency), then set it to Optimize=true, /// because this is what the user usually wants to do. avoids additional click. /// private static void SetOptimizeIfOnlyOneValueConfiguration(SingleValuedParameterConfiguration pc) { var vc = pc.ValueConfigurations[0] as ParameterizedValueConfiguration; if (vc != null) { if (vc.ParameterConfigurations.Count == 1) { vc.ParameterConfigurations.Single().Optimize = true; } } } } }