1 package liblinear;
2
3 import static liblinear.Linear.copyOf;
4
5 import java.io.File;
6 import java.io.IOException;
7 import java.io.Reader;
8 import java.io.Serializable;
9 import java.io.Writer;
10 import java.util.Arrays;
11
12
13
14
15
16
17
18 public final class Model implements Serializable {
19
20 private static final long serialVersionUID = -6456047576741854834L;
21
22 double bias;
23
24
25 int[] label;
26
27 int nr_class;
28
29 int nr_feature;
30
31 SolverType solverType;
32
33
34 double[] w;
35
36
37
38
39 public int getNrClass() {
40 return nr_class;
41 }
42
43
44
45
46 public int getNrFeature() {
47 return nr_feature;
48 }
49
50 public int[] getLabels() {
51 return copyOf(label, nr_class);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public double[] getFeatureWeights() {
74 return Linear.copyOf(w, w.length);
75 }
76
77
78
79
80 public double getBias() {
81 return bias;
82 }
83
84 @Override
85 public String toString() {
86 StringBuilder sb = new StringBuilder("Model");
87 sb.append(" bias=").append(bias);
88 sb.append(" nr_class=").append(nr_class);
89 sb.append(" nr_feature=").append(nr_feature);
90 sb.append(" solverType=").append(solverType);
91 return sb.toString();
92 }
93
94 @Override
95 public int hashCode() {
96 final int prime = 31;
97 int result = 1;
98 long temp;
99 temp = Double.doubleToLongBits(bias);
100 result = prime * result + (int)(temp ^ (temp >>> 32));
101 result = prime * result + Arrays.hashCode(label);
102 result = prime * result + nr_class;
103 result = prime * result + nr_feature;
104 result = prime * result + ((solverType == null) ? 0 : solverType.hashCode());
105 result = prime * result + Arrays.hashCode(w);
106 return result;
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) return true;
112 if (obj == null) return false;
113 if (getClass() != obj.getClass()) return false;
114 Model other = (Model)obj;
115 if (Double.doubleToLongBits(bias) != Double.doubleToLongBits(other.bias)) return false;
116 if (!Arrays.equals(label, other.label)) return false;
117 if (nr_class != other.nr_class) return false;
118 if (nr_feature != other.nr_feature) return false;
119 if (solverType == null) {
120 if (other.solverType != null) return false;
121 } else if (!solverType.equals(other.solverType)) return false;
122 if (!equals(w, other.w)) return false;
123 return true;
124 }
125
126
127
128
129
130
131 protected static boolean equals(double[] a, double[] a2) {
132 if (a == a2) return true;
133 if (a == null || a2 == null) return false;
134
135 int length = a.length;
136 if (a2.length != length) return false;
137
138 for (int i = 0; i < length; i++)
139 if (a[i] != a2[i]) return false;
140
141 return true;
142 }
143
144
145
146
147 public void save(File file) throws IOException {
148 Linear.saveModel(file, this);
149 }
150
151
152
153
154 public void save(Writer writer) throws IOException {
155 Linear.saveModel(writer, this);
156 }
157
158
159
160
161 public static Model load(File file) throws IOException {
162 return Linear.loadModel(file);
163 }
164
165
166
167
168 public static Model load(Reader inputReader) throws IOException {
169 return Linear.loadModel(inputReader);
170 }
171 }