Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs @ 6340

Last change on this file since 6340 was 6340, checked in by abeham, 14 years ago

#1465

  • updated branch from trunk
File size: 5.6 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;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
28  [StorableClass]
29  public abstract class SymbolicExpressionGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionGrammar {
30    #region fields & properties
31    [Storable(DefaultValue = false)]
32    private bool readOnly;
33    public bool ReadOnly {
34      get { return readOnly; }
35      set {
36        if (readOnly != value) {
37          readOnly = value;
38          OnReadOnlyChanged();
39        }
40      }
41    }
42
43    [Storable]
44    private int minimumFunctionDefinitions;
45    public int MinimumFunctionDefinitions {
46      get { return minimumFunctionDefinitions; }
47      set {
48        minimumFunctionDefinitions = value;
49        UpdateAdfConstraints();
50      }
51    }
52    [Storable]
53    private int maximumFunctionDefinitions;
54    public int MaximumFunctionDefinitions {
55      get { return maximumFunctionDefinitions; }
56      set {
57        maximumFunctionDefinitions = value;
58        UpdateAdfConstraints();
59      }
60    }
61    [Storable]
62    private int minimumFunctionArguments;
63    public int MinimumFunctionArguments {
64      get { return minimumFunctionArguments; }
65      set { minimumFunctionArguments = value; }
66    }
67    [Storable]
68    private int maximumFunctionArguments;
69    public int MaximumFunctionArguments {
70      get { return maximumFunctionArguments; }
71      set { maximumFunctionArguments = value; }
72    }
73
74    [Storable]
75    private ProgramRootSymbol programRootSymbol;
76    public ProgramRootSymbol ProgramRootSymbol {
77      get { return programRootSymbol; }
78    }
79    ISymbol ISymbolicExpressionGrammar.ProgramRootSymbol {
80      get { return ProgramRootSymbol; }
81    }
82    [Storable(Name = "ProgramRootSymbol")]
83    private ISymbol StorableProgramRootSymbol {
84      get { return programRootSymbol; }
85      set { programRootSymbol = (ProgramRootSymbol)value; }
86    }
87
88    private StartSymbol startSymbol;
89    public StartSymbol StartSymbol {
90      get { return startSymbol; }
91    }
92    ISymbol ISymbolicExpressionGrammar.StartSymbol {
93      get { return StartSymbol; }
94    }
95    [Storable(Name = "StartSymbol")]
96    private ISymbol StorableStartSymbol {
97      get { return startSymbol; }
98      set { startSymbol = (StartSymbol)value; }
99    }
100
101    [Storable]
102    private Defun defunSymbol;
103    protected Defun DefunSymbol {
104      get { return defunSymbol; }
105    }
106    [Storable(Name = "DefunSymbol")]
107    private ISymbol StorableDefunSymbol {
108      get { return defunSymbol; }
109      set { defunSymbol = (Defun)value; }
110    }
111    #endregion
112
113    [StorableConstructor]
114    protected SymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
115    protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner)
116      : base(original, cloner) {
117      programRootSymbol = cloner.Clone(original.programRootSymbol);
118      startSymbol = cloner.Clone(original.StartSymbol);
119      defunSymbol = cloner.Clone(original.defunSymbol);
120
121      maximumFunctionArguments = original.maximumFunctionArguments;
122      minimumFunctionArguments = original.minimumFunctionArguments;
123      maximumFunctionDefinitions = original.maximumFunctionDefinitions;
124      minimumFunctionDefinitions = original.minimumFunctionDefinitions;
125    }
126
127    public SymbolicExpressionGrammar(string name, string description)
128      : base(name, description) {
129      programRootSymbol = new ProgramRootSymbol();
130      AddSymbol(programRootSymbol);
131      SetSubtreeCount(programRootSymbol, 1, 1);
132
133      startSymbol = new StartSymbol();
134      AddSymbol(startSymbol);
135      SetSubtreeCount(startSymbol, 1, 1);
136
137      defunSymbol = new Defun();
138      AddSymbol(defunSymbol);
139      SetSubtreeCount(defunSymbol, 1, 1);
140
141      AddAllowedChildSymbol(programRootSymbol, startSymbol, 0);
142      UpdateAdfConstraints();
143    }
144
145    private void UpdateAdfConstraints() {
146      SetSubtreeCount(programRootSymbol, minimumFunctionDefinitions + 1, maximumFunctionDefinitions + 1);
147
148      // ADF branches maxFunctionDefinitions
149      for (int argumentIndex = 1; argumentIndex < maximumFunctionDefinitions + 1; argumentIndex++) {
150        RemoveAllowedChildSymbol(programRootSymbol, defunSymbol, argumentIndex);
151        AddAllowedChildSymbol(programRootSymbol, defunSymbol, argumentIndex);
152      }
153    }
154
155    public event EventHandler ReadOnlyChanged;
156    protected virtual void OnReadOnlyChanged() {
157      var handler = ReadOnlyChanged;
158      if (handler != null)
159        handler(this, EventArgs.Empty);
160    }
161
162    #region IStatefulItem
163    void IStatefulItem.InitializeState() { }
164    void IStatefulItem.ClearState() {
165      ReadOnly = false;
166    }
167    #endregion
168  }
169}
Note: See TracBrowser for help on using the repository browser.