Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/30/15 16:37:14 (9 years ago)
Author:
aballeit
Message:

#2283 added SelectionIndicator for MCTS and problems SantaFeAnt, SymbolicRegression10, RoyalSymbol; updated SantaFeAnt grammar

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch
Files:
2 edited

Legend:

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

    r12781 r12815  
    11using System;
    22using System.Collections.Generic;
     3using System.Diagnostics;
    34using System.Drawing;
    45using System.Linq;
     
    2122    {
    2223        protected readonly int maxLen;
    23         protected readonly IProblem problem;
     24        protected readonly ISymbolicExpressionTreeProblem problem;
    2425        protected readonly IGrammar grammar;
    2526        protected readonly Random random;
     
    2930        protected bool isPaused = false;
    3031        protected object pauseLock = new object();
    31 
    32         public MonteCarloTreeSearch(IProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy,
     32        protected int goodSelections;
     33
     34        public MonteCarloTreeSearch(ISymbolicExpressionTreeProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy,
    3335            ISimulation simulationPolicy)
    3436        {
     
    3941            this.behaviourPolicy = behaviourPolicy;
    4042            this.simulation = simulationPolicy;
     43
     44            this.problem.GenerateProblemSolutions(maxLen);
     45        }
     46
     47        public event Action<int, int> NodeSelected;
     48
     49        protected virtual void OnNodeSelectedChanged(int value, int selections)
     50        {
     51            RaiseNodeSelectedChanged(value, selections);
     52        }
     53
     54        private void RaiseNodeSelectedChanged(int value, int selections)
     55        {
     56            var handler = NodeSelected;
     57            if (handler != null) handler(value, selections);
    4158        }
    4259
     
    4865        }
    4966
     67        public int GoodSelections { get { return this.goodSelections; } }
     68
     69        protected void CheckSelection(TreeNode node, int selections)
     70        {
     71            if (problem.IsParentOfProblemSolution(node.phrase, maxLen))
     72            {
     73                goodSelections++;
     74            }
     75
     76            OnNodeSelectedChanged(goodSelections, selections);
     77        }
     78
    5079        public override void Run(int maxIterations)
    5180        {
    5281            Reset();
     82            int selections = 0;
    5383            for (int i = 0; !StopRequested && i < maxIterations; i++)
    5484            {
     
    6797                        currentNode.GetChildActionInfos());
    6898                    currentNode = currentNode.children[currentActionIndex];
     99                    selections++;
     100                    CheckSelection(currentNode, selections);
    69101                }
    70102
     
    77109                        currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos())
    78110                            ];
     111                    selections++;
     112                    CheckSelection(currentNode, selections);
    79113                }
    80114                if (currentNode.phrase.Length <= maxLen)
     
    122156        protected void Reset()
    123157        {
     158            goodSelections = 0;
    124159            StopRequested = false;
    125160            bestQuality = 0.0;
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/MonteCarloTreeSearch_PruneLeaves.cs

    r12781 r12815  
    1 using HeuristicLab.Algorithms.Bandits;
     1using System.Diagnostics;
     2using HeuristicLab.Algorithms.Bandits;
    23using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base;
    34using HeuristicLab.Algorithms.MonteCarloTreeSearch.Simulation;
     
    1112    public class MonteCarloTreeSearch_PruneLeaves : MonteCarloTreeSearch
    1213    {
    13         public MonteCarloTreeSearch_PruneLeaves(IProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy,
    14             ISimulation simulationPolicy):base(problem,maxLen,random,behaviourPolicy,simulationPolicy)
     14        public MonteCarloTreeSearch_PruneLeaves(ISymbolicExpressionTreeProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy,
     15            ISimulation simulationPolicy)
     16            : base(problem, maxLen, random, behaviourPolicy, simulationPolicy)
    1517        {
    1618        }
     
    1921        {
    2022            Reset();
     23            int selections = 0;
    2124            for (int i = 0; !StopRequested && i < maxIterations; i++)
    2225            {
     
    3538                        currentNode.GetChildActionInfos());
    3639                    currentNode = currentNode.children[currentActionIndex];
     40                    selections++;
     41                    CheckSelection(currentNode, selections);
    3742                }
    3843
     
    5459                            currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos())
    5560                                ];
     61                        selections++;
     62                        CheckSelection(currentNode, selections);
    5663                    }
    5764                    else
     
    6976
    7077                    Propagate(currentNode, quality);
    71 
    7278                }
    7379            }
Note: See TracChangeset for help on using the changeset viewer.