Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/05/10 18:52:23 (14 years ago)
Author:
gkronber
Message:

Implemented initialization of Variable and Constant terminal nodes. #938 (Data types and operators for regression problems)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbol.cs

    r3223 r3269  
    2626using System.Linq;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Data;
     29using HeuristicLab.Parameters;
    2830
    2931namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3032  [StorableClass]
    3133  [Item("Symbol", "Represents a symbol in a symbolic function tree.")]
    32   public abstract class Symbol : Item {
    33     private List<List<Symbol>> allowedSubFunctions = new List<List<Symbol>>();
    34     private int minArity = -1;
    35     private int maxArity = -1;
    36     private double tickets = 1.0;
    37     private IOperator initializer;
    38     private IOperator manipulator;
    39     private int minTreeHeight = -1;
    40     private int minTreeSize = -1;
     34  public abstract class Symbol : ParameterizedNamedItem {
     35    #region Parameter Properties
     36    public IValueParameter<DoubleValue> TicketsParameter {
     37      get { return (IValueParameter<DoubleValue>)Parameters["Tickets"]; }
     38    }
     39    #endregion
    4140
    42     private string name;
    43     public virtual string Name {
    44       get { return name; }
     41    #region Properties
     42    public DoubleValue Tickets {
     43      get { return TicketsParameter.Value; }
    4544      set {
    46         if (string.IsNullOrEmpty(value)) throw new ArgumentException();
    47         if (value != name) {
    48           name = value;
    49         }
     45        if (value.Value < 0.0) throw new ArgumentException("Number of tickets must be positive");
     46        TicketsParameter.Value = value;
    5047      }
    5148    }
     49    #endregion
    5250
    53     protected Symbol() {
    54       name = this.GetType().Name;
    55     }
    56 
    57     public int MinSubTrees {
    58       get {
    59         return minArity;
    60       }
    61       protected internal set {
    62         if (value < 0) throw new ArgumentException();
    63         if (minArity != value) {
    64           minArity = value;
    65           while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<Symbol>());
    66           ResetCachedValues();
    67         }
    68       }
    69     }
    70 
    71     public int MaxSubTrees {
    72       get {
    73         return maxArity;
    74       }
    75       protected internal set {
    76         if (value < 0) throw new ArgumentException();
    77         if (value < minArity) throw new ArgumentException();
    78         if (value != maxArity) {
    79           maxArity = value;
    80           while (allowedSubFunctions.Count > maxArity) allowedSubFunctions.RemoveAt(allowedSubFunctions.Count - 1);
    81           while (maxArity > allowedSubFunctions.Count) {
    82             if (allowedSubFunctions.Count > 0) {
    83               // copy the list of allowed sub-functions from the previous slot
    84               allowedSubFunctions.Add(new List<Symbol>(allowedSubFunctions[allowedSubFunctions.Count - 1]));
    85             } else {
    86               // add empty list
    87               allowedSubFunctions.Add(new List<Symbol>());
    88             }
    89           }
    90           ResetCachedValues();
    91         }
    92       }
    93     }
    94 
    95 
    96     public int MinTreeSize {
    97       get {
    98         if (minTreeSize <= 0) {
    99           RecalculateMinimalTreeSize();
    100         }
    101         // Debug.Assert(minTreeSize > 0);
    102         return minTreeSize;
    103       }
    104     }
    105 
    106     public int MinTreeHeight {
    107       get {
    108         if (minTreeHeight <= 0) {
    109           RecalculateMinimalTreeHeight();
    110         }
    111         // Debug.Assert(minTreeHeight > 0);
    112         return minTreeHeight;
    113       }
    114     }
    115 
    116     public double Tickets {
    117       get { return tickets; }
    118       set {
    119         if (value < 0.0) throw new ArgumentException("Number of tickets must be positive");
    120         if (value != tickets) {
    121           tickets = value;
    122         }
    123       }
    124     }
    125 
    126     public IOperator Initializer {
    127       get { return initializer; }
    128       set {
    129         if (initializer != value) {
    130           initializer = value;
    131         }
    132       }
    133     }
    134 
    135     public IOperator Manipulator {
    136       get { return manipulator; }
    137       set {
    138         if (manipulator != value) {
    139           manipulator = value;
    140         }
    141       }
     51    protected Symbol()
     52      : base() {
     53      Parameters.Add(new ValueParameter<DoubleValue>("Tickets", new DoubleValue(1.0)));
    14254    }
    14355
     
    14557      return new SymbolicExpressionTreeNode(this);
    14658    }
    147 
    148     public IEnumerable<Symbol> GetAllowedSubFunctions(int index) {
    149       if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
    150       return allowedSubFunctions[index];
    151     }
    152 
    153     public void AddAllowedSubFunction(Symbol symbol, int index) {
    154       if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
    155       if (allowedSubFunctions[index] == null) {
    156         allowedSubFunctions[index] = new List<Symbol>();
    157       }
    158       if (!allowedSubFunctions[index].Contains(symbol)) {
    159         allowedSubFunctions[index].Add(symbol);
    160       }
    161       ResetCachedValues();
    162     }
    163     public void RemoveAllowedSubFunction(Symbol symbol, int index) {
    164       if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
    165       if (allowedSubFunctions[index].Contains(symbol)) {
    166         allowedSubFunctions[index].Remove(symbol);
    167         ResetCachedValues();
    168       }
    169     }
    170 
    171     private void ResetCachedValues() {
    172       minTreeHeight = -1;
    173       minTreeSize = -1;
    174     }
    175 
    176     public bool IsAllowedSubFunction(Symbol symbol, int index) {
    177       return GetAllowedSubFunctions(index).Contains(symbol);
    178     }
    179 
    180     private void RecalculateMinimalTreeSize() {
    181       if (MinSubTrees == 0) minTreeSize = 1;
    182       else {
    183         minTreeSize = int.MaxValue; // prevent infinite recursion       
    184         minTreeSize = 1 + (from slot in Enumerable.Range(0, MinSubTrees)
    185                            let minForSlot = (from function in GetAllowedSubFunctions(slot)
    186                                              where function != this
    187                                              select function.MinTreeSize).DefaultIfEmpty(0).Min()
    188                            select minForSlot).Sum();
    189       }
    190     }
    191 
    192     private void RecalculateMinimalTreeHeight() {
    193       if (MinSubTrees == 0) minTreeHeight = 1;
    194       else {
    195         minTreeHeight = int.MaxValue;
    196         minTreeHeight = 1 + (from slot in Enumerable.Range(0, MinSubTrees)
    197                              let minForSlot = (from function in GetAllowedSubFunctions(slot)
    198                                                where function != this
    199                                                select function.MinTreeHeight).DefaultIfEmpty(0).Min()
    200                              select minForSlot).Max();
    201       }
    202     }
    203 
    204     public override string ToString() {
    205       return name;
    206     }
    20759  }
    20860}
Note: See TracChangeset for help on using the changeset viewer.