Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3376 was 3376, checked in by swagner, 14 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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