コード例 #1
0
        static void Main(string[] args)
        {
            //As.Add(new StateInfo("a1"));
            //As.Add(new StateInfo("a2"));
            //As.Add(new StateInfo("a3"));

            //Bs.Add(new StateInfo("b1"));
            //Bs.Add(new StateInfo("b2"));
            //Bs.Add(new StateInfo("b3"));

            //Rec(As, Bs, Correspondences);



            //var z1 = new StructAtom<string>("z1");

            //var w1 = new StructAtom<string>("w1");
            //var w2 = new StructAtom<string>("w2");
            //var w3 = new StructAtom<string>("w3");

            //FiniteStateMachine<StructAtom<string>, StructAtom<string>> fsmA = new FiniteStateMachine<StructAtom<string>, StructAtom<string>>();
            //var a1 = new FSMState<StructAtom<string>, StructAtom<string>>(fsmA, States.a1);
            //var a2 = new FSMState<StructAtom<string>, StructAtom<string>>(fsmA, States.a2);
            //var a3 = new FSMState<StructAtom<string>, StructAtom<string>>(fsmA, States.a3);
            //fsmA.AddOutgoing(a1, z1, a2, w1);
            //fsmA.AddOutgoing(a2, z1, a3, w2);
            //fsmA.AddOutgoing(a3, z1, a1, w3);

            //FiniteStateMachine<StructAtom<string>, StructAtom<string>> fsmB = new FiniteStateMachine<StructAtom<string>, StructAtom<string>>();
            //var b1 = new FSMState<StructAtom<string>, StructAtom<string>>(fsmB, States.b1);
            //var b2 = new FSMState<StructAtom<string>, StructAtom<string>>(fsmB, States.b2);
            //var b3 = new FSMState<StructAtom<string>, StructAtom<string>>(fsmB, States.b3);
            //fsmB.AddOutgoing(b1, z1, b3, w1);
            //fsmB.AddOutgoing(b3, z1, b2, w2);
            //fsmB.AddOutgoing(b2, z1, b1, w3);

            //var res = fsmA.IsIsomorphic(fsmB);
            //if(res != null)
            //    PrintCorrespondences(res);

            var z1 = new StructAtom <string>("z1");
            var z2 = new StructAtom <string>("z2");
            var z3 = new StructAtom <string>("z3");
            var z4 = new StructAtom <string>("z4");


            var w1 = new StructAtom <string>("w1");
            var w2 = new StructAtom <string>("w2");
            var w3 = new StructAtom <string>("w3");

            FiniteStateMachine <StructAtom <string>, StructAtom <string> > fsm =
                new FiniteStateMachine <StructAtom <string>, StructAtom <string> >("b");

            var a1 = new FSMState <StructAtom <string>, StructAtom <string> >(fsm, StateCores.a1);
            var a2 = new FSMState <StructAtom <string>, StructAtom <string> >(fsm, StateCores.a2);
            var a3 = new FSMState <StructAtom <string>, StructAtom <string> >(fsm, StateCores.a3);
            var a4 = new FSMState <StructAtom <string>, StructAtom <string> >(fsm, StateCores.a4);
            var a5 = new FSMState <StructAtom <string>, StructAtom <string> >(fsm, StateCores.a5);
            var a6 = new FSMState <StructAtom <string>, StructAtom <string> >(fsm, StateCores.a6);

            fsm.AddState(a1);
            fsm.AddState(a2);
            fsm.AddState(a3);
            fsm.AddState(a4);
            fsm.AddState(a5);
            fsm.AddState(a6);

            //fsm.AddOutgoing(a1, z1, a1, w2, 0.5);
            fsm.AddOutgoing(a1, z1, a2, w1, 0.5);
            fsm.AddOutgoing(a1, z2, a6, w2);
            fsm.AddOutgoing(a1, z3, a6, w1);
            fsm.AddOutgoing(a1, z4, a2, w3);

            fsm.AddOutgoing(a2, z1, a5, w2);
            fsm.AddOutgoing(a2, z2, a1, w1);
            fsm.AddOutgoing(a2, z3, a1, w1);
            fsm.AddOutgoing(a2, z4, a5, w3);

            fsm.AddOutgoing(a3, z1, a1, w1);
            fsm.AddOutgoing(a3, z2, a5, w3);
            fsm.AddOutgoing(a3, z3, a5, w1);
            fsm.AddOutgoing(a3, z4, a1, w1);

            fsm.AddOutgoing(a4, z1, a6, w1);
            fsm.AddOutgoing(a4, z2, a2, w2);
            fsm.AddOutgoing(a4, z3, a2, w2);
            fsm.AddOutgoing(a4, z4, a6, w3);

            fsm.AddOutgoing(a5, z1, a1, w3);
            fsm.AddOutgoing(a5, z2, a1, w1);
            fsm.AddOutgoing(a5, z3, a2, w3);
            fsm.AddOutgoing(a5, z4, a4, w1);

            fsm.AddOutgoing(a6, z1, a2, w2);
            fsm.AddOutgoing(a6, z2, a6, w2);
            fsm.AddOutgoing(a6, z3, a5, w3);
            fsm.AddOutgoing(a6, z4, a3, w1);

            fsm.InitialState = a1;

            List <Partition <FSMState <StructAtom <string>, StructAtom <string> > > > pis =
                new List <Partition <FSMState <StructAtom <string>, StructAtom <string> > > >();

            var pi1 = new Partition <FSMState <StructAtom <string>, StructAtom <string> > >();

            pi1.Add(new[]
            {
                a1,
                a2,
                a3,
                a4
            });
            pi1.Add(new[]
            {
                a5,
                a6
            });

            var pi2 = new Partition <FSMState <StructAtom <string>, StructAtom <string> > >();

            pi2.Add(new[]
            {
                a1,
                a2,
                a5,
                a6
            });
            pi2.Add(new[]
            {
                a3,
                a4
            });

            var pi3 = new Partition <FSMState <StructAtom <string>, StructAtom <string> > >();

            pi3.Add(new[]
            {
                a1,
                a3,
                a5
            });
            pi3.Add(new[]
            {
                a2,
                a4,
                a6
            });

            DecompositionAlgorithm <StructAtom <string>, StructAtom <string> > alg =
                new DecompositionAlgorithm <StructAtom <string>, StructAtom <string> >(fsm, new[] { pi1, pi2, pi3 });

            var net = alg.Solve();

            ProcessFSM(fsm);
            Console.WriteLine();
            ProcessFSM(net);

            var inputs = new StructAtom <string>[]
            {
                z1,
                z2,
                z3,
                z4,
                z4,
                z3,
                z2,
                z1,
                z3,
                z2,
                z4,
                z1
            };

            ProcessFSMLin(inputs, fsm, net);
            fsm.Randomize();
            ProcessFSMLin(inputs, fsm, net);


            HashSet <FSMState <StructAtom <string>, StructAtom <string> > > set = new HashSet <FSMState <StructAtom <string>, StructAtom <string> > >(new []
            {
                a1,
                a2,
                a3,
                a4,
                a5,
                a6
            });
            var res = set.GetSubsets();

            foreach (var re in res)
            {
                foreach (var i in re)
                {
                    Console.Write(" {0}", i);
                }
                Console.WriteLine();
            }
            Console.WriteLine("----------");



            IEnumerable <Partition <FSMState <StructAtom <string>, StructAtom <string> > > > partitions =
                Partition <FSMState <StructAtom <string>, StructAtom <string> > > .GetAllPartitions(set, 1);//.Where(p => p.Count() >= 2 && p.Count() <= 4);

            var preParts = Partition <FSMState <StructAtom <string>, StructAtom <string> > > .FilterSamePartitions(partitions).ToArray();

            List <List <Partition <FSMState <StructAtom <string>, StructAtom <string> > > > > partsLists = Partition <FSMState <StructAtom <string>, StructAtom <string> > > .
                                                                                                           GetAllOrtPartitionSets(
                preParts,
                //new[] {pi1, pi2},
                set,
                2,
                //3);
                preParts.Count() - 1);

            //Console.WriteLine(Partition<FSMState<StructAtom<string>, StructAtom<string>>>.GetAllPartitions(set,1).Count().ToString());
            foreach (var partsList in partsLists.Take(100))
            {
                foreach (var partition in partsList)
                {
                    foreach (var hashSet in partition)
                    {
                        foreach (var item in hashSet)
                        {
                            Console.Write("{0}", item);
                        }
                        Console.Write(" ");
                    }
                    Console.Write("\t");
                }
                Console.WriteLine("");
            }
            Console.WriteLine();
            Console.ReadKey();
        }
コード例 #2
0
ファイル: FSMXmlWorker.cs プロジェクト: erad888/fsm-decompose
        public void ParseFromNode(XmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            value = new FiniteStateMachine <StructAtom <string>, StructAtom <string> >();

            try
            {
                for (int i = 0; i < node.ChildNodes.Count; ++i)
                {
                    var childNode = node.ChildNodes[i];
                    switch (childNode.Name.ToLower())
                    {
                    case "stateset":
                    {
                        for (int j = 0; j < childNode.ChildNodes.Count; ++j)
                        {
                            var childChildNode = childNode.ChildNodes[j];
                            if (childChildNode.Name.ToLower() == "transition")
                            {
                                StateXmlWorker w = new StateXmlWorker();
                                w.FSM = value;
                                w.ParseFromNode(childChildNode);
                                var state = w.Value as FSMState <StructAtom <string>, StructAtom <string> >;
                                if (state != null)
                                {
                                    value.AddState(state);
                                }
                            }
                        }
                    }
                    break;

                    case "inputset":
                    {
                        for (int j = 0; j < childNode.ChildNodes.Count; ++j)
                        {
                            var childChildNode = childNode.ChildNodes[j];
                            if (childChildNode.Name.ToLower() == "input")
                            {
                                StructAtomStringXmlWorker w = new StructAtomStringXmlWorker();
                                w.ParseFromNode(childChildNode);
                                var inp = w.Value as StructAtom <string>;
                                if (inp != null)
                                {
                                    value.AddInput(inp);
                                }
                            }
                        }
                    }
                    break;

                    case "outputset":
                    {
                        for (int j = 0; j < childNode.ChildNodes.Count; ++j)
                        {
                            var childChildNode = childNode.ChildNodes[j];
                            if (childChildNode.Name.ToLower() == "output")
                            {
                                StructAtomStringXmlWorker w = new StructAtomStringXmlWorker();
                                w.ParseFromNode(childChildNode);
                                var inp = w.Value as StructAtom <string>;
                                if (inp != null)
                                {
                                    value.AddOutput(inp);
                                }
                            }
                        }
                    }
                    break;

                    case "keyname":
                        value.KeyName = childNode.InnerText;
                        break;

                    case "initialstate":
                    {
                        StateXmlWorker w = new StateXmlWorker();
                        w.FSM = value;
                        w.ParseFromNode(childNode);
                        var state = w.Value as FSMState <StructAtom <string>, StructAtom <string> >;
                        if (state != null)
                        {
                            value.InitialState = state;
                        }
                    }
                    break;
                    }
                }

                for (int i = 0; i < node.ChildNodes.Count; ++i)
                {
                    var childNode = node.ChildNodes[i];
                    switch (childNode.Name.ToLower())
                    {
                    case "transitions":
                    {
                        for (int j = 0; j < childNode.ChildNodes.Count; ++j)
                        {
                            var childChildNode = childNode.ChildNodes[j];
                            if (childChildNode.Name.ToLower() == "transition")
                            {
                                TransitionXmlWorker w = new TransitionXmlWorker();
                                w.FSM = value;
                                w.ParseFromNode(childChildNode);
                                var tr = w.Value as Transition <StructAtom <string>, StructAtom <string> >;
                                if (tr != null)
                                {
                                    foreach (var destinationState in tr.destinationStates)
                                    {
                                        value.AddOutgoing(tr.SourceState, tr.Input, destinationState.DestState, destinationState.Output, destinationState.Probability);
                                    }
                                    //value.Transitions.Add(tr.ToString(), tr);
                                }
                            }
                        }
                    }
                    break;
                    }
                }
            }
            catch (Exception exc)
            {
                value = null;
                throw exc;
            }
        }