Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/MonteCarloTreeSearch_PruneLeaves.cs @ 12829

Last change on this file since 12829 was 12829, checked in by aballeit, 9 years ago

#2283 MCTS avoid stackoverflow..

File size: 3.0 KB
RevLine 
[12815]1using System.Diagnostics;
2using HeuristicLab.Algorithms.Bandits;
[12781]3using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base;
4using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
5using HeuristicLab.Problems.GrammaticalOptimization;
6using System;
7using System.Linq;
8using System.Threading;
9
10namespace HeuristicLab.Algorithms.MonteCarloTreeSearch
11{
12    public class MonteCarloTreeSearch_PruneLeaves : MonteCarloTreeSearch
13    {
[12815]14        public MonteCarloTreeSearch_PruneLeaves(ISymbolicExpressionTreeProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy,
15            ISimulation simulationPolicy)
16            : base(problem, maxLen, random, behaviourPolicy, simulationPolicy)
[12781]17        {
18        }
19
20        public override void Run(int maxIterations)
21        {
22            Reset();
[12815]23            int selections = 0;
[12829]24            TreeNode currentNode;
25            string phrase;
26            string simulatedPhrase;
27            double quality;
[12781]28            for (int i = 0; !StopRequested && i < maxIterations; i++)
29            {
[12829]30                currentNode = rootNode;
[12781]31
32                while (!currentNode.IsLeaf())
33                {
34                    int currentActionIndex = behaviourPolicy.SelectAction(random,
35                        currentNode.GetChildActionInfos());
36                    currentNode = currentNode.children[currentActionIndex];
[12827]37                    //selections++;
38                    //CheckSelection(currentNode, selections);
[12781]39                }
40
[12829]41                phrase = currentNode.phrase;
[12781]42
43                if (phrase.Length <= maxLen)
44                {
45                    // Version 2:
46                    if (currentNode.children != null && !currentNode.children.Any())
47                    {
48                        // already removed all child nodes so remove it too..
49                        currentNode.parent.children.Remove(currentNode);
50                        continue;
51                    }
52                    ExpandTreeNode(currentNode);
53                    if (currentNode.children.Any())
54                    {
55                        currentNode =
56                            currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos())
57                                ];
[12827]58                        //selections++;
59                        //CheckSelection(currentNode, selections);
[12781]60                    }
61                    else
62                    {
63                        // Version 2:
64                        // remove currentNode from tree..
65                        currentNode.parent.children.Remove(currentNode);
66                    }
67                }
68                if (currentNode.phrase.Length <= maxLen)
69                {
[12829]70                    quality = simulation.Simulate(currentNode, out simulatedPhrase);
[12781]71                    OnSolutionEvaluated(simulatedPhrase, quality);
72
73                    Propagate(currentNode, quality);
74                }
75            }
76        }
77    }
78}
Note: See TracBrowser for help on using the repository browser.