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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using System.Diagnostics;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Constraints;
|
---|
29 | using HeuristicLab.DataAnalysis;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Functions {
|
---|
32 | public class Variable : FunctionBase {
|
---|
33 |
|
---|
34 | public const string WEIGHT = "Weight";
|
---|
35 | public const string OFFSET = "SampleOffset";
|
---|
36 | public const string INDEX = "Variable";
|
---|
37 |
|
---|
38 | public override string Description {
|
---|
39 | get { return @"Variable reads a value from a dataset, multiplies that value with a given factor and returns the result.
|
---|
40 | The variable 'SampleOffset' can be used to read a value from previous or following rows.
|
---|
41 | The index of the row that is actually read is SampleIndex+SampleOffset)."; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public Variable()
|
---|
45 | : base() {
|
---|
46 | AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
|
---|
47 | GetVariableInfo(INDEX).Local = true;
|
---|
48 | AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None));
|
---|
49 | GetVariableInfo(WEIGHT).Local = true;
|
---|
50 | AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));
|
---|
51 | GetVariableInfo(OFFSET).Local = true;
|
---|
52 |
|
---|
53 | ConstrainedDoubleData weight = new ConstrainedDoubleData();
|
---|
54 | // initialize a totally arbitrary range for the weight = [-20.0, 20.0]
|
---|
55 | weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
|
---|
56 | AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));
|
---|
57 |
|
---|
58 | ConstrainedIntData variable = new ConstrainedIntData();
|
---|
59 | AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
|
---|
60 |
|
---|
61 | ConstrainedIntData sampleOffset = new ConstrainedIntData();
|
---|
62 | // initialize a totally arbitrary default range for sampleoffset = [-10, 10]
|
---|
63 | sampleOffset.AddConstraint(new IntBoundedConstraint(0, 0));
|
---|
64 | AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));
|
---|
65 |
|
---|
66 | // variable can't have suboperators
|
---|
67 | AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override IFunctionTree GetTreeNode() {
|
---|
71 | return new VariableFunctionTree(this);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // can't apply a variable
|
---|
75 | public override double Apply(Dataset dataset, int sampleIndex, double[] args) {
|
---|
76 | throw new NotSupportedException();
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override void Accept(IFunctionVisitor visitor) {
|
---|
80 | visitor.Visit(this);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | class VariableFunctionTree : FunctionTree {
|
---|
84 | private ConstrainedDoubleData weight;
|
---|
85 | private ConstrainedIntData index;
|
---|
86 | private ConstrainedIntData offset;
|
---|
87 |
|
---|
88 | public VariableFunctionTree() : base() { }
|
---|
89 | public VariableFunctionTree(Variable variable) : base(variable) {
|
---|
90 | UpdateCachedValues();
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void UpdateCachedValues() {
|
---|
94 | weight = (ConstrainedDoubleData)GetLocalVariable(Variable.WEIGHT).Value;
|
---|
95 | index = (ConstrainedIntData)GetLocalVariable(Variable.INDEX).Value;
|
---|
96 | offset = (ConstrainedIntData)GetLocalVariable(Variable.OFFSET).Value;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public override double Evaluate(Dataset dataset, int sampleIndex) {
|
---|
100 | return weight.Data * dataset.GetValue(sampleIndex + offset.Data, index.Data);
|
---|
101 | }
|
---|
102 |
|
---|
103 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
104 | VariableFunctionTree clone = (VariableFunctionTree)base.Clone(clonedObjects);
|
---|
105 | clone.UpdateCachedValues();
|
---|
106 | return clone;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
110 | base.Populate(node, restoredObjects);
|
---|
111 | UpdateCachedValues();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|