Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/SymbolicExpressionGrammer/SymbolicExpressionGrammarValueConfiguration.cs @ 7840

Last change on this file since 7840 was 7840, checked in by spimming, 12 years ago

#1853:

  • included files from metaopt branch
  • ignored mutation and crossover
  • updated plugin frame
File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.ParameterConfigurationTreeEncoding {
29  [StorableClass]
30  public class SymbolicExpressionGrammarValueConfiguration : ParameterizedValueConfiguration {
31
32    #region Constructors and Cloning
33    [StorableConstructor]
34    protected SymbolicExpressionGrammarValueConfiguration(bool deserializing) : base(deserializing) { }
35    public SymbolicExpressionGrammarValueConfiguration() : base() { }
36    public SymbolicExpressionGrammarValueConfiguration(ISymbolicExpressionGrammar grammar) {
37      this.IsOptimizable = true;
38      this.ActualValue = new ConstrainedValue(grammar, grammar.GetType(), new ItemSet<IItem> { grammar }, false);
39    }
40    protected SymbolicExpressionGrammarValueConfiguration(SymbolicExpressionGrammarValueConfiguration original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new SymbolicExpressionGrammarValueConfiguration(this, cloner);
45    }
46    #endregion
47
48    protected override void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
49      var grammar = (ISymbolicExpressionGrammar)item;
50      foreach (Symbol symbol in grammar.Symbols) {
51        var svc = new SymbolValueConfiguration(symbol);
52        var pc = new SingleValuedParameterConfiguration(symbol.Name, svc);
53        svc.ParentOptimizable = pc;
54        SetOptimizeIfOnlyOneValueConfiguration(pc);
55        this.parameterConfigurations.Add(pc);
56      }
57    }
58
59    public virtual void Parameterize(ISymbolicExpressionGrammar grammar) {
60      foreach (Symbol symbol in grammar.Symbols) {
61        var symbolValueConfiguration = (SymbolValueConfiguration)this.parameterConfigurations.Single(pc => pc.Name == symbol.Name).ValueConfigurations.First();
62        symbolValueConfiguration.Parameterize(symbol);
63      }
64    }
65
66    /// <summary>
67    /// If only 1 parameter exists (usually this is InitialFrequency), then set it to Optimize=true,
68    /// because this is what the user usually wants to do. avoids additional click.
69    /// </summary>
70    private static void SetOptimizeIfOnlyOneValueConfiguration(SingleValuedParameterConfiguration pc) {
71      var vc = pc.ValueConfigurations[0] as ParameterizedValueConfiguration;
72      if (vc != null) {
73        if (vc.ParameterConfigurations.Count == 1) {
74          vc.ParameterConfigurations.Single().Optimize = true;
75        }
76      }
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.