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