-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathProblem.cs
More file actions
190 lines (170 loc) · 5.74 KB
/
Copy pathProblem.cs
File metadata and controls
190 lines (170 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* 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 <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Globalization;
namespace Bigtree.Algorithm.SVM
{
/// <summary>
/// Encapsulates a problem, or set of vectors which must be classified.
/// </summary>
[Serializable]
public class Problem
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="count">Number of vectors</param>
/// <param name="y">The class labels</param>
/// <param name="x">Vector data.</param>
/// <param name="maxIndex">Maximum index for a vector</param>
public Problem(int count, double[] y, Node[][] x, int maxIndex)
{
Count = count;
Y = y;
X = x;
MaxIndex = maxIndex;
}
/// <summary>
/// Empty Constructor. Nothing is initialized.
/// </summary>
public Problem()
{
}
/// <summary>
/// Number of vectors.
/// </summary>
public int Count { get; set; }
/// <summary>
/// Class labels.
/// </summary>
public double[] Y { get; set; }
/// <summary>
/// Vector data.
/// </summary>
public Node[][] X { get; set; }
/// <summary>
/// Maximum index for a vector.
/// </summary>
public int MaxIndex { get; set; }
public override bool Equals(object obj)
{
Problem other = obj as Problem;
if (other == null)
return false;
return other.Count == Count &&
other.MaxIndex == MaxIndex &&
other.X.IsEqual(X) &&
other.Y.IsEqual(Y);
}
public override int GetHashCode()
{
return Count.GetHashCode() +
MaxIndex.GetHashCode() +
X.ComputeHashcode() +
Y.ComputeHashcode();
}
/// <summary>
/// Reads a problem from a stream.
/// </summary>
/// <param name="stream">Stream to read from</param>
/// <returns>The problem</returns>
public static Problem Read(Stream stream)
{
TemporaryCulture.Start();
StreamReader input = new StreamReader(stream);
List<double> vy = new List<double>();
List<Node[]> vx = new List<Node[]>();
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);
}
/// <summary>
/// Writes a problem to a stream.
/// </summary>
/// <param name="stream">The stream to write the problem to.</param>
/// <param name="problem">The problem to write.</param>
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:0.000000}", problem.X[i][j].Index, problem.X[i][j].Value);
output.Write("\n");
}
output.Flush();
TemporaryCulture.Stop();
}
/// <summary>
/// Reads a Problem from a file.
/// </summary>
/// <param name="filename">The file to read from.</param>
/// <returns>the Probem</returns>
public static Problem Read(string filename)
{
FileStream input = File.OpenRead(filename);
try
{
return Read(input);
}
finally
{
input.Close();
}
}
/// <summary>
/// Writes a problem to a file. This will overwrite any previous data in the file.
/// </summary>
/// <param name="filename">The file to write to</param>
/// <param name="problem">The problem to write</param>
public static void Write(string filename, Problem problem)
{
FileStream output = File.Open(filename, FileMode.Create);
try
{
Write(output, problem);
}
finally
{
output.Close();
}
}
}
}