Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/FunctionLibraryInjector.cs @ 1000

Last change on this file since 1000 was 674, checked in by gkronber, 16 years ago

fixed #309 (FunctionLibraryInjector does not set constraints for variables)

File size: 8.7 KB
RevLine 
[645]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
[668]32namespace HeuristicLab.GP.StructureIdentification.Classification {
[645]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
[674]67      variable.SetConstraints(allowedIndexes, 0, 0);
68
[668]69      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary));
[645]70      return null;
71    }
72
73    private void InitDefaultOperatorLibrary() {
74      variable = new StructId.Variable();
75      StructId.Constant constant = new StructId.Constant();
76
77      StructId.Addition addition = new StructId.Addition();
78      StructId.And and = new StructId.And();
79      StructId.Average average = new StructId.Average();
80      StructId.Cosinus cosinus = new StructId.Cosinus();
81      StructId.Division division = new StructId.Division();
82      StructId.Equal equal = new StructId.Equal();
83      StructId.Exponential exponential = new StructId.Exponential();
84      StructId.GreaterThan greaterThan = new StructId.GreaterThan();
85      StructId.IfThenElse ifThenElse = new StructId.IfThenElse();
86      StructId.LessThan lessThan = new StructId.LessThan();
87      StructId.Logarithm logarithm = new StructId.Logarithm();
88      StructId.Multiplication multiplication = new StructId.Multiplication();
89      StructId.Not not = new StructId.Not();
90      StructId.Or or = new StructId.Or();
91      StructId.Power power = new StructId.Power();
92      StructId.Signum signum = new StructId.Signum();
93      StructId.Sinus sinus = new StructId.Sinus();
94      StructId.Sqrt sqrt = new StructId.Sqrt();
95      StructId.Subtraction subtraction = new StructId.Subtraction();
96      StructId.Tangens tangens = new StructId.Tangens();
97      StructId.Xor xor = new StructId.Xor();
98
99      IFunction[] booleanFunctions = new IFunction[] {
100        and,
101        equal,
102        greaterThan,
103        lessThan,
104        not,
105        or,
106        xor
107      };
108      IFunction[] doubleFunctions = new IFunction[] {
109        variable,
110        constant,
111        addition,
112        average,
113        cosinus,
114        division,
115        exponential,
116        ifThenElse,
117        logarithm,
118        multiplication,
119        power,
120        signum,
121        sinus,
122        sqrt,
123        subtraction,
124        tangens
125      };
126
127      SetAllowedSubOperators(and, booleanFunctions);
128      SetAllowedSubOperators(equal, doubleFunctions);
129      SetAllowedSubOperators(greaterThan, doubleFunctions);
130      SetAllowedSubOperators(lessThan, doubleFunctions);
131      SetAllowedSubOperators(not, booleanFunctions);
132      SetAllowedSubOperators(or, booleanFunctions);
133      SetAllowedSubOperators(xor, booleanFunctions);
134      SetAllowedSubOperators(addition, doubleFunctions);
135      SetAllowedSubOperators(average, doubleFunctions);
136      SetAllowedSubOperators(cosinus, doubleFunctions);
137      SetAllowedSubOperators(division, doubleFunctions);
138      SetAllowedSubOperators(exponential, doubleFunctions);
139      SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
140      SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
141      SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
142      SetAllowedSubOperators(logarithm, doubleFunctions);
143      SetAllowedSubOperators(multiplication, doubleFunctions);
144      SetAllowedSubOperators(power, doubleFunctions);
145      SetAllowedSubOperators(signum, doubleFunctions);
146      SetAllowedSubOperators(sinus, doubleFunctions);
147      SetAllowedSubOperators(sqrt, doubleFunctions);
148      SetAllowedSubOperators(subtraction, doubleFunctions);
149      SetAllowedSubOperators(tangens, doubleFunctions);
150
151      operatorLibrary = new GPOperatorLibrary();
152      operatorLibrary.GPOperatorGroup.AddOperator(variable);
153      operatorLibrary.GPOperatorGroup.AddOperator(constant);
154      operatorLibrary.GPOperatorGroup.AddOperator(addition);
155      operatorLibrary.GPOperatorGroup.AddOperator(average);
156      operatorLibrary.GPOperatorGroup.AddOperator(and);
157      operatorLibrary.GPOperatorGroup.AddOperator(cosinus);
158      operatorLibrary.GPOperatorGroup.AddOperator(division);
159      operatorLibrary.GPOperatorGroup.AddOperator(equal);
160      operatorLibrary.GPOperatorGroup.AddOperator(exponential);
161      operatorLibrary.GPOperatorGroup.AddOperator(greaterThan);
162      operatorLibrary.GPOperatorGroup.AddOperator(ifThenElse);
163      operatorLibrary.GPOperatorGroup.AddOperator(lessThan);
164      operatorLibrary.GPOperatorGroup.AddOperator(logarithm);
165      operatorLibrary.GPOperatorGroup.AddOperator(multiplication);
166      operatorLibrary.GPOperatorGroup.AddOperator(not);
167      operatorLibrary.GPOperatorGroup.AddOperator(power);
168      operatorLibrary.GPOperatorGroup.AddOperator(or);
169      operatorLibrary.GPOperatorGroup.AddOperator(signum);
170      operatorLibrary.GPOperatorGroup.AddOperator(sinus);
171      operatorLibrary.GPOperatorGroup.AddOperator(sqrt);
172      operatorLibrary.GPOperatorGroup.AddOperator(subtraction);
173      operatorLibrary.GPOperatorGroup.AddOperator(tangens);
174      operatorLibrary.GPOperatorGroup.AddOperator(xor);
175    }
176
177    private void SetAllowedSubOperators(IFunction f, IFunction[] gs) {
178      foreach(IConstraint c in f.Constraints) {
179        if(c is SubOperatorTypeConstraint) {
180          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
181          typeConstraint.Clear();
182          foreach(IFunction g in gs) {
183            typeConstraint.AddOperator(g);
184          }
185        } else if(c is AllSubOperatorsTypeConstraint) {
186          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
187          typeConstraint.Clear();
188          foreach(IFunction g in gs) {
189            typeConstraint.AddOperator(g);
190          }
191        }
192      }
193    }
194
195    private void SetAllowedSubOperators(IFunction f, int p, IFunction[] gs) {
196      foreach(IConstraint c in f.Constraints) {
197        if(c is SubOperatorTypeConstraint) {
198          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
199          if(typeConstraint.SubOperatorIndex.Data == p) {
200            typeConstraint.Clear();
201            foreach(IFunction g in gs) {
202              typeConstraint.AddOperator(g);
203            }
204          }
205        }
206      }
207    }
208  }
209}
Note: See TracBrowser for help on using the repository browser.