source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 12 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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