Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

File size: 5.2 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.Core;
27using System.Xml;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  [StorableClass]
33  [Item("GlobalSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
34  public class GlobalSymbolicExpressionGrammar : DefaultSymbolicExpressionGrammar {
35    [Storable]
36    private int minFunctionDefinitions;
37    public int MinFunctionDefinitions {
38      get { return minFunctionDefinitions; }
39      set {
40        minFunctionDefinitions = value;
41        Reset();
42      }
43    }
44    [Storable]
45    private int maxFunctionDefinitions;
46    public int MaxFunctionDefinitions {
47      get { return maxFunctionDefinitions; }
48      set {
49        maxFunctionDefinitions = value;
50        Reset();
51      }
52    }
53    [Storable]
54    private int minFunctionArguments;
55    public int MinFunctionArguments {
56      get { return minFunctionArguments; }
57      set {
58        minFunctionArguments = value;
59        Reset();
60      }
61    }
62    [Storable]
63    private int maxFunctionArguments;
64    public int MaxFunctionArguments {
65      get { return maxFunctionArguments; }
66      set {
67        maxFunctionArguments = value;
68        Reset();
69      }
70    }
71    [Storable]
72    private ISymbolicExpressionGrammar mainBranchGrammar;
73
74    public GlobalSymbolicExpressionGrammar() : base() { } // empty constructor for cloning
75
76    public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar )
77      : base() {
78      maxFunctionArguments = 3;
79      maxFunctionDefinitions = 3;
80      this.mainBranchGrammar = mainBranchGrammar;
81      Initialize();
82    }
83
84    private void Reset() {
85      base.Reset();
86      Initialize();
87    }
88
89    private void Initialize() {
90      // remove the start symbol of the default grammar
91      RemoveSymbol(StartSymbol);
92
93      StartSymbol = new ProgramRootSymbol();
94      var defunSymbol = new Defun();
95      AddSymbol(StartSymbol);
96      AddSymbol(defunSymbol);
97
98      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
99      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
100      SetMinSubtreeCount(defunSymbol, 1);
101      SetMaxSubtreeCount(defunSymbol, 1);
102
103      // copy symbols from mainBranchGrammar
104      foreach (var symb in mainBranchGrammar.Symbols) {
105        AddSymbol(symb);
106        SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
107        SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
108      }
109
110      // the start symbol of the mainBranchGrammar is allowed as the result producing branch
111      SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
112
113      // ADF branches maxFunctionDefinitions
114      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
115        SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
116      }
117
118      // copy syntax constraints from mainBranchGrammar
119      foreach (var parent in mainBranchGrammar.Symbols) {
120        for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
121          foreach (var child in mainBranchGrammar.Symbols) {
122            if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
123              SetAllowedChild(parent, child, i);
124            }
125          }
126        }
127      }
128
129      // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
130      foreach (var symb in mainBranchGrammar.Symbols) {
131        if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
132          SetAllowedChild(defunSymbol, symb, 0);
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.