Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/IntervalArithmeticGrammar.cs

Last change on this file was 17880, checked in by chaider, 4 years ago

#3073

  • Added AQ to IntervalArithmeticGrammar
  • Fixed bug in IABoundsEstimator
File size: 4.7 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HEAL.Attic;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7
8namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
9  [StorableType("5A2300A0-D0FC-4F2D-B910-F86384FE9052")]
10  [Item("IntervalArithmeticGrammar", "Represents a grammar for interval arithmetic")]
11  public class IntervalArithmeticGrammar : DataAnalysisGrammar, ISymbolicDataAnalysisGrammar {
12    public IntervalArithmeticGrammar() : base(ItemAttribute.GetName(typeof(IntervalArithmeticGrammar)),
13      ItemAttribute.GetDescription(typeof(IntervalArithmeticGrammar))) {
14      Initialize();
15    }
16
17    [StorableConstructor]
18    public IntervalArithmeticGrammar(StorableConstructorFlag _) : base(_) { }
19
20    protected IntervalArithmeticGrammar(IntervalArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
21    public IntervalArithmeticGrammar(string name, string description) : base(name, description) { }
22
23    public override IDeepCloneable Clone(Cloner cloner) {
24      return new IntervalArithmeticGrammar(this, cloner);
25    }
26
27    private void Initialize() {
28      #region Symbols
29
30      var add = new Addition();
31      var sub = new Subtraction();
32      var mul = new Multiplication();
33      var div = new Division();
34      var sin = new Sine();
35      var cos = new Cosine();
36      var tan = new Tangent();
37      var log = new Logarithm();
38      var pow = new Power();
39      var square = new Square();
40      var root = new Root();
41      var sqrt = new SquareRoot();
42      var exp = new Exponential();
43      var tanh = new HyperbolicTangent();
44      var constant = new Constant();
45      var aq = new AnalyticQuotient();
46      constant.MinValue = -20;
47      constant.MaxValue = 20;
48      var variableSymbol = new Variable();
49
50      #endregion
51
52      //Special symbols
53      var offset = new Addition {Name = "Offset"};
54      var scaling = new Multiplication {Name = "Scaling"};
55      //all other symbols
56      var allSymbols = new List<Symbol> {
57        add, sub, mul, div, constant, variableSymbol, sin, cos, tan, log, pow, square, root, sqrt, exp,
58        tanh, aq
59      };
60
61      var arithmeticSymbols = new List<Symbol> {add, sub, mul, div};
62      var trigonometricSymbols = new List<Symbol> {sin, cos, tan, tanh};
63      var exponentialAndLogarithmicSymbols = new List<Symbol> {exp, log};
64      var powerSymbols = new List<Symbol> {square, pow, sqrt, root};
65      var specialSymbols = new List<Symbol> {aq};
66      var realValueSymbols = new List<Symbol>
67        {add, sub, mul, div, sin, cos, tan, tanh, exp, log, variableSymbol, constant, aq};
68
69
70      //Add special symbols
71      AddSymbol(offset);
72      AddSymbol(scaling);
73      //Add all other symbols
74      foreach (var symb in allSymbols) AddSymbol(symb);
75
76      #region define subtree count for special symbols
77
78      foreach (var symb in arithmeticSymbols) SetSubtreeCount(symb, 2, 2);
79
80      foreach (var symb in trigonometricSymbols) SetSubtreeCount(symb, 1, 1);
81
82      foreach (var symb in exponentialAndLogarithmicSymbols) SetSubtreeCount(symb, 1, 1);
83
84      SetSubtreeCount(offset, 2, 2);
85      SetSubtreeCount(scaling, 2, 2);
86      SetSubtreeCount(pow, 2, 2);
87      SetSubtreeCount(root, 2, 2);
88      SetSubtreeCount(square, 1, 1);
89      SetSubtreeCount(sqrt, 1, 1);
90      SetSubtreeCount(aq, 2, 2);
91
92      #endregion
93
94      #region child symbols config
95
96      AddAllowedChildSymbol(StartSymbol, offset);
97
98      //Define childs for offset
99      AddAllowedChildSymbol(offset, scaling, 0);
100      AddAllowedChildSymbol(offset, constant, 1);
101
102      //Define childs for scaling
103      foreach (var symb in allSymbols) AddAllowedChildSymbol(scaling, symb, 0);
104      AddAllowedChildSymbol(scaling, constant, 1);
105
106      //Define childs for realvalue symbols
107      foreach (var symb in realValueSymbols) {
108        foreach (var c in realValueSymbols) AddAllowedChildSymbol(symb, c);
109      }
110
111      //define childs for power symbols
112      foreach (var symb in powerSymbols) {
113        AddAllowedChildSymbol(symb, variableSymbol, 0);
114        AddAllowedChildSymbol(symb, constant, 1);
115      }
116
117      //define child for sqrt and square
118      foreach (var c in realValueSymbols) {
119        AddAllowedChildSymbol(square, c, 0);
120        AddAllowedChildSymbol(sqrt, c, 0);
121      }
122
123      //define child for aq
124      foreach (var c in realValueSymbols) {
125        AddAllowedChildSymbol(aq, c, 0);
126        AddAllowedChildSymbol(aq, c, 1);
127      }
128
129      #endregion
130
131      Symbols.First(s => s is Power).Enabled = false;
132      Symbols.First(s => s is Root).Enabled = false;
133      Symbols.First(s => s is AnalyticQuotient).Enabled = false;
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.