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.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
31 | using HeuristicLab.Problems.ArtificialAnt.Symbols;
|
---|
32 | namespace HeuristicLab.Problems.ArtificialAnt {
|
---|
33 | [StorableClass]
|
---|
34 | public class ArtificialAntExpressionGrammar : DefaultSymbolicExpressionGrammar {
|
---|
35 |
|
---|
36 | public ArtificialAntExpressionGrammar()
|
---|
37 | : base() {
|
---|
38 | Initialize();
|
---|
39 | }
|
---|
40 | [StorableConstructor]
|
---|
41 | protected ArtificialAntExpressionGrammar(bool deserializing) : base(deserializing) { }
|
---|
42 |
|
---|
43 | private void Initialize() {
|
---|
44 | var ifFoodAhead = new IfFoodAhead();
|
---|
45 | var prog2 = new Prog2();
|
---|
46 | var prog3 = new Prog3();
|
---|
47 | var move = new Move();
|
---|
48 | var left = new Left();
|
---|
49 | var right = new Right();
|
---|
50 | var defun = new Defun();
|
---|
51 | var allSymbols = new List<Symbol>() { ifFoodAhead, prog2, prog3, move, left, right };
|
---|
52 | var nonTerminalSymbols = new List<Symbol>() { ifFoodAhead, prog2, prog3 };
|
---|
53 |
|
---|
54 | allSymbols.ForEach(s => AddSymbol(s));
|
---|
55 | SetMinSubtreeCount(ifFoodAhead, 2);
|
---|
56 | SetMaxSubtreeCount(ifFoodAhead, 2);
|
---|
57 | SetMinSubtreeCount(prog2, 2);
|
---|
58 | SetMaxSubtreeCount(prog2, 2);
|
---|
59 | SetMinSubtreeCount(prog3, 3);
|
---|
60 | SetMaxSubtreeCount(prog3, 3);
|
---|
61 | SetMinSubtreeCount(move, 0);
|
---|
62 | SetMaxSubtreeCount(move, 0);
|
---|
63 | SetMinSubtreeCount(left, 0);
|
---|
64 | SetMaxSubtreeCount(left, 0);
|
---|
65 | SetMinSubtreeCount(right, 0);
|
---|
66 | SetMaxSubtreeCount(right, 0);
|
---|
67 |
|
---|
68 | // each symbols is allowed as child of the start symbol
|
---|
69 | allSymbols.ForEach(s => SetAllowedChild(StartSymbol, s, 0));
|
---|
70 |
|
---|
71 | // each symbol is allowed as child of all other symbols (except for terminals that have MaxSubtreeCount == 0
|
---|
72 | foreach (var parent in allSymbols) {
|
---|
73 | for (int argIndex = 0; argIndex < GetMaxSubtreeCount(parent); argIndex++) {
|
---|
74 | foreach (var child in allSymbols) {
|
---|
75 | SetAllowedChild(parent, child, argIndex);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|