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