Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17282 was 16996, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.MetaOptimization for new persistence

File size: 2.5 KB
Line 
1using System.Linq;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
5using HEAL.Attic;
6
7namespace HeuristicLab.Problems.MetaOptimization {
8  [StorableType("73F1CF21-22C7-4E3A-952B-CB077AC37978")]
9  public class SymbolicExpressionGrammarValueConfiguration : ParameterizedValueConfiguration {
10
11    #region Constructors and Cloning
12    [StorableConstructor]
13    protected SymbolicExpressionGrammarValueConfiguration(StorableConstructorFlag _) : base(_) { }
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.