public void Push(LtmdpChosenValue value)
        {
            if (_memoryBuffer.SizeInBytes <= Count * sizeof(LtmdpChosenValue))
            {
                _memoryBuffer.Resize(_memoryBuffer.SizeInBytes * 2, zeroMemory: true);
                _buffer = (LtmdpChosenValue *)_memoryBuffer.Pointer;
            }

            _buffer[Count++] = value;
        }
Example #2
0
        /// <summary>
        ///   Sets the choices that should be made during the next step.
        /// </summary>
        /// <param name="choices">The choices that should be made.</param>
        internal override void SetChoices(int[] choices)
        {
            Requires.NotNull(choices, nameof(choices));

            for (var i = 0; i < choices.Length; i++)
            {
                var newChosenValue =
                    new LtmdpChosenValue
                {
                    ContinuationId = i,
                    OptionIndex    = choices[i]
                };
                _chosenValues.Push(newChosenValue);
                _valueCount.Push(0);
            }
        }
Example #3
0
        public override bool PrepareNextPath()
        {
            if (_choiceIndex != _valueCount.Count - 1)
            {
                throw new NondeterminismException();
            }

            // Reset the choice counter as each path starts from the beginning
            _choiceIndex = -1;

            // If this is the first path of the state, we definitely have to enumerate it
            if (_firstPath)
            {
                _firstPath = false;
                return(true);
            }

            // Let's go through the entire stack to determine what we have to do next
            while (_chosenValues.Count > 0)
            {
                // Remove the value we've chosen last -- we've already chosen it, so we're done with it
                var chosenValue = _chosenValues.Remove();

                // If we have at least one other value to choose, let's do that next
                if (_valueCount.Peek() > chosenValue.OptionIndex + 1)
                {
                    _continuationId = chosenValue.ContinuationId + 1;

                    var newChosenValue =
                        new LtmdpChosenValue
                    {
                        OptionIndex    = chosenValue.OptionIndex + 1,
                        ContinuationId = _continuationId,
                    };
                    _chosenValues.Push(newChosenValue);
                    return(true);
                }

                // Otherwise, we've chosen all values of the last choice, so we're done with it
                _valueCount.Remove();
            }

            AssertThatDatastructureIsIntact();

            // If we reach this point, we know that we've chosen all values of all choices, so there are no further paths
            return(false);
        }
Example #4
0
        public override int HandleProbabilisticChoice(int valueCount)
        {
            ++_choiceIndex;

            // If we have a preselected value that we should choose for the current path, return it
            var chosenValuesMaxIndex = _chosenValues.Count - 1;

            if (_choiceIndex <= chosenValuesMaxIndex)
            {
                return(_chosenValues[_choiceIndex].OptionIndex);
            }

            // We haven't encountered this choice before; store the value count and return the first value
            _valueCount.Push(valueCount);
            var oldContinuationId = _continuationId;

            _continuationId = _nextFreeContinuationId;

            var newChosenValue =
                new LtmdpChosenValue
            {
                OptionIndex    = 0,
                ContinuationId = _continuationId,
            };

            _chosenValues.Push(newChosenValue);

            _nextFreeContinuationId = _nextFreeContinuationId + valueCount;
            LtmdpStepGraph.ProbabilisticSplit(oldContinuationId, _continuationId, _continuationId + valueCount - 1);

            // set default values
            var defaultValue = 1.0 / valueCount;

            for (var i = 0; i < valueCount; i++)
            {
                LtmdpStepGraph.SetProbabilityOfContinuationId(_continuationId + i, defaultValue);
            }

            return(0);
        }