Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/GESymbolicExpressionGrammar.cs @ 10884

Last change on this file since 10884 was 10276, checked in by sawinkle, 11 years ago

#2109:
Refactoring:

  • Removed recursive version of mapping in /Mappers/DepthFirstMapper.cs, because only the iterative one is used.
  • Removed unused using statements and unused commented code.
File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.Problems.GrammaticalEvolution {
32  [StorableClass]
33  [Item("GESymbolicExpressionGrammar", "Represents a grammar for functional expressions for grammatical evolution.")]
34  public class GESymbolicExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
35    [StorableConstructor]
36    protected GESymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
37    protected GESymbolicExpressionGrammar(GESymbolicExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
38    public GESymbolicExpressionGrammar()
39      : base(ItemAttribute.GetName(typeof(GESymbolicExpressionGrammar)), ItemAttribute.GetDescription(typeof(GESymbolicExpressionGrammar))) {
40      // empty ctor is necessary to allow creation of new GEGrammars from the GUI.
41      // the problem creates a new correctly configured grammar when the grammar is set
42    }
43    internal GESymbolicExpressionGrammar(IEnumerable<string> variableNames, int nConstants)
44      : base(ItemAttribute.GetName(typeof(GESymbolicExpressionGrammar)), ItemAttribute.GetDescription(typeof(GESymbolicExpressionGrammar))) {
45      // this ctor is called by the problem as only the problem knows the allowed input variables
46      Initialize(variableNames, nConstants);
47    }
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new GESymbolicExpressionGrammar(this, cloner);
50    }
51
52    private void Initialize(IEnumerable<string> variableNames, int nConstants) {
53      #region symbol declaration
54      var add = new Addition();
55      var sub = new Subtraction();
56      var mul = new Multiplication();
57      var div = new Division();
58      var mean = new Average();
59      var log = new Logarithm();
60      var pow = new Power();
61      var square = new Square();
62      var root = new Root();
63      var sqrt = new SquareRoot();
64      var exp = new Exponential();
65
66      // we use our own random number generator here because we assume
67      // that grammars are only initialized once when setting the grammar in the problem.
68      // This means everytime the grammar parameter in the problem is changed
69      // we initialize the constants to new values
70      var rand = new MersenneTwister();
71      // warm up
72      for (int i = 0; i < 1000; i++) rand.NextDouble();
73
74      var constants = new List<Constant>(nConstants);
75      for (int i = 0; i < nConstants; i++) {
76        var constVal = rand.NextDouble() * 20.0 - 10.0;
77        var constant = new Constant();
78        constant.Name = string.Format("{0:0.000}", constVal);
79        constant.MinValue = constVal;
80        constant.MaxValue = constVal;
81        constant.ManipulatorSigma = 0.0;
82        constant.ManipulatorMu = 0.0;
83        constant.MultiplicativeManipulatorSigma = 0.0;
84        constants.Add(constant);
85      }
86
87      var variables = new List<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>();
88      foreach (var variableName in variableNames) {
89        var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
90        variableSymbol.Name = variableName;
91        variableSymbol.WeightManipulatorMu = 0.0;
92        variableSymbol.WeightManipulatorSigma = 0.0;
93        variableSymbol.WeightMu = 1.0;
94        variableSymbol.WeightSigma = 0.0;
95        variableSymbol.MultiplicativeWeightManipulatorSigma = 0.0;
96        variableSymbol.AllVariableNames = new[] { variableName };
97        variableSymbol.VariableNames = new[] { variableName };
98        variables.Add(variableSymbol);
99      }
100
101      #endregion
102
103      AddSymbol(add);
104      AddSymbol(sub);
105      AddSymbol(mul);
106      AddSymbol(div);
107      AddSymbol(mean);
108      AddSymbol(log);
109      AddSymbol(pow);
110      AddSymbol(square);
111      AddSymbol(root);
112      AddSymbol(sqrt);
113      AddSymbol(exp);
114      constants.ForEach(AddSymbol);
115      variables.ForEach(AddSymbol);
116
117      #region subtree count configuration
118      SetSubtreeCount(add, 2, 2);
119      SetSubtreeCount(sub, 2, 2);
120      SetSubtreeCount(mul, 2, 2);
121      SetSubtreeCount(div, 2, 2);
122      SetSubtreeCount(mean, 2, 2);
123      SetSubtreeCount(log, 1, 1);
124      SetSubtreeCount(pow, 2, 2);
125      SetSubtreeCount(square, 1, 1);
126      SetSubtreeCount(root, 2, 2);
127      SetSubtreeCount(sqrt, 1, 1);
128      SetSubtreeCount(exp, 1, 1);
129      constants.ForEach((c) => SetSubtreeCount(c, 0, 0));
130      variables.ForEach((v) => SetSubtreeCount(v, 0, 0));
131      #endregion
132
133      var functions = new ISymbol[] { add, sub, mul, div, mean, log, pow, root, square, sqrt };
134      var terminalSymbols = variables.Concat<ISymbol>(constants);
135      var allSymbols = functions.Concat(terminalSymbols);
136
137      #region allowed child symbols configuration
138      foreach (var s in allSymbols) {
139        AddAllowedChildSymbol(StartSymbol, s);
140      }
141      foreach (var parentSymb in functions)
142        foreach (var childSymb in allSymbols) {
143          AddAllowedChildSymbol(parentSymb, childSymb);
144        }
145
146      #endregion
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.