Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Functions/FunctionBase.cs @ 4078

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

fixed two loose ends from r525 (#263):

  • update all SubOperatorTypeConstraints when an operator is removed from the operator library
  • invalidate the cached list of allowed sub functions in FunctionBase when one of the constraints is changed
File size: 5.9 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;
30using System.Diagnostics;
31
32namespace HeuristicLab.Functions {
33  /// <summary>
34  /// Functions are like operators except that they do not allow sub-operators and the normal form of evaluation
35  /// is to evaluate all children first.
36  /// </summary>
37  public abstract class FunctionBase : OperatorBase, IFunction {
38    public const string INITIALIZATION = "Initialization";
39    public const string MANIPULATION = "Manipulation";
40    private List<IFunction>[] allowedSubFunctions;
41    private int minArity = -1;
42    private int maxArity = -1;
43
44    public virtual double Apply(Dataset dataset, int sampleIndex, double[] args) {
45      throw new NotImplementedException();
46    }
47
48    public virtual void Accept(IFunctionVisitor visitor) {
49      visitor.Visit(this);
50    }
51
52    public virtual IFunctionTree GetTreeNode() {
53      return new BakedFunctionTree(this);
54    }
55
56    public int MinArity {
57      get {
58        if(minArity < 0) RefreshArity();
59        return minArity;
60      }
61    }
62
63    public int MaxArity {
64      get {
65        if(maxArity < 0) RefreshArity();
66        return maxArity;
67      }
68    }
69
70    private void RefreshArity() {
71      minArity = 2; maxArity = 2; // default arity is 2
72      foreach(IConstraint constraint in Constraints) {
73        NumberOfSubOperatorsConstraint theConstraint = constraint as NumberOfSubOperatorsConstraint;
74        if(theConstraint != null) {
75          minArity = theConstraint.MinOperators.Data;
76          maxArity = theConstraint.MaxOperators.Data;
77        }
78      }
79    }
80
81    public IList<IFunction> AllowedSubFunctions(int index) {
82      if(allowedSubFunctions == null) {
83        // first time: analyze the constraint and create a cached copy of the allowed sub-functions
84        allowedSubFunctions = new List<IFunction>[MaxArity];
85        for(int i = 0; i < MaxArity; i++) {
86          allowedSubFunctions[i] = GetAllowedSubFunctions(i);
87        }
88      }
89      return allowedSubFunctions[index];
90    }
91
92    private List<IFunction> GetAllowedSubFunctions(int index) {
93      List<IFunction> allowedSubFunctions = new List<IFunction>();
94      foreach(IConstraint constraint in Constraints) {
95        if(constraint is SubOperatorTypeConstraint) {
96          SubOperatorTypeConstraint subOpConstraint = constraint as SubOperatorTypeConstraint;
97          if(subOpConstraint.SubOperatorIndex.Data == index) {
98            foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions.Add(f);
99            subOpConstraint.Changed += new EventHandler(subOpConstraint_Changed); // register an event-handler to invalidate the cache on constraing changes
100            return allowedSubFunctions;
101          }
102        } else if(constraint is AllSubOperatorsTypeConstraint) {
103          AllSubOperatorsTypeConstraint subOpConstraint = constraint as AllSubOperatorsTypeConstraint;
104          foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions.Add(f);
105          subOpConstraint.Changed += new EventHandler(subOpConstraint_Changed); // register an event-handler to invalidate the cache on constraint changes
106          return allowedSubFunctions;
107        }
108      }
109      return allowedSubFunctions;
110    }
111
112    private void subOpConstraint_Changed(object sender, EventArgs e) {
113      allowedSubFunctions = null;
114    }
115
116    // operator-tree style evaluation is not supported for functions.
117    public override IOperation Apply(IScope scope) {
118      throw new NotSupportedException();
119    }
120
121    private static readonly List<IOperator> emptySubOperatorList = new List<IOperator>();
122    public override IList<IOperator> SubOperators {
123      get { return emptySubOperatorList; }
124    }
125
126    public override void AddSubOperator(IOperator subOperator) {
127      throw new NotSupportedException();
128    }
129
130    public override bool TryAddSubOperator(IOperator subOperator) {
131      throw new NotSupportedException();
132    }
133
134    public override bool TryAddSubOperator(IOperator subOperator, int index) {
135      throw new NotSupportedException();
136    }
137
138    public override bool TryAddSubOperator(IOperator subOperator, int index, out ICollection<IConstraint> violatedConstraints) {
139      throw new NotSupportedException();
140    }
141
142    public override bool TryAddSubOperator(IOperator subOperator, out ICollection<IConstraint> violatedConstraints) {
143      throw new NotSupportedException();
144    }
145
146    public override void AddSubOperator(IOperator subOperator, int index) {
147      throw new NotSupportedException();
148    }
149
150    public override void RemoveSubOperator(int index) {
151      throw new NotSupportedException();
152    }
153
154    public override bool TryRemoveSubOperator(int index) {
155      throw new NotSupportedException();
156    }
157
158    public override bool TryRemoveSubOperator(int index, out ICollection<IConstraint> violatedConstraints) {
159      throw new NotSupportedException();
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.