Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.GP.StructureIdentification/3.4/ConfigurableFunctionLibraryInjector.cs @ 3962

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

Worked on different dispatching of deterministic and non-deterministic modeling algorithms. #635

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