Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5695 was 5695, checked in by mkommend, 12 years ago

#1418: Implemented persistence of grammars. Currently it does not work, because the Tuple type is not serializable (see #1442).

File size: 4.9 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 { minimumFunctionArguments = value; }
52    }
53    [Storable]
54    private int maximumFunctionArguments;
55    public int MaximumFunctionArguments {
56      get { return maximumFunctionArguments; }
57      set { maximumFunctionArguments = value; }
58    }
59
60    [Storable]
61    private ProgramRootSymbol programRootSymbol;
62    public ProgramRootSymbol ProgramRootSymbol {
63      get { return programRootSymbol; }
64    }
65    ISymbol ISymbolicExpressionGrammar.ProgramRootSymbol {
66      get { return ProgramRootSymbol; }
67    }
68    [Storable(Name = "ProgramRootSymbol")]
69    private ISymbol StorableProgramRootSymbol {
70      get { return programRootSymbol; }
71      set { programRootSymbol = (ProgramRootSymbol)value; }
72    }
73
74    private StartSymbol startSymbol;
75    public StartSymbol StartSymbol {
76      get { return startSymbol; }
77    }
78    ISymbol ISymbolicExpressionGrammar.StartSymbol {
79      get { return StartSymbol; }
80    }
81    [Storable(Name = "StartSymbol")]
82    private ISymbol StorableStartSymbol {
83      get { return startSymbol; }
84      set { startSymbol = (StartSymbol)value; }
85    }
86
87    [Storable]
88    private Defun defunSymbol;
89    protected Defun DefunSymbol {
90      get { return defunSymbol; }
91    }
92    [Storable(Name = "DefunSymbol")]
93    private ISymbol StorableDefunSymbol {
94      get { return defunSymbol; }
95      set { defunSymbol = (Defun)value; }
96    }
97    #endregion
98
99    [StorableConstructor]
100    protected SymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
101    protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner)
102      : base(original, cloner) {
103      programRootSymbol = (ProgramRootSymbol)cloner.Clone(original.programRootSymbol);
104      startSymbol = (StartSymbol)cloner.Clone(original.StartSymbol);
105      defunSymbol = (Defun)cloner.Clone(original.defunSymbol);
106      maximumFunctionArguments = original.maximumFunctionArguments;
107      minimumFunctionArguments = original.minimumFunctionArguments;
108      maximumFunctionDefinitions = original.maximumFunctionDefinitions;
109      minimumFunctionDefinitions = original.minimumFunctionDefinitions;
110    }
111
112    public SymbolicExpressionGrammar(string name, string description)
113      : base(name, description) {
114      programRootSymbol = new ProgramRootSymbol();
115      AddSymbol(programRootSymbol);
116      SetSubtreeCount(programRootSymbol, 1, 1);
117
118      startSymbol = new StartSymbol();
119      AddSymbol(startSymbol);
120      SetSubtreeCount(startSymbol, 1, 1);
121
122      defunSymbol = new Defun();
123      AddSymbol(defunSymbol);
124      SetSubtreeCount(defunSymbol, 1, 1);
125
126      AddAllowedChildSymbol(programRootSymbol, startSymbol, 0);
127      UpdateAdfConstraints();
128    }
129
130    private void UpdateAdfConstraints() {
131      SetSubtreeCount(programRootSymbol, minimumFunctionDefinitions + 1, maximumFunctionDefinitions + 1);
132
133      // ADF branches maxFunctionDefinitions
134      for (int argumentIndex = 1; argumentIndex < maximumFunctionDefinitions + 1; argumentIndex++) {
135        AddAllowedChildSymbol(programRootSymbol, defunSymbol, argumentIndex);
136      }
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.