Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Functions/FunctionBase.cs @ 528

Last change on this file since 528 was 528, checked in by gkronber, 16 years ago

fixed a bug introduced with r525 (#263)

File size: 5.3 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.DataAnalysis;
29using HeuristicLab.Constraints;
30
31namespace HeuristicLab.Functions {
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;
40    private int minArity = -1;
41    private int maxArity = -1;
42
43    public virtual double Apply(Dataset dataset, int sampleIndex, double[] args) {
44      throw new NotImplementedException();
45    }
46
47    public virtual void Accept(IFunctionVisitor visitor) {
48      visitor.Visit(this);
49    }
50
51    public virtual IFunctionTree GetTreeNode() {
52      return new BakedFunctionTree(this);
53    }
54
55    public int MinArity {
56      get {
57        if(minArity < 0) RefreshArity();
58        return minArity;
59      }
60    }
61
62    public int MaxArity {
63      get {
64        if(maxArity < 0) RefreshArity();
65        return maxArity;
66      }
67    }
68
69    private void RefreshArity() {
70      minArity = 2; maxArity = 2; // default arity is 2
71      foreach(IConstraint constraint in Constraints) {
72        NumberOfSubOperatorsConstraint theConstraint = constraint as NumberOfSubOperatorsConstraint;
73        if(theConstraint != null) {
74          minArity = theConstraint.MinOperators.Data;
75          maxArity = theConstraint.MaxOperators.Data;
76        }
77      }
78    }
79
80    public IList<IFunction> AllowedSubFunctions(int index) {
81      if(allowedSubFunctions == null) {
82        allowedSubFunctions = new List<IFunction>[MaxArity];
83        for(int i = 0; i < MaxArity; i++) {
84          foreach(IConstraint constraint in Constraints) {
85            if(constraint is SubOperatorTypeConstraint) {
86              SubOperatorTypeConstraint subOpConstraint = constraint as SubOperatorTypeConstraint;
87              if(subOpConstraint.SubOperatorIndex.Data == i) {
88                allowedSubFunctions[i] = new List<IFunction>();
89                foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions[i].Add(f);
90                break;
91              }
92            } else if(constraint is AllSubOperatorsTypeConstraint) {
93              AllSubOperatorsTypeConstraint subOpConstraint = constraint as AllSubOperatorsTypeConstraint;
94              allowedSubFunctions[i] = new List<IFunction>();
95              foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions[i].Add(f);
96              break;
97            }
98          }
99        }
100      }
101      return allowedSubFunctions[index];
102    }
103
104    // operator-tree style evaluation is not supported for functions.
105    public override IOperation Apply(IScope scope) {
106      throw new NotSupportedException();
107    }
108
109    private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>();
110    public override IList<IOperator> SubOperators {
111      get { return emptySubOperatorList; }
112    }
113
114    public override void AddSubOperator(IOperator subOperator) {
115      throw new NotSupportedException();
116    }
117
118    public override bool TryAddSubOperator(IOperator subOperator) {
119      throw new NotSupportedException();
120    }
121
122    public override bool TryAddSubOperator(IOperator subOperator, int index) {
123      throw new NotSupportedException();
124    }
125
126    public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) {
127      throw new NotSupportedException();
128    }
129
130    public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) {
131      throw new NotSupportedException();
132    }
133
134    public override void AddSubOperator(IOperator subOperator, int index) {
135      throw new NotSupportedException();
136    }
137
138    public override void RemoveSubOperator(int index) {
139      throw new NotSupportedException();
140    }
141
142    public override bool TryRemoveSubOperator(int index) {
143      throw new NotSupportedException();
144    }
145
146    public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) {
147      throw new NotSupportedException();
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.