1 | using System.Linq;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | namespace 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 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 | }
|
---|