Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/FullFunctionalVectorExpressionGrammar.cs @ 18082

Last change on this file since 18082 was 17460, checked in by pfleck, 5 years ago

#3040

  • Added full functional grammar for vectors.
  • Added sum and mean aggregation for vectors.
File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
31  [StorableType("C913F5C6-AF16-4F2C-BA93-2D5AE1F8E68A")]
32  [Item("FullFunctionalVectorExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
33  public class FullFunctionalVectorExpressionGrammar : DataAnalysisGrammar, ISymbolicDataAnalysisGrammar {
34    [StorableConstructor]
35    protected FullFunctionalVectorExpressionGrammar(StorableConstructorFlag _) : base(_) { }
36    protected FullFunctionalVectorExpressionGrammar(FullFunctionalVectorExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
37    public FullFunctionalVectorExpressionGrammar()
38      : base(ItemAttribute.GetName(typeof(FullFunctionalVectorExpressionGrammar)), ItemAttribute.GetDescription(typeof(FullFunctionalVectorExpressionGrammar))) {
39      Initialize();
40    }
41
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new FullFunctionalVectorExpressionGrammar(this, cloner);
44    }
45
46    private void Initialize() {
47      var add = new Addition();
48      var sub = new Subtraction();
49      var mul = new Multiplication();
50      var div = new Division();
51
52      var sin = new Sine();
53      var cos = new Cosine();
54      var tan = new Tangent();
55
56      var log = new Logarithm();
57      var exp = new Exponential();
58
59
60      var pow = new Power { InitialFrequency = 0.0 };
61      var root = new Root { InitialFrequency = 0.0 };
62      var square = new Square { InitialFrequency = 0.0 };
63      var sqrt = new SquareRoot { InitialFrequency = 0.0 };
64      var cube = new Cube { InitialFrequency = 0.0 };
65      var cubeRoot = new CubeRoot { InitialFrequency = 0.0 };
66
67      var constant = new Constant { MinValue = -20, MaxValue = 20 };
68      var variable = new Variable();
69      var binFactorVariable = new BinaryFactorVariable();
70      var factorVariable = new FactorVariable();
71      var vectorVariable = new Variable() { Name = "Vector Variable" };
72
73      var sum = new Sum();
74      var mean = new Average() { Name = "Mean" };
75
76
77      var allSymbols = new List<Symbol>() {
78        add, sub, mul, div,
79        sin, cos, tan,
80        log, exp,
81        square, sqrt, cube, cubeRoot, pow, root,
82        constant, variable, binFactorVariable, factorVariable, vectorVariable,
83        sum, mean
84      };
85      var unaryFunctionSymbols = new List<Symbol>() {
86        sin, cos, tan,
87        log, exp
88      };
89
90      var binaryFunctionSymbols = new List<Symbol>() { pow, root };
91      var ternarySymbols = new List<Symbol>() { add, sub, mul, div };
92      var terminalSymbols = new List<Symbol>() { variable, binFactorVariable, factorVariable, vectorVariable, constant };
93
94      var aggregationSymbols = new List<Symbol>() { sum, mean };
95
96      foreach (var symb in allSymbols)
97        AddSymbol(symb);
98
99      foreach (var funSymb in ternarySymbols) {
100        SetSubtreeCount(funSymb, 1, 3);
101      }
102      foreach (var funSymb in unaryFunctionSymbols) {
103        SetSubtreeCount(funSymb, 1, 1);
104      }
105      foreach (var funSymb in binaryFunctionSymbols) {
106        SetSubtreeCount(funSymb, 2, 2);
107      }
108      foreach (var terminalSymbol in terminalSymbols) {
109        SetSubtreeCount(terminalSymbol, 0, 0);
110      }
111      foreach (var aggrSymb in aggregationSymbols) {
112        SetSubtreeCount(aggrSymb, 1, 1);
113      }
114
115
116      // allow each symbol as child of the start symbol
117      foreach (var symb in allSymbols) {
118        AddAllowedChildSymbol(StartSymbol, symb);
119        AddAllowedChildSymbol(DefunSymbol, symb);
120      }
121
122      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
123      foreach (var parent in allSymbols.Except(terminalSymbols)) {
124        foreach (var child in allSymbols)
125          AddAllowedChildSymbol(parent, child);
126      }
127    }
128
129    public override void ConfigureVariableSymbols(IDataAnalysisProblemData problemData) {
130      base.ConfigureVariableSymbols(problemData);
131
132      var dataset = problemData.Dataset;
133      foreach (var varSymbol in Symbols.OfType<VariableBase>().Where(sym => sym.Name == "Variable")) {
134        if (!varSymbol.Fixed) {
135          varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => dataset.VariableHasType<double>(x));
136          varSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => dataset.VariableHasType<double>(x));
137        }
138      }
139      foreach (var varSymbol in Symbols.OfType<VariableBase>().Where(sym => sym.Name == "Vector Variable")) {
140        if (!varSymbol.Fixed) {
141          varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => dataset.VariableHasType<DoubleVector>(x));
142          varSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => dataset.VariableHasType<DoubleVector>(x));
143        }
144      }
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.