Free cookie consent management tool by TermsFeed Policy Generator

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

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

worked on #237 (FunctionLibraryInjector that injects default function libraries with a few parameters)

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