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("Logarithm", "Logarithm transformation x' = log(x, Base)")]
|
---|
33 | [StorableClass]
|
---|
34 | public class LogarithmTransformation : Transformation<double> {
|
---|
35 | #region Parameters
|
---|
36 | private FixedValueParameter<DoubleValue> BaseParameter {
|
---|
37 | get { return (FixedValueParameter<DoubleValue>)Parameters["Base"]; }
|
---|
38 | }
|
---|
39 | #endregion
|
---|
40 |
|
---|
41 | #region Properties
|
---|
42 | public double Base {
|
---|
43 | get { return BaseParameter.Value.Value; }
|
---|
44 | set { BaseParameter.Value.Value = value; }
|
---|
45 | }
|
---|
46 | #endregion
|
---|
47 |
|
---|
48 | #region Constructor, Cloning & Persistence
|
---|
49 | public LogarithmTransformation()
|
---|
50 | : base() {
|
---|
51 | Parameters.Add(new FixedValueParameter<DoubleValue>("Base", "Base the logarithm", new DoubleValue(Math.E)));
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected LogarithmTransformation(LogarithmTransformation original, Cloner cloner)
|
---|
55 | : base(original, cloner) {
|
---|
56 | }
|
---|
57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
58 | return new LogarithmTransformation(this, cloner);
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | protected LogarithmTransformation(bool deserializing)
|
---|
63 | : base(deserializing) {
|
---|
64 | }
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | public override bool Check(IEnumerable<double> data, out string errorMessage) {
|
---|
68 | if (data.Any(x => x <= 0)) {
|
---|
69 | errorMessage = "Log is not available for zero or negative values";
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | return base.Check(data, out errorMessage);
|
---|
73 | }
|
---|
74 |
|
---|
75 | public override IEnumerable<double> Apply(IEnumerable<double> data) {
|
---|
76 | return Apply(data, Base);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
|
---|
80 | return InverseApply(data, Base);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public static IEnumerable<double> Apply(IEnumerable<double> data, double @base = Math.E) {
|
---|
84 | return data.Select(x => Math.Log(x, @base));
|
---|
85 | }
|
---|
86 |
|
---|
87 | public static IEnumerable<double> InverseApply(IEnumerable<double> data, double @base = Math.E) {
|
---|
88 | return ExponentialTransformation.Apply(data, @base);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | } |
---|