Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs @ 5991

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

#1418: Reintegrated branch into trunk.

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