1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using System.Linq;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Modeling {
|
---|
30 | public class LinearScaler : OperatorBase {
|
---|
31 | public LinearScaler()
|
---|
32 | : base() {
|
---|
33 | AddVariableInfo(new VariableInfo("Values", "Matrix of predicted and original values that should be scaled", typeof(DoubleMatrixData), VariableKind.In | VariableKind.Out));
|
---|
34 | AddVariableInfo(new VariableInfo("Alpha", "Maximization problem", typeof(BoolData), VariableKind.New | VariableKind.In));
|
---|
35 | AddVariableInfo(new VariableInfo("Beta", "The best solution of the run", typeof(IScope), VariableKind.New | VariableKind.In));
|
---|
36 | AddVariableInfo(new VariableInfo("UpperEstimationLimit", "Upper limit for estimated value (optional)", typeof(DoubleData), VariableKind.In));
|
---|
37 | AddVariableInfo(new VariableInfo("LowerEstimationLimit", "Lower limit for estimated value (optional)", typeof(DoubleData), VariableKind.In));
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override IOperation Apply(IScope scope) {
|
---|
41 | double[,] values = GetVariableValue<DoubleMatrixData>("Values", scope, true).Data;
|
---|
42 | DoubleData alpha = GetVariableValue<DoubleData>("Alpha", scope, true, false);
|
---|
43 | DoubleData beta = GetVariableValue<DoubleData>("Beta", scope, true, false);
|
---|
44 | DoubleData lowerEstimationLimit = GetVariableValue<DoubleData>("LowerEstimationLimit", scope, true, false);
|
---|
45 | DoubleData upperEstimationLimit = GetVariableValue<DoubleData>("UpperEstimationLimit", scope, true, false);
|
---|
46 |
|
---|
47 | double a, b;
|
---|
48 | if (alpha == null || beta == null) {
|
---|
49 | // one of the variables is missing -> recalculate
|
---|
50 | CalculateScalingParameters(values, out b, out a);
|
---|
51 | scope.AddVariable(new Variable(scope.TranslateName("Alpha"), new DoubleData(a)));
|
---|
52 | scope.AddVariable(new Variable(scope.TranslateName("Beta"), new DoubleData(b)));
|
---|
53 | } else {
|
---|
54 | // both variables are set already
|
---|
55 | a = alpha.Data;
|
---|
56 | b = beta.Data;
|
---|
57 | }
|
---|
58 | // check if upper and lower limit are set and use -Inf and +Inf as a default
|
---|
59 | double lowerLimit = lowerEstimationLimit == null ? double.NegativeInfinity : lowerEstimationLimit.Data;
|
---|
60 | double upperLimit = upperEstimationLimit == null ? double.PositiveInfinity : upperEstimationLimit.Data;
|
---|
61 | // apply scaling
|
---|
62 | Scale(values, a, b, lowerLimit, upperLimit);
|
---|
63 |
|
---|
64 | return null;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | public static void CalculateScalingParameters(double[,] values, out double beta, out double alpha) {
|
---|
69 | var result = from row in Enumerable.Range(0, values.GetLength(0))
|
---|
70 | let t = values[row, SimpleEvaluatorBase.ORIGINAL_INDEX]
|
---|
71 | let e = values[row, SimpleEvaluatorBase.ESTIMATION_INDEX]
|
---|
72 | select new { Estimation = e, Target = t };
|
---|
73 |
|
---|
74 | // calculate alpha and beta on the subset of rows with valid values
|
---|
75 | var filteredResult = result.Where(x => IsValidValue(x.Target) && IsValidValue(x.Estimation));
|
---|
76 | var target = filteredResult.Select(x => x.Target);
|
---|
77 | var estimation = filteredResult.Select(x => x.Estimation);
|
---|
78 | double tMean = target.Sum() / target.Count();
|
---|
79 | double xMean = estimation.Sum() / estimation.Count();
|
---|
80 | double sumXT = 0;
|
---|
81 | double sumXX = 0;
|
---|
82 | foreach (var r in result) {
|
---|
83 | double x = r.Estimation;
|
---|
84 | double t = r.Target;
|
---|
85 | sumXT += (x - xMean) * (t - tMean);
|
---|
86 | sumXX += (x - xMean) * (x - xMean);
|
---|
87 | }
|
---|
88 | if (sumXX != 0) {
|
---|
89 | beta = sumXT / sumXX;
|
---|
90 | } else {
|
---|
91 | beta = 1;
|
---|
92 | }
|
---|
93 | alpha = tMean - beta * xMean;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public static void Scale(double[,] values, double alpha, double beta, double lowerEstimationLimit, double upperEstimationLimit) {
|
---|
97 | int rows = values.GetLength(0);
|
---|
98 | for (int row = 0; row < rows; row++) {
|
---|
99 | double estimatedValue = values[row, SimpleEvaluatorBase.ESTIMATION_INDEX];
|
---|
100 | if (double.IsNaN(estimatedValue))
|
---|
101 | estimatedValue = upperEstimationLimit;
|
---|
102 | values[row, SimpleEvaluatorBase.ESTIMATION_INDEX] =
|
---|
103 | Math.Min(Math.Max(estimatedValue * beta + alpha, lowerEstimationLimit), upperEstimationLimit); ;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | private static bool IsValidValue(double d) {
|
---|
108 | return !double.IsInfinity(d) && !double.IsNaN(d);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|