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