Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/FunctionLibraryInjectors/ArithmeticFunctionLibraryInjector.cs @ 2682

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

Added new predefined function libraries for symbolic regression algorithms. Changed CEDMA dispatcher to choose a function library randomly. #813 (GP structure-identification algorithms that use only a simple function library)

File size: 4.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 {
29  [SymbolicRegressionFunctionLibraryInjector]
30  public class ArithmeticFunctionLibraryInjector : FunctionLibraryInjectorBase {
31    public const string MINTIMEOFFSET = "MinTimeOffset";
32    public const string MAXTIMEOFFSET = "MaxTimeOffset";
33
34    public const string DIFFERENTIALS_ALLOWED = "Differentials";
35
36    private int minTimeOffset;
37    private int maxTimeOffset;
38
39    public override string Description {
40      get { return @"Injects a function library with (+, -, *,  /) symbols."; }
41    }
42
43    public ArithmeticFunctionLibraryInjector()
44      : base() {
45      AddVariableInfo(new VariableInfo(MINTIMEOFFSET, "Minimal time offset for all features", typeof(IntData), VariableKind.In));
46      AddVariableInfo(new VariableInfo(MAXTIMEOFFSET, "Maximal time offset for all feature", typeof(IntData), VariableKind.In));
47
48      AddVariable(DIFFERENTIALS_ALLOWED, false);
49    }
50
51    private void AddVariable(string name, bool allowed) {
52      AddVariableInfo(new VariableInfo(name, name + " allowed", typeof(BoolData), Core.VariableKind.New));
53      GetVariableInfo(name).Local = true;
54      AddVariable(new Core.Variable(name, new BoolData(allowed)));
55    }
56
57    public override IOperation Apply(IScope scope) {
58      // try to get minTimeOffset (use 0 as default if not available)
59      IItem minTimeOffsetItem = GetVariableValue(MINTIMEOFFSET, scope, true, false);
60      minTimeOffset = minTimeOffsetItem == null ? 0 : ((IntData)minTimeOffsetItem).Data;
61      // try to get maxTimeOffset (use 0 as default if not available)
62      IItem maxTimeOffsetItem = GetVariableValue(MAXTIMEOFFSET, scope, true, false);
63      maxTimeOffset = maxTimeOffsetItem == null ? 0 : ((IntData)maxTimeOffsetItem).Data;
64
65      return base.Apply(scope);
66    }
67
68    protected override FunctionLibrary CreateFunctionLibrary() {
69      FunctionLibrary functionLibrary = new FunctionLibrary();
70
71      Variable variable = new Variable();
72      Constant constant = new Constant();
73      Differential differential = new Differential();
74      Addition addition = new Addition();
75      Division division = new Division();
76      Multiplication multiplication = new Multiplication();
77      Subtraction subtraction = new Subtraction();
78
79      List<IFunction> doubleFunctions = new List<IFunction>();
80      if (GetVariableValue<BoolData>(DIFFERENTIALS_ALLOWED, null, false).Data)
81        doubleFunctions.Add(differential);
82      doubleFunctions.Add(variable);
83      doubleFunctions.Add(constant);
84      doubleFunctions.Add(addition);
85      doubleFunctions.Add(division);
86      doubleFunctions.Add(multiplication);
87      doubleFunctions.Add(subtraction);
88
89      SetAllowedSubOperators(addition, doubleFunctions);
90      SetAllowedSubOperators(division, doubleFunctions);
91      SetAllowedSubOperators(multiplication, doubleFunctions);
92      SetAllowedSubOperators(subtraction, doubleFunctions);
93
94      if (GetVariableValue<BoolData>(DIFFERENTIALS_ALLOWED, null, false).Data)
95        functionLibrary.AddFunction(differential);
96      functionLibrary.AddFunction(variable);
97      functionLibrary.AddFunction(constant);
98      functionLibrary.AddFunction(addition);
99      functionLibrary.AddFunction(division);
100      functionLibrary.AddFunction(multiplication);
101      functionLibrary.AddFunction(subtraction);
102
103      variable.SetConstraints(minTimeOffset, maxTimeOffset);
104      differential.SetConstraints(minTimeOffset, maxTimeOffset);
105
106      return functionLibrary;
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.