[3253] | 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 |
|
---|
| 22 |
|
---|
| 23 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3257] | 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols;
|
---|
[3269] | 31 | using HeuristicLab.Data;
|
---|
[3257] | 32 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
|
---|
[3253] | 33 | [StorableClass]
|
---|
[3269] | 34 | public class ArithmeticExpressionGrammar : NamedItemCollection<Symbol>, ISymbolicExpressionGrammar {
|
---|
[3253] | 35 |
|
---|
[3269] | 36 | private List<string> variableNames = new List<string>();
|
---|
| 37 | public IEnumerable<string> VariableNames {
|
---|
| 38 | get { return variableNames; }
|
---|
| 39 | set {
|
---|
| 40 | variableNames = new List<string>(value);
|
---|
| 41 | var variable = (HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols.Variable)allSymbols[5];
|
---|
| 42 | variable.VariableNames = new ItemList<HeuristicLab.Data.StringValue>(variableNames.Select(x => new StringValue(x)));
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[3253] | 46 | public ArithmeticExpressionGrammar()
|
---|
[3269] | 47 | : this(allSymbols) {
|
---|
[3253] | 48 | }
|
---|
[3269] | 49 |
|
---|
| 50 | public ArithmeticExpressionGrammar(IEnumerable<Symbol> symbols)
|
---|
| 51 | : base(symbols) {
|
---|
| 52 | allSymbols = new List<Symbol>(symbols);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[3253] | 55 | #region ISymbolicExpressionGrammar Members
|
---|
| 56 | [Storable]
|
---|
[3257] | 57 | private StartSymbol startSymbol = new StartSymbol();
|
---|
[3253] | 58 | public Symbol StartSymbol {
|
---|
| 59 | get { return startSymbol; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | [Storable]
|
---|
| 63 | private static List<Symbol> allSymbols = new List<Symbol>() {
|
---|
| 64 | new Addition(),
|
---|
| 65 | new Subtraction(),
|
---|
| 66 | new Multiplication(),
|
---|
| 67 | new Division(),
|
---|
| 68 | new Constant(),
|
---|
[3257] | 69 | new HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols.Variable()
|
---|
[3253] | 70 | };
|
---|
| 71 | [Storable]
|
---|
| 72 | private Dictionary<Type, Dictionary<int, IEnumerable<Symbol>>> allowedSymbols = new Dictionary<Type, Dictionary<int, IEnumerable<Symbol>>>() {
|
---|
| 73 | {
|
---|
[3257] | 74 | typeof(StartSymbol),
|
---|
[3253] | 75 | new Dictionary<int, IEnumerable<Symbol>>()
|
---|
| 76 | {
|
---|
| 77 | { 0, allSymbols},
|
---|
| 78 | }
|
---|
| 79 | }, {
|
---|
| 80 | typeof(Addition),
|
---|
| 81 | new Dictionary<int, IEnumerable<Symbol>>()
|
---|
| 82 | {
|
---|
| 83 | { 0, allSymbols},
|
---|
| 84 | { 1, allSymbols}
|
---|
| 85 | }
|
---|
| 86 | },
|
---|
| 87 | {
|
---|
| 88 | typeof(Subtraction),
|
---|
| 89 | new Dictionary<int, IEnumerable<Symbol>>()
|
---|
| 90 | {
|
---|
| 91 | { 0, allSymbols},
|
---|
| 92 | { 1, allSymbols}
|
---|
| 93 | }
|
---|
| 94 | },
|
---|
| 95 | {
|
---|
| 96 | typeof(Multiplication),
|
---|
| 97 | new Dictionary<int, IEnumerable<Symbol>>()
|
---|
| 98 | {
|
---|
| 99 | { 0, allSymbols},
|
---|
| 100 | { 1, allSymbols}
|
---|
| 101 | }
|
---|
| 102 | },
|
---|
| 103 | {
|
---|
| 104 | typeof(Division),
|
---|
| 105 | new Dictionary<int, IEnumerable<Symbol>>()
|
---|
| 106 | {
|
---|
| 107 | { 0, allSymbols},
|
---|
| 108 | { 1, allSymbols}
|
---|
| 109 | }
|
---|
| 110 | },
|
---|
| 111 | };
|
---|
| 112 | public IEnumerable<Symbol> AllowedSymbols(Symbol parent, int argumentIndex) {
|
---|
| 113 | return allowedSymbols[parent.GetType()][argumentIndex];
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | [Storable]
|
---|
| 117 | private Dictionary<Type, int> minLength = new Dictionary<Type, int>() {
|
---|
[3257] | 118 | {typeof(StartSymbol), 1},
|
---|
[3253] | 119 | {typeof(Addition), 3},
|
---|
| 120 | {typeof(Subtraction), 3},
|
---|
| 121 | {typeof(Multiplication), 4},
|
---|
| 122 | {typeof(Division), 4},
|
---|
| 123 | {typeof(Constant), 1},
|
---|
[3257] | 124 | {typeof(HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols.Variable), 1},
|
---|
[3253] | 125 | };
|
---|
| 126 | public int MinimalExpressionLength(Symbol start) {
|
---|
| 127 | return minLength[start.GetType()];
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | [Storable]
|
---|
| 131 | private Dictionary<Type, int> maxLength = new Dictionary<Type, int>() {
|
---|
[3257] | 132 | {typeof(StartSymbol), int.MaxValue},
|
---|
[3253] | 133 | {typeof(Addition), int.MaxValue},
|
---|
| 134 | {typeof(Subtraction), int.MaxValue},
|
---|
| 135 | {typeof(Multiplication), int.MaxValue},
|
---|
| 136 | {typeof(Division), int.MaxValue},
|
---|
[3257] | 137 | {typeof(HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols.Variable), 1},
|
---|
[3253] | 138 | {typeof(Constant), 1},
|
---|
| 139 | };
|
---|
| 140 | public int MaximalExpressionLength(Symbol start) {
|
---|
| 141 | return maxLength[start.GetType()];
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | [Storable]
|
---|
| 145 | private Dictionary<Type, int> minDepth = new Dictionary<Type, int>() {
|
---|
[3257] | 146 | {typeof(StartSymbol), 1},
|
---|
[3253] | 147 | {typeof(Addition), 1},
|
---|
| 148 | {typeof(Subtraction), 1},
|
---|
| 149 | {typeof(Multiplication), 1},
|
---|
| 150 | {typeof(Division), 1},
|
---|
| 151 | {typeof(Constant), 0},
|
---|
[3257] | 152 | {typeof(HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols.Variable), 0}
|
---|
[3253] | 153 | };
|
---|
| 154 | public int MinimalExpressionDepth(Symbol start) {
|
---|
| 155 | return minDepth[start.GetType()];
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | [Storable]
|
---|
| 159 | private Dictionary<Type, int> subTrees = new Dictionary<Type, int>() {
|
---|
[3257] | 160 | {typeof(StartSymbol), 1},
|
---|
[3253] | 161 | {typeof(Addition), 2},
|
---|
| 162 | {typeof(Subtraction), 2},
|
---|
| 163 | {typeof(Multiplication), 2},
|
---|
| 164 | {typeof(Division), 2},
|
---|
[3257] | 165 | {typeof(HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Symbols.Variable), 0},
|
---|
[3253] | 166 | {typeof(Constant), 0},
|
---|
| 167 | };
|
---|
| 168 | public int MinSubTrees(Symbol start) {
|
---|
| 169 | return subTrees[start.GetType()];
|
---|
| 170 | }
|
---|
| 171 | public int MaxSubTrees(Symbol start) {
|
---|
| 172 | return subTrees[start.GetType()];
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | #endregion
|
---|
| 176 |
|
---|
| 177 | #region ISymbolicExpressionGrammar Members
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | public bool IsValidExpression(SymbolicExpressionTree expression) {
|
---|
| 181 | if (expression.Root.Symbol != StartSymbol) return false;
|
---|
| 182 | return IsValidExpression(expression.Root);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | #endregion
|
---|
| 186 |
|
---|
| 187 | private bool IsValidExpression(SymbolicExpressionTreeNode root) {
|
---|
| 188 | if (root.SubTrees.Count < MinSubTrees(root.Symbol)) return false;
|
---|
| 189 | if (root.SubTrees.Count > MaxSubTrees(root.Symbol)) return false;
|
---|
| 190 | for (int i = 0; i < root.SubTrees.Count; i++) {
|
---|
| 191 | if (!AllowedSymbols(root.Symbol, i).Contains(root.SubTrees[i].Symbol)) return false;
|
---|
| 192 | if (!IsValidExpression(root.SubTrees[i])) return false;
|
---|
| 193 | }
|
---|
| 194 | return true;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | }
|
---|