#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.TradeRules;
using HeuristicLab.Problems.DataAnalysis.Symbolic;
using System;
using HeuristicLab.Core;
namespace HeuristicLab.Problems.TradeRules {
[StorableClass]
[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.")]
public class Grammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar
{
private const string ArithmeticFunctionsName = "Arithmetic Functions";
private const string TerminalsName = "Terminals";
private const string ComparisonsName = "Comparisons";
private const string BooleanOperatorsName = "Boolean Operators";
private const string ConditionalSymbolsName = "ConditionalSymbols";
private const string BooleanConstant = "Boolean Constant";
private const string TradeMean = "Average Trade";
private const string MACDIndicator = "MACD Indicator";
private const string SerialTime = "Serial Operators";
[StorableConstructor]
protected Grammar(bool deserializing) : base(deserializing) { }
protected Grammar(Grammar original, Cloner cloner) : base(original, cloner) { }
public Grammar()
: base(ItemAttribute.GetName(typeof(Grammar)), ItemAttribute.GetDescription(typeof(Grammar))) {
Initialize();
}
public override IDeepCloneable Clone(Cloner cloner)
{
return new Grammar(this, cloner);
}
private void Initialize()
{
#region symbol declaration
var add = new Addition();
var sub = new Subtraction();
var mul = new Multiplication();
var mean = new AverageTrade();
var expMovAverage = new EMA();
var macd = new MACD();
var RSI = new RSI();
var gt = new GreaterThan();
var lt = new LessThan();
var and = new And();
var or = new Or();
var not = new Not();
var constantint = new ConstantInt();
var boolean = new BoolConstant();
var max = new Max();
var min = new Min();
var lag = new Lag();
var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
#endregion
#region group symbol declaration
var arithmeticSymbols = new GroupSymbol(ArithmeticFunctionsName, new List() { add, sub, mul});
var terminalSymbols = new GroupSymbol(TerminalsName, new List {constantint, variableSymbol });
var comparisonSymbols = new GroupSymbol(ComparisonsName, new List { gt, lt });
var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List { and, or, not });
var booleanConstantsSymbols = new GroupSymbol(BooleanConstant, new List { boolean });
var meanTradeSymbols = new GroupSymbol(TradeMean, new List { mean});
var indicatorTradeSymbols = new GroupSymbol(MACDIndicator, new List { macd,RSI, expMovAverage });
var serialTime = new GroupSymbol(SerialTime, new List {max,min, lag });
#endregion
AddSymbol(comparisonSymbols);
AddSymbol(booleanOperationSymbols);
AddSymbol(arithmeticSymbols);
AddSymbol(terminalSymbols);
AddSymbol(booleanConstantsSymbols);
AddSymbol(meanTradeSymbols);
AddSymbol(indicatorTradeSymbols);
AddSymbol(serialTime);
#region subtree count configuration
SetSubtreeCount(arithmeticSymbols, 2, 2);
SetSubtreeCount(terminalSymbols, 0, 0);
SetSubtreeCount(booleanConstantsSymbols, 0, 0);
SetSubtreeCount(meanTradeSymbols, 1, 1);
SetSubtreeCount(macd, 3, 3);
SetSubtreeCount(RSI, 1, 1);
SetSubtreeCount(expMovAverage, 1, 1);
SetSubtreeCount(comparisonSymbols, 2, 2);
SetSubtreeCount(and, 2, 2);
SetSubtreeCount(or, 2, 2);
SetSubtreeCount(not, 1, 1);
SetSubtreeCount(serialTime, 1, 1);
#endregion
#region allowed child symbols configuration
//AddAllowedChildSymbol(StartSymbol, booleanOperationSymbols);
//AddAllowedChildSymbol(StartSymbol, comparisonSymbols);
AddAllowedChildSymbol(StartSymbol, expMovAverage);
AddAllowedChildSymbol(booleanOperationSymbols, comparisonSymbols);
AddAllowedChildSymbol(booleanOperationSymbols, booleanConstantsSymbols);
//AddAllowedChildSymbol(booleanOperationSymbols, indicatorTradeSymbols);
AddAllowedChildSymbol(comparisonSymbols, meanTradeSymbols);
AddAllowedChildSymbol(comparisonSymbols, arithmeticSymbols);
AddAllowedChildSymbol(comparisonSymbols, terminalSymbols);
AddAllowedChildSymbol(comparisonSymbols, serialTime);
AddAllowedChildSymbol(arithmeticSymbols, arithmeticSymbols);
AddAllowedChildSymbol(arithmeticSymbols, meanTradeSymbols);
AddAllowedChildSymbol(arithmeticSymbols, terminalSymbols);
AddAllowedChildSymbol(arithmeticSymbols, serialTime);
AddAllowedChildSymbol(serialTime, constantint);
AddAllowedChildSymbol(meanTradeSymbols, constantint);
AddAllowedChildSymbol(indicatorTradeSymbols, constantint);
#endregion
}
public void ConfigureAsDefaultRegressionGrammar() {
// Symbols.First(s => s is Constant).Enabled = false;
}
public void ConfigureAsDefaultClassificationGrammar()
{
// Symbols.First(s => s is Constant).Enabled = false;
}
}
}