Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4633 was 4262, checked in by mkommend, 14 years ago

Made DefaultGrammar abstract and sealed GlobalGrammar (ticket #1028).

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