// end static class ExactBlock
        public static void Main(string[] args)
        {
            TransducerGraph fa = new TransducerGraph();

            fa.AddArc(fa.GetStartNode(), "1", "a", string.Empty);
            fa.AddArc(fa.GetStartNode(), "2", "b", string.Empty);
            fa.AddArc(fa.GetStartNode(), "3", "c", string.Empty);
            fa.AddArc("1", "4", "a", string.Empty);
            fa.AddArc("2", "4", "a", string.Empty);
            fa.AddArc("3", "5", "c", string.Empty);
            fa.AddArc("4", "6", "c", string.Empty);
            fa.AddArc("5", "6", "c", string.Empty);
            fa.SetEndNode("6");
            System.Console.Out.WriteLine(fa);
            ExactAutomatonMinimizer minimizer = new ExactAutomatonMinimizer();

            System.Console.Out.WriteLine(minimizer.MinimizeFA(fa));
            System.Console.Out.WriteLine("Starting...");
            Timing.StartTime();
            TransducerGraph randomFA          = TransducerGraph.CreateRandomGraph(100, 10, 1.0, 10, new ArrayList());
            TransducerGraph minimizedRandomFA = minimizer.MinimizeFA(randomFA);

            System.Console.Out.WriteLine(randomFA);
            System.Console.Out.WriteLine(minimizedRandomFA);
            Timing.Tick("done. ( " + randomFA.GetArcs().Count + " arcs to " + minimizedRandomFA.GetArcs().Count + " arcs)");
        }
Example #2
0
        /// <summary>Takes time linear in number of arcs.</summary>
        public virtual TransducerGraph PushLambdas(TransducerGraph graph, ClassicCounter lambda)
        {
            TransducerGraph result = null;

            result = graph.Clone();
            // arcs have been copied too so we don't mess up graph
            ICollection <TransducerGraph.Arc> arcs = result.GetArcs();

            foreach (TransducerGraph.Arc arc in arcs)
            {
                double sourceLambda = lambda.GetCount(arc.GetSourceNode());
                double targetLambda = lambda.GetCount(arc.GetTargetNode());
                double oldOutput    = ((double)arc.GetOutput());
                double newOutput    = oldOutput + targetLambda - sourceLambda;
                arc.SetOutput(newOutput);
            }
            // do initialOutput
            double startLambda = lambda.GetCount(result.GetStartNode());

            if (startLambda != 0.0)
            {
                // add it back to the outbound arcs from start (instead of adding it to the initialOutput)
                ICollection <TransducerGraph.Arc> startArcs = result.GetArcsBySource(result.GetStartNode());
                foreach (TransducerGraph.Arc arc_1 in startArcs)
                {
                    double oldOutput = ((double)arc_1.GetOutput());
                    double newOutput = oldOutput + startLambda;
                    arc_1.SetOutput(newOutput);
                }
            }
            // do finalOutput
            foreach (object o in result.GetEndNodes())
            {
                double endLambda = lambda.GetCount(o);
                if (endLambda != 0.0)
                {
                    // subtract it from the inbound arcs to end (instead of subtracting it from the finalOutput)
                    ICollection <TransducerGraph.Arc> endArcs = result.GetArcsByTarget(o);
                    foreach (TransducerGraph.Arc arc_1 in endArcs)
                    {
                        double oldOutput = ((double)arc_1.GetOutput());
                        double newOutput = oldOutput - endLambda;
                        arc_1.SetOutput(newOutput);
                    }
                }
            }
            return(result);
        }
        protected internal virtual TransducerGraph BuildMinimizedFA()
        {
            TransducerGraph minimizedFA   = new TransducerGraph();
            TransducerGraph unminimizedFA = GetUnminimizedFA();

            foreach (TransducerGraph.Arc arc in unminimizedFA.GetArcs())
            {
                ICollection <TransducerGraph.Arc> source = ProjectNode(arc.GetSourceNode());
                ICollection <TransducerGraph.Arc> target = ProjectNode(arc.GetTargetNode());
                try
                {
                    if (minimizedFA.CanAddArc(source, target, arc.GetInput(), arc.GetOutput()))
                    {
                        minimizedFA.AddArc(source, target, arc.GetInput(), arc.GetOutput());
                    }
                }
                catch (Exception)
                {
                }
            }
            //throw new IllegalArgumentException();
            minimizedFA.SetStartNode(ProjectNode(unminimizedFA.GetStartNode()));
            foreach (object o in unminimizedFA.GetEndNodes())
            {
                minimizedFA.SetEndNode(ProjectNode(o));
            }
            return(minimizedFA);
        }
        // assumes that the path already has EPSILON as the last element.
        public static void AddOnePathToGraph(IList path, double count, int markovOrder, TransducerGraph graph)
        {
            object source = graph.GetStartNode();

            for (int j = 0; j < path.Count; j++)
            {
                object input          = path[j];
                TransducerGraph.Arc a = graph.GetArcBySourceAndInput(source, input);
                if (a != null)
                {
                    // increment the arc weight
                    a.output = ((double)a.output) + count;
                }
                else
                {
                    object target;
                    if (input.Equals(TransducerGraph.EpsilonInput))
                    {
                        target = "END";
                    }
                    else
                    {
                        // to ensure they all share the same end node
                        if (markovOrder == 0)
                        {
                            // we all transition back to the same state
                            target = source;
                        }
                        else
                        {
                            if (markovOrder > 0)
                            {
                                // the state is described by the partial history
                                target = path.SubList((j < markovOrder ? 0 : j - markovOrder + 1), j + 1);
                            }
                            else
                            {
                                // the state is described by the full history
                                target = path.SubList(0, j + 1);
                            }
                        }
                    }
                    double output = count;
                    a = new TransducerGraph.Arc(source, target, input, output);
                    graph.AddArc(a);
                }
                source = a.GetTargetNode();
            }
            graph.SetEndNode(source);
        }