/*
* SVM.NET Library
* Copyright (C) 2008 Matthew Johnson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace SVM {
///
/// Encapsulates a problem, or set of vectors which must be classified.
///
[Serializable]
public class Problem {
private int _count;
private double[] _Y;
private Node[][] _X;
private int _maxIndex;
///
/// Constructor.
///
/// Number of vectors
/// The class labels
/// Vector data.
/// Maximum index for a vector
public Problem(int count, double[] y, Node[][] x, int maxIndex) {
_count = count;
_Y = y;
_X = x;
_maxIndex = maxIndex;
}
///
/// Empty Constructor. Nothing is initialized.
///
public Problem() {
}
///
/// Number of vectors.
///
public int Count {
get {
return _count;
}
set {
_count = value;
}
}
///
/// Class labels.
///
public double[] Y {
get {
return _Y;
}
set {
_Y = value;
}
}
///
/// Vector data.
///
public Node[][] X {
get {
return _X;
}
set {
_X = value;
}
}
///
/// Maximum index for a vector.
///
public int MaxIndex {
get {
return _maxIndex;
}
set {
_maxIndex = value;
}
}
///
/// Reads a problem from a stream.
///
/// Stream to read from
/// The problem
public static Problem Read(Stream stream) {
TemporaryCulture.Start();
StreamReader input = new StreamReader(stream);
List vy = new List();
List vx = new List();
int max_index = 0;
while (input.Peek() > -1) {
string[] parts = input.ReadLine().Trim().Split();
vy.Add(double.Parse(parts[0]));
int m = parts.Length - 1;
Node[] x = new Node[m];
for (int j = 0; j < m; j++) {
x[j] = new Node();
string[] nodeParts = parts[j + 1].Split(':');
x[j].Index = int.Parse(nodeParts[0]);
x[j].Value = double.Parse(nodeParts[1]);
}
if (m > 0)
max_index = Math.Max(max_index, x[m - 1].Index);
vx.Add(x);
}
TemporaryCulture.Stop();
return new Problem(vy.Count, vy.ToArray(), vx.ToArray(), max_index);
}
///
/// Writes a problem to a stream.
///
/// The stream to write the problem to.
/// The problem to write.
public static void Write(Stream stream, Problem problem) {
TemporaryCulture.Start();
StreamWriter output = new StreamWriter(stream);
for (int i = 0; i < problem.Count; i++) {
output.Write(problem.Y[i]);
for (int j = 0; j < problem.X[i].Length; j++)
output.Write(" {0}:{1}", problem.X[i][j].Index, problem.X[i][j].Value);
output.WriteLine();
}
output.Flush();
TemporaryCulture.Stop();
}
///
/// Reads a Problem from a file.
///
/// The file to read from.
/// the Probem
public static Problem Read(string filename) {
FileStream input = File.OpenRead(filename);
try {
return Read(input);
}
finally {
input.Close();
}
}
///
/// Writes a problem to a file. This will overwrite any previous data in the file.
///
/// The file to write to
/// The problem to write
public static void Write(string filename, Problem problem) {
FileStream output = File.Open(filename, FileMode.Create);
try {
Write(output, problem);
}
finally {
output.Close();
}
}
}
}