Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MemoryOperations/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/FullFunctionalExpressionGrammar.cs @ 5867

Last change on this file since 5867 was 4897, checked in by gkronber, 14 years ago

Implemented set-memory and read-memory operation for GP-based data-analysis. #1290

File size: 4.6 KB
RevLine 
[3841]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
[4722]23using HeuristicLab.Common;
[3841]24using HeuristicLab.Core;
[4068]25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
[3841]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableClass]
31  [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
32  public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar {
[4722]33    [StorableConstructor]
34    protected FullFunctionalExpressionGrammar(bool deserializing) : base(deserializing) { }
35    protected FullFunctionalExpressionGrammar(FullFunctionalExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
[3841]36    public FullFunctionalExpressionGrammar()
37      : base() {
38      Initialize();
39    }
40
[4722]41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new FullFunctionalExpressionGrammar(this, cloner);
43    }
44
[3841]45    private void Initialize() {
46      var add = new Addition();
47      var sub = new Subtraction();
48      var mul = new Multiplication();
49      var div = new Division();
50      var mean = new Average();
51      var sin = new Sine();
52      var cos = new Cosine();
53      var tan = new Tangent();
54      var log = new Logarithm();
55      var exp = new Exponential();
56      var @if = new IfThenElse();
57      var gt = new GreaterThan();
58      var lt = new LessThan();
59      var and = new And();
60      var or = new Or();
61      var not = new Not();
[4897]62      var setMem = new SetMem();
63      var readMem = new ReadMem();
[3841]64      var constant = new Constant();
65      constant.MinValue = -20;
66      constant.MaxValue = 20;
[4249]67      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
68      var laggedVariable = new LaggedVariable();
69      laggedVariable.InitialFrequency = 0.0;
[3841]70
[4897]71      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, setMem, readMem, constant, variableSymbol, laggedVariable };
72      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not, setMem};
[3841]73      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
[3993]74      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
[4897]75      var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable, readMem };
[3841]76
77      foreach (var symb in allSymbols)
78        AddSymbol(symb);
79
80      foreach (var funSymb in functionSymbols) {
81        SetMinSubtreeCount(funSymb, 1);
82        SetMaxSubtreeCount(funSymb, 3);
83      }
84      foreach (var funSymb in unaryFunctionSymbols) {
85        SetMinSubtreeCount(funSymb, 1);
86        SetMaxSubtreeCount(funSymb, 1);
87      }
88      foreach (var funSymb in binaryFunctionSymbols) {
89        SetMinSubtreeCount(funSymb, 2);
90        SetMaxSubtreeCount(funSymb, 2);
91      }
92
[4249]93      foreach (var terminalSymbol in terminalSymbols) {
94        SetMinSubtreeCount(terminalSymbol, 0);
95        SetMaxSubtreeCount(terminalSymbol, 0);
96      }
97
[3841]98      SetMinSubtreeCount(@if, 3);
99      SetMaxSubtreeCount(@if, 3);
100
[4249]101
[3841]102      // allow each symbol as child of the start symbol
103      foreach (var symb in allSymbols) {
104        SetAllowedChild(StartSymbol, symb, 0);
105      }
106
107      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
108      foreach (var parent in allSymbols) {
109        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
110          foreach (var child in allSymbols) {
111            SetAllowedChild(parent, child, i);
112          }
113      }
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.