Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionBase.cs @ 2202

Last change on this file since 2202 was 2202, checked in by gkronber, 15 years ago

Created a branch for #713

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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 System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Data;
26using HeuristicLab.Core;
27using System.Xml;
28using HeuristicLab.Constraints;
29using System.Diagnostics;
30
31namespace HeuristicLab.GP {
32  public abstract class FunctionBase : ItemBase, IFunction {
33    private List<List<IFunction>> allowedSubFunctions = new List<List<IFunction>>();
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;
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]; }
106    }
107
108    public virtual IFunctionTree GetTreeNode() {
109      return new BakedFunctionTree(this);
110    }
111
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);
195        }
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);
208        }
209        height = Math.Max(height, minHeight);
210      }
211      minTreeHeight = height + 1;
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.