- Timestamp:
- 07/28/09 19:24:23 (15 years ago)
- Location:
- branches/GP-Refactoring-713
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionBase.cs
r1529 r2202 30 30 31 31 namespace HeuristicLab.GP { 32 /// <summary> 33 /// Functions are like operators except that they do not allow sub-operators and the normal form of evaluation 34 /// is to evaluate all children first. 35 /// </summary> 36 public abstract class FunctionBase : OperatorBase, IFunction { 37 public const string INITIALIZATION = "Initialization"; 38 public const string MANIPULATION = "Manipulation"; 39 private List<IFunction>[] allowedSubFunctions; 32 public abstract class FunctionBase : ItemBase, IFunction { 33 private List<List<IFunction>> allowedSubFunctions = new List<List<IFunction>>(); 40 34 private int minArity = -1; 41 35 private int maxArity = -1; 42 43 public virtual double Apply() { 44 throw new NotImplementedException(); 36 private double tickets = 1.0; 37 private IOperator initializer; 38 private IOperator manipulator; 39 private int minTreeHeight = -1; 40 private int minTreeSize = -1; 41 42 public virtual string Name { 43 get { return this.GetType().Name; } 44 } 45 46 public virtual string Description { 47 get { return "Description for this function is missing (TODO)"; } 48 } 49 50 public int MinArity { 51 get { 52 return minArity; 53 } 54 protected set { 55 minArity = value; 56 while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<IFunction>()); 57 } 58 } 59 60 public int MaxArity { 61 get { 62 return maxArity; 63 } 64 protected set { 65 maxArity = value; 66 while (allowedSubFunctions.Count > maxArity) allowedSubFunctions.RemoveAt(allowedSubFunctions.Count - 1); 67 while (maxArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<IFunction>()); 68 } 69 } 70 71 72 public int MinTreeSize { 73 get { 74 if (minTreeSize <= 0) RecalculateMinimalTreeSize(); 75 return minTreeSize; 76 } 77 } 78 79 public int MinTreeHeight { 80 get { 81 if (minTreeHeight <= 0) RecalculateMinimalTreeHeight(); 82 return minTreeHeight; 83 } 84 } 85 86 public double Tickets { 87 get { return tickets; } 88 set { 89 if (value < 0.0) throw new ArgumentException("Number of tickets must be positive"); 90 else tickets = value; 91 } 92 } 93 94 public IOperator Initializer { 95 get { return initializer; } 96 set { initializer = value; } 97 } 98 99 public IOperator Manipulator { 100 get { return manipulator; } 101 set { manipulator = value; } 102 } 103 104 public virtual IEnumerable<string> LocalParameterNames { 105 get { return new string[0]; } 45 106 } 46 107 … … 49 110 } 50 111 51 public int MinArity { 52 get { 53 if(minArity < 0) RefreshArity(); 54 return minArity; 55 } 56 } 57 58 public int MaxArity { 59 get { 60 if(maxArity < 0) RefreshArity(); 61 return maxArity; 62 } 63 } 64 65 private void RefreshArity() { 66 minArity = 2; maxArity = 2; // default arity is 2 67 foreach(IConstraint constraint in Constraints) { 68 NumberOfSubOperatorsConstraint theConstraint = constraint as NumberOfSubOperatorsConstraint; 69 if(theConstraint != null) { 70 minArity = theConstraint.MinOperators.Data; 71 maxArity = theConstraint.MaxOperators.Data; 112 113 //private List<IConstraint> constraints = new List<IConstraint>(); 114 //public ICollection<IConstraint> Constraints { 115 // get { return constraints; } 116 //} 117 118 //private void RefreshArity() { 119 // minArity = 2; maxArity = 2; // default arity is 2 120 // foreach (IConstraint constraint in Constraints) { 121 // NumberOfSubOperatorsConstraint theConstraint = constraint as NumberOfSubOperatorsConstraint; 122 // if (theConstraint != null) { 123 // minArity = theConstraint.MinOperators.Data; 124 // maxArity = theConstraint.MaxOperators.Data; 125 // } 126 // } 127 //} 128 129 public ICollection<IFunction> GetAllowedSubFunctions(int index) { 130 if (index < 0 || index > MaxArity) throw new ArgumentException("Index outside of allowed range. index = " + index); 131 //if (allowedSubFunctions == null) { 132 // // first time: analyze the constraint and create a cached copy of the allowed sub-functions 133 // allowedSubFunctions = new List<IFunction>[MaxArity]; 134 // for (int i = 0; i < MaxArity; i++) { 135 // allowedSubFunctions[i] = GetAllowedSubFunctions(i); 136 // } 137 //} 138 return allowedSubFunctions[index]; 139 } 140 141 public void AddAllowedSubFunction(IFunction function, int index) { 142 if (index < 0 || index > MaxArity) throw new ArgumentException("Index outside of allowed range. index = " + index); 143 if (allowedSubFunctions[index] == null) { 144 allowedSubFunctions[index] = new List<IFunction>(); 145 } 146 if (!allowedSubFunctions[index].Contains(function)) { 147 allowedSubFunctions[index].Add(function); 148 } 149 } 150 151 public void RemoveAllowedSubFunction(IFunction function, int index) { 152 if (index < 0 || index > MaxArity) throw new ArgumentException("Index outside of allowed range. index = " + index); 153 allowedSubFunctions[index].Add(function); 154 } 155 156 public bool IsAllowedSubFunction(IFunction function, int index) { 157 return GetAllowedSubFunctions(index).Contains(function); 158 } 159 160 //private List<IFunction> GetAllowedSubFunctions(int index) { 161 // List<IFunction> allowedSubFunctions = new List<IFunction>(); 162 // foreach (IConstraint constraint in Constraints) { 163 // if (constraint is SubOperatorTypeConstraint) { 164 // SubOperatorTypeConstraint subOpConstraint = constraint as SubOperatorTypeConstraint; 165 // if (subOpConstraint.SubOperatorIndex.Data == index) { 166 // foreach (IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions.Add(f); 167 // subOpConstraint.Changed += new EventHandler(subOpConstraint_Changed); // register an event-handler to invalidate the cache on constraint changes 168 // return allowedSubFunctions; 169 // } 170 // } else if (constraint is AllSubOperatorsTypeConstraint) { 171 // AllSubOperatorsTypeConstraint subOpConstraint = constraint as AllSubOperatorsTypeConstraint; 172 // foreach (IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions.Add(f); 173 // subOpConstraint.Changed += new EventHandler(subOpConstraint_Changed); // register an event-handler to invalidate the cache on constraint changes 174 // return allowedSubFunctions; 175 // } 176 // } 177 // return allowedSubFunctions; 178 //} 179 180 //private void subOpConstraint_Changed(object sender, EventArgs e) { 181 // allowedSubFunctions = null; 182 //} 183 184 public override IView CreateView() { 185 return new FunView(this); 186 } 187 188 private void RecalculateMinimalTreeSize() { 189 minTreeSize = int.MaxValue; 190 int sum = 1; 191 int minSize = int.MaxValue; 192 for (int i = 0; i < MinArity; i++) { 193 foreach (IFunction subFun in GetAllowedSubFunctions(i)) { 194 minSize = Math.Min(minSize, subFun.MinTreeSize); 72 195 } 73 } 74 } 75 76 public IList<IFunction> AllowedSubFunctions(int index) { 77 if(allowedSubFunctions == null) { 78 // first time: analyze the constraint and create a cached copy of the allowed sub-functions 79 allowedSubFunctions = new List<IFunction>[MaxArity]; 80 for(int i = 0; i < MaxArity; i++) { 81 allowedSubFunctions[i] = GetAllowedSubFunctions(i); 196 sum += minSize; 197 } 198 minTreeSize = sum; 199 } 200 201 private void RecalculateMinimalTreeHeight() { 202 minTreeHeight = int.MaxValue; 203 int height = 0; 204 int minHeight = int.MaxValue; 205 for (int i = 0; i < MinArity; i++) { 206 foreach (IFunction subFun in GetAllowedSubFunctions(i)) { 207 minHeight = Math.Min(minHeight, subFun.MinTreeHeight); 82 208 } 83 } 84 return allowedSubFunctions[index]; 85 } 86 87 private List<IFunction> GetAllowedSubFunctions(int index) { 88 List<IFunction> allowedSubFunctions = new List<IFunction>(); 89 foreach(IConstraint constraint in Constraints) { 90 if(constraint is SubOperatorTypeConstraint) { 91 SubOperatorTypeConstraint subOpConstraint = constraint as SubOperatorTypeConstraint; 92 if(subOpConstraint.SubOperatorIndex.Data == index) { 93 foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions.Add(f); 94 subOpConstraint.Changed += new EventHandler(subOpConstraint_Changed); // register an event-handler to invalidate the cache on constraing changes 95 return allowedSubFunctions; 96 } 97 } else if(constraint is AllSubOperatorsTypeConstraint) { 98 AllSubOperatorsTypeConstraint subOpConstraint = constraint as AllSubOperatorsTypeConstraint; 99 foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions.Add(f); 100 subOpConstraint.Changed += new EventHandler(subOpConstraint_Changed); // register an event-handler to invalidate the cache on constraint changes 101 return allowedSubFunctions; 102 } 103 } 104 return allowedSubFunctions; 105 } 106 107 private void subOpConstraint_Changed(object sender, EventArgs e) { 108 allowedSubFunctions = null; 109 } 110 111 // operator-tree style evaluation is not supported for functions. 112 public override IOperation Apply(IScope scope) { 113 throw new NotSupportedException(); 114 } 115 116 private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>(); 117 public override IList<IOperator> SubOperators { 118 get { return emptySubOperatorList; } 119 } 120 121 public override void AddSubOperator(IOperator subOperator) { 122 throw new NotSupportedException(); 123 } 124 125 public override bool TryAddSubOperator(IOperator subOperator) { 126 throw new NotSupportedException(); 127 } 128 129 public override bool TryAddSubOperator(IOperator subOperator, int index) { 130 throw new NotSupportedException(); 131 } 132 133 public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) { 134 throw new NotSupportedException(); 135 } 136 137 public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) { 138 throw new NotSupportedException(); 139 } 140 141 public override void AddSubOperator(IOperator subOperator, int index) { 142 throw new NotSupportedException(); 143 } 144 145 public override void RemoveSubOperator(int index) { 146 throw new NotSupportedException(); 147 } 148 149 public override bool TryRemoveSubOperator(int index) { 150 throw new NotSupportedException(); 151 } 152 153 public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) { 154 throw new NotSupportedException(); 209 height = Math.Max(height, minHeight); 210 } 211 minTreeHeight = height + 1; 155 212 } 156 213 }
Note: See TracChangeset
for help on using the changeset viewer.