Changeset 8523
- Timestamp:
- 08/23/12 17:55:05 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaAlgorithm.cs
r8471 r8523 141 141 var attributes = scaledData.GetLength(1); 142 142 143 var penalties = new Dictionary<double, Dictionary<double, double>>(); 144 foreach (var c in data.ClassValues) { 145 penalties[c] = new Dictionary<double, double>(); 146 foreach (var r in data.ClassValues) 147 penalties[c][r] = data.GetClassificationPenalty(c, r); 148 } 149 143 150 alglib.mincgstate state; 144 151 alglib.mincgreport rep; … … 147 154 alglib.mincgsetxrep(state, true); 148 155 int neighborSampleSize = neighborSamples; 149 Optimize(state, scaledData, classes, dimensions, neighborSampleSize, cancellation, reporter);156 Optimize(state, scaledData, classes, penalties, dimensions, neighborSampleSize, cancellation, reporter); 150 157 alglib.mincgresults(state, out matrix, out rep); 151 158 … … 159 166 } 160 167 161 private static void Optimize(alglib.mincgstate state, double[,] data, double[] classes, int dimensions, int neighborSampleSize, CancellationToken cancellation, Reporter reporter) {168 private static void Optimize(alglib.mincgstate state, double[,] data, double[] classes, Dictionary<double, Dictionary<double, double>> penalties, int dimensions, int neighborSampleSize, CancellationToken cancellation, Reporter reporter) { 162 169 while (alglib.mincgiteration(state)) { 163 170 if (cancellation.IsCancellationRequested) break; 164 171 if (state.needfg) { 165 Gradient(state.x, ref state.innerobj.f, state.innerobj.g, data, classes, dimensions, neighborSampleSize);172 Gradient(state.x, ref state.innerobj.f, state.innerobj.g, data, classes, penalties, dimensions, neighborSampleSize); 166 173 continue; 167 174 } … … 175 182 } 176 183 177 private static void Gradient(double[] A, ref double func, double[] grad, double[,] data, double[] classes, int dimensions, int neighborSampleSize) {184 private static void Gradient(double[] A, ref double func, double[] grad, double[,] data, double[] classes, Dictionary<double, Dictionary<double, double>> penalties, int dimensions, int neighborSampleSize) { 178 185 var instances = data.GetLength(0); 179 186 var attributes = data.GetLength(1); … … 208 215 double val; 209 216 var pi = new double[instances]; 217 func = 0; 210 218 while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) { 211 if (classes[r].IsAlmost(classes[c])) { 212 pi[r] += val; 213 } 214 } 215 219 double vp = val * penalties[classes[r]][classes[c]]; 220 pi[r] += vp; 221 func += vp; 222 } 223 224 t0 = 0; t1 = 0; 216 225 var innerSum = new double[attributes, attributes]; 217 226 while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) { 218 227 var vector = new Matrix(GetRow(data, r)).Subtract(new Matrix(GetRow(data, c))); 219 228 vector.OuterProduct(vector).Multiply(val * pi[r]).AddTo(innerSum); 220 221 if (classes[r].IsAlmost(classes[c])) { 222 vector.OuterProduct(vector).Multiply(-val).AddTo(innerSum); 223 } 224 } 225 226 func = -pi.Sum(); 229 vector.OuterProduct(vector).Multiply(-val * penalties[classes[r]][classes[c]]).AddTo(innerSum); 230 } 227 231 228 232 r = 0; 229 var newGrad = AMatrix.Multiply( -2.0).Transpose().Multiply(new Matrix(innerSum)).Transpose();233 var newGrad = AMatrix.Multiply(2.0).Transpose().Multiply(new Matrix(innerSum)).Transpose(); 230 234 foreach (var g in newGrad) { 231 235 grad[r++] = g; … … 238 242 if (!Results.ContainsKey("Optimization")) { 239 243 qualities = new DataTable("Optimization"); 240 qualities.Rows.Add(new DataRow(" Quality", string.Empty));244 qualities.Rows.Add(new DataRow("Penalty", string.Empty)); 241 245 Results.Add(new Result("Optimization", qualities)); 242 246 } else qualities = (DataTable)Results["Optimization"].Value; 243 qualities.Rows[" Quality"].Values.Add(-func / instances);244 245 if (!Results.ContainsKey(" Quality")) {246 Results.Add(new Result(" Quality", new DoubleValue(-func / instances)));247 } else ((DoubleValue)Results[" Quality"].Value).Value = -func / instances;247 qualities.Rows["Penalty"].Values.Add(func / instances); 248 249 if (!Results.ContainsKey("Penalty")) { 250 Results.Add(new Result("Penalty", new DoubleValue(func / instances))); 251 } else ((DoubleValue)Results["Penalty"].Value).Value = func / instances; 248 252 } 249 253
Note: See TracChangeset
for help on using the changeset viewer.