Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.GP.StructureIdentification/3.3/ConfigurableFunctionLibraryInjector.cs @ 1835

Last change on this file since 1835 was 1540, checked in by mkommend, 16 years ago

initial version of ConfigurableFunctionLibraryInjector (ticket #579)
discuss with GK if it should be changed to use reflection

File size: 13.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 System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using HeuristicLab.Constraints;
30using StructId = HeuristicLab.GP.StructureIdentification;
31
32namespace HeuristicLab.GP.StructureIdentification {
33  public class ConfigurableFunctionLibraryInjector : FunctionLibraryInjector {
34    private const string VARIABLES_ALLOWED = "Variables";
35    private const string CONSTANTS_ALLOWED = "Constants";
36    private const string ADDITION_ALLOWED = "Addition";
37    private const string AVERAGE_ALLOWED = "Average";
38    private const string AND_ALLOWED = "And";
39    private const string COSINUS_ALLOWED = "Cosinus";
40    private const string DIVISION_ALLOWED = "Division";
41    private const string EQUAL_ALLOWED = "Equal";
42    private const string EXPONENTIAL_ALLOWED = "Exponential";
43    private const string GREATERTHAN_ALLOWED = "GreaterThan";
44    private const string IFTHENELSE_ALLOWED = "IfThenElse";
45    private const string LESSTHAN_ALLOWED = "LessThan";
46    private const string LOGARTIHM_ALLOWED = "Logarithm";
47    private const string MULTIPLICATION_ALLOWED = "Multiplication";
48    private const string NOT_ALLOWED = "Not";
49    private const string POWER_ALLOWED = "Power";
50    private const string OR_ALLOWED = "Or";
51    private const string SIGNUM_ALLOWED = "Signum";
52    private const string SINUS_ALLOWED = "Sinus";
53    private const string SQRT_ALLOWED = "SquareRoot";
54    private const string SUBTRACTION_ALLOWED = "Subtraction";
55    private const string TANGENS_ALLOWED = "Tangens";
56    private const string XOR_ALLOWED = "Xor";
57
58
59    public override string Description {
60      get { return @"Injects a configurable function library for regression and classification problems."; }
61    }
62
63    public ConfigurableFunctionLibraryInjector()
64      : base() {
65
66      AddVariableInfo(new Core.VariableInfo(VARIABLES_ALLOWED, VARIABLES_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
67      GetVariableInfo(VARIABLES_ALLOWED).Local = true;
68      AddVariable(new Core.Variable(VARIABLES_ALLOWED,  new BoolData(true)));
69
70      AddVariableInfo(new Core.VariableInfo(CONSTANTS_ALLOWED, CONSTANTS_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
71      GetVariableInfo(CONSTANTS_ALLOWED).Local = true;
72      AddVariable(new Core.Variable(CONSTANTS_ALLOWED,  new BoolData(true)));
73
74      AddVariableInfo(new Core.VariableInfo(ADDITION_ALLOWED, ADDITION_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
75      GetVariableInfo(ADDITION_ALLOWED).Local = true;
76      AddVariable(new Core.Variable(ADDITION_ALLOWED,  new BoolData(true)));
77
78      AddVariableInfo(new Core.VariableInfo(AVERAGE_ALLOWED, AVERAGE_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
79      GetVariableInfo(AVERAGE_ALLOWED).Local = true;
80      AddVariable(new Core.Variable(AVERAGE_ALLOWED,  new BoolData(true)));
81
82      AddVariableInfo(new Core.VariableInfo(AND_ALLOWED, AND_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
83      GetVariableInfo(AND_ALLOWED).Local = true;
84      AddVariable(new Core.Variable(AND_ALLOWED,  new BoolData(true)));
85
86      AddVariableInfo(new Core.VariableInfo(COSINUS_ALLOWED, COSINUS_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
87      GetVariableInfo(COSINUS_ALLOWED).Local = true;
88      AddVariable(new Core.Variable(COSINUS_ALLOWED,  new BoolData(true)));
89
90      AddVariableInfo(new Core.VariableInfo(DIVISION_ALLOWED, DIVISION_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
91      GetVariableInfo(DIVISION_ALLOWED).Local = true;
92      AddVariable(new Core.Variable(DIVISION_ALLOWED,  new BoolData(true)));
93
94      AddVariableInfo(new Core.VariableInfo(EQUAL_ALLOWED, EQUAL_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
95      GetVariableInfo(EQUAL_ALLOWED).Local = true;
96      AddVariable(new Core.Variable(EQUAL_ALLOWED,  new BoolData(true)));
97
98      AddVariableInfo(new Core.VariableInfo(EXPONENTIAL_ALLOWED, EXPONENTIAL_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
99      GetVariableInfo(EXPONENTIAL_ALLOWED).Local = true;
100      AddVariable(new Core.Variable(EXPONENTIAL_ALLOWED,  new BoolData(true)));
101
102      AddVariableInfo(new Core.VariableInfo(GREATERTHAN_ALLOWED, GREATERTHAN_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
103      GetVariableInfo(GREATERTHAN_ALLOWED).Local = true;
104      AddVariable(new Core.Variable(GREATERTHAN_ALLOWED,  new BoolData(true)));
105
106      AddVariableInfo(new Core.VariableInfo(IFTHENELSE_ALLOWED, IFTHENELSE_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
107      GetVariableInfo(IFTHENELSE_ALLOWED).Local = true;
108      AddVariable(new Core.Variable(IFTHENELSE_ALLOWED,  new BoolData(true)));
109
110      AddVariableInfo(new Core.VariableInfo(LESSTHAN_ALLOWED, LESSTHAN_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
111      GetVariableInfo(LESSTHAN_ALLOWED).Local = true;
112      AddVariable(new Core.Variable(LESSTHAN_ALLOWED,  new BoolData(true)));
113
114      AddVariableInfo(new Core.VariableInfo(LOGARTIHM_ALLOWED, LOGARTIHM_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
115      GetVariableInfo(LOGARTIHM_ALLOWED).Local = true;
116      AddVariable(new Core.Variable(LOGARTIHM_ALLOWED,  new BoolData(true)));
117
118      AddVariableInfo(new Core.VariableInfo(MULTIPLICATION_ALLOWED, MULTIPLICATION_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
119      GetVariableInfo(MULTIPLICATION_ALLOWED).Local = true;
120      AddVariable(new Core.Variable(MULTIPLICATION_ALLOWED,  new BoolData(true)));
121
122      AddVariableInfo(new Core.VariableInfo(NOT_ALLOWED, NOT_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
123      GetVariableInfo(NOT_ALLOWED).Local = true;
124      AddVariable(new Core.Variable(NOT_ALLOWED,  new BoolData(true)));
125
126      AddVariableInfo(new Core.VariableInfo(POWER_ALLOWED, POWER_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
127      GetVariableInfo(POWER_ALLOWED).Local = true;
128      AddVariable(new Core.Variable(POWER_ALLOWED,  new BoolData(true)));
129
130      AddVariableInfo(new Core.VariableInfo(OR_ALLOWED, OR_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
131      GetVariableInfo(OR_ALLOWED).Local = true;
132      AddVariable(new Core.Variable(OR_ALLOWED,  new BoolData(true)));
133
134      AddVariableInfo(new Core.VariableInfo(SIGNUM_ALLOWED, SIGNUM_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
135      GetVariableInfo(SIGNUM_ALLOWED).Local = true;
136      AddVariable(new Core.Variable(SIGNUM_ALLOWED,  new BoolData(true)));
137
138      AddVariableInfo(new Core.VariableInfo(SINUS_ALLOWED, SINUS_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
139      GetVariableInfo(SINUS_ALLOWED).Local = true;
140      AddVariable(new Core.Variable(SINUS_ALLOWED,  new BoolData(true)));
141
142      AddVariableInfo(new Core.VariableInfo(SQRT_ALLOWED, SQRT_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
143      GetVariableInfo(SQRT_ALLOWED).Local = true;
144      AddVariable(new Core.Variable(SQRT_ALLOWED, new BoolData(true)));
145
146      AddVariableInfo(new Core.VariableInfo(SUBTRACTION_ALLOWED, SUBTRACTION_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
147      GetVariableInfo(SUBTRACTION_ALLOWED).Local = true;
148      AddVariable(new Core.Variable(SUBTRACTION_ALLOWED,  new BoolData(true)));
149
150      AddVariableInfo(new Core.VariableInfo(TANGENS_ALLOWED, TANGENS_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
151      GetVariableInfo(TANGENS_ALLOWED).Local = true;
152      AddVariable(new Core.Variable(TANGENS_ALLOWED, new BoolData(true)));
153
154      AddVariableInfo(new Core.VariableInfo(XOR_ALLOWED, XOR_ALLOWED + " allowed", typeof(BoolData), Core.VariableKind.New));
155      GetVariableInfo(XOR_ALLOWED).Local = true;
156      AddVariable(new Core.Variable(XOR_ALLOWED,  new BoolData(true)));
157    }
158
159    //public override IView CreateView() {
160    //  return new ConfigurableFunctionLibraryInjectorView(this);
161    //}
162
163    public override IOperation Apply(IScope scope) {
164      base.Apply(scope);
165
166      GPOperatorLibrary functionLibrary = (GPOperatorLibrary)scope.GetVariable(scope.TranslateName(FUNCTIONLIBRARY)).Value;
167
168      if (!((BoolData)GetVariable(VARIABLES_ALLOWED).Value).Data)
169        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Variable)));
170      if (!((BoolData)GetVariable(CONSTANTS_ALLOWED).Value).Data)
171        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Constant)));
172      if (!((BoolData)GetVariable(ADDITION_ALLOWED).Value).Data)
173        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Addition)));
174      if (!((BoolData)GetVariable(AVERAGE_ALLOWED).Value).Data)
175        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Average)));
176      if (!((BoolData)GetVariable(AND_ALLOWED).Value).Data)
177        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(And)));
178      if (!((BoolData)GetVariable(COSINUS_ALLOWED).Value).Data)
179        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Cosinus)));
180      if (!((BoolData)GetVariable(DIVISION_ALLOWED).Value).Data)
181        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Division)));
182      if (!((BoolData)GetVariable(EQUAL_ALLOWED).Value).Data)                                                       
183        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Equal)));
184      if (!((BoolData)GetVariable(EXPONENTIAL_ALLOWED).Value).Data)
185        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Exponential)));
186      if (!((BoolData)GetVariable(GREATERTHAN_ALLOWED).Value).Data)
187        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(GreaterThan)));
188      if (!((BoolData)GetVariable(IFTHENELSE_ALLOWED).Value).Data)
189        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(IfThenElse)));
190      if (!((BoolData)GetVariable(LESSTHAN_ALLOWED).Value).Data)
191        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(LessThan)));
192      if (!((BoolData)GetVariable(LOGARTIHM_ALLOWED).Value).Data)
193        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Logarithm)));
194      if (!((BoolData)GetVariable(MULTIPLICATION_ALLOWED).Value).Data)
195        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Multiplication)));
196      if (!((BoolData)GetVariable(NOT_ALLOWED).Value).Data)
197        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Not)));
198      if (!((BoolData)GetVariable(POWER_ALLOWED).Value).Data)
199        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Power)));
200      if (!((BoolData)GetVariable(OR_ALLOWED).Value).Data)
201        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Or)));
202      if (!((BoolData)GetVariable(SIGNUM_ALLOWED).Value).Data)
203        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Signum)));
204      if (!((BoolData)GetVariable(SINUS_ALLOWED).Value).Data)
205        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Sinus)));
206      if (!((BoolData)GetVariable(SQRT_ALLOWED).Value).Data)
207        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Sqrt)));
208      if (!((BoolData)GetVariable(SUBTRACTION_ALLOWED).Value).Data)
209        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Subtraction)));
210      if (!((BoolData)GetVariable(TANGENS_ALLOWED).Value).Data)
211        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Tangens)));
212      if (!((BoolData)GetVariable(XOR_ALLOWED).Value).Data)
213        functionLibrary.GPOperatorGroup.RemoveOperator(FindOperator(functionLibrary.GPOperatorGroup, typeof(Xor)));
214
215
216      return null;
217    }
218
219    private IFunction FindOperator(GPOperatorGroup g, Type t)  {
220      foreach (IFunction func in g.Operators)
221        if (func.GetType() == t)
222          return func;
223      return null;
224  }
225
226
227  }
228}
Note: See TracBrowser for help on using the repository browser.