Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/02/15 14:44:48 (9 years ago)
Author:
aballeit
Message:

#2283 MCTS avoid stackoverflow..

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/MonteCarloTreeSearch.cs

    r12827 r12829  
    2828        protected readonly ISimulation simulation;
    2929        protected TreeNode rootNode;
    30         protected bool isPaused = false;
    31         protected object pauseLock = new object();
    3230        protected int goodSelections;
    3331
     
    5957
    6058        public bool StopRequested { get; set; }
    61 
    62         public bool IsPaused
    63         {
    64             get { return this.isPaused; }
    65         }
    66 
     59       
    6760        public int GoodSelections { get { return this.goodSelections; } }
    6861
     
    8174            Reset();
    8275            int selections = 0;
     76            TreeNode currentNode;
     77            string phrase;
     78            string simulatedPhrase;
     79            double quality;
     80
    8381            for (int i = 0; !StopRequested && i < maxIterations; i++)
    8482            {
    85                 lock (pauseLock)
    86                 {
    87                     if (isPaused)
    88                     {
    89                         Monitor.Wait(pauseLock);
    90                     }
    91                 }
    92                 TreeNode currentNode = rootNode;
     83                currentNode = rootNode;
    9384
    9485                while (!currentNode.IsLeaf())
     
    10192                }
    10293
    103                 string phrase = currentNode.phrase;
     94                phrase = currentNode.phrase;
    10495
    10596                if (!grammar.IsTerminal(phrase) && phrase.Length <= maxLen)
     
    114105                if (currentNode.phrase.Length <= maxLen)
    115106                {
    116                     string simulatedPhrase;
    117                     double quality = simulation.Simulate(currentNode, out simulatedPhrase);
     107                    quality = simulation.Simulate(currentNode, out simulatedPhrase);
    118108                    OnSolutionEvaluated(simulatedPhrase, quality);
    119109
     
    126116        {
    127117            // create children on the first visit
     118            Sequence newSequence;
     119            TreeNode childNode;
    128120            if (treeNode.children == null)
    129121            {
     
    140132                        foreach (Sequence alternative in grammar.GetAlternatives(symbol))
    141133                        {
    142                             Sequence newSequence = new Sequence(phrase);
     134                            newSequence = new Sequence(phrase);
    143135                            newSequence.ReplaceAt(i, 1, alternative);
    144136                            if (newSequence.Length <= maxLen)
    145137                            {
    146                                 TreeNode childNode = new TreeNode(treeNode, newSequence.ToString(),
     138                                childNode = new TreeNode(treeNode, newSequence.ToString(),
    147139                                    behaviourPolicy.CreateActionInfo(), treeNode.level + 1);
    148140                                treeNode.children.Add(childNode);
     
    350342            return HexConverter(Color.FromArgb(newR, newG, newB));
    351343        }
    352 
    353         public void PauseContinue()
    354         {
    355             lock (pauseLock)
    356             {
    357                 if (isPaused)
    358                 {
    359                     isPaused = false;
    360                     Monitor.Pulse(pauseLock);
    361                 }
    362                 else
    363                 {
    364                     isPaused = true;
    365                 }
    366             }
    367         }
    368344    }
    369345}
Note: See TracChangeset for help on using the changeset viewer.