Ejemplo n.º 1
0
        public void AddTransition(NodeChain <T> from, Node <T> to, int weight = 1)
        {
            // TODO: Verify all nodes are present in model's sets

            var toNode = to.Value != null?AddNode(to.Value) : to;

            var fromChain = new NodeChain <T>();

            foreach (var node in from)
            {
                if (node.Value == null)
                {
                    fromChain.Add(node);
                }
                else
                {
                    fromChain.Add(AddNode(node.Value));
                }
            }

            if (Transitions.ContainsKey(from))
            {
                if (Transitions[from].ContainsKey(to))
                {
                    Transitions[from][to].Weight += weight;
                }
                else
                {
                    var transition = new Transition <T>()
                    {
                        Chain    = from,
                        EndState = to,
                        Weight   = weight
                    };

                    Transitions[from].Add(to, transition);
                }
            }
            else
            {
                var transitions = new Dictionary <Node <T>, Transition <T> >
                {
                    {
                        to,
                        new Transition <T>()
                        {
                            Chain    = from,
                            EndState = to,
                            Weight   = weight
                        }
                    }
                };

                Transitions.Add(from, transitions);
            }
        }
Ejemplo n.º 2
0
        public static NodeChain <T> ToChain <T>(this Node <T> node)
        {
            var chain = new NodeChain <T>();

            chain.Add(node);
            return(chain);
        }
Ejemplo n.º 3
0
        public static NodeChain <T> ToChain <T>(this IEnumerable <Node <T> > list)
        {
            var chain = new NodeChain <T>();

            foreach (var item in list)
            {
                chain.Add(item);
            }

            return(chain);
        }
Ejemplo n.º 4
0
        public List <T> GenerateRandomChain(Random random)
        {
            var randomWalk = new NodeChain <T>();

            Node <T> currentState = GetNext(randomWalk, random);

            while (currentState.Type != NodeType.End)
            {
                randomWalk.Add(currentState);
                currentState = GetNext(randomWalk, random);
            }

            return(randomWalk.Select(s => s.Value).ToList());
        }