Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs @ 5691

Last change on this file since 5691 was 5691, checked in by mkommend, 14 years ago

#1418: Fixed error in grammars.

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Common;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24
25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
26  [StorableClass]
27  public abstract class SymbolicExpressionGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionGrammar {
28    #region fields & properties
29    [Storable]
30    private int minimumFunctionDefinitions;
31    public int MinimumFunctionDefinitions {
32      get { return minimumFunctionDefinitions; }
33      set {
34        minimumFunctionDefinitions = value;
35        UpdateAdfConstraints();
36      }
37    }
38    [Storable]
39    private int maximumFunctionDefinitions;
40    public int MaximumFunctionDefinitions {
41      get { return maximumFunctionDefinitions; }
42      set {
43        maximumFunctionDefinitions = value;
44        UpdateAdfConstraints();
45      }
46    }
47    [Storable]
48    private int minimumFunctionArguments;
49    public int MinimumFunctionArguments {
50      get { return minimumFunctionArguments; }
51      set {
52        minimumFunctionArguments = value;
53      }
54    }
55    [Storable]
56    private int maximumFunctionArguments;
57    public int MaximumFunctionArguments {
58      get { return maximumFunctionArguments; }
59      set {
60        maximumFunctionArguments = value;
61      }
62    }
63
64    [Storable]
65    private ProgramRootSymbol programRootSymbol;
66    public ProgramRootSymbol ProgramRootSymbol {
67      get { return programRootSymbol; }
68    }
69    ISymbol ISymbolicExpressionGrammar.ProgramRootSymbol {
70      get { return ProgramRootSymbol; }
71    }
72
73    [Storable]
74    private StartSymbol startSymbol;
75    public StartSymbol StartSymbol {
76      get { return startSymbol; }
77    }
78    ISymbol ISymbolicExpressionGrammar.StartSymbol {
79      get { return StartSymbol; }
80    }
81
82    [Storable]
83    private Defun defunSymbol;
84    protected Defun DefunSymbol {
85      get { return defunSymbol; }
86    }
87    #endregion
88
89    [StorableConstructor]
90    protected SymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
91    protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner)
92      : base(original, cloner) {
93      programRootSymbol = (ProgramRootSymbol)cloner.Clone(original.programRootSymbol);
94      startSymbol = (StartSymbol)cloner.Clone(original.StartSymbol);
95      defunSymbol = (Defun)cloner.Clone(original.defunSymbol);
96      maximumFunctionArguments = original.maximumFunctionArguments;
97      minimumFunctionArguments = original.minimumFunctionArguments;
98      maximumFunctionDefinitions = original.maximumFunctionDefinitions;
99      minimumFunctionDefinitions = original.minimumFunctionDefinitions;
100    }
101
102    public SymbolicExpressionGrammar(string name, string description)
103      : base(name, description) {
104      programRootSymbol = new ProgramRootSymbol();
105      AddSymbol(programRootSymbol);
106      SetSubtreeCount(programRootSymbol, 1, 1);
107
108      startSymbol = new StartSymbol();
109      AddSymbol(startSymbol);
110      SetSubtreeCount(startSymbol, 1, 1);
111
112      defunSymbol = new Defun();
113      AddSymbol(defunSymbol);
114      SetSubtreeCount(defunSymbol, 1, 1);
115
116      AddAllowedChildSymbol(programRootSymbol, startSymbol, 0);
117      UpdateAdfConstraints();
118    }
119
120    private void UpdateAdfConstraints() {
121      SetSubtreeCount(programRootSymbol, minimumFunctionDefinitions + 1, maximumFunctionDefinitions + 1);
122
123      // ADF branches maxFunctionDefinitions
124      for (int argumentIndex = 1; argumentIndex < maximumFunctionDefinitions + 1; argumentIndex++) {
125        AddAllowedChildSymbol(programRootSymbol, defunSymbol, argumentIndex);
126      }
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.