Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/FunctionLibraryInjector.cs @ 468

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

ironed out a few more problems (#237 FunctionLibraryInjector that injects default function libraries with a few parameters)

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