示例#1
0
        /// <summary>
        ///     Функция выдачи денег
        /// </summary>
        /// <param name="requestedSum">Требуемая сумма</param>
        /// <returns>Money</returns>
        public Money WithdrawMoney(decimal requestedSum)
        {
            Log.Debug("Withdraw operation running");

            var decompositionAlgorithm = new DecompositionAlgorithm();
            var outputedMoney          = decompositionAlgorithm.Decompose(requestedSum, ref _balance, ref _money);

            try
            {
                _moneyWriter.WriteMoney(_money);
            }

            catch (Exception ex)
            {
                Log.Error("Could not update current money status:" + ex);
            }

            return(outputedMoney);
        }
示例#2
0
        public Money WithdrawMoney(decimal requestedSum)
        {
            Log.Info("Request sum for withdraw " + requestedSum);
            IDecomposable algorithm = new DecompositionAlgorithm();
            AtmState      state;
            Money         issuedMoney = new Money(algorithm.DecomposeMoney(AllMoney.Banknotes, requestedSum, out state));

            CurrentState = state;
            if (CurrentState == AtmState.Ok)
            {
                UpdateMoney(issuedMoney);
                Log.Info(string.Format("State: {0}. Issued money {1}", CurrentState,
                                       MoneyConverter.ConvertToString(issuedMoney)));
            }
            else
            {
                Log.Info(string.Format("State: {0}. Issued money 0", CurrentState));
            }
            return(issuedMoney);
        }
示例#3
0
        //public FiniteStateMachine<StructAtom<string>, StructAtom<string>> fsm = new FiniteStateMachine<StructAtom<string>, StructAtom<string>>("fsm");

        //private void InitTestFSM()
        //{
        //    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");

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

        //    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, 0.3);
        //    fsm.AddOutgoing(a2, z1, a1, w2, 0.2);
        //    fsm.AddOutgoing(a2, z1, a2, w2, 0.3);

        //    fsm.AddOutgoing(a2, z2, a1, w1, 0.2);
        //    fsm.AddOutgoing(a2, z2, a2, w2, 0.2);
        //    fsm.AddOutgoing(a2, z2, a3, w3, 0.2);
        //    fsm.AddOutgoing(a2, z2, a4, w3, 0.2);

        //    fsm.AddOutgoing(a2, z3, a1, w1);

        //    fsm.AddOutgoing(a2, z4, a5, w3, 0.7);
        //    fsm.AddOutgoing(a2, z4, a6, w2, 0.3);
        //    //
        //    fsm.AddOutgoing(a3, z1, a1, w1, 0.6);
        //    fsm.AddOutgoing(a3, z1, a2, w1, 0.2);

        //    fsm.AddOutgoing(a3, z2, a5, w3, 0.8);
        //    fsm.AddOutgoing(a3, z2, a2, w3, 0.1);

        //    fsm.AddOutgoing(a3, z3, a5, w1, 0.9);
        //    fsm.AddOutgoing(a3, z3, a6, w1, 0.1);

        //    fsm.AddOutgoing(a3, z4, a1, w1, 0.5);
        //    fsm.AddOutgoing(a3, z4, a2, w2, 0.5);
        //    //
        //    fsm.AddOutgoing(a4, z1, a6, w1);

        //    fsm.AddOutgoing(a4, z2, a2, w2, 0.8);

        //    fsm.AddOutgoing(a4, z3, a2, w2, 0.6);
        //    //fsm.AddOutgoing(a4, z3, a2, w2, 0.4);

        //    fsm.AddOutgoing(a4, z4, a6, w3);
        //    //
        //    fsm.AddOutgoing(a5, z1, a1, w3);

        //    fsm.AddOutgoing(a5, z2, a1, w1, 0.3);
        //    fsm.AddOutgoing(a5, z2, a2, w2, 0.7);

        //    fsm.AddOutgoing(a5, z3, a2, w3, 0.8);
        //    fsm.AddOutgoing(a5, z3, a3, w3, 0.2);

        //    fsm.AddOutgoing(a5, z4, a4, w1, 0.9);
        //    //
        //    fsm.AddOutgoing(a6, z1, a2, w2, 0.6);
        //    fsm.AddOutgoing(a6, z1, a1, w2, 0.1);
        //    fsm.AddOutgoing(a6, z1, a3, w3, 0.1);
        //    fsm.AddOutgoing(a6, z1, a4, w3, 0.1);
        //    fsm.AddOutgoing(a6, z1, a5, w1, 0.1);

        //    fsm.AddOutgoing(a6, z2, a6, w2);

        //    fsm.AddOutgoing(a6, z3, a5, w3, 0.7);
        //    fsm.AddOutgoing(a6, z3, a6, w3, 0.3);

        //    fsm.AddOutgoing(a6, z4, a3, w1);
        //    //
        //    fsm.InitialState = a1;
        //}

        //public FSMNet<StructAtom<string>, StructAtom<string>> GetNet()
        //{

        //    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>>();

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

        //    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 });

        //    return alg.Solve();
        //}

        //private enum StateCores
        //{
        //    a1 = 0,
        //    a2,
        //    a3,
        //    a4,
        //    a5,
        //    a6,
        //}

        //private void Init()
        //{
        //    Enum x1 = X.x1; Enum x2 = X.x2; Enum x3 = X.x3;

        //    //TODO: Добавить средства для добавления отношений типа ksi: Z -> X
        //    FiniteStateMachine<StructAtom<Enum>, StructAtom<Enum>> S1 = new FiniteStateMachine<StructAtom<Enum>, StructAtom<Enum>>("a");
        //    FSMState<StructAtom<Enum>, StructAtom<Enum>> b1 = new FSMState<StructAtom<Enum>, StructAtom<Enum>>(S1, B.b1);
        //    FSMState<StructAtom<Enum>, StructAtom<Enum>> b2 = new FSMState<StructAtom<Enum>, StructAtom<Enum>>(S1, B.b2);
        //    FSMState<StructAtom<Enum>, StructAtom<Enum>> b3 = new FSMState<StructAtom<Enum>, StructAtom<Enum>>(S1, B.b3);

        //    S1.AddOutgoing(b1, new StructAtom<Enum>(x1), b1, (StructAtom<Enum>)b1.StateCore);
        //    S1.AddOutgoing(b1, new StructAtom<Enum>(x2), b3);
        //    S1.AddOutgoing(b1, new StructAtom<Enum>(x3), b3);

        //    S1.AddOutgoing(b2, new StructAtom<Enum>(x1), b1);
        //    S1.AddOutgoing(b2, new StructAtom<Enum>(x2), b2);
        //    S1.AddOutgoing(b2, new StructAtom<Enum>(x3), b2);

        //    S1.AddOutgoing(b3, new StructAtom<Enum>(x1), b3);
        //    S1.AddOutgoing(b3, new StructAtom<Enum>(x2), b3);
        //    S1.AddOutgoing(b3, new StructAtom<Enum>(x3), b1);
        //}

        //enum B
        //{
        //    b1,
        //    b2,
        //    b3
        //}

        //enum X
        //{
        //    x1,
        //    x2,
        //    x3
        //}

        private void btnDecompose_Click(object sender, EventArgs e)
        {
            if (fsmEditControl.fsm != null)
            {
                if (fsmEditControl.fsm.CheckInitialization())
                {
                    var frm = new frmOrtPartitionSetEdit();
                    if (frm.Show(fsmEditControl.fsm) == System.Windows.Forms.DialogResult.OK)
                    {
                        FSMNet <StructAtom <string>, StructAtom <string> > net = null;
                        try
                        {
                            var alg =
                                new DecompositionAlgorithm <StructAtom <string>, StructAtom <string> >(fsmEditControl.fsm,
                                                                                                       frm.Partitions);
                            net = alg.Solve();
                        }
                        catch (Exception exc)
                        {
                            net = null;
                            MessageBox.Show("Ошибка при декомпозиции автомата: " + exc.Message, this.Text,
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                        if (net != null)
                        {
                            var frmResult = new frmDecomposeResult();
                            frmResult.Show(net);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Текущий автомат не до конца инициализирован", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("Не выбран автомат для декомпозиции", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
示例#4
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();
        }