Ejemplo n.º 1
0
        public static void ExportToGv(this DiscreteTimeMarkovChain markovChain, TextWriter sb)
        {
            sb.WriteLine("digraph S {");
            //sb.WriteLine("size = \"8,5\"");
            sb.WriteLine("node [shape=box];");
            var enumerator = markovChain.GetEnumerator();

            while (enumerator.MoveNextState())
            {
                var state = enumerator.CurrentState;
                sb.Write($" {state} [label=\"{state}\\n(");
                for (int i = 0; i < markovChain.StateFormulaLabels.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Write(",");
                    }
                    sb.Write(markovChain.StateLabeling[state][i]);
                }
                sb.WriteLine(")\"];");
                while (enumerator.MoveNextTransition())
                {
                    sb.WriteLine($"{enumerator.CurrentState} -> {enumerator.CurrentTransition.Column} [label=\"{Probability.PrettyPrint(enumerator.CurrentTransition.Value)}\"];");
                }
            }
            sb.WriteLine("}");
        }
Ejemplo n.º 2
0
        public void WriteMarkovChainToStream(TextWriter streamTransitions, TextWriter streamStateLabelings)
        {
            streamTransitions.NewLine    = "\n";
            streamStateLabelings.NewLine = "\n";

            streamTransitions.WriteLine("STATES " + _markovChain.States);
            streamTransitions.WriteLine("TRANSITIONS " + _markovChain.Transitions);

            var enumerator = _markovChain.GetEnumerator();

            while (enumerator.MoveNextState())
            {
                while (enumerator.MoveNextTransition())
                {
                    var currentColumnValue = enumerator.CurrentTransition;
                    streamTransitions.WriteLine((enumerator.CurrentState + 1) + " " + (currentColumnValue.Column + 1) + " " + currentColumnValue.Value.ToString(CultureInfo.InvariantCulture));                   //index in mrmc is 1-based
                }
            }
            streamTransitions.Flush();
            streamTransitions.Close();

            streamStateLabelings.WriteLine("#DECLARATION");

            var noStateFormulaLabels = _markovChain.StateFormulaLabels.Length;

            for (var i = 0; i < noStateFormulaLabels; i++)
            {
                var label = _markovChain.StateFormulaLabels[i];
                if (i > 0)
                {
                    streamStateLabelings.Write(" ");
                }
                streamStateLabelings.Write(label);
            }
            streamStateLabelings.WriteLine();
            streamStateLabelings.WriteLine("#END");
            var stateLabeling = _markovChain.StateLabeling;

            for (var state = 0; state < _markovChain.States; state++)
            {
                streamStateLabelings.Write(state + 1);               //index in mrmc is 1-based
                for (var i = 0; i < noStateFormulaLabels; i++)
                {
                    var label = _markovChain.StateFormulaLabels[i];
                    if (stateLabeling[state][i])
                    {
                        streamStateLabelings.Write(" " + label);
                    }
                }
                streamStateLabelings.WriteLine();
            }
            streamStateLabelings.Flush();
            streamStateLabelings.Close();
        }
Ejemplo n.º 3
0
            public UnderlyingDigraph(DiscreteTimeMarkovChain markovChain)
            {
                //Assumption "every node is reachable" is fulfilled due to the construction
                var newGraph = new BidirectionalGraph();

                BaseGraph = newGraph;

                var enumerator = markovChain.GetEnumerator();

                while (enumerator.MoveNextState())
                {
                    while (enumerator.MoveNextTransition())
                    {
                        if (enumerator.CurrentTransition.Value > 0.0)
                        {
                            newGraph.AddVerticesAndEdge(new Edge(enumerator.CurrentState, enumerator.CurrentTransition.Column));
                        }
                    }
                }
            }