Free cookie consent management tool by TermsFeed Policy Generator

source: branches/sluengo/HeuristicLab.Problems.TradeRules/Grammar.cs @ 9147

Last change on this file since 9147 was 9139, checked in by sluengo, 12 years ago
File size: 6.6 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.TradeRules;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using System;
30using HeuristicLab.Core;
31namespace HeuristicLab.Problems.TradeRules {
32  [StorableClass]
33  [Item("Grammar", "Represents a grammar for functional expressions in which special syntactic constraints are enforced so that boolean and real-valued expressions are not mixed.")]
34    public class Grammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar
35    {
36    private const string ArithmeticFunctionsName = "Arithmetic Functions";
37    private const string TerminalsName = "Terminals";
38    private const string ComparisonsName = "Comparisons";
39    private const string BooleanOperatorsName = "Boolean Operators";
40    private const string ConditionalSymbolsName = "ConditionalSymbols";
41    private const string BooleanConstant = "Boolean Constant";
42    private const string TradeMean = "Average Trade";
43    private const string MACDIndicator = "MACD Indicator";
44    private const string SerialTime = "Serial Operators";
45     
46    [StorableConstructor]
47    protected Grammar(bool deserializing) : base(deserializing) { }
48    protected Grammar(Grammar original, Cloner cloner) : base(original, cloner) { }
49    public Grammar()
50      : base(ItemAttribute.GetName(typeof(Grammar)), ItemAttribute.GetDescription(typeof(Grammar))) {
51      Initialize();
52    }
53    public override IDeepCloneable Clone(Cloner cloner)
54    {
55        return new Grammar(this, cloner);
56    }
57
58    private void Initialize()
59    {
60        #region symbol declaration
61        var add = new Addition();
62        var sub = new Subtraction();
63        var mul = new Multiplication();
64
65        var mean = new AverageTrade();
66        var expMovAverage = new EMA();
67        var macd = new MACD();
68        var RSI = new RSI();
69
70        var gt = new GreaterThan();
71        var lt = new LessThan();
72        var and = new And();
73        var or = new Or();
74        var not = new Not();
75
76        var constantint = new ConstantInt();
77        var boolean = new BoolConstant();
78
79        var max = new Max();
80        var min = new Min();
81        var lag = new Lag();
82        var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
83        #endregion
84
85        #region group symbol declaration
86        var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul});
87        var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> {constantint, variableSymbol });
88
89        var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
90        var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not });
91        var booleanConstantsSymbols = new GroupSymbol(BooleanConstant, new List<ISymbol> { boolean });
92        var meanTradeSymbols = new GroupSymbol(TradeMean, new List<ISymbol> { mean});
93        var indicatorTradeSymbols = new GroupSymbol(MACDIndicator, new List<ISymbol> { macd,RSI, expMovAverage });
94       
95        var serialTime = new GroupSymbol(SerialTime, new List<ISymbol> {max,min, lag });
96
97        #endregion
98
99        AddSymbol(comparisonSymbols);
100        AddSymbol(booleanOperationSymbols);
101        AddSymbol(arithmeticSymbols);
102        AddSymbol(terminalSymbols);
103        AddSymbol(booleanConstantsSymbols);
104        AddSymbol(meanTradeSymbols);
105        AddSymbol(indicatorTradeSymbols);
106        AddSymbol(serialTime);
107       
108
109        #region subtree count configuration
110        SetSubtreeCount(arithmeticSymbols, 2, 2);
111        SetSubtreeCount(terminalSymbols, 0, 0);
112
113        SetSubtreeCount(booleanConstantsSymbols, 0, 0);
114        SetSubtreeCount(meanTradeSymbols, 1, 1);
115        SetSubtreeCount(macd, 3, 3);
116        SetSubtreeCount(RSI, 1, 1);
117        SetSubtreeCount(expMovAverage, 1, 1);
118        SetSubtreeCount(comparisonSymbols, 2, 2);
119        SetSubtreeCount(and, 2, 2);
120        SetSubtreeCount(or, 2, 2);
121        SetSubtreeCount(not, 1, 1);
122        SetSubtreeCount(serialTime, 1, 1);
123      #endregion
124
125      #region allowed child symbols configuration
126        //AddAllowedChildSymbol(StartSymbol, booleanOperationSymbols);
127        //AddAllowedChildSymbol(StartSymbol, comparisonSymbols);
128        AddAllowedChildSymbol(StartSymbol, expMovAverage);
129
130        AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
131        AddAllowedChildSymbol(booleanOperationSymbols, booleanConstantsSymbols);
132        //AddAllowedChildSymbol(booleanOperationSymbols, indicatorTradeSymbols);
133
134        AddAllowedChildSymbol(comparisonSymbols, meanTradeSymbols);
135        AddAllowedChildSymbol(comparisonSymbols, arithmeticSymbols);
136        AddAllowedChildSymbol(comparisonSymbols, terminalSymbols);
137        AddAllowedChildSymbol(comparisonSymbols, serialTime);
138
139        AddAllowedChildSymbol(arithmeticSymbols, arithmeticSymbols);
140        AddAllowedChildSymbol(arithmeticSymbols, meanTradeSymbols);
141        AddAllowedChildSymbol(arithmeticSymbols, terminalSymbols);
142        AddAllowedChildSymbol(arithmeticSymbols, serialTime);
143
144        AddAllowedChildSymbol(serialTime, constantint);
145
146        AddAllowedChildSymbol(meanTradeSymbols, constantint);
147        AddAllowedChildSymbol(indicatorTradeSymbols, constantint);
148      #endregion
149    }
150
151   public void ConfigureAsDefaultRegressionGrammar() {
152
153      // Symbols.First(s => s is Constant).Enabled = false;
154   }
155
156   public void ConfigureAsDefaultClassificationGrammar()
157   {
158     //  Symbols.First(s => s is Constant).Enabled = false;
159   }
160  }
161}
Note: See TracBrowser for help on using the repository browser.