Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.GP/3.3/FunctionBase.cs @ 5740

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

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 5.7 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  /// <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() {
44      throw new NotImplementedException();
45    }
46
47    public virtual IFunctionTree GetTreeNode() {
48      return new BakedFunctionTree(this);
49    }
50
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;
72        }
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);
82        }
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();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.