Changeset 15439
- Timestamp:
- 10/28/17 20:32:53 (7 years ago)
- Location:
- branches/MCTS-SymbReg-2796
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/ExprHash.cs
r15414 r15439 145 145 Contract.Assert(topOfStack == 0); 146 146 return stack[topOfStack].Sum(); 147 default: throw new InvalidOperationException(); 147 148 } 148 149 } -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionAlgorithm.cs
r15438 r15439 74 74 public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter { 75 75 get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; } 76 } 76 } 77 77 public IFixedValueParameter<DoubleValue> PunishmentFactorParameter { 78 78 get { return (IFixedValueParameter<DoubleValue>)Parameters[PunishmentFactorParameterName]; } … … 260 260 var problemData = (IRegressionProblemData)Problem.ProblemData.Clone(); 261 261 if (!AllowedFactors.CheckedItems.Any()) throw new ArgumentException("At least on type of factor must be allowed"); 262 var state = MctsSymbolicRegressionStatic.CreateState(problemData, (uint)Seed, MaxVariableReferences, ScaleVariables, 262 var state = MctsSymbolicRegressionStatic.CreateState(problemData, (uint)Seed, MaxVariableReferences, ScaleVariables, 263 263 ConstantOptimizationIterations, Lambda, 264 264 collectPareto, … … 291 291 if (bestQ > bestQuality.Value) { 292 292 bestSolutionIteration.Value = i; 293 if (state.BestSolutionTrainingQuality > 0.99999) break; 293 if (state.BestSolutionTrainingQuality > 0.99999) break; 294 294 } 295 295 bestQuality.Value = bestQ; … … 317 317 318 318 // final results (assumes that at least one iteration was calculated) 319 if (n > 0) { 319 if (n > 0) { 320 320 if (bestQ > bestQuality.Value) { 321 321 bestSolutionIteration.Value = iterations.Value + n; -
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) { -
branches/MCTS-SymbReg-2796/Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs
r15438 r15439 1485 1485 1486 1486 Console.WriteLine(mctsSymbReg.ExecutionTime); 1487 Assert.AreEqual(expectedNumberOfSolutions, ((IntValue)mctsSymbReg.Results[" Iterations"].Value).Value);1487 Assert.AreEqual(expectedNumberOfSolutions, ((IntValue)mctsSymbReg.Results["Effective rollouts"].Value).Value); 1488 1488 } 1489 1489
Note: See TracChangeset
for help on using the changeset viewer.