/// <summary>
        ///   Generates a <see cref="LabeledTransitionMarkovDecisionProcess" /> for the model created by <paramref name="createModel" />.
        /// </summary>
        /// <param name="createModel">Creates the model that should be checked.</param>
        internal LabeledTransitionMarkovDecisionProcess GenerateLtmdp(AnalysisModelCreator createModel)
        {
            using (var modelTraverser = new ModelTraverser(createModel, Configuration, LabeledTransitionMarkovDecisionProcess.TransitionSize,
                                                           FormulaManager.NeedsStutteringState))
            {
                _mdp = new LabeledTransitionMarkovDecisionProcess(modelTraverser.Context.ModelCapacity.NumberOfStates, modelTraverser.Context.ModelCapacity.NumberOfTransitions);
                _mdp.StateFormulaLabels = FormulaManager.FinalStateFormulaLabels.ToArray();

                if (FormulaManager.NeedsStutteringState)
                {
                    _mdp.CreateStutteringState(modelTraverser.Context.StutteringStateIndex);
                }

                modelTraverser.Context.TraversalParameters.TransitionModifiers.AddRange(FormulaManager.TransitionModifierGenerators);
                modelTraverser.Context.TraversalParameters.BatchedTransitionActions.Add(() => new LabeledTransitionMarkovDecisionProcess.LtmdpBuilderDuringTraversal(_mdp));

                modelTraverser.Context.Output.WriteLine("Generating labeled transition markov decision process.");
                modelTraverser.TraverseModelAndReport();

                // StateStorage must be freed manually. Reason is that invariant checker does not free up the
                // space, because it might be necessary for other usages of the ModelTraversers (e.g. StateGraphGenerator
                // which keeps the States for the StateGraph)
                modelTraverser.Context.States.SafeDispose();
            }


            if (Configuration.WriteGraphvizModels)
            {
                FormulaManager.PrintStateFormulas(FormulaManager.FinalStateFormulas, Configuration.DefaultTraceOutput);
                Configuration.DefaultTraceOutput.WriteLine("Ltmdp Model");
                _mdp.ExportToGv(Configuration.DefaultTraceOutput);
            }

            return(_mdp);
        }
        internal NestedMarkovDecisionProcess ConvertToNmdp(LabeledTransitionMarkovDecisionProcess ltmdp)
        {
            var ltmdpToNmdp = new LtmdpToNmdp(ltmdp);
            var nmdp        = ltmdpToNmdp.NestedMarkovDecisionProcess;

            if (Configuration.WriteGraphvizModels)
            {
                Configuration.DefaultTraceOutput.WriteLine("Nmdp Model");
                nmdp.ExportToGv(Configuration.DefaultTraceOutput);
            }
            return(nmdp);
        }
예제 #3
0
        /// <summary>
        ///   Initializes a new instance.
        /// </summary>
        /// <param name="createModel">Creates the model that should be checked.</param>
        /// <param name="executableStateFormulas">The state formulas that can be evaluated over the generated state graph.</param>
        /// <param name="output">The callback that should be used to output messages.</param>
        /// <param name="configuration">The analysis configuration that should be used.</param>
        internal LtmdpGenerator(AnalysisModelCreator <TExecutableModel> createModel, Formula terminateEarlyCondition, Formula[] executableStateFormulas,
                                Action <string> output, AnalysisConfiguration configuration)
            : base(createModel, output, configuration, LabeledTransitionMarkovDecisionProcess.TransitionSize)
        {
            _mdp = new LabeledTransitionMarkovDecisionProcess(Context.ModelCapacity.NumberOfStates, Context.ModelCapacity.NumberOfTransitions);
            _mdp.StateFormulaLabels = executableStateFormulas.Select(stateFormula => stateFormula.Label).ToArray();

            Context.TraversalParameters.BatchedTransitionActions.Add(() => new LabeledTransitionMarkovDecisionProcess.LtmdpBuilderDuringTraversal <TExecutableModel>(_mdp, configuration));
            if (terminateEarlyCondition != null)
            {
                _mdp.CreateStutteringState(Context.StutteringStateIndex);
                var terminalteEarlyFunc = StateFormulaSetEvaluatorCompilationVisitor.Compile(_mdp.StateFormulaLabels, terminateEarlyCondition);
                Context.TraversalParameters.TransitionModifiers.Add(() => new EarlyTerminationModifier <TExecutableModel>(terminalteEarlyFunc));
            }
        }
예제 #4
0
            public UnderlyingDigraph(LabeledTransitionMarkovDecisionProcess ltmdp)
            {
                //Assumption "every node is reachable" is fulfilled due to the construction
                _ltmdp = ltmdp;
                _transitionTargetNo = ltmdp.TransitionTargets;
                _cidNo     = ltmdp.ContinuationGraphSize;
                _stateNo   = ltmdp.SourceStates.Count;
                _baseGraph = new BidirectionalGraph <EdgeType>();

                var initialCid = _ltmdp.GetRootContinuationGraphLocationOfInitialState();

                AddNodesOfContinuationId(initialCid);

                foreach (var sourceState in _ltmdp.SourceStates)
                {
                    var stateRootCid = _ltmdp.GetRootContinuationGraphLocationOfState(sourceState);
                    AddNodesOfContinuationId(stateRootCid);
                    AddStateToRootCidTransition(sourceState, stateRootCid);
                }
                AddTransitionTargets();
            }
예제 #5
0
        public LtmdpToNmdp(LabeledTransitionMarkovDecisionProcess ltmdp)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            Console.Out.WriteLine("Starting to convert labeled transition Markov Decision Process to Nested Markov Decision Process");
            Console.Out.WriteLine($"Ltmdp: States {ltmdp.SourceStates.Count}, TransitionTargets {ltmdp.TransitionTargets}, ContinuationGraphSize {ltmdp.ContinuationGraphSize}");
            _ltmdp = ltmdp;
            CreateStates();
            var modelCapacity = new ModelCapacityByModelSize(MdpStates, ltmdp.ContinuationGraphSize * 8L);

            NestedMarkovDecisionProcess = new NestedMarkovDecisionProcess(modelCapacity);
            NestedMarkovDecisionProcess.StateFormulaLabels = ltmdp.StateFormulaLabels;
            SetStateLabeling();
            ConvertInitialStates();
            ConvertTransitions();
            stopwatch.Stop();
            _ltmdp = null;
            Console.Out.WriteLine($"Completed transformation in {stopwatch.Elapsed}");
            Console.Out.WriteLine($"Nmdp: States {NestedMarkovDecisionProcess.States}, ContinuationGraphSize {NestedMarkovDecisionProcess.ContinuationGraphSize}");
        }
예제 #6
0
        public static void ExportToGv(this LabeledTransitionMarkovDecisionProcess ltmdp, TextWriter sb)
        {
            sb.WriteLine("digraph S {");
            //sb.WriteLine("size = \"8,5\"");
            sb.WriteLine("node [shape=box];");

            var initialStateName = "initialState";

            sb.WriteLine($" {initialStateName} [shape=point,width=0.0,height=0.0,label=\"\"];");
            var initialCid = ltmdp.GetRootContinuationGraphLocationOfInitialState();

            ExportCid(ltmdp, sb, initialStateName, false, initialCid);

            foreach (var state in ltmdp.SourceStates)
            {
                sb.Write($" {state} [label=\"{state}");
                sb.WriteLine("\"];");

                var cid      = ltmdp.GetRootContinuationGraphLocationOfState(state);
                var fromNode = state.ToString();
                ExportCid(ltmdp, sb, fromNode, false, cid);
            }
            sb.WriteLine("}");
        }
예제 #7
0
 // Note: Should be used with using(var modelchecker = new ...)
 public BuiltinLtmdpModelChecker(LabeledTransitionMarkovDecisionProcess mdp, TextWriter output = null)
     : base(mdp, output)
 {
     Requires.That(true, "Need CompactStateStorage to use this model checker");
     mdp.AssertIsDense();
 }
예제 #8
0
 public TransitionTargetEnumerator(LabeledTransitionMarkovDecisionProcess ltmdp)
 {
     _ltmdp       = ltmdp;
     CurrentIndex = -1;
 }
예제 #9
0
 public DirectChildrenEnumerator(LabeledTransitionMarkovDecisionProcess ltmdp, long parentContinuationId)
 {
     ContinuationGraphElement   = ltmdp._continuationGraph[parentContinuationId];
     CurrentChildContinuationId = ContinuationGraphElement.From - 1;
     ParentContinuationId       = parentContinuationId;
 }
예제 #10
0
 public TreeTraversal(LabeledTransitionMarkovDecisionProcess ltmdp, long parentContinuationId)
 {
     ParentContinuationId = parentContinuationId;
     Ltmdp = ltmdp;
 }
        // Note: Should be used with using(var modelchecker = new ...)
        public ConfigurationDependentLtmdpModelChecker(AnalysisConfiguration configuration, LabeledTransitionMarkovDecisionProcess markovChain, TextWriter output = null)
            : base(markovChain, output)
        {
            switch (configuration.LtmdpModelChecker)
            {
            case SafetyChecking.LtmdpModelChecker.BuiltInLtmdp:
                Requires.That(configuration.UseCompactStateStorage, "Need CompactStateStorage to use this algorithm");
                _ltmdpModelChecker = new BuiltinLtmdpModelChecker(Ltmdp, output);
                break;

            case SafetyChecking.LtmdpModelChecker.BuiltInNmdp:
                var nmdp = ConvertToNmdp(configuration, Ltmdp);
                _nmdpModelChecker = new BuiltinNmdpModelChecker(nmdp, output);
                break;

            case SafetyChecking.LtmdpModelChecker.BuildInMdpWithNewStates:
                nmdp = ConvertToNmdp(configuration, Ltmdp);
                var mdp = ConvertToMdpWithNewStates(configuration, nmdp, false);
                _mdpModelChecker = new BuiltinMdpModelChecker(mdp, output);
                break;

            case SafetyChecking.LtmdpModelChecker.BuildInMdpWithNewStatesConstantDistance:
                nmdp             = ConvertToNmdp(configuration, Ltmdp);
                mdp              = ConvertToMdpWithNewStates(configuration, nmdp, true);
                _mdpModelChecker = new BuiltinMdpModelChecker(mdp, output);
                break;

            case SafetyChecking.LtmdpModelChecker.BuildInMdpWithFlattening:
                nmdp             = ConvertToNmdp(configuration, Ltmdp);
                mdp              = ConvertToMdpWithFlattening(configuration, nmdp);
                _mdpModelChecker = new BuiltinMdpModelChecker(mdp, output);
                break;

            default:
                throw new NotImplementedException();
            }
        }
예제 #12
0
        private static void ExportCid(LabeledTransitionMarkovDecisionProcess ltmdp, TextWriter sb, string fromNode, bool fromProbabilistic, long currentCid)
        {
            LabeledTransitionMarkovDecisionProcess.ContinuationGraphElement choice = ltmdp.GetContinuationGraphElement(currentCid);
            if (choice.IsChoiceTypeUnsplitOrFinal)
            {
                var thisNode = $"cid{currentCid}";
                sb.WriteLine($" {thisNode} [ shape=point,width=0.1,height=0.1,label=\"\" ];");

                if (fromProbabilistic)
                {
                    sb.WriteLine($" {fromNode}->{thisNode} [ arrowhead =\"onormal\", label=\"{choice.Probability.ToString(CultureInfo.InvariantCulture)}\"];");
                }
                else
                {
                    sb.WriteLine($" {fromNode}->{thisNode} [ arrowhead =\"normal\"];");
                }

                var transitionTarget = ltmdp.GetTransitionTarget(choice.To);
                sb.Write($" {thisNode} -> {transitionTarget.TargetState} [ arrowhead =\"normal\",");
                sb.Write("label=\"");
                for (int i = 0; i < ltmdp.StateFormulaLabels.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Write(",");
                    }
                    if (transitionTarget.Formulas[i])
                    {
                        sb.Write("t");
                    }
                    else
                    {
                        sb.Write("f");
                    }
                }
                sb.WriteLine("\"];");
            }
            else if (choice.IsChoiceTypeForward)
            {
                // only forward node (no recursion)
                // do not print thisNode
                var toNode = $"cid{choice.To}";
                sb.WriteLine($" {fromNode}->{toNode} [ style =\"dashed\", label=\"{choice.Probability.ToString(CultureInfo.InvariantCulture)}\"];");
            }
            else
            {
                // we print how we came to this node
                var thisNode = $"cid{currentCid}";
                sb.WriteLine($" {thisNode} [ shape=point,width=0.1,height=0.1,label=\"\" ];");

                if (fromProbabilistic)
                {
                    sb.WriteLine($" {fromNode}->{thisNode} [ arrowhead =\"onormal\", label=\"{choice.Probability.ToString(CultureInfo.InvariantCulture)}\"];");
                }
                else
                {
                    sb.WriteLine($" {fromNode}->{thisNode} [ arrowhead =\"normal\"];");
                }



                for (var i = choice.From; i <= choice.To; i++)
                {
                    ExportCid(ltmdp, sb, thisNode, choice.IsChoiceTypeProbabilitstic, i);
                }
            }
        }
예제 #13
0
 // Note: Should be used with using(var modelchecker = new ...)
 protected LtmdpModelChecker(LabeledTransitionMarkovDecisionProcess mdp, TextWriter output = null)
 {
     Ltmdp   = mdp;
     _output = output;
 }
 /// <summary>
 ///   Initializes a new instance.
 /// </summary>
 /// <param name="ltmdp">The Markov chain that should be built up.</param>
 public LtmdpBuilderDuringTraversal(LabeledTransitionMarkovDecisionProcess ltmdp)
 {
     Requires.NotNull(ltmdp, nameof(ltmdp));
     _ltmdp = ltmdp;
 }