public void update_context()
        {
            Debug.Assert(this.History.Count >= this.Depth, "history is shorter than depth in update_context");
            this.Context = new List <CTWContextTreeNode>();
            this.Context.Add(this.Root);
            CTWContextTreeNode node     = this.Root;
            int updateDepth             = 1;
            IEnumerable <int> historyIe = History;

            foreach (int symbol in historyIe.Reverse())
            {
                if (node.Children.ContainsKey(symbol))
                {
                    node = node.Children[symbol];
                }
                else
                {
                    CTWContextTreeNode newNode = new CTWContextTreeNode(this);
                    node.Children[symbol] = newNode;
                    this.TreeSize        += 1;
                    node = newNode;
                }
                this.Context.Add(node);
                updateDepth += 1;
                if (updateDepth > this.Depth)
                {
                    break;
                }
            }
        }
 public void Clear()
 {
     this.History = new List<int>();
     this.Root.Tree = null; //TODO: is this working - will it free memory? Test!
     //missing?: del self.root
     this.Root = new CTWContextTreeNode(this);
     this.TreeSize = 1;
     this.Context = new List<CTWContextTreeNode>();
 }
 public void Clear()
 {
     this.History   = new List <int>();
     this.Root.Tree = null; //TODO: is this working - will it free memory? Test!
     //missing?: del self.root
     this.Root     = new CTWContextTreeNode(this);
     this.TreeSize = 1;
     this.Context  = new List <CTWContextTreeNode>();
 }
 public CTWContextTree(int depth)
 {
     Context = new List<CTWContextTreeNode>();
     Debug.Assert(depth >= 0);
     this.Depth = depth;
     this.History = new List<int> ();
     this.Root = new CTWContextTreeNode(this);   //refact: most of this is in clear()
     this.TreeSize = 1;
     this.Clear();
 }
 public CTWContextTree(int depth)
 {
     Context = new List <CTWContextTreeNode>();
     Debug.Assert(depth >= 0);
     this.Depth    = depth;
     this.History  = new List <int> ();
     this.Root     = new CTWContextTreeNode(this); //refact: most of this is in clear()
     this.TreeSize = 1;
     this.Clear();
 }
        public bool SameNode(int meI, CTWContextTreeNode he)
        {
            var me = this.Nodes[meI];

            return(Utils.FloatCompare(me.LogProbability, he.LogProbability) &&
                   Utils.FloatCompare(me.LogKt, he.LogKt) &&
                   Utils.FloatCompare(me.NumberOf0S, he.NumberOf0S) &&
                   Utils.FloatCompare(me.NumberOf1S, he.NumberOf1S) &&
                   (me.Child1 != -1) == he.Children.ContainsKey(1) &&
                   (me.Child0 != -1) == he.Children.ContainsKey(0));
        }
        public void TestCtwContextTreeNode()
        {
            var tree = new CTWContextTree(5);
            var n = new CTWContextTreeNode(tree);

            Assert.AreEqual(0.0, n.LogKt, 0.001);
            Assert.AreEqual(0.0, n.LogProbability, 0.001);
            Assert.AreEqual(0, n.SymbolCount(0));
            Assert.AreEqual(0, n.SymbolCount(1));
            Assert.AreEqual(0, n.NumberOf0S);
            Assert.AreEqual(0, n.NumberOf1S);
            Assert.AreEqual(tree, n.Tree);
            Assert.AreEqual(0, n.Visits());
            Assert.AreEqual(true, n.IsLeaf());
            Assert.AreEqual(1, n.Size());

            n.Update(1);
            n.Update(0);
            n.Update(0);
            n.Update(0);
            n.Update(1);

            Assert.AreEqual(-4.4465, n.LogKt, 0.001);
            Assert.AreEqual(-4.44656, n.LogProbability, 0.001);
            Assert.AreEqual(3, n.SymbolCount(0));
            Assert.AreEqual(2, n.SymbolCount(1));
            Assert.AreEqual(3, n.NumberOf0S);
            Assert.AreEqual(2, n.NumberOf1S);
            Assert.AreEqual(tree, n.Tree);
            Assert.AreEqual(5, n.Visits());
            Assert.AreEqual(true, n.IsLeaf());
            Assert.AreEqual(1, n.Size());

            n.Revert(1);

            Assert.AreEqual(-3.2425, n.LogKt, 0.001);
            Assert.AreEqual(-3.24259, n.LogProbability, 0.001);
            Assert.AreEqual(3, n.SymbolCount(0));
            Assert.AreEqual(1, n.SymbolCount(1));
            Assert.AreEqual(3, n.NumberOf0S);
            Assert.AreEqual(1, n.NumberOf1S);
            Assert.AreEqual(tree, n.Tree);
            Assert.AreEqual(4, n.Visits());
            Assert.AreEqual(true, n.IsLeaf());
            Assert.AreEqual(1, n.Size());

            //Todo:test non-leaf
        }
 public void update_tree(int[] symbolList)
 {
     foreach (int symbol in symbolList)
     {
         if (this.History.Count >= this.Depth)
         {
             this.update_context();
             for (int i = this.Depth - 1; i >= 0; i--)
             {
                 CTWContextTreeNode contextNode = this.Context[i];
                 contextNode.Update(symbol);
             }
         }
         this.update_tree_history(symbol);
     }
 }
        public bool compare(int meI, CTWContextTreeNode he)
        {
            var me = this.Nodes[meI];

            if (this.SameNode(meI, he))
            {
                bool result = true;

                if (me.Child1 != -1)
                {
                    result = result && this.compare(me.Child1, he.Children[1]);
                }
                if (me.Child0 != -1)
                {
                    result = result && this.compare(me.Child0, he.Children[0]);
                }
                return(result);
            }
            return(false);
        }
        public void revert_tree(int symbolCount = 1)
        {
            for (int i = 0; i < symbolCount; i++)
            {
                if (this.History.Count == 0)
                {
                    return;
                }
                int symbol = this.History.Last();
                this.History.RemoveAt(this.History.Count - 1);

                if (this.History.Count >= this.Depth)
                {
                    this.update_context();
                    for (int j = this.Depth - 1; j >= 0; j--)
                    {
                        CTWContextTreeNode node = this.Context[j];
                        node.Revert(symbol);
                    }
                }
            }
        }
        public bool SameNode(int meI, CTWContextTreeNode he)
        {
            var me = this.Nodes[meI];

            return Utils.FloatCompare(me.LogProbability, he.LogProbability) &&
                Utils.FloatCompare(me.LogKt, he.LogKt) &&
                Utils.FloatCompare(me.NumberOf0S, he.NumberOf0S) &&
                Utils.FloatCompare(me.NumberOf1S, he.NumberOf1S) &&
                (me.Child1 != -1) == he.Children.ContainsKey(1) &&
                (me.Child0 != -1) == he.Children.ContainsKey(0);
        }
        public bool compare(int meI, CTWContextTreeNode he)
        {
            var me = this.Nodes[meI];

            if (this.SameNode(meI, he))
            {
                bool result = true;

                if (me.Child1 != -1)
                {
                    result = result && this.compare(me.Child1, he.Children[1]);
                }
                if (me.Child0 != -1)
                {
                    result = result && this.compare(me.Child0, he.Children[0]);
                }
                return result;
            }
            return false;
        }
Exemple #13
0
 public void update_context()
 {
     Debug.Assert(this.History.Count >= this.Depth, "history is shorter than depth in update_context");
     this.Context = new List<CTWContextTreeNode>();
     this.Context.Add(this.Root);
     CTWContextTreeNode node = this.Root;
     int updateDepth = 1;
     IEnumerable<int> historyIe = History;
     foreach (int symbol in historyIe.Reverse())
     {
         if (node.Children.ContainsKey(symbol))
         {
             node = node.Children[symbol];
         }
         else
         {
             CTWContextTreeNode newNode = new CTWContextTreeNode(this);
             node.Children[symbol] = newNode;
             this.TreeSize += 1;
             node = newNode;
         }
         this.Context.Add(node);
         updateDepth += 1;
         if (updateDepth > this.Depth)
         {
             break;
         }
     }
 }