Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/27/13 20:17:17 (11 years ago)
Author:
gkronber
Message:

#2026 worked on random search solver (now all examples are working)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GPDL/Examples/symbreg Koza.txt

    r10061 r10086  
    33  double[,] x;
    44  double[] y;
     5  int[] rows;
    56  string[] variableNames;
     7  double[] randomConsts;
     8 
    69  Dictionary<string,int> nameToCol;
    710 
     
    1821
    1922  double RSquared(IEnumerable<double> xs, IEnumerable<double> ys) {
    20      HeuristicLab.Problems.DataAnalysis.OnlineCalculatorError error;
    21      var r2 = HeuristicLab.Problems.DataAnalysis.OnlinePearsonsRSquaredCalculator.Calculate(xs, ys, out error);
    22      if(error == HeuristicLab.Problems.DataAnalysis.OnlineCalculatorError.None) return r2;
    23      else return 0.0;
    24   }
     23    // calculate Pearson's correlation in one pass over xs and ys
     24    double sumx = 0.0;
     25    double sumy = 0.0;
     26    double sumxSq = 0.0;
     27    double sumySq = 0.0;
     28    double sumxy = 0.0;
     29    int n = 0;
     30    var xEnum = xs.GetEnumerator();
     31    var yEnum = ys.GetEnumerator();
     32    while(xEnum.MoveNext() & yEnum.MoveNext()) {
     33      sumx += xEnum.Current;
     34      sumy += yEnum.Current;
     35      sumxSq += xEnum.Current * xEnum.Current;
     36      sumySq += yEnum.Current * yEnum.Current;
     37      sumxy += xEnum.Current * yEnum.Current;
     38      n++;
     39    }
     40    System.Diagnostics.Debug.Assert(!(xEnum.MoveNext() | yEnum.MoveNext()));
     41
     42    double num;
     43    double den;
     44    double r = 0.0;
     45    num = sumxy - ( ( sumx * sumy ) / n );
     46    den = Math.Sqrt( ( sumxSq - ( sumx*sumx ) / n ) *
     47                     ( sumySq - ( sumy*sumy ) / n ) );
     48    if(den > 0){
     49      r = num / den;
     50    }
     51    return r*r;
     52  }
    2553>>
    2654
     
    3260  x = new double[n, 10];
    3361  y = new double[n];
    34   for(int row = 0; row < 500; row++) {
     62  for(int row = 0; row < n; row++) {
    3563    for(int col = 0; col < 10; col++) {
    3664      x[row, col] = rand.NextDouble() * 2.0 - 1.0;
     
    4270             x[row, 2] * x[row, 5] + x[row, 9];
    4371  }
     72 
     73  rows = System.Linq.Enumerable.Range(0, n).ToArray();
     74 
     75  // generate 100 random constants in [-100.0 .. 100.0[
     76  randomConsts = Enumerable.Range(0, 100).Select(i => rand.NextDouble()*200.0 - 100.0).ToArray();
    4477>>
    4578
     
    5588  ERC<<out double val>>
    5689    CONSTRAINTS
    57     val IN RANGE <<-100>> .. <<100>>
     90      val IN SET << randomConsts >>
    5891  .
    5992
    6093  Var<<out string varName>>
    6194    CONSTRAINTS
    62     varName IN SET <<variableNames>>
     95      varName IN SET << variableNames >>
    6396  .
    6497
     
    73106    | Multiplication<<row, out val>>
    74107    | Var<<out varName>>                                   SEM << val = GetValue(x, varName, row); >>
    75     | ERC<<out val>>
     108    /* | ERC<<out val>> */
    76109  .
    77110
     
    89122  .
    90123
    91 MAXIMIZE                                                   /* could also use the keyword MINIMIZE here */
     124MAXIMIZE
    92125  <<
    93     var rows = System.Linq.Enumerable.Range(0, x.GetLength(0));
    94126    var predicted = rows.Select(r => {
    95127      double result;
Note: See TracChangeset for help on using the changeset viewer.