Example #1
0
        public void CalculateAncestorsTest(MarkovChainExample example)
        {
            var markovChain = example.MarkovChain;

            var underlyingDigraph = markovChain.CreateUnderlyingDigraph();
            var nodesToIgnore     = new Dictionary <long, bool>();

            var result1 = underlyingDigraph.BaseGraph.GetAncestors(example.StatesSatisfyDirectlyLabel1Formula.Keys, nodesToIgnore.ContainsKey);

            foreach (var result in result1.Keys)
            {
                Assert.True(example.AncestorsOfStatesWithLabel1.ContainsKey(result), $"state {result} not found in expected results (label1)");
            }
            foreach (var result in example.AncestorsOfStatesWithLabel1.Keys)
            {
                Assert.True(result1.ContainsKey(result), $"state {result} not found in calculated results (label1)");
            }


            var result2 = underlyingDigraph.BaseGraph.GetAncestors(example.StatesSatisfyDirectlyLabel2Formula.Keys, nodesToIgnore.ContainsKey);

            foreach (var result in result2.Keys)
            {
                Assert.True(example.AncestorsOfStatesWithLabel2.ContainsKey(result), $"state {result} not found in expected results (label2)");
            }
            foreach (var result in example.AncestorsOfStatesWithLabel2.Keys)
            {
                Assert.True(result2.ContainsKey(result), $"state {result} not found in calculated results (label2)");
            }
        }
        public void ProbabilityToReach_Label1(MarkovChainExample example)
        {
            var dtmc = example.MarkovChain;

            var finallyLabel1 = new UnaryFormula(MarkovChainExample.Label1Formula, UnaryOperator.Finally);

            using (var checker = new ExternalDtmcModelCheckerMrmc(dtmc, Output.TextWriterAdapter()))
            {
                var result = checker.CalculateProbability(finallyLabel1);
                result.Is(example.ProbabilityFinallyLabel1, 0.0001).ShouldBe(true);
            }
        }
        public void ProbabilityIn10Steps_Label1UntilLabel2(MarkovChainExample example)
        {
            var dtmc = example.MarkovChain;

            var label1UntilLabel2 = new BoundedBinaryFormula(MarkovChainExample.Label1Formula, BinaryOperator.Until, MarkovChainExample.Label2Formula, 10);

            using (var prismChecker = new BuiltinDtmcModelChecker(dtmc, Output.TextWriterAdapter()))
            {
                var result = prismChecker.CalculateProbability(label1UntilLabel2);
                result.Is(example.ProbabilityLabel1UntilLabel2, 0.0001).ShouldBe(true);
            }
        }
        public void ProbabilityToReachIn10Steps_Label1(MarkovChainExample example)
        {
            var dtmc = example.MarkovChain;

            var finallyLabel1 = new BoundedUnaryFormula(MarkovChainExample.Label1Formula, UnaryOperator.Finally, 10);

            using (var prismChecker = new BuiltinDtmcModelChecker(dtmc, Output.TextWriterAdapter()))
            {
                var result = prismChecker.CalculateProbability(finallyLabel1);
                result.Is(example.ProbabilityFinally10Label1, 0.0001).ShouldBe(true);
            }
        }
Example #5
0
        public void FormulaEvaluatorTest(MarkovChainExample example)
        {
            var markovChain = example.MarkovChain;

            var evaluateStateFormulaLabel1   = markovChain.CreateFormulaEvaluator(MarkovChainExample.Label1Formula);
            var evaluateStateFormulaLabel2   = markovChain.CreateFormulaEvaluator(MarkovChainExample.Label2Formula);
            var evaluateStateFormulaExample1 = markovChain.CreateFormulaEvaluator(example.ExampleFormula1);
            var evaluateStateFormulaExample2 = markovChain.CreateFormulaEvaluator(example.ExampleFormula2);

            var satisfyStateFormulaLabel1   = 0;
            var satisfyStateFormulaLabel2   = 0;
            var satisfyStateFormulaExample1 = 0;
            var satisfyStateFormulaExample2 = 0;

            for (var i = 0; i < markovChain.States; i++)
            {
                if (evaluateStateFormulaLabel1(i))
                {
                    Assert.True(example.StatesSatisfyDirectlyLabel1Formula.ContainsKey(i), $"Formula is satisfied in state {i}, which is not expected (label1)");
                    satisfyStateFormulaLabel1++;
                }
                if (evaluateStateFormulaLabel2(i))
                {
                    Assert.True(example.StatesSatisfyDirectlyLabel2Formula.ContainsKey(i), $"Formula is satisfied in state {i}, which is not expected (label2)");
                    satisfyStateFormulaLabel2++;
                }
                if (evaluateStateFormulaExample1(i))
                {
                    Assert.True(example.StatesSatisfyDirectlyExampleFormula1.ContainsKey(i), $"Formula is satisfied in state {i}, which is not expected (exampleformula1)");
                    satisfyStateFormulaExample1++;
                }
                if (evaluateStateFormulaExample2(i))
                {
                    Assert.True(example.StatesSatisfyDirectlyExampleFormula2.ContainsKey(i), $"Formula is satisfied in state {i}, which is not expected (exampleformula2)");
                    satisfyStateFormulaExample2++;
                }
            }
            Assert.Equal(example.StatesSatisfyDirectlyLabel1Formula.Count, satisfyStateFormulaLabel1);
            Assert.Equal(example.StatesSatisfyDirectlyLabel2Formula.Count, satisfyStateFormulaLabel2);
            Assert.Equal(example.StatesSatisfyDirectlyExampleFormula1.Count, satisfyStateFormulaExample1);
            Assert.Equal(example.StatesSatisfyDirectlyExampleFormula2.Count, satisfyStateFormulaExample2);
        }
Example #6
0
        public void SumOfAllDistributionsOk(MarkovChainExample example)
        {
            var markovChain = example.MarkovChain;

            markovChain.ProbabilityMatrix.PrintMatrix(Output.Log);
            markovChain.ValidateStates();
            markovChain.PrintPathWithStepwiseHighestProbability(10);
            var enumerator = markovChain.GetEnumerator();
            var counter    = 0.0;

            while (enumerator.MoveNextState())
            {
                var counterOfTransition = 0.0;
                while (enumerator.MoveNextTransition())
                {
                    counterOfTransition += enumerator.CurrentTransition.Value;
                }
                counter += counterOfTransition;
                var resultIsNotFarFrom1 = counterOfTransition >= 1.0 - 1.0E-16 && counterOfTransition <= 1.0 + 1.0E-16;
                Assert.Equal(resultIsNotFarFrom1, true);
            }
            Assert.Equal(1.0 * markovChain.States, counter);
        }