1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Problems.GaussianProcessTuning {
|
---|
9 | [StorableClass]
|
---|
10 | [Item("Gaussian Process Configuration Grammar", "The grammar for the Gaussian process configration problem.")]
|
---|
11 | public sealed class Grammar : SymbolicExpressionGrammar {
|
---|
12 | [Storable]
|
---|
13 | private int dimension;
|
---|
14 | public int Dimension {
|
---|
15 | get { return dimension; }
|
---|
16 | set {
|
---|
17 | if (dimension != value) {
|
---|
18 | dimension = value;
|
---|
19 | UpdateSymbolDimension();
|
---|
20 | }
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | [StorableConstructor]
|
---|
25 | private Grammar(bool deserializing) : base(deserializing) { }
|
---|
26 | private Grammar(Grammar original, Cloner cloner)
|
---|
27 | : base(original, cloner) {
|
---|
28 | }
|
---|
29 |
|
---|
30 | public Grammar()
|
---|
31 | : base("Gaussian Process Configuration Grammar", "The grammar for the Gaussian process configuration problem.") {
|
---|
32 | Dimension = 10;
|
---|
33 | Initialize();
|
---|
34 | }
|
---|
35 |
|
---|
36 | [StorableHook(HookType.AfterDeserialization)]
|
---|
37 | private void AfterDeserialization() {
|
---|
38 | }
|
---|
39 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
40 | return new Grammar(this, cloner);
|
---|
41 | }
|
---|
42 | private void Initialize() {
|
---|
43 | // create all symbols
|
---|
44 | //var meanOne = new MeanOne();
|
---|
45 | var meanConst = new MeanConst();
|
---|
46 | var meanLinear = new MeanLinear(Dimension);
|
---|
47 | //var meanScale = new MeanScale();
|
---|
48 | var meanSum = new MeanSum();
|
---|
49 | var meanProd = new MeanProd();
|
---|
50 | //var meanPow2 = new MeanPow(2) { Name = "MeanPow2" };
|
---|
51 | //var meanPow3 = new MeanPow(3) { Name = "MeanPow3" };
|
---|
52 | //var meanMask = new MeanMask(Dimension);
|
---|
53 | var covConst = new CovConst();
|
---|
54 | var covLin = new CovLin();
|
---|
55 | var covLinArd = new CovLinArd(Dimension);
|
---|
56 | var covSeArd = new CovSeArd(Dimension);
|
---|
57 | var covSeIso = new CovSeIso();
|
---|
58 | var covRQard = new CovRQArd(Dimension);
|
---|
59 | var covRQiso = new CovRQIso();
|
---|
60 | var covMatern1 = new CovMatern(1);
|
---|
61 | covMatern1.Name = "CovMatern1";
|
---|
62 | var covMatern3 = new CovMatern(3);
|
---|
63 | covMatern3.Name = "CovMatern3";
|
---|
64 | var covMatern5 = new CovMatern(5);
|
---|
65 | covMatern5.Name = "CovMatern5";
|
---|
66 | var covPeriodic = new CovPeriodic();
|
---|
67 | var covNoise = new CovNoise();
|
---|
68 | var covScale = new CovScale();
|
---|
69 | var covSum = new CovSum();
|
---|
70 | var covProd = new CovProd();
|
---|
71 | var covMask = new CovMask(Dimension);
|
---|
72 | var likGauss = new LikGauss();
|
---|
73 |
|
---|
74 | var configurationStartSymbol = new ConfigurationStartSymbol();
|
---|
75 |
|
---|
76 | var meanSymbols = new List<ISymbol>()
|
---|
77 | {
|
---|
78 | meanConst,
|
---|
79 | meanLinear,
|
---|
80 | meanSum,
|
---|
81 | meanProd,
|
---|
82 | //meanPow2,
|
---|
83 | //meanPow3,
|
---|
84 | //meanMask,
|
---|
85 | };
|
---|
86 | var covSymbols = new List<ISymbol>()
|
---|
87 | {
|
---|
88 | covConst,
|
---|
89 | covLin,
|
---|
90 | covLinArd,
|
---|
91 | covSeArd,
|
---|
92 | covSeIso,
|
---|
93 | covRQiso,
|
---|
94 | covRQard,
|
---|
95 | covMatern1,
|
---|
96 | covMatern3,
|
---|
97 | covMatern5,
|
---|
98 | covPeriodic,
|
---|
99 | covNoise,
|
---|
100 | covScale,
|
---|
101 | covSum,
|
---|
102 | covProd,
|
---|
103 | covMask,
|
---|
104 | };
|
---|
105 | var likSymbols = new List<ISymbol>()
|
---|
106 | {
|
---|
107 | likGauss
|
---|
108 | };
|
---|
109 | var allSymbols = covSymbols.Concat(meanSymbols).Concat(likSymbols);
|
---|
110 |
|
---|
111 | AddSymbol(configurationStartSymbol);
|
---|
112 | // add all symbols to the grammar
|
---|
113 | foreach (var s in allSymbols)
|
---|
114 | AddSymbol(s);
|
---|
115 |
|
---|
116 | AddAllowedChildSymbol(StartSymbol, configurationStartSymbol, 0);
|
---|
117 |
|
---|
118 | foreach (var meanSymbol in meanSymbols) {
|
---|
119 | AddAllowedChildSymbol(configurationStartSymbol, meanSymbol, 0);
|
---|
120 | }
|
---|
121 | foreach (var covSymbol in covSymbols) {
|
---|
122 | AddAllowedChildSymbol(configurationStartSymbol, covSymbol, 1);
|
---|
123 | }
|
---|
124 | foreach (var likSymbol in likSymbols) {
|
---|
125 | AddAllowedChildSymbol(configurationStartSymbol, likSymbol, 2);
|
---|
126 | }
|
---|
127 | foreach (var meanFunctionSymbol in new ISymbol[] { meanSum, meanProd }) {
|
---|
128 | foreach (var meanSymbol in meanSymbols) {
|
---|
129 | for (int i = 0; i < meanFunctionSymbol.MaximumArity; i++) {
|
---|
130 | AddAllowedChildSymbol(meanFunctionSymbol, meanSymbol, i);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | foreach (var covFunctionSymbol in new ISymbol[] { covSum, covProd, covMask, covScale }) {
|
---|
136 | foreach (var covSymbol in covSymbols) {
|
---|
137 | for (int i = 0; i < covFunctionSymbol.MaximumArity; i++) {
|
---|
138 | AddAllowedChildSymbol(covFunctionSymbol, covSymbol, i);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void UpdateSymbolDimension() {
|
---|
145 | foreach (var s in Symbols.OfType<IDimensionSymbol>()) {
|
---|
146 | s.Dimension = dimension;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|