[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[645] | 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;
|
---|
[6803] | 23 | using System.Collections.Generic;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[645] | 25 | using HeuristicLab.Core;
|
---|
[16565] | 26 | using HEAL.Attic;
|
---|
[645] | 27 |
|
---|
[5499] | 28 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[16565] | 29 | [StorableType("8ECC852D-3FE4-4AD8-9F70-582C7F874AE6")]
|
---|
[3223] | 30 | [Item("Symbol", "Represents a symbol in a symbolic function tree.")]
|
---|
[5499] | 31 | public abstract class Symbol : NamedItem, ISymbol {
|
---|
[3269] | 32 | #region Properties
|
---|
[3484] | 33 | [Storable]
|
---|
[3294] | 34 | private double initialFrequency;
|
---|
| 35 | public double InitialFrequency {
|
---|
| 36 | get { return initialFrequency; }
|
---|
[2202] | 37 | set {
|
---|
[3294] | 38 | if (value < 0.0) throw new ArgumentException("InitialFrequency must be positive");
|
---|
[3824] | 39 | if (value != initialFrequency) {
|
---|
| 40 | initialFrequency = value;
|
---|
| 41 | OnChanged(EventArgs.Empty);
|
---|
| 42 | }
|
---|
[645] | 43 | }
|
---|
| 44 | }
|
---|
[6803] | 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 |
|
---|
[3824] | 70 | public override bool CanChangeName {
|
---|
[6233] | 71 | get { return !(this is IReadOnlySymbol); }
|
---|
[3824] | 72 | }
|
---|
[5635] | 73 | public override bool CanChangeDescription {
|
---|
| 74 | get { return false; }
|
---|
| 75 | }
|
---|
[6803] | 76 |
|
---|
| 77 | public abstract int MinimumArity { get; }
|
---|
| 78 | public abstract int MaximumArity { get; }
|
---|
[3269] | 79 | #endregion
|
---|
[645] | 80 |
|
---|
[4722] | 81 | [StorableConstructor]
|
---|
[16565] | 82 | protected Symbol(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 83 | protected Symbol(Symbol original, Cloner cloner)
|
---|
| 84 | : base(original, cloner) {
|
---|
| 85 | initialFrequency = original.initialFrequency;
|
---|
[6803] | 86 | enabled = original.enabled;
|
---|
| 87 | @fixed = original.@fixed;
|
---|
[4722] | 88 | }
|
---|
[645] | 89 |
|
---|
[3993] | 90 | protected Symbol(string name, string description)
|
---|
| 91 | : base(name, description) {
|
---|
| 92 | initialFrequency = 1.0;
|
---|
[6803] | 93 | enabled = true;
|
---|
| 94 | @fixed = false;
|
---|
[3993] | 95 | }
|
---|
| 96 |
|
---|
[6803] | 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 |
|
---|
[5510] | 106 | public virtual ISymbolicExpressionTreeNode CreateTreeNode() {
|
---|
[3223] | 107 | return new SymbolicExpressionTreeNode(this);
|
---|
[645] | 108 | }
|
---|
[3484] | 109 |
|
---|
[6803] | 110 | public virtual IEnumerable<ISymbol> Flatten() {
|
---|
| 111 | yield return this;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[3824] | 114 | public event EventHandler Changed;
|
---|
[6803] | 115 | protected virtual void OnChanged(EventArgs e) {
|
---|
[3824] | 116 | EventHandler handlers = Changed;
|
---|
| 117 | if (handlers != null)
|
---|
| 118 | handlers(this, e);
|
---|
| 119 | }
|
---|
[645] | 120 | }
|
---|
| 121 | }
|
---|