Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/SymbolicExpressionGrammar/SymbolicExpressionGrammarValueConfiguration.cs @ 15740

Last change on this file since 15740 was 9827, checked in by jkarder, 11 years ago

#1099:

  • adapted MetaOpt NamedItems once more
    • gave more meaningful names to some items
    • removed unreferenced default ctors
    • changed access modifiers
File size: 2.6 KB
Line 
1using System.Linq;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6
7namespace HeuristicLab.Problems.MetaOptimization {
8  [StorableClass]
9  public class SymbolicExpressionGrammarValueConfiguration : ParameterizedValueConfiguration {
10
11    #region Constructors and Cloning
12    [StorableConstructor]
13    protected SymbolicExpressionGrammarValueConfiguration(bool deserializing) : base(deserializing) { }
14    public SymbolicExpressionGrammarValueConfiguration(ISymbolicExpressionGrammar grammar) {
15      this.IsOptimizable = true;
16      this.ActualValue = new ConstrainedValue(grammar, grammar.GetType(), new ItemSet<IItem> { grammar }, false);
17      this.Name = grammar.ItemName;
18    }
19    protected SymbolicExpressionGrammarValueConfiguration(SymbolicExpressionGrammarValueConfiguration original, Cloner cloner)
20      : base(original, cloner) {
21    }
22    public override IDeepCloneable Clone(Cloner cloner) {
23      return new SymbolicExpressionGrammarValueConfiguration(this, cloner);
24    }
25    #endregion
26
27    protected override void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
28      var grammar = (ISymbolicExpressionGrammar)item;
29      foreach (Symbol symbol in grammar.Symbols) {
30        var svc = new SymbolValueConfiguration(symbol);
31        var pc = new SingleValuedParameterConfiguration(symbol.Name, svc);
32        svc.ParentOptimizable = pc;
33        SetOptimizeIfOnlyOneValueConfiguration(pc);
34        this.parameterConfigurations.Add(pc);
35      }
36    }
37
38    public virtual void Parameterize(ISymbolicExpressionGrammar grammar) {
39      foreach (Symbol symbol in grammar.Symbols) {
40        var symbolValueConfiguration = (SymbolValueConfiguration)this.parameterConfigurations.Single(pc => pc.Name == symbol.Name).ValueConfigurations.First();
41        symbolValueConfiguration.Parameterize(symbol);
42      }
43    }
44
45    /// <summary>
46    /// If only 1 parameter exists (usually this is InitialFrequency), then set it to Optimize=true,
47    /// because this is what the user usually wants to do. avoids additional click.
48    /// </summary>
49    private static void SetOptimizeIfOnlyOneValueConfiguration(SingleValuedParameterConfiguration pc) {
50      var vc = pc.ValueConfigurations[0] as ParameterizedValueConfiguration;
51      if (vc != null) {
52        if (vc.ParameterConfigurations.Count == 1) {
53          vc.ParameterConfigurations.Single().Optimize = true;
54        }
55      }
56    }
57  }
58}
Note: See TracBrowser for help on using the repository browser.