Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3251 was 3251, checked in by gkronber, 13 years ago

Fixed issues with persistence. #952 (Artificial Ant Problem for 3.3)

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