Exemple #1
0
 public Search(SearchParameters p, Player searchingPlayer, SearchResults searchResults, Game game)
 {
     _p = p;
       _searchResults = searchResults;
       _game = game;
       _root = new InnerResult(_game.CalculateHash(), searchingPlayer.IsMax, 0);
 }
Exemple #2
0
 public Search(SearchParameters p, Player searchingPlayer, SearchResults searchResults, Game game)
 {
     _p             = p;
     _searchResults = searchResults;
     _game          = game;
     _root          = new InnerResult(_game.CalculateHash(), searchingPlayer.IsMax, 0);
 }
Exemple #3
0
        public void EvaluateBranch(int index, ISearchNode searchNode, InnerResult parentResult)
        {
            LogFile.Debug("{0} start eval move {1}", searchNode, index);

            // Create a snapshot, of the game before traversing
            // a branch.
            var snaphost = _game.CreateSnapshot();

            searchNode.SetResult(index);
            ParentResult = parentResult;
            ResultIndex  = index;

            // Traverse this branch, and build result subtree.
            _game.Simulate(_search.SearchUntilDepth);

            if (parentResult.HasChildrenWithIndex(index) == false)
            {
                // No children were added on this branch and the game has
                // finished or we have reached our intended depth.
                // Add a leaf node with game score.
                var leafResult = new LeafResult(_game.Score, SearchDepth);
                parentResult.AddChild(index, leafResult);
            }

            // Restore the game from the snapshot.
            _game.RollbackToSnapshot(snaphost);

            LogFile.Debug("{0} stop eval move {1}", searchNode, index);
        }
        public SearchWorker(Search search, InnerResult rootResult, Game game, SearchResults searchResults)
        {
            _game = game;

              _search = search;
              _searchResults = searchResults;
              _parentResult = new Trackable<InnerResult>(rootResult).Initialize(game.ChangeTracker);
              _moveIndex.Initialize(game.ChangeTracker);
        }
Exemple #5
0
        public SearchWorker(Search search, InnerResult rootResult, Game game, SearchResults searchResults)
        {
            _game = game;

            _search        = search;
            _searchResults = searchResults;
            _parentResult  = new Trackable <InnerResult>(rootResult).Initialize(game.ChangeTracker);
            _moveIndex.Initialize(game.ChangeTracker);
        }
Exemple #6
0
        private SearchWorker CreateWorker(InnerResult rootResult, Game game)
        {
            var worker = new SearchWorker(this, rootResult, game, _searchResults);

            lock (_workersLock)
            {
                _workers.Add(worker.Id, worker);
                _numWorkersCreated++;
            }

            return(worker);
        }
Exemple #7
0
        public bool CreateOrGetExistingResult(int gameState, bool isMax, int stepCount, out InnerResult searchResult)
        {
            lock (_access)
            {
                if (_results.TryGetValue(gameState, out searchResult))
                {
                    return(false);
                }

                searchResult = new InnerResult(
                    id: gameState,
                    isMax: isMax,
                    stepCount: stepCount);

                _results.Add(gameState, searchResult);
            }

            return(true);
        }
        public bool CreateOrGetExistingResult(int gameState, bool isMax, int stepCount, out InnerResult searchResult)
        {
            lock (_access)
              {
            if (_results.TryGetValue(gameState, out searchResult))
            {
              return false;
            }

            searchResult = new InnerResult(
              id: gameState,
              isMax: isMax,
              stepCount: stepCount);

            _results.Add(gameState, searchResult);
              }

              return true;
        }
Exemple #9
0
        public Task EvaluateBranch(SearchWorker worker, ISearchNode rootNode, InnerResult rootResult, int moveIndex)
        {
            var shouldCreateNewWorker = IsItFeasibleToCreateNewWorker(rootNode, moveIndex);

            if (shouldCreateNewWorker)
            {
                rootNode = new CopyService().CopyRoot(rootNode);
                worker   = CreateWorker(rootResult, rootNode.Game);

                var task = Task.Factory.StartNew(() =>
                {
                    worker.EvaluateBranch(moveIndex, rootNode, rootResult);
                    RemoveWorker(worker);
                }, TaskCreationOptions.PreferFairness);

                return(task);
            }
            worker.EvaluateBranch(moveIndex, rootNode, rootResult);
            return(null);
        }
Exemple #10
0
        public Task EvaluateBranch(SearchWorker worker, ISearchNode rootNode, InnerResult rootResult, int moveIndex)
        {
            var shouldCreateNewWorker = IsItFeasibleToCreateNewWorker(rootNode, moveIndex);

              if (shouldCreateNewWorker)
              {
            rootNode = new CopyService().CopyRoot(rootNode);
            worker = CreateWorker(rootResult, rootNode.Game);

            var task = Task.Factory.StartNew(() =>
              {
            worker.EvaluateBranch(moveIndex, rootNode, rootResult);
            RemoveWorker(worker);
              }, TaskCreationOptions.PreferFairness);

            return task;
              }
              worker.EvaluateBranch(moveIndex, rootNode, rootResult);
              return null;
        }
Exemple #11
0
        private void EvaluateBranches(ISearchNode searchNode, InnerResult rootResult)
        {
            LogFile.Debug("Evaluating moves of node {0}", searchNode);

            var tasks = new List <Task>();

            for (var i = 0; i < searchNode.ResultCount; i++)
            {
                var task = _search.EvaluateBranch(
                    worker: this,
                    rootNode: searchNode,
                    rootResult: rootResult,
                    moveIndex: i);

                if (task != null)
                {
                    tasks.Add(task);
                }
            }

            Task.WaitAll(tasks.ToArray());
            return;
        }
Exemple #12
0
        private SearchWorker CreateWorker(InnerResult rootResult, Game game)
        {
            var worker = new SearchWorker(this, rootResult, game, _searchResults);

              lock (_workersLock)
              {
            _workers.Add(worker.Id, worker);
            _numWorkersCreated++;
              }

              return worker;
        }
Exemple #13
0
        public void EvaluateBranch(int index, ISearchNode searchNode, InnerResult parentResult)
        {
            LogFile.Debug("{0} start eval move {1}", searchNode, index);

              // Create a snapshot, of the game before traversing
              // a branch.
              var snaphost = _game.CreateSnapshot();

              searchNode.SetResult(index);
              ParentResult = parentResult;
              ResultIndex = index;

              // Traverse this branch, and build result subtree.
              _game.Simulate(_search.SearchUntilDepth);

              if (parentResult.HasChildrenWithIndex(index) == false)
              {
            // No children were added on this branch and the game has
            // finished or we have reached our intended depth.
            // Add a leaf node with game score.
            var leafResult = new LeafResult(_game.Score, SearchDepth);
            parentResult.AddChild(index, leafResult);
              }

              // Restore the game from the snapshot.
              _game.RollbackToSnapshot(snaphost);

              LogFile.Debug("{0} stop eval move {1}", searchNode, index);
        }
Exemple #14
0
        private void EvaluateBranches(ISearchNode searchNode, InnerResult rootResult)
        {
            LogFile.Debug("Evaluating moves of node {0}", searchNode);

              var tasks = new List<Task>();

              for (var i = 0; i < searchNode.ResultCount; i++)
              {
            var task = _search.EvaluateBranch(
              worker: this,
              rootNode: searchNode,
              rootResult: rootResult,
              moveIndex: i);

            if (task != null)
              tasks.Add(task);
              }

              Task.WaitAll(tasks.ToArray());
              return;
        }