1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
29 | [StorableClass]
|
---|
30 | [Item("Symbol", "Represents a symbol in a symbolic function tree.")]
|
---|
31 | public abstract class Symbol : NamedItem, ISymbol {
|
---|
32 | #region Properties
|
---|
33 | [Storable]
|
---|
34 | private double initialFrequency;
|
---|
35 | public double InitialFrequency {
|
---|
36 | get { return initialFrequency; }
|
---|
37 | set {
|
---|
38 | if (value < 0.0) throw new ArgumentException("InitialFrequency must be positive");
|
---|
39 | if (value != initialFrequency) {
|
---|
40 | initialFrequency = value;
|
---|
41 | OnChanged(EventArgs.Empty);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [Storable(DefaultValue = true)]
|
---|
47 | private bool enabled;
|
---|
48 | public virtual bool Enabled {
|
---|
49 | get { return enabled; }
|
---|
50 | set {
|
---|
51 | if (value != enabled) {
|
---|
52 | enabled = value;
|
---|
53 | OnChanged(EventArgs.Empty);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [Storable(DefaultValue = false)]
|
---|
59 | private bool @fixed;
|
---|
60 | public bool Fixed {
|
---|
61 | get { return @fixed; }
|
---|
62 | set {
|
---|
63 | if (value != @fixed) {
|
---|
64 | @fixed = value;
|
---|
65 | OnChanged(EventArgs.Empty);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override bool CanChangeName {
|
---|
71 | get { return !(this is IReadOnlySymbol); }
|
---|
72 | }
|
---|
73 | public override bool CanChangeDescription {
|
---|
74 | get { return false; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public abstract int MinimumArity { get; }
|
---|
78 | public abstract int MaximumArity { get; }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | [StorableConstructor]
|
---|
82 | protected Symbol(bool deserializing) : base(deserializing) { }
|
---|
83 | protected Symbol(Symbol original, Cloner cloner)
|
---|
84 | : base(original, cloner) {
|
---|
85 | initialFrequency = original.initialFrequency;
|
---|
86 | enabled = original.enabled;
|
---|
87 | @fixed = original.@fixed;
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected Symbol(string name, string description)
|
---|
91 | : base(name, description) {
|
---|
92 | initialFrequency = 1.0;
|
---|
93 | enabled = true;
|
---|
94 | @fixed = false;
|
---|
95 | }
|
---|
96 |
|
---|
97 | [StorableHook(HookType.AfterDeserialization)]
|
---|
98 | private void AfterDeserialization() {
|
---|
99 | // BackwardsCompatibility3.3
|
---|
100 | #region Backwards compatible code, remove with 3.4
|
---|
101 | if (initialFrequency.IsAlmost(0.0) && !(this is GroupSymbol)) enabled = false;
|
---|
102 | #endregion
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | public virtual ISymbolicExpressionTreeNode CreateTreeNode() {
|
---|
107 | return new SymbolicExpressionTreeNode(this);
|
---|
108 | }
|
---|
109 |
|
---|
110 | public virtual IEnumerable<ISymbol> Flatten() {
|
---|
111 | yield return this;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public event EventHandler Changed;
|
---|
115 | protected virtual void OnChanged(EventArgs e) {
|
---|
116 | EventHandler handlers = Changed;
|
---|
117 | if (handlers != null)
|
---|
118 | handlers(this, e);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|