public void Search_TowProbabilisticStates_ReturnBetterState(int degreeOfParallelism, ParallelismMode parallelismMode)
        {
            A.CallTo(() => probabilisticState1.GetNeighbors()).Returns(new List <Tuple <double, IEnumerable <IState> > >()
            {
                new Tuple <double, IEnumerable <IState> >(0.4, new List <IState> {
                    evaluation2State
                }),
                new Tuple <double, IEnumerable <IState> >(0.6, new List <IState> {
                    evaluationNagitive2State
                }),
            });
            A.CallTo(() => probabilisticState2.GetNeighbors()).Returns(new List <Tuple <double, IEnumerable <IState> > >()
            {
                new Tuple <double, IEnumerable <IState> >(0.1, new List <IState> {
                    evaluation2State
                }),
                new Tuple <double, IEnumerable <IState> >(0.4, new List <IState> {
                    evaluationNagitive2State
                }),
                new Tuple <double, IEnumerable <IState> >(0.5, new List <IState> {
                    evaluation2State
                }),
            });

            var searchEngine = TestUtils.GetBasicSearchEngine(parallelismMode, degreeOfParallelism);
            var searchResult = searchEngine.Search(startState, 10);

            Assert.AreEqual(probabilisticState2, searchResult.NextMove, $"Should have found {nameof(probabilisticState2)} as the nextState");
            Assert.IsTrue(Math.Abs(searchResult.Evaluation - 0.4) < 0.01, "Evaluation should have been .4; actual result is " + searchResult.Evaluation);
        }
Beispiel #2
0
        public static ProbablisticStateWrapper GetNextState(this IProbabilisticState probabilisticState)
        {
            var sumOfAllPossibilities = probabilisticState.GetNeighbors().Select(t => t.Item1).Sum();
            var num = GetRandomNumberUpTo(sumOfAllPossibilities);
            var sum = 0.0;

            foreach (var neighbor in probabilisticState.GetNeighbors())
            {
                sum += neighbor.Item1;
                if (sum >= num)
                {
                    return(new ProbablisticStateWrapper(neighbor.Item2, probabilisticState));
                }
            }

            return(null);
        }
Beispiel #3
0
        public SearchResult EvaluateChildren(IProbabilisticState startState, SearchContext searchContext)
        {
            var neighbors = startState.GetNeighbors().ToArray();

            if (!neighbors.Any())
            {
                var evaluation = startState.Evaluate(searchContext.CurrentDepth, searchContext.StatesUpTillNow, searchOptions);
                return(new SearchResult(evaluation, startState));
            }

            var storedStates = new ConcurrentDictionary <IState, double>();
            var results      = new List <Tuple <double, Task <SearchResult> > >();

            foreach (var neighbor in neighbors)
            {
                var wrappedState = new ProbablisticStateWrapper(neighbor.Item2, startState);
                var searchResult = threadManager.Invoke(() =>
                                                        deterministicSearchUtils.EvaluateChildren(wrappedState, searchContext.CloneWithMaxAlphaAndBeta(), storedStates), searchContext.CurrentDepth);
                results.Add(new Tuple <double, Task <SearchResult> >(neighbor.Item1, searchResult));
            }

            return(Reduce(results, startState));
        }
Beispiel #4
0
 public static void SetAsEndState(this IProbabilisticState state) =>
 A.CallTo(() => state.GetNeighbors()).Returns(new List <Tuple <double, IEnumerable <IState> > >());