Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3742 was 3541, checked in by gkronber, 15 years ago

Fixed bugs in persistence of grammars.#937 (Data types and operators for symbolic expression tree encoding)

File size: 5.3 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        Initialize();
43      }
44    }
45    [Storable]
46    private int maxFunctionDefinitions;
47    public int MaxFunctionDefinitions {
48      get { return maxFunctionDefinitions; }
49      set {
50        maxFunctionDefinitions = value;
51        Initialize();
52      }
53    }
54    [Storable]
55    private int minFunctionArguments;
56    public int MinFunctionArguments {
57      get { return minFunctionArguments; }
58      set {
59        minFunctionArguments = value;
60        Initialize();
61      }
62    }
63    [Storable]
64    private int maxFunctionArguments;
65    public int MaxFunctionArguments {
66      get { return maxFunctionArguments; }
67      set {
68        maxFunctionArguments = value;
69        Initialize();
70      }
71    }
72    [Storable]
73    private ISymbolicExpressionGrammar mainBranchGrammar;
74
75    public GlobalSymbolicExpressionGrammar() : base() { } // empty constructor for cloning
76
77    public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar )
78      : base() {
79      maxFunctionArguments = 3;
80      maxFunctionDefinitions = 3;
81      this.mainBranchGrammar = mainBranchGrammar;
82      Initialize();
83    }
84
85    private void Initialize() {
86      base.Clear();
87
88      // remove the start symbol of the default grammar
89      RemoveSymbol(StartSymbol);
90
91      StartSymbol = new ProgramRootSymbol();
92      var defunSymbol = new Defun();
93      AddSymbol(StartSymbol);
94      AddSymbol(defunSymbol);
95
96      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
97      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
98      SetMinSubtreeCount(defunSymbol, 1);
99      SetMaxSubtreeCount(defunSymbol, 1);
100
101      if (mainBranchGrammar != null) {
102        // copy symbols from mainBranchGrammar
103        foreach (var symb in mainBranchGrammar.Symbols) {
104          AddSymbol(symb);
105          SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
106          SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
107        }
108
109        // the start symbol of the mainBranchGrammar is allowed as the result producing branch
110        SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
111
112        // ADF branches maxFunctionDefinitions
113        for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
114          SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
115        }
116
117        // copy syntax constraints from mainBranchGrammar
118        foreach (var parent in mainBranchGrammar.Symbols) {
119          for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
120            foreach (var child in mainBranchGrammar.Symbols) {
121              if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
122                SetAllowedChild(parent, child, i);
123              }
124            }
125          }
126        }
127
128        // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
129        foreach (var symb in mainBranchGrammar.Symbols) {
130          if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
131            SetAllowedChild(defunSymbol, symb, 0);
132        }
133      }
134    }
135    public override IDeepCloneable Clone(Cloner cloner) {
136      GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner);
137      clone.maxFunctionArguments = maxFunctionArguments;
138      clone.maxFunctionDefinitions = maxFunctionDefinitions;
139      clone.minFunctionArguments = minFunctionArguments;
140      clone.minFunctionDefinitions = minFunctionDefinitions;
141      clone.mainBranchGrammar = mainBranchGrammar;
142      return clone;
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.