Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/30/21 18:01:39 (4 years ago)
Author:
gkronber
Message:

#3106: minor changes to alg but no big improvement

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/ContinuedFractionRegression/Algorithm.cs

    r17971 r17972  
    2424      var x = problemData.Dataset.ToArray(problemData.AllowedInputVariables.Concat(new[] { problemData.TargetVariable }),
    2525        problemData.TrainingIndices);
    26       var nVars = x.GetLength(1);
     26      var nVars = x.GetLength(1) - 1;
    2727      var rand = new MersenneTwister(31415);
    28       CFRAlgorithm(nVars, depth: 4, 0.10, x, out var best, out var bestObj, rand, numGen: 200, cancellationToken);
     28      CFRAlgorithm(nVars, depth: 6, 0.10, x, out var best, out var bestObj, rand, numGen: 200, stagnatingGens: 5, cancellationToken);
    2929    }
    3030
    3131    private void CFRAlgorithm(int nVars, int depth, double mutationRate, double[,] trainingData,
    3232      out ContinuedFraction best, out double bestObj,
    33       IRandom rand, int numGen,
    34       CancellationToken cancellation) {
     33      IRandom rand, int numGen, int stagnatingGens,
     34      CancellationToken cancellationToken) {
    3535      /* Algorithm 1 */
    3636      /* Generate initial population by a randomized algorithm */
     
    3838      best = pop.pocket;
    3939      bestObj = pop.pocketObjValue;
    40 
    41       for (int gen = 1; gen <= numGen && !cancellation.IsCancellationRequested; gen++) {
     40      var bestObjGen = 0;
     41      for (int gen = 1; gen <= numGen && !cancellationToken.IsCancellationRequested; gen++) {
    4242        /* mutate each current solution in the population */
    4343        var pop_mu = Mutate(pop, mutationRate, rand);
     
    4747        /* local search optimization of current solutions */
    4848        foreach (var agent in pop_r.IterateLevels()) {
    49           LocalSearchSimplex(agent.current, trainingData, rand);
     49          LocalSearchSimplex(agent.current, ref agent.currentObjValue, trainingData, rand);
    5050        }
    5151
    5252        foreach (var agent in pop_r.IteratePostOrder()) agent.MaintainInvariant(); // Deviates from Alg1 in paper
    53 
    54         /* TODO
    55         if (stagnating(curQuality, bestQuality, numStagnatingGensForReset)) {
    56           Reset(pop_r.root);
    57         }
    58         */
    5953
    6054        /* replace old population with evolved population */
     
    6559          best = pop.pocket;
    6660          bestObj = pop.pocketObjValue;
     61          bestObjGen = gen;
    6762          Results.AddOrUpdateResult("MSE (best)", new DoubleValue(bestObj));
     63        }
     64
     65
     66        if (gen > bestObjGen + stagnatingGens) {
     67          bestObjGen = gen; // wait at least stagnatingGens until resetting again
     68          // Reset(pop, nVars, depth, rand, trainingData);
     69          InitialPopulation(nVars, depth, rand, trainingData);
    6870        }
    6971      }
     
    9597      return pop;
    9698    }
     99
     100    // TODO: reset is not described in the paper
     101    private void Reset(Agent root, int nVars, int depth, IRandom rand, double[,] trainingData) {
     102      root.pocket = new ContinuedFraction(nVars, depth, rand);
     103      root.current = new ContinuedFraction(nVars, depth, rand);
     104
     105      root.currentObjValue = Evaluate(root.current, trainingData);
     106      root.pocketObjValue = Evaluate(root.pocket, trainingData);
     107
     108      /* within each agent, the pocket solution always holds the better value of guiding
     109       * function than its current solution
     110       */
     111      root.MaintainInvariant();
     112    }
     113
     114
    97115
    98116    private Agent RecombinePopulation(Agent pop, IRandom rand, int nVars) {
     
    182200             agent.currentObjValue > 2 * agent.pocketObjValue)
    183201            ToggleVariables(agent.current, rand); // major mutation
    184           else ModifyVariable(agent.current, rand); // soft mutation
     202          else
     203            ModifyVariable(agent.current, rand); // soft mutation
    185204        }
    186205      }
     
    258277        var hi = cfrac.h[i];
    259278        var hi1 = cfrac.h[i - 1];
    260         var denom = hi.beta + dot(dataPoint, hi.coef) + res;
    261         var numerator = hi1.beta + dot(dataPoint, hi1.coef);
     279        var denom = hi.beta + dot(hi.vars, hi.coef, dataPoint) + res;
     280        var numerator = hi1.beta + dot(hi1.vars, hi1.coef, dataPoint);
    262281        res = numerator / denom;
    263282      }
     
    265284    }
    266285
    267     private static double dot(double[] x, double[] y) {
     286    private static double dot(bool[] filter, double[] x, double[] y) {
    268287      var s = 0.0;
    269288      for (int i = 0; i < x.Length; i++)
    270         s += x[i] * y[i];
     289        if (filter[i])
     290          s += x[i] * y[i];
    271291      return s;
    272292    }
    273293
    274294
    275     private static ContinuedFraction LocalSearchSimplex(ContinuedFraction ch, double[,] trainingData, IRandom rand) {
     295    private static void LocalSearchSimplex(ContinuedFraction ch, ref double quality, double[,] trainingData, IRandom rand) {
    276296      double uniformPeturbation = 1.0;
    277297      double tolerance = 1e-3;
     
    281301      int numSelectedRows = numRows / 5; // 20% of the training samples
    282302
     303      quality = Evaluate(ch, trainingData); // get quality with origial coefficients
     304
    283305      double[] origCoeff = ExtractCoeff(ch);
    284       if (origCoeff.Length == 0) return ch; // no parameters to optimize
    285 
    286       var bestQuality = Evaluate(ch, trainingData); // get quality with origial coefficients
     306      if (origCoeff.Length == 0) return; // no parameters to optimize
     307
     308      var bestQuality = quality;
    287309      var bestCoeff = origCoeff;
    288310
     
    317339
    318340      SetCoeff(ch, bestCoeff);
    319       return ch;
     341      quality = bestQuality;
    320342    }
    321343
     
    338360      var coeff = new List<double>();
    339361      foreach (var hi in ch.h) {
     362        coeff.Add(hi.beta);
    340363        for (int vIdx = 0; vIdx < hi.vars.Length; vIdx++) {
    341364          if (hi.vars[vIdx]) coeff.Add(hi.coef[vIdx]);
     
    348371      int k = 0;
    349372      foreach (var hi in ch.h) {
     373        hi.beta = curCoeff[k++];
    350374        for (int vIdx = 0; vIdx < hi.vars.Length; vIdx++) {
    351375          if (hi.vars[vIdx]) hi.coef[vIdx] = curCoeff[k++];
Note: See TracChangeset for help on using the changeset viewer.