Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntExpressionGrammar.cs @ 3257

Last change on this file since 3257 was 3257, checked in by gkronber, 14 years ago

Moved general set of symbols and evaluator into Encodings.SymbolicExpressionTreeEncoding. #937 (Data types and operators for symbolic expression tree encoding)

File size: 5.2 KB
Line 
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
23using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
24using System.Collections.Generic;
25using System;
26using System.Linq;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
30namespace HeuristicLab.Problems.ArtificialAnt {
31  [StorableClass]
32  public class ArtificialAntExpressionGrammar : Item, ISymbolicExpressionGrammar {
33
34    public ArtificialAntExpressionGrammar()
35      : base() {
36    }
37    #region ISymbolicExpressionGrammar Members
38
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 IfFoodAhead(),
48      new Prog2(),
49      new Prog3(),
50      new Move(),
51      new Left(),
52      new Right()
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(IfFoodAhead),
64        new Dictionary<int, IEnumerable<Symbol>>()
65        {
66          { 0, allSymbols},
67          { 1, allSymbols}
68        }
69      },
70      {
71        typeof(Prog2),
72        new Dictionary<int, IEnumerable<Symbol>>()
73        {
74          { 0, allSymbols},
75          { 1, allSymbols}
76        }
77      },
78      {
79        typeof(Prog3),
80        new Dictionary<int, IEnumerable<Symbol>>()
81        {
82          { 0, allSymbols},
83          { 1, allSymbols},
84          { 2, allSymbols}
85        }
86      },
87    };
88    public IEnumerable<Symbol> AllowedSymbols(Symbol parent, int argumentIndex) {
89      return allowedSymbols[parent.GetType()][argumentIndex];
90    }
91
92    [Storable]
93    private Dictionary<Type, int> minLength = new Dictionary<Type, int>() {
94      {typeof(StartSymbol), 1},
95      {typeof(IfFoodAhead), 3},
96      {typeof(Prog2), 3},
97      {typeof(Prog3), 4},
98      {typeof(Move), 1},
99      {typeof(Left), 1},
100      {typeof(Right), 1}
101    };
102    public int MinimalExpressionLength(Symbol start) {
103      return minLength[start.GetType()];
104    }
105
106    [Storable]
107    private Dictionary<Type, int> maxLength = new Dictionary<Type, int>() {
108      {typeof(StartSymbol), int.MaxValue},
109      {typeof(IfFoodAhead), int.MaxValue},
110      {typeof(Prog2), int.MaxValue},
111      {typeof(Prog3), int.MaxValue},
112      {typeof(Move), 1},
113      {typeof(Left), 1},
114      {typeof(Right), 1}
115    };
116    public int MaximalExpressionLength(Symbol start) {
117      return maxLength[start.GetType()];
118    }
119
120    [Storable]
121    private Dictionary<Type, int> minDepth = new Dictionary<Type, int>() {
122      {typeof(StartSymbol), 1},
123      {typeof(IfFoodAhead), 1},
124      {typeof(Prog2), 1},
125      {typeof(Prog3), 1},
126      {typeof(Move), 0},
127      {typeof(Left), 0},
128      {typeof(Right), 0}
129    };
130    public int MinimalExpressionDepth(Symbol start) {
131      return minDepth[start.GetType()];
132    }
133
134
135    [Storable]
136    private Dictionary<Type, int> subTrees = new Dictionary<Type, int>() {
137      {typeof(StartSymbol), 1},
138      {typeof(IfFoodAhead), 2},
139      {typeof(Prog2), 2},
140      {typeof(Prog3), 3},
141      {typeof(Move), 0},
142      {typeof(Left), 0},
143      {typeof(Right), 0}
144    };
145    public int MinSubTrees(Symbol start) {
146      return subTrees[start.GetType()];
147    }
148    public int MaxSubTrees(Symbol start) {
149      return subTrees[start.GetType()];
150    }
151
152    #endregion
153
154    #region ISymbolicExpressionGrammar Members
155
156
157    public bool IsValidExpression(SymbolicExpressionTree expression) {
158      if (expression.Root.Symbol != StartSymbol) return false;
159      return IsValidExpression(expression.Root);
160    }
161
162    #endregion
163
164    private bool IsValidExpression(SymbolicExpressionTreeNode root) {
165      if (root.SubTrees.Count < MinSubTrees(root.Symbol)) return false;
166      if (root.SubTrees.Count > MaxSubTrees(root.Symbol)) return false;
167      for (int i = 0; i < root.SubTrees.Count; i++) {
168        if (!AllowedSymbols(root.Symbol, i).Contains(root.SubTrees[i].Symbol)) return false;
169        if (!IsValidExpression(root.SubTrees[i])) return false;
170      }
171      return true;
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.