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