Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2283 added TreeInfos to MCTS excel export

File size: 3.1 KB
Line 
1using System.Diagnostics;
2using HeuristicLab.Algorithms.Bandits;
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    {
14        public MonteCarloTreeSearch_PruneLeaves(ISymbolicExpressionTreeProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy,
15            ISimulation simulationPolicy)
16            : base(problem, maxLen, random, behaviourPolicy, simulationPolicy)
17        {
18        }
19
20        public override void Run(int maxIterations)
21        {
22            Reset();
23            int selections = 0;
24            for (int i = 0; !StopRequested && i < maxIterations; i++)
25            {
26                lock (pauseLock)
27                {
28                    if (isPaused)
29                    {
30                        Monitor.Wait(pauseLock);
31                    }
32                }
33                TreeNode currentNode = rootNode;
34
35                while (!currentNode.IsLeaf())
36                {
37                    int currentActionIndex = behaviourPolicy.SelectAction(random,
38                        currentNode.GetChildActionInfos());
39                    currentNode = currentNode.children[currentActionIndex];
40                    //selections++;
41                    //CheckSelection(currentNode, selections);
42                }
43
44                string phrase = currentNode.phrase;
45
46                if (phrase.Length <= maxLen)
47                {
48                    // Version 2:
49                    if (currentNode.children != null && !currentNode.children.Any())
50                    {
51                        // already removed all child nodes so remove it too..
52                        currentNode.parent.children.Remove(currentNode);
53                        continue;
54                    }
55                    ExpandTreeNode(currentNode);
56                    if (currentNode.children.Any())
57                    {
58                        currentNode =
59                            currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos())
60                                ];
61                        //selections++;
62                        //CheckSelection(currentNode, selections);
63                    }
64                    else
65                    {
66                        // Version 2:
67                        // remove currentNode from tree..
68                        currentNode.parent.children.Remove(currentNode);
69                    }
70                }
71                if (currentNode.phrase.Length <= maxLen)
72                {
73                    string simulatedPhrase;
74                    double quality = simulation.Simulate(currentNode, out simulatedPhrase);
75                    OnSolutionEvaluated(simulatedPhrase, quality);
76
77                    Propagate(currentNode, quality);
78                }
79            }
80        }
81    }
82}
Note: See TracBrowser for help on using the repository browser.