1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
32 | [Item("Linear", "Linear transformation x' = Scale * x + Offset")]
|
---|
33 | [StorableClass]
|
---|
34 | public class LinearTransformation : Transformation<double> {
|
---|
35 |
|
---|
36 | #region Parameters
|
---|
37 | private FixedValueParameter<DoubleValue> ScaleParameter {
|
---|
38 | get { return (FixedValueParameter<DoubleValue>)Parameters["Scale"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | private FixedValueParameter<DoubleValue> OffsetParameter {
|
---|
42 | get { return (FixedValueParameter<DoubleValue>)Parameters["Offset"]; }
|
---|
43 | }
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | #region Properties
|
---|
47 | public double Scale {
|
---|
48 | get { return ScaleParameter.Value.Value; }
|
---|
49 | set { ScaleParameter.Value.Value = value; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public double Offset {
|
---|
53 | get { return OffsetParameter.Value.Value; }
|
---|
54 | set { OffsetParameter.Value.Value = value; }
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region Constructor, Cloning & Persistence
|
---|
59 | public LinearTransformation()
|
---|
60 | : base() {
|
---|
61 | Parameters.Add(new FixedValueParameter<DoubleValue>("Scale", "Multiplicative factor (Slope)", new DoubleValue(1.0)));
|
---|
62 | Parameters.Add(new FixedValueParameter<DoubleValue>("Offset", "Additive factor (Intercept)", new DoubleValue(0.0)));
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected LinearTransformation(LinearTransformation original, Cloner cloner)
|
---|
66 | : base(original, cloner) {
|
---|
67 | }
|
---|
68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
69 | return new LinearTransformation(this, cloner);
|
---|
70 | }
|
---|
71 |
|
---|
72 | [StorableConstructor]
|
---|
73 | protected LinearTransformation(bool deserializing)
|
---|
74 | : base(deserializing) {
|
---|
75 | }
|
---|
76 | #endregion
|
---|
77 |
|
---|
78 | public override IEnumerable<double> Apply(IEnumerable<double> data) {
|
---|
79 | return Apply(data, Scale, Offset);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
|
---|
83 | return InverseApply(data, Scale, Offset);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | public static IEnumerable<double> Apply(IEnumerable<double> data, double scale = 1.0, double offset = 0.0) {
|
---|
88 | if (scale.IsAlmost(0.0))
|
---|
89 | throw new InvalidOperationException($"Cannot transform with a {nameof(scale)} of zero because inverse transformation would be invalid.");
|
---|
90 |
|
---|
91 | return data.Select(x => scale * x + offset);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public static IEnumerable<double> InverseApply(IEnumerable<double> data, double scale = 1.0, double offset = 0.0) {
|
---|
95 | if (scale.IsAlmost(0.0))
|
---|
96 | throw new InvalidOperationException($"Cannot inverse transform with a {nameof(scale)} of zero.");
|
---|
97 |
|
---|
98 | return data.Select(x => (x - offset) / scale);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | } |
---|