Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/17/17 09:56:30 (5 years ago)
Author:
gkronber
Message:

#2796: debugging

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs

    r15416 r15420  
    7171    // TODO: is it OK to initialize all constants to 1 (Obj 2)?
    7272    // TODO: improve memory usage
     73    // TODO: support empty test partition
    7374    #region static API
    7475
     
    138139      private readonly double[] predBuf, testPredBuf;
    139140      private readonly double[][] gradBuf;
     141
     142      // debugging stats
     143      // calculate for each level the number of alternatives the average 'inequality' of tries and 'inequality' of quality over the alternatives for each trie
     144      // inequality can be calculated using the Gini coefficient
     145      internal readonly double[] giniCoeffs = new double[100];
     146     
    140147
    141148      public State(IRegressionProblemData problemData, uint randSeed, int maxVariables, bool scaleVariables,
     
    436443        }
    437444      }
     445
    438446      #endregion
     447
     448#if DEBUG
     449      internal void ClearStats() {
     450        for (int i = 0; i < giniCoeffs.Length; i++) giniCoeffs[i] = -1;
     451      }
     452      internal void WriteStats() {
     453        Console.WriteLine(string.Join("\t", giniCoeffs.TakeWhile(x => x >= 0).Select(x => string.Format("{0:N3}", x))));
     454      }
     455
     456#endif
     457
    439458    }
    440459
     
    496515      bool success = false;
    497516      do {
     517#if DEBUG
     518        mctsState.ClearStats();
     519#endif
    498520        automaton.Reset();
    499521        success = TryTreeSearchRec2(rand, tree, automaton, eval, treePolicy, mctsState, out q);
     
    502524      mctsState.effectiveRollouts++;
    503525
     526#if DEBUG
     527      mctsState.WriteStats();
     528#endif
    504529      //if (mctsState.effectiveRollouts % 100 == 1) {
    505         // Console.WriteLine(WriteTree(tree, mctsState));
    506         // Console.WriteLine(TraceTree(tree, mctsState));
     530      // Console.WriteLine(WriteTree(tree, mctsState));
     531      // Console.WriteLine(TraceTree(tree, mctsState));
    507532      //}
    508533      return q;
    509534    }
    510 
    511535
    512536    // search forward
     
    540564            selectedIdx = treePolicy.Select(state.children[tree].Select(ch => ch.actionStatistics), rand);
    541565          }
     566
     567          // STATS
     568          state.giniCoeffs[tree.level] = InequalityCoefficient(state.children[tree].Select(ch => (double)ch.actionStatistics.AverageQuality));
     569
    542570          tree = state.children[tree][selectedIdx];
    543571
     
    548576          int nFs;
    549577          automaton.FollowStates(automaton.CurrentState, out possibleFollowStates, out nFs);
    550           while (nFs == 1 && !automaton.IsEvalState(possibleFollowStates[0]) && !automaton.IsFinalState(possibleFollowStates[0])) {
     578          while (automaton.CurrentState != tree.state && nFs == 1 &&
     579            !automaton.IsEvalState(possibleFollowStates[0]) && !automaton.IsFinalState(possibleFollowStates[0])) {
    551580            automaton.Goto(possibleFollowStates[0]);
    552581            automaton.FollowStates(automaton.CurrentState, out possibleFollowStates, out nFs);
     
    658687    }
    659688
     689    private static double InequalityCoefficient(IEnumerable<double> xs) {
     690      var arr = xs.ToArray();
     691      var sad = 0.0;
     692      var sum = 0.0;
     693
     694      for(int i=0;i<arr.Length;i++) {
     695        for(int j=0;j<arr.Length;j++) {
     696          sad += Math.Abs(arr[i] - arr[j]);
     697          sum += arr[j];
     698        }
     699      }
     700      return 0.5 * sad / sum;     
     701    }
    660702
    661703    private static double TransformQuality(double q) {
     
    871913      return Disassembler.CodeToString(code);
    872914    }
     915
    873916
    874917    private static string WriteStatistics(Tree tree, State state) {
Note: See TracChangeset for help on using the changeset viewer.