1  using System.Diagnostics;


2  using HeuristicLab.Algorithms.Bandits;


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  {


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  }

