Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis.Transformations/3.3/LinearTransformation.cs @ 10909

Last change on this file since 10909 was 10909, checked in by tsteinre, 10 years ago
  • modified Transformations Names / Parameter Names
File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Transformations {
33  [Item("Linear Transformation", "f(x) = k * x + d | Represents a linear transformation with multiplication and addition.")]
34  public class LinearTransformation : Transformation<double> {
35    protected const string MultiplierParameterName = "Multiplier";
36    protected const string AddendParameterName = "Addend";
37
38    #region Parameters
39    public IValueParameter<DoubleValue> MultiplierParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters[MultiplierParameterName]; }
41    }
42    public IValueParameter<DoubleValue> AddendParameter {
43      get { return (IValueParameter<DoubleValue>)Parameters[AddendParameterName]; }
44    }
45    #endregion
46
47    #region properties
48    public double Multiplier {
49      get { return MultiplierParameter.Value.Value; }
50      protected set {
51        MultiplierParameter.Value.Value = value;
52      }
53    }
54
55    public double Addend {
56      get { return AddendParameter.Value.Value; }
57      protected set {
58        AddendParameter.Value.Value = value;
59      }
60    }
61    #endregion
62
63    [StorableConstructor]
64    protected LinearTransformation(bool deserializing) : base(deserializing) { }
65    protected LinearTransformation(Transformation<double> original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public LinearTransformation(IEnumerable<string> allowedColumns)
69      : base(allowedColumns) {
70      Parameters.Add(new ValueParameter<DoubleValue>(MultiplierParameterName, "k | Multiplier for linear transformation", new DoubleValue(1.0)));
71      Parameters.Add(new ValueParameter<DoubleValue>(AddendParameterName, "d | Addend for linear transformation", new DoubleValue(0.0)));
72    }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new LinearTransformation(this, cloner);
76    }
77
78    public override IEnumerable<double> Apply(IEnumerable<double> data) {
79      return data.Select(e => e * Multiplier + Addend);
80    }
81
82    public override bool Check(IEnumerable<double> data, out string errorMsg) {
83      errorMsg = null;
84      if (Multiplier == 0.0) {
85        errorMsg = String.Format("Multiplicand is 0, all {0} entries will be set to {1}. Inverse apply will not be possible (division by 0).", data.Count(), Addend);
86        return false;
87      }
88      return true;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.