Free cookie consent management tool by TermsFeed Policy Generator

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

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

Improved GlobalSymbolicExpressionGrammar regarding memory efficiency and fixed a bug in cloning. #938

File size: 5.6 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using System.Xml;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
31
32namespace 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    public GlobalSymbolicExpressionGrammar() : base() { } // empty constructor for cloning
75
76    public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar )
77      : base() {
78      maxFunctionArguments = 3;
79      maxFunctionDefinitions = 3;
80      Initialize(mainBranchGrammar);
81    }
82
83    private void Initialize(ISymbolicExpressionGrammar mainBranchGrammar) {
84      base.Clear();
85
86      // remove the start symbol of the default grammar
87      RemoveSymbol(StartSymbol);
88
89      StartSymbol = new ProgramRootSymbol();
90      defunSymbol = new Defun();
91      AddSymbol(StartSymbol);
92      AddSymbol(defunSymbol);
93
94      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
95      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
96      SetMinSubtreeCount(defunSymbol, 1);
97      SetMaxSubtreeCount(defunSymbol, 1);
98
99      // ADF branches maxFunctionDefinitions
100      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
101        SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
102      }
103
104      if (mainBranchGrammar != null) {
105        // copy symbols from mainBranchGrammar
106        foreach (var symb in mainBranchGrammar.Symbols) {
107          AddSymbol(symb);
108          SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
109          SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
110        }
111
112        // the start symbol of the mainBranchGrammar is allowed as the result producing branch
113        SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
114
115        // copy syntax constraints from mainBranchGrammar
116        foreach (var parent in mainBranchGrammar.Symbols) {
117          for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
118            foreach (var child in mainBranchGrammar.Symbols) {
119              if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
120                SetAllowedChild(parent, child, i);
121              }
122            }
123          }
124        }
125
126        // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
127        foreach (var symb in mainBranchGrammar.Symbols) {
128          if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
129            SetAllowedChild(defunSymbol, symb, 0);
130        }
131      }
132    }
133    private void UpdateAdfConstraints() {
134      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
135      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
136     
137      // ADF branches maxFunctionDefinitions
138      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
139        SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
140      }
141    }
142
143    public override IDeepCloneable Clone(Cloner cloner) {
144      GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner);
145      clone.defunSymbol = defunSymbol;
146      clone.maxFunctionArguments = maxFunctionArguments;
147      clone.maxFunctionDefinitions = maxFunctionDefinitions;
148      clone.minFunctionArguments = minFunctionArguments;
149      clone.minFunctionDefinitions = minFunctionDefinitions;
150      return clone;
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.