Esempio n. 1
0
        public IEnumerator TestPerformanceOnLargeTree()
        {
            var planGraph = PlanGraphUtility.BuildTree(actionsPerState: 2, resultsPerAction: 1, depth: 10);

            var nodeCount = planGraph.Size;
            var depthMap  = new NativeHashMap <int, int>(nodeCount, Allocator.TempJob);
            var queue     = new NativeQueue <StateHorizonPair <int> >(Allocator.TempJob);

            planGraph.GetExpandedDepthMap(0, depthMap, queue);

            var selectedUnexpandedStates = new NativeList <int>(1, Allocator.Persistent);
            var allExpandedStates        = new NativeMultiHashMap <int, int>(1, Allocator.Persistent);

            yield return(null);

            // Set up performance test
            Measure.Method(() =>
            {
                var selectJob = new SelectionJob <int, int>()
                {
                    StateExpansionBudget      = 1,
                    RootStateKey              = 0,
                    StateDepthLookup          = depthMap,
                    StateInfoLookup           = planGraph.StateInfoLookup,
                    ActionLookup              = planGraph.ActionLookup,
                    ActionInfoLookup          = planGraph.ActionInfoLookup,
                    ResultingStateLookup      = planGraph.ResultingStateLookup,
                    StateTransitionInfoLookup = planGraph.StateTransitionInfoLookup,

                    SelectedUnexpandedStates = selectedUnexpandedStates,
                    AllSelectedStates        = allExpandedStates
                };
                selectJob.Schedule().Complete();
            }).WarmupCount(1).MeasurementCount(30).IterationsPerMeasurement(1).CleanUp(() =>
            {
                depthMap.Clear();
                queue.Clear();
                planGraph.GetExpandedDepthMap(0, depthMap, queue);

                selectedUnexpandedStates.Clear();
                allExpandedStates.Clear();
            }).Run();

            depthMap.Dispose();
            queue.Dispose();
            planGraph.Dispose();
            selectedUnexpandedStates.Dispose();
            allExpandedStates.Dispose();

            // Check performance times
            PerformanceUtility.AssertRange(0.04, 0.15);
        }
        public void MatchManyExistingStates()
        {
            const int kRootState   = 0;
            const int kActionCount = 1000;

            PlanGraph <int, StateInfo, int, ActionInfo, StateTransitionInfo> planGraph = default;
            NativeMultiHashMap <int, int> binnedStateKeys = default;
            NativeQueue <int>             newStatesQueue  = default;
            NativeList <StateTransitionInfoPair <int, int, StateTransitionInfo> > statesToProcess = default;
            NativeQueue <int> newStatesToDestroy = default;

            Measure.Method(() =>
            {
                var stateTransitionInfoLookup = planGraph.StateTransitionInfoLookup;
                var resultingStateLookup      = planGraph.ResultingStateLookup;

                var expansionJob = new GraphExpansionJob <int, int, TestStateDataContext, int>
                {
                    BinnedStateKeys             = binnedStateKeys,
                    NewStateTransitionInfoPairs = statesToProcess.AsDeferredJobArray(),

                    ActionLookup              = planGraph.ActionLookup.AsParallelWriter(),
                    ActionInfoLookup          = planGraph.ActionInfoLookup.AsParallelWriter(),
                    StateTransitionInfoLookup = stateTransitionInfoLookup.AsParallelWriter(),
                    ResultingStateLookup      = resultingStateLookup.AsParallelWriter(),
                    NewStates        = newStatesQueue.AsParallelWriter(),
                    PredecessorGraph = planGraph.PredecessorGraph.AsParallelWriter(),
                    StateDataContext = new TestStateDataContext(),
                    StatesToDestroy  = newStatesToDestroy.AsParallelWriter(),
                };

                expansionJob.Schedule(statesToProcess, default).Complete();
            }).SetUp(() =>
            {
                // One root node and all children nodes of a single depth
                planGraph = PlanGraphUtility.BuildTree(kActionCount, 1, 1);
                planGraph.ExpandBy(kActionCount, kActionCount);

                newStatesQueue     = new NativeQueue <int>(Allocator.TempJob);
                newStatesToDestroy = new NativeQueue <int>(Allocator.TempJob);

                // Extend graph by one depth with the same number of actions / resulting states that loop back on themselves
                statesToProcess = new NativeList <StateTransitionInfoPair <int, int, StateTransitionInfo> >(kActionCount, Allocator.TempJob);
                for (var i = 0; i < kActionCount; i++)
                {
                    statesToProcess.Add(new StateTransitionInfoPair <int, int, StateTransitionInfo>(kRootState, i, i, new StateTransitionInfo()
                    {
                        Probability = 1, TransitionUtilityValue = 1
                    }));
                }

                binnedStateKeys = GetBinnedStateKeys(planGraph);
            }).CleanUp(() =>
            {
                planGraph.Dispose();
                newStatesQueue.Dispose();
                statesToProcess.Dispose();
                binnedStateKeys.Dispose();
                newStatesToDestroy.Dispose();
            }).WarmupCount(1).MeasurementCount(30).IterationsPerMeasurement(1).Run();

            PerformanceUtility.AssertRange(4.3, 6.25);
        }