Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjector.cs @ 4783

Last change on this file since 4783 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: 8.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 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 FunctionLibraryInjector : OperatorBase {
34    private const string TARGETVARIABLE = "TargetVariable";
35    private const string ALLOWEDFEATURES = "AllowedFeatures";
36    protected const string FUNCTIONLIBRARY = "FunctionLibrary";
37
38    public override string Description {
39      get { return @"Injects a default function library for regression and classification problems."; }
40    }
41
42    public FunctionLibraryInjector()
43      : base() {
44      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));
45      AddVariableInfo(new VariableInfo(ALLOWEDFEATURES, "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));
46      AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New));
47    }
48
49    public override IOperation Apply(IScope scope) {
50      StructId.Variable variable;
51      GPOperatorLibrary operatorLibrary;
52
53      ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>(ALLOWEDFEATURES, scope, true);
54      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
55
56      // remove the target-variable in case it occures in allowed features
57      List<IntData> ts = allowedFeatures.FindAll(d => d.Data == targetVariable);
58      foreach (IntData t in ts) allowedFeatures.Remove(t);
59
60      variable = new StructId.Variable();
61      StructId.Constant constant = new StructId.Constant();
62
63      StructId.Addition addition = new StructId.Addition();
64      StructId.And and = new StructId.And();
65      StructId.Average average = new StructId.Average();
66      StructId.Cosinus cosinus = new StructId.Cosinus();
67      StructId.Division division = new StructId.Division();
68      StructId.Equal equal = new StructId.Equal();
69      StructId.Exponential exponential = new StructId.Exponential();
70      StructId.GreaterThan greaterThan = new StructId.GreaterThan();
71      StructId.IfThenElse ifThenElse = new StructId.IfThenElse();
72      StructId.LessThan lessThan = new StructId.LessThan();
73      StructId.Logarithm logarithm = new StructId.Logarithm();
74      StructId.Multiplication multiplication = new StructId.Multiplication();
75      StructId.Not not = new StructId.Not();
76      StructId.Or or = new StructId.Or();
77      StructId.Power power = new StructId.Power();
78      StructId.Signum signum = new StructId.Signum();
79      StructId.Sinus sinus = new StructId.Sinus();
80      StructId.Sqrt sqrt = new StructId.Sqrt();
81      StructId.Subtraction subtraction = new StructId.Subtraction();
82      StructId.Tangens tangens = new StructId.Tangens();
83      StructId.Xor xor = new StructId.Xor();
84
85      IFunction[] booleanFunctions = new IFunction[] {
86        and,
87        equal,
88        greaterThan,
89        lessThan,
90        not,
91        or,
92        xor
93      };
94      IFunction[] doubleFunctions = new IFunction[] {
95        variable,
96        constant,
97        addition,
98        average,
99        cosinus,
100        division,
101        exponential,
102        ifThenElse,
103        logarithm,
104        multiplication,
105        power,
106        signum,
107        sinus,
108        sqrt,
109        subtraction,
110        tangens
111      };
112
113      SetAllowedSubOperators(and, booleanFunctions);
114      SetAllowedSubOperators(equal, doubleFunctions);
115      SetAllowedSubOperators(greaterThan, doubleFunctions);
116      SetAllowedSubOperators(lessThan, doubleFunctions);
117      SetAllowedSubOperators(not, booleanFunctions);
118      SetAllowedSubOperators(or, booleanFunctions);
119      SetAllowedSubOperators(xor, booleanFunctions);
120      SetAllowedSubOperators(addition, doubleFunctions);
121      SetAllowedSubOperators(average, doubleFunctions);
122      SetAllowedSubOperators(cosinus, doubleFunctions);
123      SetAllowedSubOperators(division, doubleFunctions);
124      SetAllowedSubOperators(exponential, doubleFunctions);
125      SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
126      SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
127      SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
128      SetAllowedSubOperators(logarithm, doubleFunctions);
129      SetAllowedSubOperators(multiplication, doubleFunctions);
130      SetAllowedSubOperators(power, doubleFunctions);
131      SetAllowedSubOperators(signum, doubleFunctions);
132      SetAllowedSubOperators(sinus, doubleFunctions);
133      SetAllowedSubOperators(sqrt, doubleFunctions);
134      SetAllowedSubOperators(subtraction, doubleFunctions);
135      SetAllowedSubOperators(tangens, doubleFunctions);
136
137      operatorLibrary = new GPOperatorLibrary();
138      operatorLibrary.GPOperatorGroup.AddOperator(variable);
139      operatorLibrary.GPOperatorGroup.AddOperator(constant);
140      operatorLibrary.GPOperatorGroup.AddOperator(addition);
141      operatorLibrary.GPOperatorGroup.AddOperator(average);
142      operatorLibrary.GPOperatorGroup.AddOperator(and);
143      operatorLibrary.GPOperatorGroup.AddOperator(cosinus);
144      operatorLibrary.GPOperatorGroup.AddOperator(division);
145      operatorLibrary.GPOperatorGroup.AddOperator(equal);
146      operatorLibrary.GPOperatorGroup.AddOperator(exponential);
147      operatorLibrary.GPOperatorGroup.AddOperator(greaterThan);
148      operatorLibrary.GPOperatorGroup.AddOperator(ifThenElse);
149      operatorLibrary.GPOperatorGroup.AddOperator(lessThan);
150      operatorLibrary.GPOperatorGroup.AddOperator(logarithm);
151      operatorLibrary.GPOperatorGroup.AddOperator(multiplication);
152      operatorLibrary.GPOperatorGroup.AddOperator(not);
153      operatorLibrary.GPOperatorGroup.AddOperator(power);
154      operatorLibrary.GPOperatorGroup.AddOperator(or);
155      operatorLibrary.GPOperatorGroup.AddOperator(signum);
156      operatorLibrary.GPOperatorGroup.AddOperator(sinus);
157      operatorLibrary.GPOperatorGroup.AddOperator(sqrt);
158      operatorLibrary.GPOperatorGroup.AddOperator(subtraction);
159      operatorLibrary.GPOperatorGroup.AddOperator(tangens);
160      operatorLibrary.GPOperatorGroup.AddOperator(xor);
161
162      int[] allowedIndexes = new int[allowedFeatures.Count];
163      for (int i = 0; i < allowedIndexes.Length; i++) {
164        allowedIndexes[i] = allowedFeatures[i].Data;
165      }
166
167      variable.SetConstraints(allowedIndexes, 0, 0);
168
169      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary));
170      return null;
171    }
172
173    private void SetAllowedSubOperators(IFunction f, IFunction[] gs) {
174      foreach (IConstraint c in f.Constraints) {
175        if (c is SubOperatorTypeConstraint) {
176          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
177          typeConstraint.Clear();
178          foreach (IFunction g in gs) {
179            typeConstraint.AddOperator(g);
180          }
181        } else if (c is AllSubOperatorsTypeConstraint) {
182          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
183          typeConstraint.Clear();
184          foreach (IFunction g in gs) {
185            typeConstraint.AddOperator(g);
186          }
187        }
188      }
189    }
190
191    private void SetAllowedSubOperators(IFunction f, int p, IFunction[] gs) {
192      foreach (IConstraint c in f.Constraints) {
193        if (c is SubOperatorTypeConstraint) {
194          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
195          if (typeConstraint.SubOperatorIndex.Data == p) {
196            typeConstraint.Clear();
197            foreach (IFunction g in gs) {
198              typeConstraint.AddOperator(g);
199            }
200          }
201        }
202      }
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.