Ejemplo n.º 1
0
        public int[] AddEvent(OnlineEvent ev)
        {
            int[] result = null;
            if (Position == 0)
            {
                Clear();
                _probs   = GetInitialProbabilities(HmmData, ev.Value);
                Position = 1;
            }
            else
            {
                var      observation   = ev.Value;
                double[] newProbs      = new double[HmmData.HiddenStatesCount];
                int[]    optimalStates = new int[HmmData.HiddenStatesCount];
                var      newLeaves     = new HmmOnlineNode[HmmData.HiddenStatesCount];
                for (int k = 0; k < HmmData.HiddenStatesCount; ++k)
                {
                    int    maxState = -1;
                    double maxProb  = Double.MinValue;
                    for (int t = 0; t < HmmData.HiddenStatesCount; ++t)
                    {
                        double currentProb = GetTransitionProbability(HmmData, k, t) + _probs[t];
                        if (maxProb < currentProb)
                        {
                            maxProb  = currentProb;
                            maxState = t;
                        }
                    }
                    optimalStates[k] = maxState;
                    newProbs[k]      = maxProb + GetEmissionProbability(HmmData, ev.Value, k);

                    var node = new HmmOnlineNode();
                    node.Position = Position;
                    node.State    = k;
                    node.Parent   = _leaves[maxState];
                    newLeaves[k]  = node;
                }
                _backpointers.Add(optimalStates);

                Tree.Compress();
                var newRoot = Tree.Root;
                if (Root != newRoot && newRoot != null)
                {
                    LastLikelihood = newProbs[newRoot.State];
                    result         = Traceback(Position - newRoot.Position - 1, newRoot.State, false);
                    _leaves        = newLeaves;
                    Root           = newRoot;
                }

                foreach (var leave in newLeaves)
                {
                    Tree.AddLast(leave);
                }
                _probs = newProbs;
                ++Position;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void ReadFields(BinaryReader input)
        {
            var probsLength = input.ReadInt32();

            _probs = new double[probsLength];
            for (int i = 0; i < probsLength; i++)
            {
                _probs[i] = input.ReadDouble();
            }

            _backpointers = new List <int[]>();
            int backpointersSize = input.ReadInt32();
            int stateNumber      = input.ReadInt32();

            for (int i = 0; i < backpointersSize; ++i)
            {
                int[] optimalStates = new int[stateNumber];
                for (int j = 0; j < optimalStates.Length; ++j)
                {
                    optimalStates[j] = input.ReadInt32();
                }
                _backpointers.Add(optimalStates);
            }

            Position       = input.ReadInt32();
            LastLikelihood = input.ReadDouble();

            int mapSize = input.ReadInt32();
            var nodeMap = new BiDictionary <HmmOnlineNode, int>(); // HashBiMap.create(mapSize);

            for (int i = 0; i < mapSize; ++i)
            {
                HmmOnlineNode.Read(nodeMap, input);
            }

            Tree = new HmmOnlineTree();
            int treeSize = input.ReadInt32();

            for (int i = 0; i < treeSize; ++i)
            {
                Tree.AddLast(HmmOnlineNode.Read(nodeMap, input));
            }

            Root = HmmOnlineNode.Read(nodeMap, input);

            _leaves = new HmmOnlineNode[stateNumber];
            for (int i = 0; i < _leaves.Length; ++i)
            {
                _leaves[i] = HmmOnlineNode.Read(nodeMap, input);
            }
        }
Ejemplo n.º 3
0
 public void AddLast(HmmOnlineNode node)
 {
     if (First == null)
     {
         First = node;
     }
     if (Last != null)
     {
         Last.Next = node;
     }
     node.Next     = null;
     node.Previous = Last;
     Last          = node;
     ++Size;
 }
Ejemplo n.º 4
0
        static int MapNode(BiDictionary <HmmOnlineNode, int> map, HmmOnlineNode node)
        {
            if (node == null)
            {
                return(-1);
            }
            int value;

            if (!map.TryGetValue(node, out value))
            {
                value = map.Count;
                map.Add(node, value);
                return(value);
            }
            return(value);
        }
Ejemplo n.º 5
0
        private void Clear()
        {
            _probs  = null;
            Tree    = new HmmOnlineTree();
            _leaves = new HmmOnlineNode[HmmData.HiddenStatesCount];
            Root    = null;
            for (int i = 0; i < HmmData.HiddenStatesCount; ++i)
            {
                var node = new HmmOnlineNode();
                node.Position = 0;
                node.State    = i;
                Tree.AddLast(node);
                _leaves[i] = node;
            }

            _backpointers = new List <int[]>();
            Position      = 0;

            LastLikelihood = Double.MinValue;
        }
Ejemplo n.º 6
0
        public static HmmOnlineNode Read(BiDictionary <HmmOnlineNode, int> map, BinaryReader input)
        {
            bool first = input.ReadBoolean();
            int  index = input.ReadInt32();

            if (first)
            {
                HmmOnlineNode node = new HmmOnlineNode();
                node.Position    = input.ReadInt32();
                node.State       = input.ReadInt32();
                node.ChildNumber = input.ReadInt32();
                map.Add(node, index);
                node.Parent = Read(map, input);
                return(node);
            }
            if (index == -1)
            {
                return(null);
            }

            return(map.Reverse[index]);
        }
Ejemplo n.º 7
0
        public void Remove(HmmOnlineNode node)
        {
            if (node.Previous != null)
            {
                node.Previous.Next = node.Next;
            }
            if (node.Next != null)
            {
                node.Next.Previous = node.Previous;
            }

            if (First == node)
            {
                First = First.Next;
            }
            if (Last == node)
            {
                Last = Last.Previous;
            }

            --Size;
        }
Ejemplo n.º 8
0
        public static int Write(BiDictionary <HmmOnlineNode, int> map, HashSet <HmmOnlineNode> written, HmmOnlineNode node, BinaryWriter output)
        {
            if (node == null)
            {
                output.Write(false);
                output.Write(-1);
                return(-1);
            }
            int index = map[node];

            if (!written.Contains(node))
            {
                output.Write(true);
                output.Write(index);
                output.Write(node.Position);
                output.Write(node.State);
                output.Write(node.ChildNumber);
                written.Add(node);
                Write(map, written, node.Parent, output);
            }
            else
            {
                output.Write(false);
                output.Write(index);
            }

            return(index);
        }
Ejemplo n.º 9
0
        public void Write(BinaryWriter output)
        {
            output.Write(_probs.Length);
            foreach (var p in _probs)
            {
                output.Write(p);
            }

            output.Write(_backpointers.Count);
            output.Write(_leaves.Length);
            foreach (var optimalStates in _backpointers)
            {
                foreach (var state in optimalStates)
                {
                    output.Write(state);
                }
            }

            output.Write(Position);
            output.Write(LastLikelihood);

            //serializing compressed backpointers tree
            //3 * |H| - 2 is the upper bound of node number
            var nodeMap = new BiDictionary <HmmOnlineNode, int>(); // 3 * leaves.length - 2;

            var node = Tree.First;

            while (node != null)
            {
                MapNode(nodeMap, node);
                node = node.Next;
            }

            MapNode(nodeMap, Root);
            foreach (var leave in _leaves)
            {
                MapNode(nodeMap, leave);
            }

            var alreadyWritten = new HashSet <HmmOnlineNode>();

            output.Write(nodeMap.Count);
            foreach (var entry in nodeMap.Keys)
            {
                HmmOnlineNode.Write(nodeMap, alreadyWritten, entry, output);
            }

            output.Write(Tree.Size);
            node = Tree.First;
            while (node != null)
            {
                HmmOnlineNode.Write(nodeMap, alreadyWritten, node, output);
                node = node.Next;
            }

            HmmOnlineNode.Write(nodeMap, alreadyWritten, Root, output);

            foreach (var leave in _leaves)
            {
                HmmOnlineNode.Write(nodeMap, alreadyWritten, leave, output);
            }
        }