source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/SymbolicExpressionGrammar/SymbolicExpressionGrammarValueConfiguration.cs @ 6023

Last change on this file since 6023 was 6023, checked in by cneumuel, 9 years ago

#1215

  • added missing files
File size: 2.5 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() : base() { }
15    public SymbolicExpressionGrammarValueConfiguration(ISymbolicExpressionGrammar grammar) {
16      this.IsOptimizable = true;
17      this.ActualValue = new ConstrainedValue(grammar, grammar.GetType(), new ItemSet<IItem> { grammar }, false);
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 pc = new SingleValuedParameterConfiguration(symbol.Name, new SymbolValueConfiguration(symbol));
31        SetOptimizeIfOnlyOneValueConfiguration(pc);
32        this.parameterConfigurations.Add(pc);
33      }
34    }
35
36    public virtual void Parameterize(ISymbolicExpressionGrammar grammar) {
37      foreach (Symbol symbol in grammar.Symbols) {
38        ((SymbolValueConfiguration)this.parameterConfigurations.Single(pc => pc.Name == symbol.Name).ValueConfigurations.First()).Parameterize(symbol);
39      }
40    }
41
42    /// <summary>
43    /// If only 1 parameter exists (usually this is InitialFrequency), then set it to Optimize=true,
44    /// because this is what the user usually wants to do. avoids additional click.
45    /// </summary>
46    private static void SetOptimizeIfOnlyOneValueConfiguration(SingleValuedParameterConfiguration pc) {
47      var vc = pc.ValueConfigurations[0] as ParameterizedValueConfiguration;
48      if (vc != null) {
49        if (vc.ParameterConfigurations.Count == 1) {
50          vc.ParameterConfigurations.Single().Optimize = true;
51        }
52      }
53    }
54  }
55}
Note: See TracBrowser for help on using the repository browser.