Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Networks/3.2/FunctionLibraryInjector.cs @ 2821

Last change on this file since 2821 was 2821, checked in by gkronber, 14 years ago

Removed access to local variable DIFFERENTIALS_ALLOWED in time series prognosis engines. #877 (Predefined GP engines for time series prognosis are defect because of the removal of local variables of function library injectors in #748)

File size: 6.3 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.Collections.Generic;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.GP.Interfaces;
26using HeuristicLab.GP;
27
28namespace HeuristicLab.GP.StructureIdentification.Networks {
29  public class FunctionLibraryInjector : FunctionLibraryInjectorBase {
30    public const string MINTIMEOFFSET = "MinTimeOffset";
31    public const string MAXTIMEOFFSET = "MaxTimeOffset";
32
33    public const string DIFFERENTIALS_ALLOWED = "Differentials";
34
35    private int minTimeOffset;
36    private int maxTimeOffset;
37
38    public override string Description {
39      get { return @"Injects a function library for network structure identification."; }
40    }
41
42    public FunctionLibraryInjector()
43      : base() {
44      AddVariableInfo(new VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), VariableKind.In));
45      AddVariableInfo(new VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), VariableKind.In));
46
47      AddVariable(DIFFERENTIALS_ALLOWED, false);
48    }
49
50    private void AddVariable(string name, bool allowed) {
51      AddVariableInfo(new VariableInfo(name, name + " allowed", typeof(BoolData), Core.VariableKind.New));
52      GetVariableInfo(name).Local = true;
53      AddVariable(new Core.Variable(name, new BoolData(allowed)));
54    }
55
56    public override IOperation Apply(IScope scope) {
57      // try to get minTimeOffset (use 0 as default if not available)
58      IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false);
59      minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
60      // try to get maxTimeOffset (use 0 as default if not available)
61      IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false);
62      maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
63
64      return base.Apply(scope);
65    }
66
67    protected override FunctionLibrary CreateFunctionLibrary() {
68      return Create(minTimeOffset, maxTimeOffset);
69    }
70
71    public static FunctionLibrary Create(int minTimeOffset, int maxTimeOffset) {
72      FunctionLibrary functionLibrary = new FunctionLibrary();
73
74      #region f0 (...) -> double
75      Variable variable = new Variable();
76      Constant constant = new Constant();
77      Differential differential = new Differential();
78      Addition addition = new Addition();
79      Division division = new Division();
80      Multiplication multiplication = new Multiplication();
81      Subtraction subtraction = new Subtraction();
82
83      List<IFunction> f0Functions =
84        new List<IFunction>() {
85          differential, variable, constant, addition, subtraction,
86          division, multiplication};
87
88
89      #endregion
90
91      #region f1: (...) -> (double -> double)
92      OpenParameter openPar = new OpenParameter();
93      OpenExp openExp = new OpenExp();
94      OpenLog openLog = new OpenLog();
95      //OpenSqrt openSqrt = new OpenSqrt();
96      //OpenSqr openSqr = new OpenSqr();
97      Flip flip = new Flip();
98      AdditionF1 addF1 = new AdditionF1();
99      SubtractionF1 subF1 = new SubtractionF1();
100      MultiplicationF1 mulF1 = new MultiplicationF1();
101      DivisionF1 divF1 = new DivisionF1();
102
103      List<IFunction> f1Functions = new List<IFunction>() {
104        openPar,
105        openExp, openLog, flip, // openSqrt, openSqr,
106        addF1, subF1, mulF1, divF1
107      };
108      #endregion
109
110      #region f2: (...) -> (double, double -> double)
111      Cycle cycle = new Cycle();
112      OpenAddition openAdd = new OpenAddition();
113      OpenSubtraction openSub = new OpenSubtraction();
114      OpenMultiplication openMul = new OpenMultiplication();
115      OpenDivision openDivision = new OpenDivision();
116      List<IFunction> f2Functions = new List<IFunction>() { openAdd, openSub, openMul, openDivision, cycle };
117      #endregion
118
119
120      SetAllowedSubOperators(addition, f0Functions);
121      SetAllowedSubOperators(division, f0Functions);
122      SetAllowedSubOperators(multiplication, f0Functions);
123      SetAllowedSubOperators(subtraction, f0Functions);
124
125      SetAllowedSubOperators(openExp, f1Functions);
126      SetAllowedSubOperators(openLog, f1Functions);
127      //SetAllowedSubOperators(openSqrt, f1Functions);
128      //SetAllowedSubOperators(openSqr, f1Functions);
129      SetAllowedSubOperators(flip, f1Functions);
130      SetAllowedSubOperators(addF1, 0, f1Functions);
131      SetAllowedSubOperators(addF1, 1, f0Functions);
132      SetAllowedSubOperators(subF1, 0, f1Functions);
133      SetAllowedSubOperators(subF1, 1, f0Functions);
134      SetAllowedSubOperators(mulF1, 0, f1Functions);
135      SetAllowedSubOperators(mulF1, 1, f0Functions);
136      SetAllowedSubOperators(divF1, 0, f1Functions);
137      SetAllowedSubOperators(divF1, 1, f0Functions);
138
139      SetAllowedSubOperators(cycle, f2Functions);
140      SetAllowedSubOperators(openAdd, f1Functions);
141      SetAllowedSubOperators(openDivision, f1Functions);
142      SetAllowedSubOperators(openMul, f1Functions);
143      SetAllowedSubOperators(openSub, f1Functions);
144
145      f0Functions.ForEach(x => functionLibrary.AddFunction(x));
146      f1Functions.ForEach(x => functionLibrary.AddFunction(x));
147      f2Functions.ForEach(x => functionLibrary.AddFunction(x));
148
149      variable.SetConstraints(minTimeOffset, maxTimeOffset);
150      differential.SetConstraints(minTimeOffset, maxTimeOffset);
151      openPar.SetConstraints(minTimeOffset, maxTimeOffset);
152
153
154      return functionLibrary;
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.