Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/28/17 20:32:53 (7 years ago)
Author:
gkronber
Message:

#2796 more refactoring and working on unit tests

File:
1 edited

Legend:

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

    r15438 r15439  
    2323using System.Collections.Generic;
    2424using System.Diagnostics;
    25 using System.Diagnostics.Contracts;
    2625using System.Linq;
    2726using System.Text;
     
    6160    //       --> TODO: Which heuristics can we apply?
    6261    // TODO: Solve Poly-10
     62    // TODO: rename everything as this is not MCTS anymore
    6363    // TODO: After state unification the recursive backpropagation of results takes a lot of time. How can this be improved?
    6464    // ~~obsolete TODO: Why is the algorithm so slow for rather greedy policies (e.g. low C value in UCB)?
     
    247247      #endregion
    248248
     249
     250#if DEBUG
     251      public string ExprStr(Automaton automaton) {
     252        byte[] code;
     253        int nParams;
     254        automaton.GetCode(out code, out nParams);
     255        var generator = new SymbolicExpressionTreeGenerator(problemData.AllowedInputVariables.ToArray());
     256        var @params = Enumerable.Repeat(1.0, nParams).ToArray();
     257        var root = generator.Exec(code, @params, nParams, null, null);
     258        var formatter = new InfixExpressionFormatter();
     259        return formatter.Format(new SymbolicExpressionTree(root));
     260      }
     261#endif
     262
    249263      private double Eval(byte[] code, int nParams) {
    250264        double[] optConsts;
     
    501515        mctsState.totalRollouts++;
    502516      } while (!success && !tree.Done);
    503       mctsState.effectiveRollouts++;
     517      if (success) {
     518        mctsState.effectiveRollouts++;
    504519
    505520#if DEBUG
    506       Console.WriteLine(ExprStr(automaton));
     521        Console.WriteLine(mctsState.ExprStr(automaton));
    507522#endif
    508       return q;
     523
     524        return q;
     525      } else return 0.0;
    509526    }
    510527
    511528    // search forward
    512     private static bool TryTreeSearchRec2(IRandom rand, Tree tree, Automaton automaton, 
     529    private static bool TryTreeSearchRec2(IRandom rand, Tree tree, Automaton automaton,
    513530      Func<byte[], int, double> eval,
    514531      State state,
     
    529546
    530547      while (!automaton.IsFinalState(automaton.CurrentState)) {
    531         Console.WriteLine(automaton.stateNames[automaton.CurrentState]);
     548        // Console.WriteLine(automaton.stateNames[automaton.CurrentState]);
    532549        if (state.children.ContainsKey(tree)) {
    533550          if (state.children[tree].All(ch => ch.Done)) {
     
    570587            // for selected states (EvalStates) we introduce state unification (detection of equivalent states)
    571588            if (automaton.IsEvalState(possibleFollowStates[i])) {
    572               var hc = Hashcode(automaton); // TODO fix unit test for structure enumeration
     589              var hc = Hashcode(automaton) * (ulong)tree.state; // TODO fix unit test for structure enumeration
    573590              if (!state.nodes.TryGetValue(hc, out child)) {
    574591                child = new Tree() {
     
    623640
    624641      // EVALUATE TREE
    625       if (automaton.IsFinalState(automaton.CurrentState)) {
     642      if (!tree.Done && automaton.IsFinalState(automaton.CurrentState)) {
    626643        tree.Done = true;
    627         tree.expr = ExprStr(automaton);
     644        tree.expr = state.ExprStr(automaton);
    628645        byte[] code; int nParams;
    629646        automaton.GetCode(out code, out nParams);
    630647        q = eval(code, nParams);
    631648        success = true;
    632         BackpropagateQuality(tree, q, state); 
     649        BackpropagateQuality(tree, q, state);
    633650      } else {
    634651        // we got stuck in roll-out (not evaluation necessary!)
     
    652669
    653670      var idx = rand.Next(list.Count);
    654       while(list[idx].Done) { idx = rand.Next(list.Count); }
     671      while (list[idx].Done) { idx = rand.Next(list.Count); }
    655672      return idx;
    656673    }
     
    717734      }
    718735      return children[selectedChildIdx];
    719     }                                           
     736    }
    720737
    721738    // scales data and extracts values from dataset into arrays
     
    778795    // for debugging only
    779796
    780 
    781     private static string ExprStr(Automaton automaton) {
    782       byte[] code;
    783       int nParams;
    784       automaton.GetCode(out code, out nParams);
    785       return Disassembler.CodeToString(code);
    786     }
    787797
    788798    private static string TraceTree(Tree tree, State state) {
Note: See TracChangeset for help on using the changeset viewer.