Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/08/17 22:32:46 (7 years ago)
Author:
gkronber
Message:

#2789 work in progress

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/MathNetNumerics-Exploration-2789/HeuristicLab.Algorithms.DataAnalysis.Experimental/SymbolicRegressionConstantOptimizationEvaluator.cs

    r15313 r15314  
    191191        //   , 0, s, 2, initialConstants.Length);
    192192      }
    193       s = Enumerable.Repeat(1.0, c.Length).ToArray();
     193      s = Enumerable.Repeat(0.01, c.Length).ToArray();
    194194
    195195      double[] originalConstants = (double[])c.Clone();
     
    200200
    201201      IDataset ds = problemData.Dataset;
    202       double[,] x = new double[rows.Count(), parameters.Count];
     202      double[,] x = new double[ds.Rows, parameters.Count];
    203203      int col = 0;
    204204      int row = 0;
    205205      foreach (var info in parameterEntries) {
    206206        row = 0;
    207         foreach (var r in rows) {
     207        // copy training rows
     208        foreach (var r in rows) {
    208209          if (ds.VariableHasType<double>(info.variableName)) {
    209210            x[row, col] = ds.GetDoubleValue(info.variableName, r + info.lag);
     
    218219
    219220      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) constraint-type", rIdx)));
    221224      double[,] constraints = new double[constraintRows.Count(), parameters.Count + 1]; // +1 for constraint for f(x)
    222225      string[,] comp = new string[constraintRows.Count(), parameters.Count + 1];
     
    243246        col++;
    244247      }
     248
    245249      // f(x) constraint
    246250      row = 0;
     
    254258      }
    255259
    256 
    257260      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);
    263263      double rho = 1000;
    264264      int outeriters = 3;
     
    292292
    293293      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)) {
    295297        UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights);
    296298        return originalQuality;
     
    317319
    318320    private static alglib.ndimensional_jac CreateJac(
    319       double[,] x, // x longer than y
     321      double[,] x, // x is larger than y
    320322      double[] y, // only targets
    321       double[,] constraints, // df/d(xi), same order as for x
    322       string[,] comparison, // {LEQ, GEQ, EQ }
    323       Func<DV, D> func,
    324       Func<DV, DV> 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) {
    325327      return (double[] c, double[] fi, double[,] jac, object o) => {
    326328        // objective function is sum of squared errors
     
    338340
    339341          double f = (double)func(p);
    340           double[] g = (double[])func_grad(p);
     342          double[] g = (double[])AD.Grad(func, p);
    341343          var e = y[rowIdx] - f;
    342344          fi[0] += e * e;
     
    348350
    349351        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) constraint-type", rIdx)));
    351353
    352354        // 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("constraint-type"))) {
     357            if (ds.GetStringValue(varName, rowIdx) == "EQ") {
    356358              throw new NotSupportedException();
    357359            }
     
    359361        }
    360362        // ineq constraints
    361         for (int rowIdx = 0; rowIdx < constraintRows; rowIdx++) {
     363        foreach (var rowIdx in constraintRows) {
     364XXX
    362365          for (int colIdx = 0; colIdx < constraints.GetLength(1); colIdx++) {
    363366            // there is a constraint value
     
    373376
    374377                fi[fidx] = factor * ((double)(func(p)) - constraints[rowIdx, colIdx]);
    375                 var g = (double[])func_grad(p);
     378                var g = (double[])AD.Grad(func, p);
    376379                for (int jacIdx = 0; jacIdx < c.Length; jacIdx++) {
    377380                  jac[fidx, jacIdx] = factor * g[nParams + jacIdx]; // skip the elements for the variable values
     
    380383              } else {
    381384                // df / dxi constraint
    382                 var g = (double[])func_grad(p);
     385                var g = (double[])AD.Grad(func, p);
    383386                fi[fidx] = factor * g[colIdx];
    384387                var hess = AD.Hessian(func, p);
Note: See TracChangeset for help on using the changeset viewer.