Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/LinearTransformation.cs @ 16628

Last change on this file since 16628 was 16628, checked in by gkronber, 5 years ago

#2971: made branch compile with current version of trunk

File size: 3.6 KB
Line 
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
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;
31using HEAL.Attic;
32
33namespace HeuristicLab.Problems.DataAnalysis {
34  [StorableType("E451C88C-A33E-43F7-BCF6-AB54E5733CEE")]
35  [Item("Linear Transformation", "f(x) = k * x + d | Represents a linear transformation with multiplication and addition.")]
36  public class LinearTransformation : Transformation<double> {
37    protected const string MultiplierParameterName = "Multiplier";
38    protected const string AddendParameterName = "Addend";
39
40    #region Parameters
41    public IValueParameter<DoubleValue> MultiplierParameter {
42      get { return (IValueParameter<DoubleValue>)Parameters[MultiplierParameterName]; }
43    }
44    public IValueParameter<DoubleValue> AddendParameter {
45      get { return (IValueParameter<DoubleValue>)Parameters[AddendParameterName]; }
46    }
47    #endregion
48
49    #region properties
50    public override string ShortName {
51      get { return "Lin"; }
52    }
53    public double Multiplier {
54      get { return MultiplierParameter.Value.Value; }
55      set {
56        MultiplierParameter.Value.Value = value;
57      }
58    }
59
60    public double Addend {
61      get { return AddendParameter.Value.Value; }
62      set {
63        AddendParameter.Value.Value = value;
64      }
65    }
66    #endregion
67
68    [StorableConstructor]
69    protected LinearTransformation(StorableConstructorFlag _) : base(_) { }
70    protected LinearTransformation(LinearTransformation original, Cloner cloner)
71      : base(original, cloner) {
72    }
73    public LinearTransformation(IEnumerable<string> allowedColumns)
74      : base(allowedColumns) {
75      Parameters.Add(new ValueParameter<DoubleValue>(MultiplierParameterName, "k | Multiplier for linear transformation", new DoubleValue(1.0)));
76      Parameters.Add(new ValueParameter<DoubleValue>(AddendParameterName, "d | Addend for linear transformation", new DoubleValue(0.0)));
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new LinearTransformation(this, cloner);
81    }
82
83    public override IEnumerable<double> Apply(IEnumerable<double> data) {
84      var m = Multiplier;
85      var a = Addend;
86      return data.Select(e => e * m + a);
87    }
88
89    public override bool Check(IEnumerable<double> data, out string errorMsg) {
90      errorMsg = null;
91      if (Multiplier.IsAlmost(0.0)) {
92        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);
93        return false;
94      }
95      return true;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.