Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GeneticProgramming.BloodGlucosePrediction/Grammar.cs @ 14251

Last change on this file since 14251 was 13870, checked in by gkronber, 8 years ago

#2608: simplified grammar by removing unnecessary symbols: exprch, exprins, exprgluc and added constants

File size: 8.9 KB
RevLine 
[13865]1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[13870]5using HeuristicLab.Problems.DataAnalysis.Symbolic;
[13865]6
7namespace HeuristicLab.Problems.GeneticProgramming.GlucosePrediction {
8  [StorableClass]
9  [Item("Blood glucose level forecast grammar", "See MedGEC Workshop at GECCO 2016")]
10  public class Grammar : SymbolicExpressionGrammar {
11    [StorableConstructor]
12    protected Grammar(bool deserializing) : base(deserializing) { }
13    protected Grammar(Grammar original, Cloner cloner) : base(original, cloner) { }
14
15    public Grammar()
16      : base("Blood glucose level forecast grammar", "See MedGEC Workshop at GECCO 2016") {
17      Initialize();
18    }
19
20    public void Initialize() {
21      var func = new SimpleSymbol("Func", "The root for the blood glucose forecasting model.", 3, 3);
22      var exprGluc = new SimpleSymbol("ExprGluc", string.Empty, 1, 1);
23      var exprIns = new SimpleSymbol("ExprIns", string.Empty, 1, 1);
24      var exprCh = new SimpleSymbol("ExprCh", string.Empty, 1, 1);
25
26      var predGlucData = new PredictedGlucoseVariableSymbol();
27      var realGlucData = new RealGlucoseVariableSymbol();
28
29      // operators for exprGluc
30      var plusGluc = new SimpleSymbol("+", "+", 2, 2);
31      var minusGluc = new SimpleSymbol("-", "-", 2, 2);
32      var prodGluc = new SimpleSymbol("*", "*", 2, 2);
33      var divGluc = new SimpleSymbol("/", "/", 2, 2);
34      var expGluc = new SimpleSymbol("Exp", "Exp", 1, 1);
35      var sinGluc = new SimpleSymbol("Sin", "Sin", 1, 1);
36      var cosGluc = new SimpleSymbol("Cos", "Cos", 1, 1);
37      var logGluc = new SimpleSymbol("Log", "Log", 1, 1);
38      // var cteGluc = new CteSymbol();
39
40      // operators for exprCh
41      var plusCh = new SimpleSymbol("+Ch", "+", 2, 2);
42      var minusCh = new SimpleSymbol("-Ch", "-", 2, 2);
43      var prodCh = new SimpleSymbol("*Ch", "*", 2, 2);
44      var divCh = new SimpleSymbol("/Ch", "/", 2, 2);
45      var expCh = new SimpleSymbol("ExpCh", "Exp", 1, 1);
46      var sinCh = new SimpleSymbol("SinCh", "Sin", 1, 1);
47      var cosCh = new SimpleSymbol("CosCh", "Cos", 1, 1);
48      var logCh = new SimpleSymbol("LogCh", "Log", 1, 1);
[13867]49      var curvedCh = new CurvedChVariableSymbol("CurvedCh", "");
[13865]50      // var cteCh = new CteSymbol();
51
52      // operators for exprIns
53      var plusIns = new SimpleSymbol("+Ins", "+", 2, 2);
54      var minusIns = new SimpleSymbol("-Ins", "-", 2, 2);
55      var prodIns = new SimpleSymbol("*Ins", "*", 2, 2);
56      var divIns = new SimpleSymbol("/Ins", "/", 2, 2);
57      var expIns = new SimpleSymbol("ExpIns", "Exp", 1, 1);
58      var sinIns = new SimpleSymbol("SinIns", "Sin", 1, 1);
59      var cosIns = new SimpleSymbol("CosIns", "Cos", 1, 1);
60      var logIns = new SimpleSymbol("LogIns", "Log", 1, 1);
[13867]61      var curvedIns = new CurvedInsVariableSymbol("CurvedIns", "");
[13865]62      var realInsVar = new RealInsulineVariableSymbol();
[13870]63
64      var constSy = new Constant();
[13865]65      // var cteCh = new CteSymbol();
66
67      AddSymbol(func);
68      AddSymbol(exprGluc);
69      AddSymbol(exprIns);
70      AddSymbol(exprCh);
71      AddSymbol(predGlucData);
72      AddSymbol(realGlucData);
73      AddSymbol(plusGluc);
74      AddSymbol(minusGluc);
75      AddSymbol(prodGluc);
76      AddSymbol(divGluc);
77      AddSymbol(expGluc);
78      AddSymbol(sinGluc);
79      AddSymbol(cosGluc);
80      AddSymbol(logGluc);
81
[13867]82      AddSymbol(curvedCh);
83
[13865]84      AddSymbol(plusCh);
85      AddSymbol(minusCh);
86      AddSymbol(prodCh);
87      AddSymbol(divCh);
88      AddSymbol(expCh);
89      AddSymbol(sinCh);
90      AddSymbol(cosCh);
91      AddSymbol(logCh);
92
[13867]93      AddSymbol(curvedIns);
[13865]94      AddSymbol(plusIns);
95      AddSymbol(minusIns);
96      AddSymbol(prodIns);
97      AddSymbol(divIns);
98      AddSymbol(expIns);
99      AddSymbol(sinIns);
100      AddSymbol(cosIns);
101      AddSymbol(logIns);
102      AddSymbol(realInsVar);
[13870]103      AddSymbol(constSy);
[13865]104
105      // <func> ::= <exprgluc> + <exprch> - <exprins>
106      AddAllowedChildSymbol(func, exprGluc, 0);
107      AddAllowedChildSymbol(func, exprCh, 1);
108      AddAllowedChildSymbol(func, exprIns, 2);
109
110      /* # Glucose
111      <exprgluc> ::= (<exprgluc> <op> <exprgluc>)
112                    |<preop> (<exprgluc>)
113                    |(<cte> <op> <exprgluc>)
114                    |predictedData(k-<idxCurr2h>)
115                    |realData(k-<idx2hOrMore>)
116      */
[13870]117      var opGlucSet = new Symbol[] { plusGluc, minusGluc, prodGluc, divGluc, expGluc, sinGluc, cosGluc, logGluc, predGlucData, realGlucData, constSy };
[13865]118      foreach (var opGluc in opGlucSet) {
119        AddAllowedChildSymbol(exprGluc, opGluc);
120      }
121
[13870]122
123      foreach (var parentOp in new Symbol[] { plusGluc, minusGluc, prodGluc, divGluc }) {
124        foreach (var childOp in opGlucSet) {
125          AddAllowedChildSymbol(parentOp, childOp, 0);
126          AddAllowedChildSymbol(parentOp, childOp, 1);
127        }
[13865]128      }
129      // <exprgluc> ::= <preop> (<exprgluc>)
[13870]130      foreach (var parentOp in new Symbol[] { expGluc, sinGluc, cosGluc, logGluc }) {
131        foreach (var childOp in opGlucSet) {
132          AddAllowedChildSymbol(parentOp, childOp, 0);
133        }
[13865]134      }
135
136      /*
137      # CH
138      <exprch> ::= (<exprch> <op> <exprch>)
139                    |<preop> (<exprch>)
140                    |(<cte> <op> <exprch>) 
141                    |(getPrevData(1,k,1) * <cte> * <curvedCH>)
142
143      # CH in 2 (0.041,24), 3 (0.027,36) and 4 (0.02,48) hours with 3 shapes of beta:
144      <curvedCH> ::= beta(0.041*Math.min(24,getPrevDataDistance(1,k,1)),2,5)
145              |beta(0.041*Math.min(24,getPrevDataDistance(1,k,1)),3,3)
146              |beta(0.041*Math.min(24,getPrevDataDistance(1,k,1)),5,2)
147              |beta(0.027*Math.min(36,getPrevDataDistance(1,k,1)),2,5)
148              |beta(0.027*Math.min(36,getPrevDataDistance(1,k,1)),3,3)
149              |beta(0.027*Math.min(36,getPrevDataDistance(1,k,1)),5,2)
150              |beta(0.02*Math.min(48,getPrevDataDistance(1,k,1)),2,5)
151              |beta(0.02*Math.min(48,getPrevDataDistance(1,k,1)),3,3)
152              |beta(0.02*Math.min(48,getPrevDataDistance(1,k,1)),5,2)
153      */
[13870]154      var opChSet = new Symbol[] { plusCh, minusCh, prodCh, divCh, expCh, sinCh, cosCh, logCh, curvedCh, constSy };
[13865]155      foreach (var opCh in opChSet) {
156        AddAllowedChildSymbol(exprCh, opCh);
157      }
158
[13870]159      foreach (var parentOp in new Symbol[] { plusCh, minusCh, prodCh, divCh }) {
160        foreach (var childOp in opChSet) {
161          AddAllowedChildSymbol(parentOp, childOp, 0);
162          AddAllowedChildSymbol(parentOp, childOp, 1);
163        }
[13865]164      }
165      // <exprch> ::= <preop> (<exprch>)
[13870]166      foreach (var parentOp in new Symbol[] { expCh, sinCh, cosCh, logCh }) {
167        foreach (var childOp in opChSet) {
168          AddAllowedChildSymbol(parentOp, childOp, 0);
169        }
[13865]170      }
171
172      /*
173       # Insulin:
174      ## Sum of insulins in past 2h minus the peak
175      ## Curve for the peak in past 2h
176      <exprins> ::= (<exprins> <op> <exprins>)
177        |<preop> (<exprins>)
178        |(<cte> <op> <exprins>)   
179        |getVariable(2,k-<idx>)
180        |(getSumOfValues(24,k,2) - getMaxValue(24,k,2) <op> (getMaxValue(24,k,2) <op> <curvedINS>))
181
182      # INS in 2 (0.041,24), 3 (0.027,36) and 4 (0.02,48) hours with 3 shapes of beta:
183      <curvedINS> ::= beta(0.041*Math.min(24,getMaxValueDistance(24,k,2)),2,5)
184              |beta(0.041*Math.min(24,getMaxValueDistance(24,k,2)),3,3)
185              |beta(0.041*Math.min(24,getMaxValueDistance(24,k,2)),5,2)
186              |beta(0.027*Math.min(36,getMaxValueDistance(36,k,2)),2,5)
187              |beta(0.027*Math.min(36,getMaxValueDistance(36,k,2)),3,3)
188              |beta(0.027*Math.min(36,getMaxValueDistance(36,k,2)),5,2)
189              |beta(0.02*Math.min(48,getMaxValueDistance(48,k,2)),2,5)
190              |beta(0.02*Math.min(48,getMaxValueDistance(48,k,2)),3,3)
191              |beta(0.02*Math.min(48,getMaxValueDistance(48,k,2)),5,2)
192
193      */
[13870]194      var opInsSet = new Symbol[] { plusIns, minusIns, prodIns, divIns, expIns, sinIns, cosIns, logIns, realInsVar, curvedIns, constSy };
[13865]195      foreach (var opIns in opInsSet) {
196        AddAllowedChildSymbol(exprIns, opIns);
197      }
198
199      // <exprins> ::= (<exprins> <op> <exprins>)
200      // <exprins> ::= (<cte> <op> <exprins>)   
[13870]201      foreach (var parentOp in new Symbol[] { plusIns, minusIns, prodIns, divIns }) {
202        foreach (var childOp in opInsSet) {
203          AddAllowedChildSymbol(parentOp, childOp, 0);
204          AddAllowedChildSymbol(parentOp, childOp, 1);
205        }
[13865]206      }
207      // <exprins> ::= <preop> (<exprins>)
208      foreach (var op in new Symbol[] { expIns, sinIns, cosIns, logIns }) {
[13870]209        foreach (var childOp in opInsSet) {
210          AddAllowedChildSymbol(op, childOp, 0);
211        }
[13865]212      }
213
214      // root is func
215      AddAllowedChildSymbol(StartSymbol, func);
216    }
217
218    public override IDeepCloneable Clone(Cloner cloner) {
219      return new Grammar(this, cloner);
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.