[2566] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.GP.Interfaces;
|
---|
| 26 | using HeuristicLab.GP;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
| 29 | [SymbolicRegressionFunctionLibraryInjector]
|
---|
| 30 | public class SimpleFunctionLibraryInjector : 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 simple function library for regression and classification problems."; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public SimpleFunctionLibraryInjector()
|
---|
| 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 | Cosinus cosinus = new Cosinus();
|
---|
| 76 | Division division = new Division();
|
---|
| 77 | Exponential exponential = new Exponential();
|
---|
| 78 | Logarithm logarithm = new Logarithm();
|
---|
| 79 | Multiplication multiplication = new Multiplication();
|
---|
| 80 | Power power = new Power();
|
---|
| 81 | Sinus sinus = new Sinus();
|
---|
| 82 | Sqrt sqrt = new Sqrt();
|
---|
| 83 | Subtraction subtraction = new Subtraction();
|
---|
| 84 | Tangens tangens = new Tangens();
|
---|
| 85 |
|
---|
| 86 | List<IFunction> valueNodes = new List<IFunction>();
|
---|
| 87 | ConditionalAddFunction(DIFFERENTIALS_ALLOWED, differential, valueNodes);
|
---|
| 88 | valueNodes.Add(variable);
|
---|
| 89 | valueNodes.Add(constant);
|
---|
| 90 |
|
---|
| 91 | List<IFunction> arithmeticFunctions = new List<IFunction>();
|
---|
| 92 | arithmeticFunctions.Add(addition);
|
---|
| 93 | arithmeticFunctions.Add(division);
|
---|
| 94 | arithmeticFunctions.Add(multiplication);
|
---|
| 95 | arithmeticFunctions.Add(subtraction);
|
---|
| 96 |
|
---|
| 97 | List<IFunction> complexFunctions = new List<IFunction>();
|
---|
| 98 | complexFunctions.Add(cosinus);
|
---|
| 99 | complexFunctions.Add(exponential);
|
---|
| 100 | complexFunctions.Add(logarithm);
|
---|
| 101 | complexFunctions.Add(power);
|
---|
| 102 | complexFunctions.Add(sinus);
|
---|
| 103 | complexFunctions.Add(sqrt);
|
---|
| 104 | complexFunctions.Add(tangens);
|
---|
| 105 |
|
---|
| 106 | List<IFunction> allFunctions = new List<IFunction>();
|
---|
| 107 | allFunctions.AddRange(arithmeticFunctions);
|
---|
| 108 | allFunctions.AddRange(complexFunctions);
|
---|
| 109 | allFunctions.AddRange(valueNodes);
|
---|
| 110 |
|
---|
| 111 | SetAllowedSubOperators(addition, allFunctions);
|
---|
| 112 | SetAllowedSubOperators(division, allFunctions);
|
---|
| 113 | SetAllowedSubOperators(multiplication, allFunctions);
|
---|
| 114 | SetAllowedSubOperators(subtraction, allFunctions);
|
---|
| 115 |
|
---|
| 116 | SetAllowedSubOperators(cosinus, valueNodes);
|
---|
| 117 | SetAllowedSubOperators(exponential, valueNodes);
|
---|
| 118 | SetAllowedSubOperators(logarithm, valueNodes);
|
---|
| 119 | SetAllowedSubOperators(power, valueNodes);
|
---|
| 120 | SetAllowedSubOperators(sinus, valueNodes);
|
---|
| 121 | SetAllowedSubOperators(sqrt, valueNodes);
|
---|
| 122 | SetAllowedSubOperators(tangens, valueNodes);
|
---|
| 123 |
|
---|
| 124 | ConditionalAddOperator(DIFFERENTIALS_ALLOWED, functionLibrary, differential);
|
---|
| 125 |
|
---|
| 126 | allFunctions.ForEach(x => functionLibrary.AddFunction(x));
|
---|
| 127 |
|
---|
| 128 | variable.SetConstraints(minTimeOffset, maxTimeOffset);
|
---|
| 129 | differential.SetConstraints(minTimeOffset, maxTimeOffset);
|
---|
| 130 |
|
---|
| 131 | return functionLibrary;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private void ConditionalAddFunction(string condName, IFunction fun, List<IFunction> list) {
|
---|
| 135 | if (GetVariableValue<BoolData>(condName, null, false).Data) list.Add(fun);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private void ConditionalAddOperator(string condName, FunctionLibrary functionLib, IFunction op) {
|
---|
| 139 | if (GetVariableValue<BoolData>(condName, null, false).Data) functionLib.AddFunction(op);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|