Beispiel #1
0
        public double CalculateMinimumFinalProbability(AutoResizeBigVector <double> cache, PrecalculatedTransitionTarget[] precalculatedTransitionTargets, double[] initialStateProbabilities)
        {
            var cid = Ltmdp.GetRootContinuationGraphLocationOfInitialState();

            cache?.Clear(cid);             //use cid as offset, because it is the smallest element
            return(CalculateMinimumProbabilityOfCid(cache, precalculatedTransitionTargets, initialStateProbabilities, cid));
        }
        internal double CalculateMaximumFinalProbability(AutoResizeBigVector <double> cache, double[] initialStateProbabilities)
        {
            var cid = Nmdp.GetRootContinuationGraphLocationOfInitialState();

            cache?.Clear(cid);             //use cid as offset, because it is the smallest element
            return(CalculateMaximumProbabilityOfCid(cache, initialStateProbabilities, cid));
        }
        private double CalculateMaximumProbabilityOfCid(AutoResizeBigVector <double> cache, double[] stateProbabilities, long currentCid)
        {
            if (cache != null && HasCacheEntry(cache, currentCid))
            {
                return(cache[(int)currentCid]);
            }
            var result = double.NaN;

            NestedMarkovDecisionProcess.ContinuationGraphElement cge = Nmdp.GetContinuationGraphElement(currentCid);
            if (cge.IsChoiceTypeUnsplitOrFinal)
            {
                var cgl = Nmdp.GetContinuationGraphLeaf(currentCid);
                result = stateProbabilities[cgl.ToState];
            }
            else
            {
                var cgi = Nmdp.GetContinuationGraphInnerNode(currentCid);
                if (cge.IsChoiceTypeForward)
                {
                    // Note, cgi.Probability is used in the branch "else if (cge.IsChoiceTypeProbabilitstic)"
                    result = CalculateMaximumProbabilityOfCid(cache, stateProbabilities, cgi.FromCid);
                }
                if (cge.IsChoiceTypeNondeterministic)
                {
                    var biggest = double.NegativeInfinity;
                    for (var i = cgi.FromCid; i <= cgi.ToCid; i++)
                    {
                        var resultOfChild = CalculateMaximumProbabilityOfCid(cache, stateProbabilities, i);
                        if (resultOfChild > biggest)
                        {
                            biggest = resultOfChild;
                        }
                    }
                    result = biggest;
                }
                else if (cge.IsChoiceTypeProbabilitstic)
                {
                    var sum = 0.0;
                    for (var i = cgi.FromCid; i <= cgi.ToCid; i++)
                    {
                        var transitionProbability = Nmdp.GetContinuationGraphElement(i).Probability;
                        var resultOfChild         = CalculateMaximumProbabilityOfCid(cache, stateProbabilities, i);
                        sum += transitionProbability * resultOfChild;
                    }
                    result = sum;
                }
            }
            if (cache != null)
            {
                cache[(int)currentCid] = result;
            }
            return(result);
        }
        internal double[] MaximumIterator(AutoResizeBigVector <double> cache, Dictionary <int, bool> exactlyOneStates, Dictionary <int, bool> exactlyZeroStates, int steps)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var stateCount = Nmdp.States;

            var xold  = new double[stateCount];
            var xnew  = CreateDerivedVector(exactlyOneStates);
            var loops = 0;

            while (loops < steps)
            {
                // switch xold and xnew
                var xtemp = xold;
                xold = xnew;
                xnew = xtemp;
                loops++;
                for (var i = 0; i < stateCount; i++)
                {
                    if (exactlyOneStates.ContainsKey(i))
                    {
                        //we could remove this line, because already set by CreateDerivedVector and never changed when we initialize xold with CreateDerivedVector(directlySatisfiedStates)
                        xnew[i] = 1.0;
                    }
                    else if (exactlyZeroStates.ContainsKey(i))
                    {
                        //we could remove this line, because already set by CreateDerivedVector and never changed when we initialize xold with CreateDerivedVector(directlySatisfiedStates)
                        xnew[i] = 0.0;
                    }
                    else
                    {
                        var cid = Nmdp.GetRootContinuationGraphLocationOfState(i);
                        cache?.Clear(cid);                         //use cid as offset, because it is the smallest element
                        xnew[i] = CalculateMaximumProbabilityOfCid(cache, xold, cid);
                    }
                }

                if (loops % 10 == 0)
                {
                    stopwatch.Stop();
                    var currentProbability = CalculateMaximumFinalProbability(cache, xnew);
                    _output?.WriteLine(
                        $"{loops} Bounded Until iterations in {stopwatch.Elapsed}. Current probability={currentProbability.ToString(CultureInfo.InvariantCulture)}");
                    stopwatch.Start();
                }
            }
            stopwatch.Stop();
            return(xnew);
        }
        internal double CalculateMaximumProbabilityToReachStateFormulaInBoundedSteps(Formula psi, int steps)
        {
            var cache = new AutoResizeBigVector <double> {
                DefaultValue = double.NaN
            };

            var psiEvaluator = Nmdp.CreateFormulaEvaluator(psi);

            var directlySatisfiedStates = CalculateSatisfiedStates(psiEvaluator);
            var excludedStates          = new Dictionary <int, bool>();   // change for \phi Until \psi

            var xnew = MaximumIterator(cache, directlySatisfiedStates, excludedStates, steps);

            var finalProbability = CalculateMaximumFinalProbability(cache, xnew);

            return(finalProbability);
        }
Beispiel #6
0
        internal double[] MaximumIterator(AutoResizeBigVector <double> cache, PrecalculatedTransitionTarget[] precalculatedTransitionTargets, int steps)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var stateCount = Ltmdp.SourceStates.Count;

            var xold  = new double[stateCount];
            var xnew  = new double[stateCount];
            var loops = 0;

            while (loops < steps)
            {
                // switch xold and xnew
                var xtemp = xold;
                xold = xnew;
                xnew = xtemp;
                loops++;
                for (var i = 0; i < stateCount; i++)
                {
                    var cid = Ltmdp.GetRootContinuationGraphLocationOfState(i);
                    cache?.Clear(cid);                     //use cid as offset, because it is the smallest element
                    xnew[i] = CalculateMaximumProbabilityOfCid(cache, precalculatedTransitionTargets, xold, cid);
                }

                if (loops % 10 == 0)
                {
                    stopwatch.Stop();
                    var currentProbability = CalculateMaximumFinalProbability(cache, precalculatedTransitionTargets, xnew);
                    _output?.WriteLine(
                        $"{loops} Bounded Until iterations in {stopwatch.Elapsed}. Current probability={currentProbability.ToString(CultureInfo.InvariantCulture)}");
                    stopwatch.Start();
                }
            }
            stopwatch.Stop();
            return(xnew);
        }
 private bool HasCacheEntry(AutoResizeBigVector <double> cache, long entry)
 {
     return(!double.IsNaN(cache[(int)entry]));
 }
Beispiel #8
0
        private double CalculateMaximumProbabilityOfCid(AutoResizeBigVector <double> cache, PrecalculatedTransitionTarget[] precalculatedTransitionTargets, double[] stateProbabilities, long currentCid)
        {
            if (cache != null && HasCacheEntry(cache, currentCid))
            {
                return(cache[(int)currentCid]);
            }
            var result = double.NaN;

            LabeledTransitionMarkovDecisionProcess.ContinuationGraphElement cge = Ltmdp.GetContinuationGraphElement(currentCid);
            if (cge.IsChoiceTypeUnsplitOrFinal)
            {
                var transitionTargetPosition = cge.To;
                var transitionTarget         = Ltmdp.GetTransitionTarget(transitionTargetPosition);

                if (precalculatedTransitionTargets[transitionTargetPosition].HasFlag(PrecalculatedTransitionTarget.Satisfied))
                {
                    result = 1.0;
                }
                else if (precalculatedTransitionTargets[transitionTargetPosition].HasFlag(PrecalculatedTransitionTarget.Excluded))
                {
                    result = 0.0;
                }
                else
                {
                    result = stateProbabilities[transitionTarget.TargetState];
                }
            }
            else
            {
                if (cge.IsChoiceTypeForward)
                {
                    // Note, cgi.Probability is used in the branch "else if (cge.IsChoiceTypeProbabilitstic)"
                    result = CalculateMaximumProbabilityOfCid(cache, precalculatedTransitionTargets, stateProbabilities, cge.From);
                }
                if (cge.IsChoiceTypeNondeterministic)
                {
                    var biggest = double.NegativeInfinity;
                    for (var i = cge.From; i <= cge.To; i++)
                    {
                        var resultOfChild = CalculateMaximumProbabilityOfCid(cache, precalculatedTransitionTargets, stateProbabilities, i);
                        if (resultOfChild > biggest)
                        {
                            biggest = resultOfChild;
                        }
                    }
                    result = biggest;
                }
                else if (cge.IsChoiceTypeProbabilitstic)
                {
                    var sum = 0.0;
                    for (var i = cge.From; i <= cge.To; i++)
                    {
                        var transitionProbability = Ltmdp.GetContinuationGraphElement(i).Probability;
                        var resultOfChild         = CalculateMaximumProbabilityOfCid(cache, precalculatedTransitionTargets, stateProbabilities, i);
                        sum += transitionProbability * resultOfChild;
                    }
                    result = sum;
                }
            }
            if (cache != null)
            {
                cache[(int)currentCid] = result;
            }
            return(result);
        }