Changeset 15439 for branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs
- Timestamp:
- 10/28/17 20:32:53 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs
r15438 r15439 23 23 using System.Collections.Generic; 24 24 using System.Diagnostics; 25 using System.Diagnostics.Contracts;26 25 using System.Linq; 27 26 using System.Text; … … 61 60 // --> TODO: Which heuristics can we apply? 62 61 // TODO: Solve Poly-10 62 // TODO: rename everything as this is not MCTS anymore 63 63 // TODO: After state unification the recursive backpropagation of results takes a lot of time. How can this be improved? 64 64 // ~~obsolete TODO: Why is the algorithm so slow for rather greedy policies (e.g. low C value in UCB)? … … 247 247 #endregion 248 248 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 249 263 private double Eval(byte[] code, int nParams) { 250 264 double[] optConsts; … … 501 515 mctsState.totalRollouts++; 502 516 } while (!success && !tree.Done); 503 mctsState.effectiveRollouts++; 517 if (success) { 518 mctsState.effectiveRollouts++; 504 519 505 520 #if DEBUG 506 Console.WriteLine(ExprStr(automaton));521 Console.WriteLine(mctsState.ExprStr(automaton)); 507 522 #endif 508 return q; 523 524 return q; 525 } else return 0.0; 509 526 } 510 527 511 528 // search forward 512 private static bool TryTreeSearchRec2(IRandom rand, Tree tree, Automaton automaton, 529 private static bool TryTreeSearchRec2(IRandom rand, Tree tree, Automaton automaton, 513 530 Func<byte[], int, double> eval, 514 531 State state, … … 529 546 530 547 while (!automaton.IsFinalState(automaton.CurrentState)) { 531 Console.WriteLine(automaton.stateNames[automaton.CurrentState]);548 // Console.WriteLine(automaton.stateNames[automaton.CurrentState]); 532 549 if (state.children.ContainsKey(tree)) { 533 550 if (state.children[tree].All(ch => ch.Done)) { … … 570 587 // for selected states (EvalStates) we introduce state unification (detection of equivalent states) 571 588 if (automaton.IsEvalState(possibleFollowStates[i])) { 572 var hc = Hashcode(automaton) ; // TODO fix unit test for structure enumeration589 var hc = Hashcode(automaton) * (ulong)tree.state; // TODO fix unit test for structure enumeration 573 590 if (!state.nodes.TryGetValue(hc, out child)) { 574 591 child = new Tree() { … … 623 640 624 641 // EVALUATE TREE 625 if ( automaton.IsFinalState(automaton.CurrentState)) {642 if (!tree.Done && automaton.IsFinalState(automaton.CurrentState)) { 626 643 tree.Done = true; 627 tree.expr = ExprStr(automaton);644 tree.expr = state.ExprStr(automaton); 628 645 byte[] code; int nParams; 629 646 automaton.GetCode(out code, out nParams); 630 647 q = eval(code, nParams); 631 648 success = true; 632 BackpropagateQuality(tree, q, state); 649 BackpropagateQuality(tree, q, state); 633 650 } else { 634 651 // we got stuck in roll-out (not evaluation necessary!) … … 652 669 653 670 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); } 655 672 return idx; 656 673 } … … 717 734 } 718 735 return children[selectedChildIdx]; 719 } 736 } 720 737 721 738 // scales data and extracts values from dataset into arrays … … 778 795 // for debugging only 779 796 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 }787 797 788 798 private static string TraceTree(Tree tree, State state) {
Note: See TracChangeset
for help on using the changeset viewer.