private double CalculateFinalProbability(double[] initialStateProbabilities)
        {
            var finalProbability = 0.0;

            var enumerator = MarkovChain.GetEnumerator();

            enumerator.SelectInitialDistribution();
            while (enumerator.MoveNextTransition())
            {
                var entry = enumerator.CurrentTransition;
                finalProbability += entry.Value * initialStateProbabilities[entry.Column];
            }
            return(finalProbability);
        }
        private SparseDoubleMatrix CreateDerivedMatrix(Dictionary <long, bool> exactlyOneStates, Dictionary <long, bool> exactlyZeroStates)
        {
            //Derived matrix is 0-based. Row i is equivalent to the probability distribution of state i (this is not the case for the Markov Chain).

            var derivedMatrix = new SparseDoubleMatrix(MarkovChain.States, MarkovChain.Transitions + MarkovChain.States);            //Transitions+States is a upper limit

            var enumerator = MarkovChain.GetEnumerator();

            for (var sourceState = 0; sourceState < MarkovChain.States; sourceState++)
            {
                enumerator.SelectSourceState(sourceState);
                derivedMatrix.SetRow(sourceState);
                if (exactlyOneStates.ContainsKey(sourceState) || exactlyZeroStates.ContainsKey(sourceState))
                {
                    // only add a self reference entry
                    derivedMatrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(sourceState, 1.0));
                }
                else
                {
                    // if state is neither exactlyOneStates nor exactlyZeroStates, it is a toCalculateState
                    var selfReferenceAdded = false;
                    while (enumerator.MoveNextTransition())
                    {
                        var columnValueEntry = enumerator.CurrentTransition;
                        var targetState      = columnValueEntry.Column;
                        if (targetState == sourceState)
                        {
                            //this implements the removal of the identity matrix
                            derivedMatrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(sourceState, columnValueEntry.Value - 1.0));
                            selfReferenceAdded = true;
                        }
                        else
                        {
                            derivedMatrix.AddColumnValueToCurrentRow(columnValueEntry);
                        }
                    }
                    if (!selfReferenceAdded)
                    {
                        //this implements the removal of the identity matrix (if not already done)
                        derivedMatrix.AddColumnValueToCurrentRow(new SparseDoubleMatrix.ColumnValue(sourceState, -1.0));
                    }
                }
                derivedMatrix.FinishRow();
            }
            return(derivedMatrix);
        }
Beispiel #3
0
        private TemporaryFile WriteFilesAndExecuteMrmc(Formula formulaToCheck, bool outputExactResult)        //returns result file
        {
            WriteMarkovChainToDisk();

            var transformationVisitor = new MrmcTransformer();

            transformationVisitor.Visit(formulaToCheck);
            var formulaToCheckString = transformationVisitor.TransformedFormula;

            bool useRewards;

            _initialStates = new Dictionary <int, double>();
            var enumerator = MarkovChain.GetEnumerator();

            enumerator.SelectInitialDistribution();
            while (enumerator.MoveNextTransition())
            {
                //enumerate initial states
                var entry = enumerator.CurrentTransition;
                var index = entry.Column + 1;                 //index in mrmc is 1-based
                if (_initialStates.ContainsKey(index))
                {
                    _initialStates.Add(index, _initialStates[index] + entry.Value);
                }
                else
                {
                    _initialStates.Add(index, entry.Value);
                }
            }

            var fileResults = new TemporaryFile("res");

            using (var fileCommandScript = new TemporaryFile("cmd"))
                using (var fileStateRewards = WriteRewardsToFile(formulaToCheck, out useRewards))
                {
                    var script = new StringBuilder();
                    script.AppendLine("set method_path gauss_jacobi");             //warning: gauss_seidel seems to be buggy in MRMC
                    script.AppendLine("set error_bound 1.0E-6");
                    script.AppendLine(formulaToCheckString);

                    //write result of every initial state to file
                    if (outputExactResult)
                    {
                        script.Append("write_res_file_result");
                    }
                    else
                    {
                        script.Append("write_res_file_state");
                    }
                    foreach (var initialState in _initialStates)
                    {
                        script.Append(" " + (initialState.Key));
                    }

                    script.AppendLine();
                    script.AppendLine("quit");

                    File.WriteAllText(fileCommandScript.FilePath, script.ToString());

                    var commandLineMode = useRewards ? "dmrm" : "dtmc";
                    var commandLineArgumentTransitionFile    = $" {_fileTransitions.FilePath}";
                    var commandLineArgumentStateLabelingFile = $" {_fileStateLabelings.FilePath}";
                    var commandLineArgumentRewardFile        = fileStateRewards != null ? $" {fileStateRewards.FilePath}" : "";
                    var commandLineArgumentCommandScriptFile = $" {fileCommandScript.FilePath}";
                    var commandLineArgumentResultsFile       = $" {fileResults.FilePath}";

                    var commandLineArguments =
                        commandLineMode
                        + commandLineArgumentTransitionFile
                        + commandLineArgumentStateLabelingFile
                        + commandLineArgumentRewardFile
                        + commandLineArgumentCommandScriptFile
                        + commandLineArgumentResultsFile;

                    var             pureModelCheckingTime    = "";
                    var             iterations               = "";
                    Action <string> extractInterestingOutput =
                        output =>
                    {
                        if (output.StartsWith("The Total Elapsed Model-Checking Time"))
                        {
                            pureModelCheckingTime = output;
                        }

                        else if (output.StartsWith("Gauss Jacobi V_B: The number of Gauss-Jacobi iterations"))
                        {
                            iterations = output;
                        }
                    };


                    var mrmc = new ExternalProcess("mrmc.exe", commandLineArguments, extractInterestingOutput);

                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    mrmc.Run();
                    stopwatch.Stop();

                    _output?.WriteLine(iterations);
                    _output?.WriteLine(pureModelCheckingTime);
                    _output?.WriteLine($"MRMC total model checking time: {stopwatch.Elapsed}");
                }
            return(fileResults);
        }
        private double CalculateProbabilityToReachStateFormulaInBoundedSteps(Formula psi, Formula phi, int steps)
        {
            // Pr[phi U psi]
            // calculate P [true U<=steps psi]

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var psiEvaluator = MarkovChain.CreateFormulaEvaluator(psi);
            var stateCount   = MarkovChain.States;

            var directlySatisfiedStates = CalculateSatisfiedStates(psiEvaluator);
            Dictionary <long, bool> excludedStates;

            if (phi == null)
            {
                excludedStates = new Dictionary <long, bool>();
            }
            else
            {
                // excludedStates = Sat(\phi) \Cup Sat(psi)
                var phiEvaluator   = MarkovChain.CreateFormulaEvaluator(phi);
                var phiOrPsiStates = CalculateSatisfiedStates(phiEvaluator);
                foreach (var directlySatisfiedState in directlySatisfiedStates)
                {
                    phiOrPsiStates[directlySatisfiedState.Key] = true;
                }
                excludedStates = CreateComplement(phiOrPsiStates);
            }

            var enumerator = MarkovChain.GetEnumerator();

            var xold  = new double[stateCount];
            var xnew  = CreateDerivedVector(directlySatisfiedStates);
            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 (directlySatisfiedStates.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 (excludedStates.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
                    {
                        enumerator.SelectSourceState(i);
                        var sum = 0.0;
                        while (enumerator.MoveNextTransition())
                        {
                            var entry = enumerator.CurrentTransition;
                            sum += entry.Value * xold[entry.Column];
                        }
                        xnew[i] = sum;
                    }
                }

                if (loops % 10 == 0)
                {
                    stopwatch.Stop();
                    var currentProbability = CalculateFinalProbability(xnew);
                    _output?.WriteLine($"{loops} Bounded Until iterations in {stopwatch.Elapsed}. Current probability={currentProbability.ToString(CultureInfo.InvariantCulture)}");
                    stopwatch.Start();
                }
            }

            var finalProbability = CalculateFinalProbability(xnew);

            stopwatch.Stop();
            return(finalProbability);
        }
        private double CalculateUnboundUntil(Formula psi, Formula phi, int iterationsLeft)
        {
            // On algorithmic verification methods for probabilistic systems (1998) by Christel Baier
            // Theorem 3.1.6 (page 36)
            // http://wwwneu.inf.tu-dresden.de/content/institutes/thi/algi/publikationen/texte/15_98.pdf

            // Pr[phi U psi]
            // calculate P [true U<=steps psi]

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var psiEvaluator = MarkovChain.CreateFormulaEvaluator(psi);
            var stateCount   = MarkovChain.States;

            var fixPointReached = iterationsLeft <= 0;

            var directlySatisfiedStates = CalculateSatisfiedStates(psiEvaluator);
            Dictionary <long, bool> excludedStates;

            if (phi == null)
            {
                excludedStates = new Dictionary <long, bool>();
            }
            else
            {
                // excludedStates = Sat(\phi) \Cup Sat(psi)
                var phiEvaluator   = MarkovChain.CreateFormulaEvaluator(phi);
                var phiOrPsiStates = CalculateSatisfiedStates(phiEvaluator);
                foreach (var directlySatisfiedState in directlySatisfiedStates)
                {
                    phiOrPsiStates[directlySatisfiedState.Key] = true;
                }
                excludedStates = CreateComplement(phiOrPsiStates);
            }

            // calculate probabilityExactlyZero (prob0)
            var probabilityExactlyZero = ProbabilityExactlyZero(directlySatisfiedStates, excludedStates);

            // calculate probabilityExactlyOne (prob1)
            var probabilityExactlyOne = ProbabilityExactlyOne(directlySatisfiedStates, excludedStates, probabilityExactlyZero);


            var enumerator = MarkovChain.GetEnumerator();

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

            while (!fixPointReached)
            {
                // switch xold and xnew
                var xtemp = xold;
                xold = xnew;
                xnew = xtemp;
                iterationsLeft--;
                loops++;
                for (var i = 0; i < stateCount; i++)
                {
                    if (probabilityExactlyOne.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 (probabilityExactlyZero.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
                    {
                        enumerator.SelectSourceState(i);
                        var sum = 0.0;
                        while (enumerator.MoveNextTransition())
                        {
                            var entry = enumerator.CurrentTransition;
                            sum += entry.Value * xold[entry.Column];
                        }
                        xnew[i] = sum;
                    }
                }

                if (loops % 10 == 0)
                {
                    stopwatch.Stop();
                    var currentProbability = CalculateFinalProbability(xnew);
                    _output?.WriteLine($"{loops} Fixpoint Until iterations in {stopwatch.Elapsed}. Current probability={currentProbability.ToString(CultureInfo.InvariantCulture)}");
                    stopwatch.Start();
                }
                if (iterationsLeft <= 0)
                {
                    fixPointReached = true;
                }
            }

            var finalProbability = CalculateFinalProbability(xnew);

            stopwatch.Stop();
            return(finalProbability);
        }
        private double CalculateProbabilityToReachStateFormulaInBoundedSteps(Formula psi, int steps)
        {
            // calculate P [true U<=steps psi]


            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var psiEvaluator = MarkovChain.CreateFormulaEvaluator(psi);
            var stateCount   = MarkovChain.States;

            var precalculatedStates = CreateEmptyPrecalculatedStatesArray();

            CalculateSatisfiedStates(precalculatedStates, psiEvaluator);
            //CalculateExcludedStates(precalculatedStates);  // change for \phi Until \psi

            var enumerator = MarkovChain.GetEnumerator();

            var xold  = new double[stateCount];
            var xnew  = CreateDerivedVector(precalculatedStates, PrecalculatedState.Satisfied);
            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 (precalculatedStates[i].HasFlag(PrecalculatedState.Satisfied))
                    {
                        //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 (precalculatedStates[i].HasFlag(PrecalculatedState.Excluded))
                    {
                        //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
                    {
                        enumerator.SelectSourceState(i);
                        var sum = 0.0;
                        while (enumerator.MoveNextTransition())
                        {
                            var entry = enumerator.CurrentTransition;
                            sum += entry.Value * xold[entry.Column];
                        }
                        xnew[i] = sum;
                    }
                }

                if (loops % 10 == 0)
                {
                    stopwatch.Stop();
                    var currentProbability = CalculateFinalProbability(xnew);
                    _output?.WriteLine($"{loops} Bounded Until iterations in {stopwatch.Elapsed}. Current probability={currentProbability.ToString(CultureInfo.InvariantCulture)}");
                    stopwatch.Start();
                }
            }

            var finalProbability = CalculateFinalProbability(xnew);

            stopwatch.Stop();
            return(finalProbability);
        }