Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification.Classification/FunctionLibraryInjector.cs @ 647

Last change on this file since 647 was 646, checked in by gkronber, 16 years ago

renamed directories (#177)

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