Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/RangeTransform.cs @ 8471

Last change on this file since 8471 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 8.2 KB
Line 
1/*
2 * SVM.NET Library
3 * Copyright (C) 2008 Matthew Johnson
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20using System;
21using System.IO;
22
23namespace SVM {
24  /// <summary>
25  /// Class which encapsulates a range transformation.
26  /// </summary>
27  public class RangeTransform : IRangeTransform {
28    /// <summary>
29    /// Default lower bound for scaling (-1).
30    /// </summary>
31    public const int DEFAULT_LOWER_BOUND = -1;
32    /// <summary>
33    /// Default upper bound for scaling (1).
34    /// </summary>
35    public const int DEFAULT_UPPER_BOUND = 1;
36
37    /// <summary>
38    /// Determines the Range transform for the provided problem.  Uses the default lower and upper bounds.
39    /// </summary>
40    /// <param name="prob">The Problem to analyze</param>
41    /// <returns>The Range transform for the problem</returns>
42    public static RangeTransform Compute(Problem prob) {
43      return Compute(prob, DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND);
44    }
45    /// <summary>
46    /// Determines the Range transform for the provided problem.
47    /// </summary>
48    /// <param name="prob">The Problem to analyze</param>
49    /// <param name="lowerBound">The lower bound for scaling</param>
50    /// <param name="upperBound">The upper bound for scaling</param>
51    /// <returns>The Range transform for the problem</returns>
52    public static RangeTransform Compute(Problem prob, double lowerBound, double upperBound) {
53      double[] minVals = new double[prob.MaxIndex];
54      double[] maxVals = new double[prob.MaxIndex];
55      for (int i = 0; i < prob.MaxIndex; i++) {
56        minVals[i] = double.MaxValue;
57        maxVals[i] = double.MinValue;
58      }
59      for (int i = 0; i < prob.Count; i++) {
60        for (int j = 0; j < prob.X[i].Length; j++) {
61          int index = prob.X[i][j].Index - 1;
62          double value = prob.X[i][j].Value;
63          minVals[index] = Math.Min(minVals[index], value);
64          maxVals[index] = Math.Max(maxVals[index], value);
65        }
66      }
67      for (int i = 0; i < prob.MaxIndex; i++) {
68        if (minVals[i] == double.MaxValue || maxVals[i] == double.MinValue) {
69          minVals[i] = 0;
70          maxVals[i] = 0;
71        }
72      }
73      return new RangeTransform(minVals, maxVals, lowerBound, upperBound);
74    }
75
76    private double[] _inputStart;
77    private double[] _inputScale;
78    private double _outputStart;
79    private double _outputScale;
80    private int _length;
81
82    /// <summary>
83    /// Constructor.
84    /// </summary>
85    /// <param name="minValues">The minimum values in each dimension.</param>
86    /// <param name="maxValues">The maximum values in each dimension.</param>
87    /// <param name="lowerBound">The desired lower bound for all dimensions.</param>
88    /// <param name="upperBound">The desired upper bound for all dimensions.</param>
89    public RangeTransform(double[] minValues, double[] maxValues, double lowerBound, double upperBound) {
90      _length = minValues.Length;
91      if (maxValues.Length != _length)
92        throw new Exception("Number of max and min values must be equal.");
93      _inputStart = new double[_length];
94      _inputScale = new double[_length];
95      for (int i = 0; i < _length; i++) {
96        _inputStart[i] = minValues[i];
97        _inputScale[i] = maxValues[i] - minValues[i];
98      }
99      _outputStart = lowerBound;
100      _outputScale = upperBound - lowerBound;
101    }
102    private RangeTransform(double[] inputStart, double[] inputScale, double outputStart, double outputScale, int length) {
103      _inputStart = inputStart;
104      _inputScale = inputScale;
105      _outputStart = outputStart;
106      _outputScale = outputScale;
107      _length = length;
108    }
109    /// <summary>
110    /// Transforms the input array based upon the values provided.
111    /// </summary>
112    /// <param name="input">The input array</param>
113    /// <returns>A scaled array</returns>
114    public Node[] Transform(Node[] input) {
115      Node[] output = new Node[input.Length];
116      for (int i = 0; i < output.Length; i++) {
117        int index = input[i].Index;
118        double value = input[i].Value;
119        output[i] = new Node(index, Transform(value, index));
120      }
121      return output;
122    }
123
124    /// <summary>
125    /// Transforms this an input value using the scaling transform for the provided dimension.
126    /// </summary>
127    /// <param name="input">The input value to transform</param>
128    /// <param name="index">The dimension whose scaling transform should be used</param>
129    /// <returns>The scaled value</returns>
130    public double Transform(double input, int index) {
131      index--;
132      double tmp = input - _inputStart[index];
133      if (_inputScale[index] == 0)
134        return 0;
135      tmp /= _inputScale[index];
136      tmp *= _outputScale;
137      return tmp + _outputStart;
138    }
139    /// <summary>
140    /// Writes this Range transform to a stream.
141    /// </summary>
142    /// <param name="stream">The stream to write to</param>
143    /// <param name="r">The range to write</param>
144    public static void Write(Stream stream, RangeTransform r) {
145      TemporaryCulture.Start();
146
147      StreamWriter output = new StreamWriter(stream);
148      output.WriteLine(r._length);
149      output.Write(r._inputStart[0].ToString("r"));
150      for (int i = 1; i < r._inputStart.Length; i++)
151        output.Write(" " + r._inputStart[i].ToString("r"));
152      output.WriteLine();
153      output.Write(r._inputScale[0].ToString("r"));
154      for (int i = 1; i < r._inputScale.Length; i++)
155        output.Write(" " + r._inputScale[i].ToString("r"));
156      output.WriteLine();
157      output.WriteLine("{0} {1}", r._outputStart.ToString("r"), r._outputScale.ToString("r"));
158      output.Flush();
159
160      TemporaryCulture.Stop();
161    }
162
163    /// <summary>
164    /// Writes this Range transform to a file.    This will overwrite any previous data in the file.
165    /// </summary>
166    /// <param name="outputFile">The file to write to</param>
167    /// <param name="r">The Range to write</param>
168    public static void Write(string outputFile, RangeTransform r) {
169      FileStream s = File.Open(outputFile, FileMode.Create);
170      try {
171        Write(s, r);
172      }
173      finally {
174        s.Close();
175      }
176    }
177
178    /// <summary>
179    /// Reads a Range transform from a file.
180    /// </summary>
181    /// <param name="inputFile">The file to read from</param>
182    /// <returns>The Range transform</returns>
183    public static RangeTransform Read(string inputFile) {
184      FileStream s = File.OpenRead(inputFile);
185      try {
186        return Read(s);
187      }
188      finally {
189        s.Close();
190      }
191    }
192
193    /// <summary>
194    /// Reads a Range transform from a stream.
195    /// </summary>
196    /// <param name="stream">The stream to read from</param>
197    /// <returns>The Range transform</returns>
198    public static RangeTransform Read(Stream stream) {
199      return Read(new StreamReader(stream));
200    }
201
202    public static RangeTransform Read(TextReader input) {
203      TemporaryCulture.Start();
204
205      int length = int.Parse(input.ReadLine());
206      double[] inputStart = new double[length];
207      double[] inputScale = new double[length];
208      string[] parts = input.ReadLine().Split();
209      for (int i = 0; i < length; i++)
210        inputStart[i] = double.Parse(parts[i]);
211      parts = input.ReadLine().Split();
212      for (int i = 0; i < length; i++)
213        inputScale[i] = double.Parse(parts[i]);
214      parts = input.ReadLine().Split();
215      double outputStart = double.Parse(parts[0]);
216      double outputScale = double.Parse(parts[1]);
217
218      TemporaryCulture.Stop();
219
220      return new RangeTransform(inputStart, inputScale, outputStart, outputScale, length);
221    }
222  }
223}
Note: See TracBrowser for help on using the repository browser.