#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.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.ParameterConfigurationEncoding { [StorableClass] public class SymbolicExpressionGrammarValueConfiguration : ParameterizedValueConfiguration { #region Constructors and Cloning [StorableConstructor] protected SymbolicExpressionGrammarValueConfiguration(bool deserializing) : base(deserializing) { } protected SymbolicExpressionGrammarValueConfiguration(SymbolicExpressionGrammarValueConfiguration original, Cloner cloner) : base(original, cloner) { } public SymbolicExpressionGrammarValueConfiguration() : base() { } public SymbolicExpressionGrammarValueConfiguration(ISymbolicExpressionGrammar grammar) : base() { this.IsOptimizable = true; this.ActualValue = new ConstrainedValue(grammar, grammar.GetType(), new ItemSet { grammar }, false); } 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; } } } } }