[12815] | 1 | using System.Diagnostics;
|
---|
| 2 | using HeuristicLab.Algorithms.Bandits;
|
---|
[12781] | 3 | using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base;
|
---|
| 4 | using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
|
---|
| 5 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 6 | using System;
|
---|
| 7 | using System.Linq;
|
---|
| 8 | using System.Threading;
|
---|
| 9 |
|
---|
| 10 | namespace 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];
|
---|
[12840] | 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..
|
---|
[12832] | 49 | currentNode.parent.RemoveChildren(currentNode);
|
---|
[13492] | 50 | i--;
|
---|
[12781] | 51 | continue;
|
---|
| 52 | }
|
---|
| 53 | ExpandTreeNode(currentNode);
|
---|
| 54 | if (currentNode.children.Any())
|
---|
| 55 | {
|
---|
| 56 | currentNode =
|
---|
| 57 | currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos())
|
---|
| 58 | ];
|
---|
[12840] | 59 | selections++;
|
---|
| 60 | CheckSelection(currentNode, selections);
|
---|
[12781] | 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
| 64 | // Version 2:
|
---|
| 65 | // remove currentNode from tree..
|
---|
[12832] | 66 | currentNode.parent.RemoveChildren(currentNode);
|
---|
[12781] | 67 | }
|
---|
| 68 | }
|
---|
| 69 | if (currentNode.phrase.Length <= maxLen)
|
---|
| 70 | {
|
---|
[12829] | 71 | quality = simulation.Simulate(currentNode, out simulatedPhrase);
|
---|
[12781] | 72 | OnSolutionEvaluated(simulatedPhrase, quality);
|
---|
| 73 |
|
---|
[12840] | 74 | OnIterationFinishedChanged(goodSelections, selections);
|
---|
| 75 |
|
---|
[12781] | 76 | Propagate(currentNode, quality);
|
---|
| 77 | }
|
---|
[13492] | 78 | else
|
---|
| 79 | {
|
---|
| 80 | i--;
|
---|
| 81 |
|
---|
| 82 | }
|
---|
[12781] | 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|