Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DatastreamAnalysis/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/LinearTransformation.cs @ 15030

Last change on this file since 15030 was 15030, checked in by jzenisek, 7 years ago

#2719 merged with trunk (revision 15029)

File size: 3.5 KB
RevLine 
[10671]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10671]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
[10694]22
[10702]23using System;
[10694]24using System.Collections.Generic;
[10671]25using System.Linq;
[10694]26using HeuristicLab.Common;
[10671]27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
[10702]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[10770]31
[11068]32namespace HeuristicLab.Problems.DataAnalysis {
[12612]33  [StorableClass]
[10909]34  [Item("Linear Transformation", "f(x) = k * x + d | Represents a linear transformation with multiplication and addition.")]
[10694]35  public class LinearTransformation : Transformation<double> {
[10777]36    protected const string MultiplierParameterName = "Multiplier";
37    protected const string AddendParameterName = "Addend";
[10671]38
39    #region Parameters
[10777]40    public IValueParameter<DoubleValue> MultiplierParameter {
41      get { return (IValueParameter<DoubleValue>)Parameters[MultiplierParameterName]; }
[10671]42    }
[10777]43    public IValueParameter<DoubleValue> AddendParameter {
44      get { return (IValueParameter<DoubleValue>)Parameters[AddendParameterName]; }
[10671]45    }
46    #endregion
47
[10702]48    #region properties
[10932]49    public override string ShortName {
50      get { return "Lin"; }
51    }
[10777]52    public double Multiplier {
53      get { return MultiplierParameter.Value.Value; }
[15030]54      set {
[10777]55        MultiplierParameter.Value.Value = value;
[10775]56      }
[10671]57    }
58
[10777]59    public double Addend {
60      get { return AddendParameter.Value.Value; }
[15030]61      set {
[10777]62        AddendParameter.Value.Value = value;
[10775]63      }
[10671]64    }
[10702]65    #endregion
[10671]66
[10702]67    [StorableConstructor]
68    protected LinearTransformation(bool deserializing) : base(deserializing) { }
[11114]69    protected LinearTransformation(LinearTransformation original, Cloner cloner)
[10702]70      : base(original, cloner) {
[10671]71    }
[10702]72    public LinearTransformation(IEnumerable<string> allowedColumns)
73      : base(allowedColumns) {
[10808]74      Parameters.Add(new ValueParameter<DoubleValue>(MultiplierParameterName, "k | Multiplier for linear transformation", new DoubleValue(1.0)));
75      Parameters.Add(new ValueParameter<DoubleValue>(AddendParameterName, "d | Addend for linear transformation", new DoubleValue(0.0)));
[10702]76    }
[10671]77
[10694]78    public override IDeepCloneable Clone(Cloner cloner) {
[10702]79      return new LinearTransformation(this, cloner);
[10671]80    }
81
[10702]82    public override IEnumerable<double> Apply(IEnumerable<double> data) {
[12612]83      var m = Multiplier;
84      var a = Addend;
85      return data.Select(e => e * m + a);
[10671]86    }
[10702]87
88    public override bool Check(IEnumerable<double> data, out string errorMsg) {
89      errorMsg = null;
[12612]90      if (Multiplier.IsAlmost(0.0)) {
[10821]91        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);
92        return false;
[10702]93      }
[10821]94      return true;
[10702]95    }
[10671]96  }
97}
Note: See TracBrowser for help on using the repository browser.