Esempio n. 1
0
        private PrecalculatedTransitionTarget[] CreatePrecalculatedTransitionTargets(Formula phi, Formula psi)
        {
            // [phi U<=steps psi]

            var psiEvaluator = Ltmdp.CreateFormulaEvaluator(psi);

            var precalculatedTransitionTargets = CreateEmptyPrecalculatedTransitionTargetArray();

            CalculateSatisfiedTargets(precalculatedTransitionTargets, psiEvaluator);
            if (phi != null)
            {
                // excludedStates = Sat(\phi) \Cup Sat(psi)
                var phiEvaluator = Ltmdp.CreateFormulaEvaluator(phi);
                Func <int, bool> calculateExcludedStates = target =>
                {
                    if (precalculatedTransitionTargets[target] == PrecalculatedTransitionTarget.Satisfied)
                    {
                        return(false);                        //satisfied states are never excluded
                    }
                    if (!phiEvaluator(target))
                    {
                        return(true);                        //exclude state if it does not satisfy phi
                    }
                    return(false);
                };
                CalculateExcludedTargets(precalculatedTransitionTargets, calculateExcludedStates);
            }
            return(precalculatedTransitionTargets);
        }
Esempio n. 2
0
        internal double CalculateMaximumFinalProbability(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(CalculateMaximumProbabilityOfCid(cache, precalculatedTransitionTargets, initialStateProbabilities, cid));
        }
Esempio n. 3
0
 private void CalculateUnderlyingDigraph()
 {
     // We use the underlying digraph to interfere the transitionTargets with the final probability
     // of 0 or 1.
     // I think, the data from the graph is also valid for the states. So, if a state-node
     // is in Prob0 or Prob1, then we can also assume that the state has always probability 0 or 1,
     // respectively. One further check can could be introduced. When we know that the probability
     // of a state is 0 or 1, then we do not have to check the outgoing transitionTargets anymore.
     if (_underlyingDigraph != null)
     {
         return;
     }
     _output.WriteLine("Creating underlying digraph");
     _underlyingDigraph = Ltmdp.CreateUnderlyingDigraph();
     _output.WriteLine("Finished creating underlying digraph");
 }
Esempio n. 4
0
        internal int CountTargetStatesOfCid(long cid)
        {
            var targetStatesCount = 0;
            Action <LabeledTransitionMarkovDecisionProcess.ContinuationGraphElement> counter = cge =>
            {
                if (cge.IsChoiceTypeUnsplitOrFinal)
                {
                    targetStatesCount++;
                }
            };
            var traverser = Ltmdp.GetTreeTraverser(cid);

            traverser.ApplyActionWithStackBasedAlgorithm(counter);

            return(targetStatesCount);
        }
Esempio n. 5
0
        internal double SumProbabilitiesOfCid(long cid)
        {
            Func <LabeledTransitionMarkovDecisionProcess.ContinuationGraphElement, double> leafCounter = (cge) =>
            {
                var myProbability = cge.Probability;
                return(myProbability);
            };
            Func <LabeledTransitionMarkovDecisionProcess.ContinuationGraphElement, IEnumerable <double>, double> innerCounter = (cge, childProbabilities) =>
            {
                var myProbability    = cge.Probability;
                var childProbability = 0.0;
                foreach (var p in childProbabilities)
                {
                    childProbability += p;
                }
                return(myProbability * childProbability);
            };
            var traverser    = Ltmdp.GetTreeTraverser(cid);
            var probabilties = traverser.ApplyFuncWithRecursionBasedAlgorithm(innerCounter, leafCounter);

            return(probabilties);
        }
Esempio n. 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);
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        internal int CountTargetStatesOfState(int state)
        {
            var cidRoot = Ltmdp.GetRootContinuationGraphLocationOfState(state);

            return(CountTargetStatesOfCid(cidRoot));
        }
Esempio n. 9
0
        internal int CountTargetStatesOfInitialState()
        {
            var cidRoot = Ltmdp.GetRootContinuationGraphLocationOfInitialState();

            return(CountTargetStatesOfCid(cidRoot));
        }
Esempio n. 10
0
        internal double SumProbabilitiesOfState(int state)
        {
            var cidRoot = Ltmdp.GetRootContinuationGraphLocationOfState(state);

            return(SumProbabilitiesOfCid(cidRoot));
        }