Changeset 15314
 Timestamp:
 08/08/17 22:32:46 (3 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/MathNetNumericsExploration2789/HeuristicLab.Algorithms.DataAnalysis.Experimental/SymbolicRegressionConstantOptimizationEvaluator.cs
r15313 r15314 191 191 // , 0, s, 2, initialConstants.Length); 192 192 } 193 s = Enumerable.Repeat( 1.0, c.Length).ToArray();193 s = Enumerable.Repeat(0.01, c.Length).ToArray(); 194 194 195 195 double[] originalConstants = (double[])c.Clone(); … … 200 200 201 201 IDataset ds = problemData.Dataset; 202 double[,] x = new double[ rows.Count(), parameters.Count];202 double[,] x = new double[ds.Rows, parameters.Count]; 203 203 int col = 0; 204 204 int row = 0; 205 205 foreach (var info in parameterEntries) { 206 206 row = 0; 207 foreach (var r in rows) { 207 // copy training rows 208 foreach (var r in rows) { 208 209 if (ds.VariableHasType<double>(info.variableName)) { 209 210 x[row, col] = ds.GetDoubleValue(info.variableName, r + info.lag); … … 218 219 219 220 var target = problemData.TargetVariable; 220 var constraintRows = Enumerable.Range(0, problemData.Dataset.Rows).Where(rIdx => double.IsNaN(ds.GetDoubleValue(target, rIdx))); 221 222 // only count constraints 223 var constraintRows = Enumerable.Range(0, problemData.Dataset.Rows).Where(rIdx => !string.IsNullOrEmpty(ds.GetStringValue("f(x) constrainttype", rIdx))); 221 224 double[,] constraints = new double[constraintRows.Count(), parameters.Count + 1]; // +1 for constraint for f(x) 222 225 string[,] comp = new string[constraintRows.Count(), parameters.Count + 1]; … … 243 246 col++; 244 247 } 248 245 249 // f(x) constraint 246 250 row = 0; … … 254 258 } 255 259 256 257 260 double[] y = ds.GetDoubleValues(problemData.TargetVariable, rows).ToArray(); 258 int n = x.GetLength(0); 259 int m = x.GetLength(1); 260 int k = c.Length; 261 262 alglib.ndimensional_jac jac = CreateJac(x, y, constraints, comp, func, AD.Grad(func)); 261 262 alglib.ndimensional_jac jac = CreateJac(x, y, ds, func); 263 263 double rho = 1000; 264 264 int outeriters = 3; … … 292 292 293 293 if (!updateConstantsInTree) UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights); 294 if (originalQuality  quality > 0.001  double.IsNaN(quality)) { 294 if ( 295 // originalQuality  quality > 0.001  296 double.IsNaN(quality)) { 295 297 UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights); 296 298 return originalQuality; … … 317 319 318 320 private static alglib.ndimensional_jac CreateJac( 319 double[,] x, // x longer than y321 double[,] x, // x is larger than y 320 322 double[] y, // only targets 321 double[,] constraints, // df/d(xi), same order as for x322 string[,] comparison, // {LEQ, GEQ, EQ }323 Func<DV, D> func,324 Func<DV, D V> func_grad) {323 // double[,] constraints, // df/d(xi), same order as for x, same number of rows as x less columns 324 // string[,] comparison, // {LEQ, GEQ, EQ } same size as constraints 325 IDataset ds, 326 Func<DV, D> func) { 325 327 return (double[] c, double[] fi, double[,] jac, object o) => { 326 328 // objective function is sum of squared errors … … 338 340 339 341 double f = (double)func(p); 340 double[] g = (double[]) func_grad(p);342 double[] g = (double[])AD.Grad(func, p); 341 343 var e = y[rowIdx]  f; 342 344 fi[0] += e * e; … … 348 350 349 351 int fidx = 1; 350 int constraintRows = constraints.GetLength(0);352 var constraintRows = Enumerable.Range(0, ds.Rows).Where(rIdx => !string.IsNullOrEmpty(ds.GetStringValue("f(x) constrainttype", rIdx))); 351 353 352 354 // eq constraints 353 for (int rowIdx = 0; rowIdx < constraintRows; rowIdx++) {354 for (int colIdx = 0; colIdx < constraints.GetLength(1); colIdx++) {355 if ( comparison[rowIdx, colIdx]== "EQ") {355 foreach (var rowIdx in constraintRows) { 356 foreach (var varName in ds.VariableNames.Where(vn => vn.EndsWith("constrainttype"))) { 357 if (ds.GetStringValue(varName, rowIdx) == "EQ") { 356 358 throw new NotSupportedException(); 357 359 } … … 359 361 } 360 362 // ineq constraints 361 for (int rowIdx = 0; rowIdx < constraintRows; rowIdx++) { 363 foreach (var rowIdx in constraintRows) { 364 XXX 362 365 for (int colIdx = 0; colIdx < constraints.GetLength(1); colIdx++) { 363 366 // there is a constraint value … … 373 376 374 377 fi[fidx] = factor * ((double)(func(p))  constraints[rowIdx, colIdx]); 375 var g = (double[]) func_grad(p);378 var g = (double[])AD.Grad(func, p); 376 379 for (int jacIdx = 0; jacIdx < c.Length; jacIdx++) { 377 380 jac[fidx, jacIdx] = factor * g[nParams + jacIdx]; // skip the elements for the variable values … … 380 383 } else { 381 384 // df / dxi constraint 382 var g = (double[]) func_grad(p);385 var g = (double[])AD.Grad(func, p); 383 386 fi[fidx] = factor * g[colIdx]; 384 387 var hess = AD.Hessian(func, p);
Note: See TracChangeset
for help on using the changeset viewer.