#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Symbols;
using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic {
[StorableClass]
[Item("SymbolicTimeSeriesPrognosisGrammar", "Represents a grammar for time series prognosis model using all available functions.")]
public class SymbolicTimeSeriesPrognosisGrammar : DefaultSymbolicExpressionGrammar {
[Storable]
private int dimension;
// for persistence
private SymbolicTimeSeriesPrognosisGrammar() : this(1) { }
[StorableConstructor]
protected SymbolicTimeSeriesPrognosisGrammar(bool deserializing) : base(deserializing) { }
protected SymbolicTimeSeriesPrognosisGrammar(SymbolicTimeSeriesPrognosisGrammar original, Cloner cloner)
: base(original, cloner) {
dimension = original.dimension;
}
public SymbolicTimeSeriesPrognosisGrammar(int dimension)
: base() {
this.dimension = dimension;
Initialize();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicTimeSeriesPrognosisGrammar(this, cloner);
}
private void Initialize() {
var add = new Addition();
var sub = new Subtraction();
var mul = new Multiplication();
var div = new Division();
var mean = new Average();
var sin = new Sine();
var cos = new Cosine();
var tan = new Tangent();
var log = new Logarithm();
var exp = new Exponential();
var @if = new IfThenElse();
var gt = new GreaterThan();
var lt = new LessThan();
var and = new And();
var or = new Or();
var not = new Not();
var constant = new Constant();
constant.MinValue = -20;
constant.MaxValue = 20;
var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
var laggedVariableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.LaggedVariable();
var integralVariableSymbol = new IntegratedVariable();
var derivedVariableSymbol = new DerivativeVariable();
var movingAverageSymbol = new MovingAverage();
var allSymbols = new List() { add, sub, mul, div, mean, sin, cos, tan, log, exp,
@if, gt, lt, and, or, not,
constant, variableSymbol, laggedVariableSymbol, derivedVariableSymbol, integralVariableSymbol, movingAverageSymbol};
var unaryFunctionSymbols = new List() { sin, cos, tan, log, exp, not };
var binaryFunctionSymbols = new List() { gt, lt };
var functionSymbols = new List() { add, sub, mul, div, mean, and, or };
var terminalSymbols = new List() { constant, variableSymbol, laggedVariableSymbol, derivedVariableSymbol, integralVariableSymbol, movingAverageSymbol };
foreach (var symb in allSymbols)
AddSymbol(symb);
foreach (var funSymb in functionSymbols) {
SetMinSubtreeCount(funSymb, 1);
SetMaxSubtreeCount(funSymb, 3);
}
foreach (var funSymb in unaryFunctionSymbols) {
SetMinSubtreeCount(funSymb, 1);
SetMaxSubtreeCount(funSymb, 1);
}
foreach (var funSymb in binaryFunctionSymbols) {
SetMinSubtreeCount(funSymb, 2);
SetMaxSubtreeCount(funSymb, 2);
}
SetMinSubtreeCount(@if, 3);
SetMaxSubtreeCount(@if, 3);
foreach (var terminalSymbol in terminalSymbols) {
SetMinSubtreeCount(terminalSymbol, 0);
SetMaxSubtreeCount(terminalSymbol, 0);
}
SetMinSubtreeCount(StartSymbol, dimension);
SetMaxSubtreeCount(StartSymbol, dimension);
// allow each symbol as child of the start symbol
foreach (var symb in allSymbols) {
for (int i = 0; i < GetMaxSubtreeCount(StartSymbol); i++)
SetAllowedChild(StartSymbol, symb, i);
}
// allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
foreach (var parent in allSymbols) {
for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
foreach (var child in allSymbols) {
SetAllowedChild(parent, child, i);
}
}
}
public void SetResultProducingBranches(int n) {
SetMinSubtreeCount(StartSymbol, n);
SetMaxSubtreeCount(StartSymbol, n);
foreach (Symbol s in Symbols) {
if (s != StartSymbol)
for (int i = 0; i < n; i++) {
SetAllowedChild(StartSymbol, s, i);
}
}
}
}
}