using System.Collections.Generic; using System.Linq; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; namespace HeuristicLab.Problems.DataAnalysis.Symbolic { [StorableType("5A2300A0-D0FC-4F2D-B910-F86384FE9052")] [Item("IntervalArithmeticGrammar", "Represents a grammar for interval arithmetic")] public class IntervalArithmeticGrammar : DataAnalysisGrammar, ISymbolicDataAnalysisGrammar { public IntervalArithmeticGrammar() : base(ItemAttribute.GetName(typeof(IntervalArithmeticGrammar)), ItemAttribute.GetDescription(typeof(IntervalArithmeticGrammar))) { Initialize(); } [StorableConstructor] public IntervalArithmeticGrammar(StorableConstructorFlag _) : base(_) { } protected IntervalArithmeticGrammar(IntervalArithmeticGrammar original, Cloner cloner) : base(original, cloner) { } public IntervalArithmeticGrammar(string name, string description) : base(name, description) { } public override IDeepCloneable Clone(Cloner cloner) { return new IntervalArithmeticGrammar(this, cloner); } private void Initialize() { #region Symbols var add = new Addition(); var sub = new Subtraction(); var mul = new Multiplication(); var div = new Division(); var sin = new Sine(); var cos = new Cosine(); var tan = new Tangent(); var log = new Logarithm(); var pow = new Power(); var square = new Square(); var root = new Root(); var sqrt = new SquareRoot(); var exp = new Exponential(); var tanh = new HyperbolicTangent(); var constant = new Constant(); var aq = new AnalyticQuotient(); constant.MinValue = -20; constant.MaxValue = 20; var variableSymbol = new Variable(); #endregion //Special symbols var offset = new Addition {Name = "Offset"}; var scaling = new Multiplication {Name = "Scaling"}; //all other symbols var allSymbols = new List { add, sub, mul, div, constant, variableSymbol, sin, cos, tan, log, pow, square, root, sqrt, exp, tanh, aq }; var arithmeticSymbols = new List {add, sub, mul, div}; var trigonometricSymbols = new List {sin, cos, tan, tanh}; var exponentialAndLogarithmicSymbols = new List {exp, log}; var powerSymbols = new List {square, pow, sqrt, root}; var specialSymbols = new List {aq}; var realValueSymbols = new List {add, sub, mul, div, sin, cos, tan, tanh, exp, log, variableSymbol, constant, aq}; //Add special symbols AddSymbol(offset); AddSymbol(scaling); //Add all other symbols foreach (var symb in allSymbols) AddSymbol(symb); #region define subtree count for special symbols foreach (var symb in arithmeticSymbols) SetSubtreeCount(symb, 2, 2); foreach (var symb in trigonometricSymbols) SetSubtreeCount(symb, 1, 1); foreach (var symb in exponentialAndLogarithmicSymbols) SetSubtreeCount(symb, 1, 1); SetSubtreeCount(offset, 2, 2); SetSubtreeCount(scaling, 2, 2); SetSubtreeCount(pow, 2, 2); SetSubtreeCount(root, 2, 2); SetSubtreeCount(square, 1, 1); SetSubtreeCount(sqrt, 1, 1); SetSubtreeCount(aq, 2, 2); #endregion #region child symbols config AddAllowedChildSymbol(StartSymbol, offset); //Define childs for offset AddAllowedChildSymbol(offset, scaling, 0); AddAllowedChildSymbol(offset, constant, 1); //Define childs for scaling foreach (var symb in allSymbols) AddAllowedChildSymbol(scaling, symb, 0); AddAllowedChildSymbol(scaling, constant, 1); //Define childs for realvalue symbols foreach (var symb in realValueSymbols) { foreach (var c in realValueSymbols) AddAllowedChildSymbol(symb, c); } //define childs for power symbols foreach (var symb in powerSymbols) { AddAllowedChildSymbol(symb, variableSymbol, 0); AddAllowedChildSymbol(symb, constant, 1); } //define child for sqrt and square foreach (var c in realValueSymbols) { AddAllowedChildSymbol(square, c, 0); AddAllowedChildSymbol(sqrt, c, 0); } //define child for aq foreach (var c in realValueSymbols) { AddAllowedChildSymbol(aq, c, 0); AddAllowedChildSymbol(aq, c, 1); } #endregion Symbols.First(s => s is Power).Enabled = false; Symbols.First(s => s is Root).Enabled = false; Symbols.First(s => s is AnalyticQuotient).Enabled = false; } } }