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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
|
---|
29 | [StorableClass]
|
---|
30 | public class SymbolicExpressionGrammarValueConfiguration : ParameterizedValueConfiguration {
|
---|
31 | #region Constructors and Cloning
|
---|
32 | [StorableConstructor]
|
---|
33 | protected SymbolicExpressionGrammarValueConfiguration(bool deserializing) : base(deserializing) { }
|
---|
34 | protected SymbolicExpressionGrammarValueConfiguration(SymbolicExpressionGrammarValueConfiguration original, Cloner cloner) : base(original, cloner) { }
|
---|
35 | public SymbolicExpressionGrammarValueConfiguration() : base() { }
|
---|
36 | public SymbolicExpressionGrammarValueConfiguration(ISymbolicExpressionGrammar grammar)
|
---|
37 | : base() {
|
---|
38 | this.IsOptimizable = true;
|
---|
39 | this.ActualValue = new ConstrainedValue(grammar, grammar.GetType(), new ItemSet<IItem> { grammar }, false);
|
---|
40 | }
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new SymbolicExpressionGrammarValueConfiguration(this, cloner);
|
---|
43 | }
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | protected override void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
|
---|
47 | var grammar = (ISymbolicExpressionGrammar)item;
|
---|
48 | foreach (Symbol symbol in grammar.Symbols) {
|
---|
49 | var svc = new SymbolValueConfiguration(symbol);
|
---|
50 | var pc = new SingleValuedParameterConfiguration(symbol.Name, svc);
|
---|
51 | svc.ParentOptimizable = pc;
|
---|
52 | SetOptimizeIfOnlyOneValueConfiguration(pc);
|
---|
53 | this.parameterConfigurations.Add(pc);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public virtual void Parameterize(ISymbolicExpressionGrammar grammar) {
|
---|
58 | foreach (Symbol symbol in grammar.Symbols) {
|
---|
59 | var symbolValueConfiguration = (SymbolValueConfiguration)this.parameterConfigurations.Single(pc => pc.Name == symbol.Name).ValueConfigurations.First();
|
---|
60 | symbolValueConfiguration.Parameterize(symbol);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /// <summary>
|
---|
65 | /// If only 1 parameter exists (usually this is InitialFrequency), then set it to Optimize=true,
|
---|
66 | /// because this is what the user usually wants to do. avoids additional click.
|
---|
67 | /// </summary>
|
---|
68 | private static void SetOptimizeIfOnlyOneValueConfiguration(SingleValuedParameterConfiguration pc) {
|
---|
69 | var vc = pc.ValueConfigurations[0] as ParameterizedValueConfiguration;
|
---|
70 | if (vc != null) {
|
---|
71 | if (vc.ParameterConfigurations.Count == 1) {
|
---|
72 | vc.ParameterConfigurations.Single().Optimize = true;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|