[3223] | 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;
|
---|
[3238] | 26 | using System.Linq;
|
---|
[3223] | 27 | using HeuristicLab.Core;
|
---|
[3251] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3257] | 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
|
---|
[3223] | 30 | namespace HeuristicLab.Problems.ArtificialAnt {
|
---|
[3251] | 31 | [StorableClass]
|
---|
[3223] | 32 | public class ArtificialAntExpressionGrammar : Item, ISymbolicExpressionGrammar {
|
---|
| 33 |
|
---|
| 34 | public ArtificialAntExpressionGrammar()
|
---|
| 35 | : base() {
|
---|
| 36 | }
|
---|
| 37 | #region ISymbolicExpressionGrammar Members
|
---|
| 38 |
|
---|
[3251] | 39 | [Storable]
|
---|
[3257] | 40 | private StartSymbol startSymbol = new StartSymbol();
|
---|
[3223] | 41 | public Symbol StartSymbol {
|
---|
| 42 | get { return startSymbol; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[3251] | 45 | [Storable]
|
---|
[3223] | 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 | };
|
---|
[3251] | 54 | [Storable]
|
---|
[3223] | 55 | private Dictionary<Type, Dictionary<int, IEnumerable<Symbol>>> allowedSymbols = new Dictionary<Type, Dictionary<int, IEnumerable<Symbol>>>() {
|
---|
| 56 | {
|
---|
[3257] | 57 | typeof(StartSymbol),
|
---|
[3223] | 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 |
|
---|
[3251] | 92 | [Storable]
|
---|
[3223] | 93 | private Dictionary<Type, int> minLength = new Dictionary<Type, int>() {
|
---|
[3257] | 94 | {typeof(StartSymbol), 1},
|
---|
[3223] | 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 |
|
---|
[3251] | 106 | [Storable]
|
---|
[3223] | 107 | private Dictionary<Type, int> maxLength = new Dictionary<Type, int>() {
|
---|
[3257] | 108 | {typeof(StartSymbol), int.MaxValue},
|
---|
[3223] | 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 |
|
---|
[3251] | 120 | [Storable]
|
---|
[3223] | 121 | private Dictionary<Type, int> minDepth = new Dictionary<Type, int>() {
|
---|
[3257] | 122 | {typeof(StartSymbol), 1},
|
---|
[3223] | 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 |
|
---|
[3251] | 135 | [Storable]
|
---|
[3223] | 136 | private Dictionary<Type, int> subTrees = new Dictionary<Type, int>() {
|
---|
[3257] | 137 | {typeof(StartSymbol), 1},
|
---|
[3223] | 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
|
---|
[3238] | 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 | }
|
---|
[3223] | 173 | }
|
---|
| 174 | }
|
---|