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 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using System.Xml;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
33 | [StorableClass]
|
---|
34 | [Item("GlobalSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
|
---|
35 | public class GlobalSymbolicExpressionGrammar : DefaultSymbolicExpressionGrammar {
|
---|
36 | [Storable]
|
---|
37 | private int minFunctionDefinitions;
|
---|
38 | public int MinFunctionDefinitions {
|
---|
39 | get { return minFunctionDefinitions; }
|
---|
40 | set {
|
---|
41 | minFunctionDefinitions = value;
|
---|
42 | UpdateAdfConstraints();
|
---|
43 | }
|
---|
44 | }
|
---|
45 | [Storable]
|
---|
46 | private int maxFunctionDefinitions;
|
---|
47 | public int MaxFunctionDefinitions {
|
---|
48 | get { return maxFunctionDefinitions; }
|
---|
49 | set {
|
---|
50 | maxFunctionDefinitions = value;
|
---|
51 | UpdateAdfConstraints();
|
---|
52 | }
|
---|
53 | }
|
---|
54 | [Storable]
|
---|
55 | private int minFunctionArguments;
|
---|
56 | public int MinFunctionArguments {
|
---|
57 | get { return minFunctionArguments; }
|
---|
58 | set {
|
---|
59 | minFunctionArguments = value;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | [Storable]
|
---|
63 | private int maxFunctionArguments;
|
---|
64 | public int MaxFunctionArguments {
|
---|
65 | get { return maxFunctionArguments; }
|
---|
66 | set {
|
---|
67 | maxFunctionArguments = value;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | [Storable]
|
---|
72 | private Defun defunSymbol;
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 | public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar )
|
---|
77 | : base() {
|
---|
78 | maxFunctionArguments = 3;
|
---|
79 | maxFunctionDefinitions = 3;
|
---|
80 | Initialize(mainBranchGrammar);
|
---|
81 | }
|
---|
82 |
|
---|
83 | //copy constructor for cloning
|
---|
84 | protected GlobalSymbolicExpressionGrammar(GlobalSymbolicExpressionGrammar copy)
|
---|
85 | : base(copy) {
|
---|
86 | this.maxFunctionArguments = copy.maxFunctionArguments;
|
---|
87 | this.minFunctionArguments = copy.minFunctionArguments;
|
---|
88 | this.maxFunctionDefinitions = copy.maxFunctionDefinitions;
|
---|
89 | this.minFunctionDefinitions = copy.minFunctionDefinitions;
|
---|
90 | }
|
---|
91 |
|
---|
92 | [StorableConstructor]
|
---|
93 | protected GlobalSymbolicExpressionGrammar(bool deserializing)
|
---|
94 | : base(deserializing) {
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void Initialize(ISymbolicExpressionGrammar mainBranchGrammar) {
|
---|
98 | base.Clear();
|
---|
99 |
|
---|
100 | // remove the start symbol of the default grammar
|
---|
101 | RemoveSymbol(StartSymbol);
|
---|
102 |
|
---|
103 | StartSymbol = new ProgramRootSymbol();
|
---|
104 | defunSymbol = new Defun();
|
---|
105 | AddSymbol(StartSymbol);
|
---|
106 | AddSymbol(defunSymbol);
|
---|
107 |
|
---|
108 | SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
|
---|
109 | SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
|
---|
110 | SetMinSubtreeCount(defunSymbol, 1);
|
---|
111 | SetMaxSubtreeCount(defunSymbol, 1);
|
---|
112 |
|
---|
113 | // ADF branches maxFunctionDefinitions
|
---|
114 | for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
|
---|
115 | SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (mainBranchGrammar != null) {
|
---|
119 | // copy symbols from mainBranchGrammar
|
---|
120 | foreach (var symb in mainBranchGrammar.Symbols) {
|
---|
121 | AddSymbol(symb);
|
---|
122 | SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
|
---|
123 | SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
|
---|
124 | }
|
---|
125 |
|
---|
126 | // the start symbol of the mainBranchGrammar is allowed as the result producing branch
|
---|
127 | SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
|
---|
128 |
|
---|
129 | // copy syntax constraints from mainBranchGrammar
|
---|
130 | foreach (var parent in mainBranchGrammar.Symbols) {
|
---|
131 | for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
|
---|
132 | foreach (var child in mainBranchGrammar.Symbols) {
|
---|
133 | if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
|
---|
134 | SetAllowedChild(parent, child, i);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
|
---|
141 | foreach (var symb in mainBranchGrammar.Symbols) {
|
---|
142 | if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
|
---|
143 | SetAllowedChild(defunSymbol, symb, 0);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | private void UpdateAdfConstraints() {
|
---|
148 | SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
|
---|
149 | SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
|
---|
150 |
|
---|
151 | // ADF branches maxFunctionDefinitions
|
---|
152 | for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
|
---|
153 | SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
158 | GlobalSymbolicExpressionGrammar clone = new GlobalSymbolicExpressionGrammar(this);
|
---|
159 | cloner.RegisterClonedObject(this, clone);
|
---|
160 | return clone;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|