Changeset 12815 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch
- Timestamp:
- 07/30/15 16:37:14 (9 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/MonteCarloTreeSearch.cs
r12781 r12815 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Diagnostics; 3 4 using System.Drawing; 4 5 using System.Linq; … … 21 22 { 22 23 protected readonly int maxLen; 23 protected readonly I Problem problem;24 protected readonly ISymbolicExpressionTreeProblem problem; 24 25 protected readonly IGrammar grammar; 25 26 protected readonly Random random; … … 29 30 protected bool isPaused = false; 30 31 protected object pauseLock = new object(); 31 32 public MonteCarloTreeSearch(IProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy, 32 protected int goodSelections; 33 34 public MonteCarloTreeSearch(ISymbolicExpressionTreeProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy, 33 35 ISimulation simulationPolicy) 34 36 { … … 39 41 this.behaviourPolicy = behaviourPolicy; 40 42 this.simulation = simulationPolicy; 43 44 this.problem.GenerateProblemSolutions(maxLen); 45 } 46 47 public event Action<int, int> NodeSelected; 48 49 protected virtual void OnNodeSelectedChanged(int value, int selections) 50 { 51 RaiseNodeSelectedChanged(value, selections); 52 } 53 54 private void RaiseNodeSelectedChanged(int value, int selections) 55 { 56 var handler = NodeSelected; 57 if (handler != null) handler(value, selections); 41 58 } 42 59 … … 48 65 } 49 66 67 public int GoodSelections { get { return this.goodSelections; } } 68 69 protected void CheckSelection(TreeNode node, int selections) 70 { 71 if (problem.IsParentOfProblemSolution(node.phrase, maxLen)) 72 { 73 goodSelections++; 74 } 75 76 OnNodeSelectedChanged(goodSelections, selections); 77 } 78 50 79 public override void Run(int maxIterations) 51 80 { 52 81 Reset(); 82 int selections = 0; 53 83 for (int i = 0; !StopRequested && i < maxIterations; i++) 54 84 { … … 67 97 currentNode.GetChildActionInfos()); 68 98 currentNode = currentNode.children[currentActionIndex]; 99 selections++; 100 CheckSelection(currentNode, selections); 69 101 } 70 102 … … 77 109 currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos()) 78 110 ]; 111 selections++; 112 CheckSelection(currentNode, selections); 79 113 } 80 114 if (currentNode.phrase.Length <= maxLen) … … 122 156 protected void Reset() 123 157 { 158 goodSelections = 0; 124 159 StopRequested = false; 125 160 bestQuality = 0.0; -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/MonteCarloTreeSearch_PruneLeaves.cs
r12781 r12815 1 using HeuristicLab.Algorithms.Bandits; 1 using System.Diagnostics; 2 using HeuristicLab.Algorithms.Bandits; 2 3 using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base; 3 4 using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation; … … 11 12 public class MonteCarloTreeSearch_PruneLeaves : MonteCarloTreeSearch 12 13 { 13 public MonteCarloTreeSearch_PruneLeaves(IProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy, 14 ISimulation simulationPolicy):base(problem,maxLen,random,behaviourPolicy,simulationPolicy) 14 public MonteCarloTreeSearch_PruneLeaves(ISymbolicExpressionTreeProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy, 15 ISimulation simulationPolicy) 16 : base(problem, maxLen, random, behaviourPolicy, simulationPolicy) 15 17 { 16 18 } … … 19 21 { 20 22 Reset(); 23 int selections = 0; 21 24 for (int i = 0; !StopRequested && i < maxIterations; i++) 22 25 { … … 35 38 currentNode.GetChildActionInfos()); 36 39 currentNode = currentNode.children[currentActionIndex]; 40 selections++; 41 CheckSelection(currentNode, selections); 37 42 } 38 43 … … 54 59 currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos()) 55 60 ]; 61 selections++; 62 CheckSelection(currentNode, selections); 56 63 } 57 64 else … … 69 76 70 77 Propagate(currentNode, quality); 71 72 78 } 73 79 }
Note: See TracChangeset
for help on using the changeset viewer.