Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added test cases for network transformation and fixed bugs in network transformation operator. #833

File size: 6.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.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(GetVariableValue<BoolData>(DIFFERENTIALS_ALLOWED, null, false).Data, minTimeOffset, maxTimeOffset);
69    }
70
71    public static FunctionLibrary Create(bool includeDifferential, 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          variable, constant, addition, subtraction,
86          division, multiplication};
87
88      if (includeDifferential)
89        f0Functions.Add(differential);
90      #endregion
91
92      #region f1: (...) -> (double -> double)
93      OpenParameter openPar = new OpenParameter();
94      OpenExp openExp = new OpenExp();
95      OpenLog openLog = new OpenLog();
96      //OpenSqrt openSqrt = new OpenSqrt();
97      //OpenSqr openSqr = new OpenSqr();
98      Flip flip = new Flip();
99      AdditionF1 addF1 = new AdditionF1();
100      SubtractionF1 subF1 = new SubtractionF1();
101      MultiplicationF1 mulF1 = new MultiplicationF1();
102      DivisionF1 divF1 = new DivisionF1();
103
104      List<IFunction> f1Functions = new List<IFunction>() {
105        openPar,
106        openExp, openLog, flip, // openSqrt, openSqr,
107        addF1, subF1, mulF1, divF1
108      };
109      #endregion
110
111      #region f2: (...) -> (double, double -> double)
112      Cycle cycle = new Cycle();
113      OpenAddition openAdd = new OpenAddition();
114      OpenSubtraction openSub = new OpenSubtraction();
115      OpenMultiplication openMul = new OpenMultiplication();
116      OpenDivision openDivision = new OpenDivision();
117      List<IFunction> f2Functions = new List<IFunction>() { openAdd, openSub, openMul, openDivision, cycle };
118      #endregion
119
120
121      SetAllowedSubOperators(addition, f0Functions);
122      SetAllowedSubOperators(division, f0Functions);
123      SetAllowedSubOperators(multiplication, f0Functions);
124      SetAllowedSubOperators(subtraction, f0Functions);
125
126      SetAllowedSubOperators(openExp, f1Functions);
127      SetAllowedSubOperators(openLog, f1Functions);
128      //SetAllowedSubOperators(openSqrt, f1Functions);
129      //SetAllowedSubOperators(openSqr, f1Functions);
130      SetAllowedSubOperators(flip, f1Functions);
131      SetAllowedSubOperators(addF1, 0, f1Functions);
132      SetAllowedSubOperators(addF1, 1, f0Functions);
133      SetAllowedSubOperators(subF1, 0, f1Functions);
134      SetAllowedSubOperators(subF1, 1, f0Functions);
135      SetAllowedSubOperators(mulF1, 0, f1Functions);
136      SetAllowedSubOperators(mulF1, 1, f0Functions);
137      SetAllowedSubOperators(divF1, 0, f1Functions);
138      SetAllowedSubOperators(divF1, 1, f0Functions);
139
140      SetAllowedSubOperators(cycle, f2Functions);
141      SetAllowedSubOperators(openAdd, f1Functions);
142      SetAllowedSubOperators(openDivision, f1Functions);
143      SetAllowedSubOperators(openMul, f1Functions);
144      SetAllowedSubOperators(openSub, f1Functions);
145
146      if (includeDifferential)
147        functionLibrary.AddFunction(differential);
148      f0Functions.ForEach(x => functionLibrary.AddFunction(x));
149      f1Functions.ForEach(x => functionLibrary.AddFunction(x));
150      f2Functions.ForEach(x => functionLibrary.AddFunction(x));
151
152      variable.SetConstraints(minTimeOffset, maxTimeOffset);
153      differential.SetConstraints(minTimeOffset, maxTimeOffset);
154      openPar.SetConstraints(minTimeOffset, maxTimeOffset);
155
156
157      return functionLibrary;
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.