using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HEAL.Attic; namespace HeuristicLab.Problems.MetaOptimization { [StorableType("73F1CF21-22C7-4E3A-952B-CB077AC37978")] public class SymbolicExpressionGrammarValueConfiguration : ParameterizedValueConfiguration { #region Constructors and Cloning [StorableConstructor] protected SymbolicExpressionGrammarValueConfiguration(StorableConstructorFlag _) : base(_) { } public SymbolicExpressionGrammarValueConfiguration(ISymbolicExpressionGrammar grammar) { this.IsOptimizable = true; this.ActualValue = new ConstrainedValue(grammar, grammar.GetType(), new ItemSet { grammar }, false); this.Name = grammar.ItemName; } 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; } } } } }