Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjector.cs @ 2174

Last change on this file since 2174 was 2174, checked in by gkronber, 15 years ago

Implemented #302 (Show variable names instead of var<index> in GP function trees).

  • Added a new operator that chooses a random value from a list of possible values.
  • Changed mutation operator for variables and differentials
  • Changed internal linear representation of function trees to support different types for local data.
File size: 13.7 KB
RevLine 
[1050]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 {
[1908]34    private const string FUNCTIONLIBRARY = "FunctionLibrary";
[1050]35    private const string TARGETVARIABLE = "TargetVariable";
[1908]36    private const string MINTIMEOFFSET = "MinTimeOffset";
37    private const string MAXTIMEOFFSET = "MaxTimeOffset";
[1050]38
[1908]39    private const string DIFFERENTIALS_ALLOWED = "Differentials";
40    private const string VARIABLES_ALLOWED = "Variables";
41    private const string CONSTANTS_ALLOWED = "Constants";
42    private const string ADDITION_ALLOWED = "Addition";
43    private const string AVERAGE_ALLOWED = "Average";
44    private const string AND_ALLOWED = "And";
45    private const string COSINUS_ALLOWED = "Cosinus";
46    private const string DIVISION_ALLOWED = "Division";
47    private const string EQUAL_ALLOWED = "Equal";
48    private const string EXPONENTIAL_ALLOWED = "Exponential";
49    private const string GREATERTHAN_ALLOWED = "GreaterThan";
50    private const string IFTHENELSE_ALLOWED = "IfThenElse";
51    private const string LESSTHAN_ALLOWED = "LessThan";
52    private const string LOGARTIHM_ALLOWED = "Logarithm";
53    private const string MULTIPLICATION_ALLOWED = "Multiplication";
54    private const string NOT_ALLOWED = "Not";
55    private const string POWER_ALLOWED = "Power";
56    private const string OR_ALLOWED = "Or";
57    private const string SIGNUM_ALLOWED = "Signum";
58    private const string SINUS_ALLOWED = "Sinus";
59    private const string SQRT_ALLOWED = "SquareRoot";
60    private const string SUBTRACTION_ALLOWED = "Subtraction";
61    private const string TANGENS_ALLOWED = "Tangens";
62    private const string XOR_ALLOWED = "Xor";
63
64
[1050]65    public override string Description {
[1908]66      get { return @"Injects a configurable function library for regression and classification problems."; }
[1050]67    }
68
69    public FunctionLibraryInjector()
70      : base() {
71      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "The target variable", typeof(IntData), VariableKind.In));
[2165]72      AddVariableInfo(new VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), VariableKind.In));
73      AddVariableInfo(new VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), VariableKind.In));
[1050]74      AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New));
[1908]75
76      AddVariable(DIFFERENTIALS_ALLOWED, false);
77      AddVariable(VARIABLES_ALLOWED, true);
78      AddVariable(CONSTANTS_ALLOWED, true);
79      AddVariable(ADDITION_ALLOWED, true);
80      AddVariable(AVERAGE_ALLOWED, false);
81      AddVariable(AND_ALLOWED, true);
82      AddVariable(COSINUS_ALLOWED, true);
83      AddVariable(DIVISION_ALLOWED, true);
84      AddVariable(EQUAL_ALLOWED, true);
85      AddVariable(EXPONENTIAL_ALLOWED, true);
86      AddVariable(GREATERTHAN_ALLOWED, true);
87      AddVariable(IFTHENELSE_ALLOWED, true);
88      AddVariable(LESSTHAN_ALLOWED, true);
89      AddVariable(LOGARTIHM_ALLOWED, true);
90      AddVariable(MULTIPLICATION_ALLOWED, true);
91      AddVariable(NOT_ALLOWED, true);
92      AddVariable(POWER_ALLOWED, true);
93      AddVariable(OR_ALLOWED, true);
94      AddVariable(SIGNUM_ALLOWED, true);
95      AddVariable(SINUS_ALLOWED, true);
96      AddVariable(SQRT_ALLOWED, true);
97      AddVariable(SUBTRACTION_ALLOWED, true);
98      AddVariable(TANGENS_ALLOWED, true);
99      AddVariable(XOR_ALLOWED, false);
[1050]100    }
101
[1908]102    private void AddVariable(string name, bool allowed) {
103      AddVariableInfo(new VariableInfo(name, name + " allowed", typeof(BoolData), Core.VariableKind.New));
104      GetVariableInfo(name).Local = true;
105      AddVariable(new Core.Variable(name, new BoolData(allowed)));
106    }
107
[1050]108    public override IOperation Apply(IScope scope) {
[1287]109      StructId.Variable variable;
110      GPOperatorLibrary operatorLibrary;
[1050]111      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
112
[1911]113      // try to get minTimeOffset (use 0 as default if not available)
114      IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false);
115      int minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
116      // try to get maxTimeOffset (use 0 as default if not available)
117      IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false);
118      int maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
119
[1050]120      variable = new StructId.Variable();
121      StructId.Constant constant = new StructId.Constant();
[1908]122      StructId.Differential differential = new Differential();
[1050]123      StructId.Addition addition = new StructId.Addition();
124      StructId.And and = new StructId.And();
125      StructId.Average average = new StructId.Average();
126      StructId.Cosinus cosinus = new StructId.Cosinus();
127      StructId.Division division = new StructId.Division();
128      StructId.Equal equal = new StructId.Equal();
129      StructId.Exponential exponential = new StructId.Exponential();
130      StructId.GreaterThan greaterThan = new StructId.GreaterThan();
131      StructId.IfThenElse ifThenElse = new StructId.IfThenElse();
132      StructId.LessThan lessThan = new StructId.LessThan();
133      StructId.Logarithm logarithm = new StructId.Logarithm();
134      StructId.Multiplication multiplication = new StructId.Multiplication();
135      StructId.Not not = new StructId.Not();
136      StructId.Or or = new StructId.Or();
137      StructId.Power power = new StructId.Power();
138      StructId.Signum signum = new StructId.Signum();
139      StructId.Sinus sinus = new StructId.Sinus();
140      StructId.Sqrt sqrt = new StructId.Sqrt();
141      StructId.Subtraction subtraction = new StructId.Subtraction();
142      StructId.Tangens tangens = new StructId.Tangens();
143      StructId.Xor xor = new StructId.Xor();
144
145
[1910]146      List<IOperator> booleanFunctions = new List<IOperator>();
147      ConditionalAddOperator(AND_ALLOWED, and, booleanFunctions);
148      ConditionalAddOperator(EQUAL_ALLOWED, equal, booleanFunctions);
149      ConditionalAddOperator(GREATERTHAN_ALLOWED, greaterThan, booleanFunctions);
150      ConditionalAddOperator(LESSTHAN_ALLOWED, lessThan, booleanFunctions);
151      ConditionalAddOperator(NOT_ALLOWED, not, booleanFunctions);
152      ConditionalAddOperator(OR_ALLOWED, or, booleanFunctions);
153      ConditionalAddOperator(XOR_ALLOWED, xor, booleanFunctions);
154
155      List<IOperator> doubleFunctions = new List<IOperator>();
156      ConditionalAddOperator(DIFFERENTIALS_ALLOWED, differential, doubleFunctions);
157      ConditionalAddOperator(VARIABLES_ALLOWED, variable, doubleFunctions);
158      ConditionalAddOperator(CONSTANTS_ALLOWED, constant, doubleFunctions);
159      ConditionalAddOperator(ADDITION_ALLOWED, addition, doubleFunctions);
160      ConditionalAddOperator(AVERAGE_ALLOWED, average, doubleFunctions);
161      ConditionalAddOperator(COSINUS_ALLOWED, cosinus, doubleFunctions);
162      ConditionalAddOperator(DIVISION_ALLOWED, division, doubleFunctions);
163      ConditionalAddOperator(EXPONENTIAL_ALLOWED, exponential, doubleFunctions);
164      ConditionalAddOperator(IFTHENELSE_ALLOWED, ifThenElse, doubleFunctions);
165      ConditionalAddOperator(LOGARTIHM_ALLOWED, logarithm, doubleFunctions);
166      ConditionalAddOperator(MULTIPLICATION_ALLOWED, multiplication, doubleFunctions);
167      ConditionalAddOperator(POWER_ALLOWED, power, doubleFunctions);
168      ConditionalAddOperator(SIGNUM_ALLOWED, signum, doubleFunctions);
169      ConditionalAddOperator(SINUS_ALLOWED, sinus, doubleFunctions);
170      ConditionalAddOperator(SQRT_ALLOWED, sqrt, doubleFunctions);
171      ConditionalAddOperator(SUBTRACTION_ALLOWED, subtraction, doubleFunctions);
172      ConditionalAddOperator(TANGENS_ALLOWED, tangens, doubleFunctions);
173
[1050]174      SetAllowedSubOperators(and, booleanFunctions);
175      SetAllowedSubOperators(equal, doubleFunctions);
176      SetAllowedSubOperators(greaterThan, doubleFunctions);
177      SetAllowedSubOperators(lessThan, doubleFunctions);
178      SetAllowedSubOperators(not, booleanFunctions);
179      SetAllowedSubOperators(or, booleanFunctions);
180      SetAllowedSubOperators(xor, booleanFunctions);
181      SetAllowedSubOperators(addition, doubleFunctions);
182      SetAllowedSubOperators(average, doubleFunctions);
183      SetAllowedSubOperators(cosinus, doubleFunctions);
184      SetAllowedSubOperators(division, doubleFunctions);
185      SetAllowedSubOperators(exponential, doubleFunctions);
186      SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
187      SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
188      SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
189      SetAllowedSubOperators(logarithm, doubleFunctions);
190      SetAllowedSubOperators(multiplication, doubleFunctions);
191      SetAllowedSubOperators(power, doubleFunctions);
192      SetAllowedSubOperators(signum, doubleFunctions);
193      SetAllowedSubOperators(sinus, doubleFunctions);
194      SetAllowedSubOperators(sqrt, doubleFunctions);
195      SetAllowedSubOperators(subtraction, doubleFunctions);
196      SetAllowedSubOperators(tangens, doubleFunctions);
197
198      operatorLibrary = new GPOperatorLibrary();
[1908]199      ConditionalAddOperator(DIFFERENTIALS_ALLOWED, operatorLibrary, differential);
200      ConditionalAddOperator(VARIABLES_ALLOWED, operatorLibrary, variable);
201      ConditionalAddOperator(CONSTANTS_ALLOWED, operatorLibrary, constant);
202      ConditionalAddOperator(ADDITION_ALLOWED, operatorLibrary, addition);
203      ConditionalAddOperator(AVERAGE_ALLOWED, operatorLibrary, average);
204      ConditionalAddOperator(AND_ALLOWED, operatorLibrary, and);
205      ConditionalAddOperator(COSINUS_ALLOWED, operatorLibrary, cosinus);
206      ConditionalAddOperator(DIVISION_ALLOWED, operatorLibrary, division);
207      ConditionalAddOperator(EQUAL_ALLOWED, operatorLibrary, equal);
208      ConditionalAddOperator(EXPONENTIAL_ALLOWED, operatorLibrary, exponential);
209      ConditionalAddOperator(GREATERTHAN_ALLOWED, operatorLibrary, greaterThan);
210      ConditionalAddOperator(IFTHENELSE_ALLOWED, operatorLibrary, ifThenElse);
211      ConditionalAddOperator(LESSTHAN_ALLOWED, operatorLibrary, lessThan);
212      ConditionalAddOperator(LOGARTIHM_ALLOWED, operatorLibrary, logarithm);
213      ConditionalAddOperator(MULTIPLICATION_ALLOWED, operatorLibrary, multiplication);
214      ConditionalAddOperator(NOT_ALLOWED, operatorLibrary, not);
215      ConditionalAddOperator(POWER_ALLOWED, operatorLibrary, power);
216      ConditionalAddOperator(OR_ALLOWED, operatorLibrary, or);
217      ConditionalAddOperator(SIGNUM_ALLOWED, operatorLibrary, signum);
218      ConditionalAddOperator(SINUS_ALLOWED, operatorLibrary, sinus);
219      ConditionalAddOperator(SQRT_ALLOWED, operatorLibrary, sqrt);
220      ConditionalAddOperator(SUBTRACTION_ALLOWED, operatorLibrary, subtraction);
221      ConditionalAddOperator(TANGENS_ALLOWED, operatorLibrary, tangens);
222      ConditionalAddOperator(XOR_ALLOWED, operatorLibrary, xor);
[1287]223
[2174]224      variable.SetConstraints(minTimeOffset, maxTimeOffset);
225      differential.SetConstraints(minTimeOffset, maxTimeOffset);
[1287]226
227      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), operatorLibrary));
[1908]228
[1287]229      return null;
[1050]230    }
231
[1910]232    private void ConditionalAddOperator(string condName, IOperator op, List<IOperator> list) {
233      if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(op);
234    }
235
[1908]236    private void ConditionalAddOperator(string condName, GPOperatorLibrary operatorLibrary, IOperator op) {
237      if (GetVariableValue<BoolData>(condName, null, false).Data) operatorLibrary.GPOperatorGroup.AddOperator(op);
238    }
239
[1910]240    private void SetAllowedSubOperators(IFunction f, List<IOperator> gs) {
[1287]241      foreach (IConstraint c in f.Constraints) {
242        if (c is SubOperatorTypeConstraint) {
[1050]243          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
244          typeConstraint.Clear();
[1910]245          foreach (IOperator g in gs) {
[1050]246            typeConstraint.AddOperator(g);
247          }
[1287]248        } else if (c is AllSubOperatorsTypeConstraint) {
[1050]249          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
250          typeConstraint.Clear();
[1910]251          foreach (IOperator g in gs) {
[1050]252            typeConstraint.AddOperator(g);
253          }
254        }
255      }
256    }
257
[1910]258    private void SetAllowedSubOperators(IFunction f, int p, List<IOperator> gs) {
[1287]259      foreach (IConstraint c in f.Constraints) {
260        if (c is SubOperatorTypeConstraint) {
[1050]261          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
[1287]262          if (typeConstraint.SubOperatorIndex.Data == p) {
[1050]263            typeConstraint.Clear();
[1910]264            foreach (IOperator g in gs) {
[1050]265              typeConstraint.AddOperator(g);
266            }
267          }
268        }
269      }
270    }
271  }
272}
Note: See TracBrowser for help on using the repository browser.