1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022010 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 


22  using System.Collections.Generic;


23  using System.Linq;


24  using HeuristicLab.Core;


25  using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;


26  using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;


27  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


28 


29  namespace HeuristicLab.Problems.ExternalEvaluation.GP {


30  [StorableClass]


31  [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]


32  public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar {


33  [Storable]


34  private HeuristicLab.Problems.ExternalEvaluation.GP.Variable variableSymbol;


35 


36  public FullFunctionalExpressionGrammar()


37  : base() {


38  Initialize();


39  }


40 


41  private void Initialize() {


42  var originalStart = StartSymbol;


43  if (!(originalStart is ProgramRootSymbol)) {


44  var root = new ProgramRootSymbol();


45  AddSymbol(root);


46  SetMinSubtreeCount(root, 1);


47  SetMaxSubtreeCount(root, 1);


48  SetAllowedChild(root, originalStart, 0);


49 


50  StartSymbol = root;


51  }


52 


53  var add = new Addition();


54  var sub = new Subtraction();


55  var mul = new Multiplication();


56  var div = new Division();


57  var mean = new Average();


58  var sin = new Sine();


59  var cos = new Cosine();


60  var tan = new Tangent();


61  var log = new Logarithm();


62  var exp = new Exponential();


63  var @if = new IfThenElse();


64  var gt = new GreaterThan();


65  var lt = new LessThan();


66  var and = new And();


67  var or = new Or();


68  var not = new Not();


69  var constant = new Constant();


70  constant.MinValue = 20;


71  constant.MaxValue = 20;


72  variableSymbol = new Variable();


73 


74  var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };


75  var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };


76  var binaryFunctionSymbols = new List<Symbol>() { gt, lt };


77  var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or};


78 


79  foreach (var symb in allSymbols)


80  AddSymbol(symb);


81 


82  foreach (var funSymb in functionSymbols) {


83  SetMinSubtreeCount(funSymb, 1);


84  SetMaxSubtreeCount(funSymb, 3);


85  }


86  foreach (var funSymb in unaryFunctionSymbols) {


87  SetMinSubtreeCount(funSymb, 1);


88  SetMaxSubtreeCount(funSymb, 1);


89  }


90  foreach (var funSymb in binaryFunctionSymbols) {


91  SetMinSubtreeCount(funSymb, 2);


92  SetMaxSubtreeCount(funSymb, 2);


93  }


94 


95  SetMinSubtreeCount(@if, 3);


96  SetMaxSubtreeCount(@if, 3);


97  SetMinSubtreeCount(constant, 0);


98  SetMaxSubtreeCount(constant, 0);


99  SetMinSubtreeCount(variableSymbol, 0);


100  SetMaxSubtreeCount(variableSymbol, 0);


101 


102  // allow each symbol as child of the start symbol


103  foreach (var symb in allSymbols) {


104  SetAllowedChild(originalStart, 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  }

