Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs @ 4689

Last change on this file since 4689 was 4674, checked in by gkronber, 14 years ago

Refactored cloning in SymbolicExpressionTreeEncoding. #922

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    [StorableConstructor]
72    private GlobalSymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
73    private GlobalSymbolicExpressionGrammar(GlobalSymbolicExpressionGrammar original, Cloner cloner)
74      : base(original, cloner) {
75      defunSymbol = (Defun)cloner.Clone(original.defunSymbol);
76      maxFunctionArguments = original.maxFunctionArguments;
77      minFunctionArguments = original.minFunctionArguments;
78      maxFunctionDefinitions = original.maxFunctionDefinitions;
79      minFunctionDefinitions = original.minFunctionDefinitions;
80    }
81
82    public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar)
83      : base(mainBranchGrammar) {
84      maxFunctionArguments = 3;
85      maxFunctionDefinitions = 3;
86
87      ProgramRootSymbol programRootSymbol = Symbols.OfType<ProgramRootSymbol>().FirstOrDefault();
88      if (programRootSymbol == null) {
89        programRootSymbol = new ProgramRootSymbol();
90        AddSymbol(programRootSymbol);
91      }
92      StartSymbol = programRootSymbol;
93
94      defunSymbol = Symbols.OfType<Defun>().FirstOrDefault();
95      if (defunSymbol == null) {
96        defunSymbol = new Defun();
97        AddSymbol(defunSymbol);
98      }
99
100      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
101      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
102      SetMinSubtreeCount(defunSymbol, 1);
103      SetMaxSubtreeCount(defunSymbol, 1);
104
105      // the start symbol of the mainBranchGrammar is allowed as the result producing branch
106      SetAllowedChild(StartSymbol, Symbols.Where(s => s.Name == mainBranchGrammar.StartSymbol.Name).First(), 0);
107
108      // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
109      foreach (var symb in mainBranchGrammar.Symbols) {
110        if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
111          SetAllowedChild(defunSymbol, Symbols.Where(s => s.Name == symb.Name).First(), 0);
112      }
113    }
114
115    [Obsolete]
116    private void Initialize(ISymbolicExpressionGrammar mainBranchGrammar) {
117      base.Clear();
118
119      // remove the start symbol of the default grammar
120      RemoveSymbol(StartSymbol);
121
122      StartSymbol = new ProgramRootSymbol();
123      defunSymbol = new Defun();
124      AddSymbol(StartSymbol);
125      AddSymbol(defunSymbol);
126
127      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
128      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
129      SetMinSubtreeCount(defunSymbol, 1);
130      SetMaxSubtreeCount(defunSymbol, 1);
131
132      // ADF branches maxFunctionDefinitions
133      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
134        SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
135      }
136
137      if (mainBranchGrammar != null) {
138        // copy symbols from mainBranchGrammar
139        foreach (var symb in mainBranchGrammar.Symbols) {
140          AddSymbol(symb);
141          SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
142          SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
143        }
144
145        // the start symbol of the mainBranchGrammar is allowed as the result producing branch
146        SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
147
148        // copy syntax constraints from mainBranchGrammar
149        foreach (var parent in mainBranchGrammar.Symbols) {
150          for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
151            foreach (var child in mainBranchGrammar.Symbols) {
152              if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
153                SetAllowedChild(parent, child, i);
154              }
155            }
156          }
157        }
158
159        // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
160        foreach (var symb in mainBranchGrammar.Symbols) {
161          if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
162            SetAllowedChild(defunSymbol, symb, 0);
163        }
164      }
165    }
166    private void UpdateAdfConstraints() {
167      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
168      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
169
170      // ADF branches maxFunctionDefinitions
171      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
172        SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
173      }
174    }
175
176    public override IDeepCloneable Clone(Cloner cloner) {
177      return new GlobalSymbolicExpressionGrammar(this, cloner);
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.