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 |
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Text;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.DataPreprocessing.Transformations;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | namespace HeuristicLab.DataPreprocessing.Implementations.Transformations {
|
---|
34 | [Item("LinearTransformation", "Represents a linear transformation with multiplication and addition.")]
|
---|
35 | public class LinearTransformation : Transformation<double> {
|
---|
36 | protected const string MultiplicandParameterName = "Multiplicand";
|
---|
37 | protected const string SummandParameterName = "Summand";
|
---|
38 |
|
---|
39 | #region Parameters
|
---|
40 | public IValueParameter<DoubleValue> MultiplicandParameter {
|
---|
41 | get { return (IValueParameter<DoubleValue>)Parameters[MultiplicandParameterName]; }
|
---|
42 | }
|
---|
43 | public IValueParameter<DoubleValue> SummandParameter {
|
---|
44 | get { return (IValueParameter<DoubleValue>)Parameters[SummandParameterName]; }
|
---|
45 | }
|
---|
46 | #endregion
|
---|
47 |
|
---|
48 | #region properties
|
---|
49 | public double Multiplicand {
|
---|
50 | get { return MultiplicandParameter.Value.Value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public double Summand {
|
---|
54 | get { return SummandParameter.Value.Value; }
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected LinearTransformation(bool deserializing) : base(deserializing) { }
|
---|
60 | protected LinearTransformation(Transformation<double> original, Cloner cloner)
|
---|
61 | : base(original, cloner) {
|
---|
62 | }
|
---|
63 | protected LinearTransformation() {
|
---|
64 | }
|
---|
65 | public LinearTransformation(IEnumerable<string> allowedColumns)
|
---|
66 | : base(allowedColumns) {
|
---|
67 | Parameters.Add(new ValueParameter<DoubleValue>(MultiplicandParameterName, "Multiplicand for linear transformation", new DoubleValue(1.0)));
|
---|
68 | Parameters.Add(new ValueParameter<DoubleValue>(SummandParameterName, "Summand for linear transformation", new DoubleValue(0.0)));
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | return new LinearTransformation(this, cloner);
|
---|
73 | }
|
---|
74 |
|
---|
75 | public override IEnumerable<double> Apply(IEnumerable<double> data) {
|
---|
76 | return data.Select(e => e * Multiplicand + Summand);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override bool Check(IEnumerable<double> data, out string errorMsg) {
|
---|
80 | StringBuilder sb = new StringBuilder();
|
---|
81 | bool success = true;
|
---|
82 | errorMsg = null;
|
---|
83 |
|
---|
84 | if (Multiplicand == 0.0) {
|
---|
85 | sb.Append(String.Format("Multiplicand is 0, all {0} entries will be set to {1}. Reversal will not be possble", data.Count(), Summand));
|
---|
86 | errorMsg = sb.ToString();
|
---|
87 | success = false;
|
---|
88 | }
|
---|
89 | return success;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|