Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11552 was 9499, checked in by sluengo, 12 years ago
File size: 6.5 KB
RevLine 
[9139]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";
[9262]43    private const string Indicators = "Indicators";
[9139]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 macd = new MACD();
[9262]67        var rsi = new RSI();
[9139]68
69        var gt = new GreaterThan();
70        var lt = new LessThan();
71        var and = new And();
72        var or = new Or();
73        var not = new Not();
74
75        var constantint = new ConstantInt();
76        var boolean = new BoolConstant();
77
78        var max = new Max();
79        var min = new Min();
80        var lag = new Lag();
81        var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
82        #endregion
83
84        #region group symbol declaration
85        var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List<ISymbol>() { add, sub, mul});
86        var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> {constantint, variableSymbol });
87
88        var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt });
89        var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not });
90        var booleanConstantsSymbols = new GroupSymbol(BooleanConstant, new List<ISymbol> { boolean });
91        var meanTradeSymbols = new GroupSymbol(TradeMean, new List<ISymbol> { mean});
[9262]92        var indicatorTradeSymbols = new GroupSymbol(Indicators, new List<ISymbol> { macd,rsi});
[9139]93       
94        var serialTime = new GroupSymbol(SerialTime, new List<ISymbol> {max,min, lag });
95
96        #endregion
97
98        AddSymbol(comparisonSymbols);
99        AddSymbol(booleanOperationSymbols);
100        AddSymbol(arithmeticSymbols);
101        AddSymbol(terminalSymbols);
102        AddSymbol(booleanConstantsSymbols);
103        AddSymbol(meanTradeSymbols);
104        AddSymbol(indicatorTradeSymbols);
105        AddSymbol(serialTime);
106       
107
108        #region subtree count configuration
109        SetSubtreeCount(arithmeticSymbols, 2, 2);
110        SetSubtreeCount(terminalSymbols, 0, 0);
111        SetSubtreeCount(booleanConstantsSymbols, 0, 0);
112        SetSubtreeCount(meanTradeSymbols, 1, 1);
113        SetSubtreeCount(macd, 3, 3);
[9499]114        SetSubtreeCount(rsi, 3, 3);
[9139]115        SetSubtreeCount(comparisonSymbols, 2, 2);
116        SetSubtreeCount(and, 2, 2);
117        SetSubtreeCount(or, 2, 2);
118        SetSubtreeCount(not, 1, 1);
119        SetSubtreeCount(serialTime, 1, 1);
120      #endregion
121
122      #region allowed child symbols configuration
[9262]123        AddAllowedChildSymbol(StartSymbol, booleanOperationSymbols);
124        AddAllowedChildSymbol(StartSymbol, comparisonSymbols);
[9139]125
126        AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
127        AddAllowedChildSymbol(booleanOperationSymbols, booleanConstantsSymbols);
[9262]128        AddAllowedChildSymbol(booleanOperationSymbols, indicatorTradeSymbols);
129        AddAllowedChildSymbol(booleanOperationSymbols, booleanOperationSymbols);
[9139]130
131        AddAllowedChildSymbol(comparisonSymbols, meanTradeSymbols);
132        AddAllowedChildSymbol(comparisonSymbols, arithmeticSymbols);
133        AddAllowedChildSymbol(comparisonSymbols, terminalSymbols);
134        AddAllowedChildSymbol(comparisonSymbols, serialTime);
135
136        AddAllowedChildSymbol(arithmeticSymbols, arithmeticSymbols);
137        AddAllowedChildSymbol(arithmeticSymbols, meanTradeSymbols);
138        AddAllowedChildSymbol(arithmeticSymbols, terminalSymbols);
139        AddAllowedChildSymbol(arithmeticSymbols, serialTime);
140
[9262]141        AddAllowedChildSymbol(indicatorTradeSymbols, constantint);
[9139]142        AddAllowedChildSymbol(serialTime, constantint);
[9262]143        AddAllowedChildSymbol(meanTradeSymbols, constantint);   
[9139]144      #endregion
145    }
146
147   public void ConfigureAsDefaultRegressionGrammar() {
148
149      // Symbols.First(s => s is Constant).Enabled = false;
150   }
151
152   public void ConfigureAsDefaultClassificationGrammar()
153   {
154     //  Symbols.First(s => s is Constant).Enabled = false;
155   }
156  }
157}
Note: See TracBrowser for help on using the repository browser.