Exemple #1
0
        public static void ExportToGv(this NestedMarkovDecisionProcess nmdp, 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 = nmdp.GetRootContinuationGraphLocationOfInitialState();

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

            for (var state = 0; state < nmdp.States; state++)
            {
                sb.Write($" {state} [label=\"{state}\\n(");
                for (int i = 0; i < nmdp.StateFormulaLabels.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Write(",");
                    }
                    sb.Write(nmdp.StateLabeling[state][i]);
                }
                sb.WriteLine(")\"];");

                var cid      = nmdp.GetRootContinuationGraphLocationOfState(state);
                var fromNode = state.ToString();
                ExportCid(nmdp, sb, fromNode, false, cid);
            }
            sb.WriteLine("}");
        }
Exemple #2
0
            public UnderlyingDigraph(NestedMarkovDecisionProcess mdp)
            {
                //Assumption "every node is reachable" is fulfilled due to the construction
                BaseGraph = new BidirectionalGraph();

                var currentState = 0;
                Action <ContinuationGraphElement> addTargetState = cge =>
                {
                    if (cge.IsChoiceTypeUnsplitOrFinal)
                    {
                        var cgl = cge.AsLeaf;
                        if (cgl.Probability > 0.0)
                        {
                            BaseGraph.AddVerticesAndEdge(new Edge(currentState, cgl.ToState));
                        }
                    }
                };

                for (currentState = 0; currentState < mdp.States; currentState++)
                {
                    var parentCid     = mdp.GetRootContinuationGraphLocationOfState(currentState);
                    var treeTraverser = mdp.GetTreeTraverser(parentCid);
                    treeTraverser.ApplyActionWithStackBasedAlgorithm(addTargetState);
                }
            }
Exemple #3
0
        private static void ExportCid(NestedMarkovDecisionProcess nmdp, TextWriter sb, string fromNode, bool fromProbabilistic, long currentCid)
        {
            NestedMarkovDecisionProcess.ContinuationGraphElement cge = nmdp.GetContinuationGraphElement(currentCid);
            if (cge.IsChoiceTypeUnsplitOrFinal)
            {
                var cgl = nmdp.GetContinuationGraphLeaf(currentCid);

                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=\"{cgl.Probability.ToString(CultureInfo.InvariantCulture)}\"];");
                }
                else
                {
                    sb.WriteLine($" {fromNode}->{thisNode} [ arrowhead =\"normal\"];");
                }

                sb.WriteLine($" {thisNode} -> {cgl.ToState} [ arrowhead =\"normal\"];");
            }
            else if (cge.IsChoiceTypeForward)
            {
                // only forward node (no recursion)
                // do not print thisNode
                var cgi    = nmdp.GetContinuationGraphInnerNode(currentCid);
                var toNode = $"cid{cgi.ToCid}";
                sb.WriteLine($" {fromNode}->{toNode} [ style =\"dashed\", label=\"{Probability.PrettyPrint(cgi.Probability)}\"];");
            }
            else
            {
                // we print how we came to this node
                var cgi = nmdp.GetContinuationGraphInnerNode(currentCid);

                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=\"{Probability.PrettyPrint(cgi.Probability)}\"];");
                }
                else
                {
                    sb.WriteLine($" {fromNode}->{thisNode} [ arrowhead =\"normal\"];");
                }

                for (var i = cgi.FromCid; i <= cgi.ToCid; i++)
                {
                    ExportCid(nmdp, sb, thisNode, cge.IsChoiceTypeProbabilitstic, i);
                }
            }
        }
        public NmdpToMdpByFlattening(NestedMarkovDecisionProcess nmdp)
            : base(nmdp)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            Console.Out.WriteLine("Starting to convert Nested Markov Decision Process to Markov Decision Process");
            Console.Out.WriteLine($"Nmdp: States {nmdp.States}, ContinuationGraphSize {nmdp.ContinuationGraphSize}");
            var modelCapacity = new ModelCapacityByModelSize(nmdp.States, nmdp.ContinuationGraphSize * 8L);

            MarkovDecisionProcess = new MarkovDecisionProcess(modelCapacity);
            MarkovDecisionProcess.StateFormulaLabels = nmdp.StateFormulaLabels;
            CopyStateLabeling();
            ConvertInitialTransitions();
            ConvertStateTransitions();
            stopwatch.Stop();
            _nmdp = null;
            Console.Out.WriteLine($"Completed transformation in {stopwatch.Elapsed}");
            Console.Out.WriteLine($"Mdp: States {MarkovDecisionProcess.States}, Transitions {MarkovDecisionProcess.Transitions}");
        }
Exemple #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}");
        }
Exemple #6
0
        public NmdpToMdpByNewStates(NestedMarkovDecisionProcess nmdp, TextWriter output = null, bool makeConstantDistanceBetweenStates = true)
            : base(nmdp)
        {
            _output = output ?? Console.Out;
            _makeConstantDistanceBetweenStates = makeConstantDistanceBetweenStates;

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            _output.WriteLine("Starting to convert Nested Markov Decision Process to Markov Decision Process");
            _output.WriteLine($"Nmdp: States {nmdp.States}, ContinuationGraphSize {nmdp.ContinuationGraphSize}");

            var newNumberOfStates      = nmdp.ContinuationGraphSize;
            var newNumberOfTransitions = nmdp.ContinuationGraphSize;

            if (_makeConstantDistanceBetweenStates)
            {
                newNumberOfStates      = 30 * newNumberOfStates;
                newNumberOfTransitions = 30 * newNumberOfTransitions;
            }

            var modelCapacity = new ModelCapacityByModelSize(newNumberOfStates, newNumberOfTransitions);

            MarkovDecisionProcess = new MarkovDecisionProcess(modelCapacity);

            _nmdpStates = _nmdp.States;

            CalculateMaxDistanceBetweenStates();
            CreateArtificalStateFormula();
            ConvertInitialTransitions();
            ConvertStateTransitions();
            AddPaddingStatesInMdp();
            SetStateLabelings();

            stopwatch.Stop();
            _nmdp = null;
            _output.WriteLine($"Completed transformation in {stopwatch.Elapsed}");
            _output.WriteLine($"Mdp: States {MarkovDecisionProcess.States}, Transitions {MarkovDecisionProcess.Transitions}");
        }
 // Note: Should be used with using(var modelchecker = new ...)
 public BuiltinNmdpModelChecker(NestedMarkovDecisionProcess nmdp, TextWriter output = null)
 {
     Nmdp               = nmdp;
     _output            = output;
     _underlyingDigraph = Nmdp.CreateUnderlyingDigraph();
 }
 // Note: Should be used with using(var modelchecker = new ...)
 public BuiltinNmdpModelChecker(NestedMarkovDecisionProcess nmdp, TextWriter output = null)
     : base(nmdp, output)
 {
     _underlyingDigraph = Nmdp.CreateUnderlyingDigraph();
 }
        private MarkovDecisionProcess ConvertToMdpWithNewStates(AnalysisConfiguration configuration, NestedMarkovDecisionProcess nmdp, bool makeConstantDistanceBetweenStates)
        {
            var nmdpToMpd = new NmdpToMdpByNewStates(nmdp, configuration.DefaultTraceOutput, makeConstantDistanceBetweenStates);
            var mdp       = nmdpToMpd.MarkovDecisionProcess;

            if (configuration.WriteGraphvizModels)
            {
                configuration.DefaultTraceOutput.WriteLine("Mdp Model");
                mdp.ExportToGv(configuration.DefaultTraceOutput);
            }
            return(mdp);
        }
        private MarkovDecisionProcess ConvertToMdpWithFlattening(AnalysisConfiguration configuration, NestedMarkovDecisionProcess nmdp)
        {
            var nmdpToMpd = new NmdpToMdpByFlattening(nmdp);
            var mdp       = nmdpToMpd.MarkovDecisionProcess;

            if (configuration.WriteGraphvizModels)
            {
                configuration.DefaultTraceOutput.WriteLine("Mdp Model");
                mdp.ExportToGv(configuration.DefaultTraceOutput);
            }
            return(mdp);
        }
Exemple #11
0
 public TreeTraversal(NestedMarkovDecisionProcess nmdp, long parentContinuationId)
 {
     ParentContinuationId = parentContinuationId;
     Nmdp = nmdp;
 }
Exemple #12
0
 // Note: Should be used with using(var modelchecker = new ...)
 protected NmdpModelChecker(NestedMarkovDecisionProcess nmdp, TextWriter output = null)
 {
     Nmdp    = nmdp;
     _output = output;
 }
Exemple #13
0
 protected NmdpToMdp(NestedMarkovDecisionProcess nmdp)
 {
     _nmdp = nmdp;
 }