//Generates a new randomly created tree (recursive). The first call should be with current depth 1.
 //This has a mandatory depth of D.
 public OperatorTree GenerateNewUniformRandomTreeFull(int maxdepth, int currentdepth, Queue<bool[]> memory, Random randomgenerator, string[] treepossibilities)
 {
     OperatorTree current = null;
       //If we are at the max depth
       if(maxdepth == currentdepth)
       {
     int memorylocation = randomgenerator.Next(0, memory.Count);
     int player = randomgenerator.Next(0, 2);
     char playerchar = ' ';
     if(player == 0)
       playerchar = 'P';
     else
       playerchar = 'O';
     current = new OperatorTree(new DataNode() { Player = playerchar, Index = memorylocation });
       }
       //Otherwise make an operator node
       else
       {
     int choice = randomgenerator.Next(0, treepossibilities.Length);
     current = new OperatorTree(new OperatorNode() { Operator = treepossibilities[choice] });
       }
       //If we didn't create a data node, attach moar things.
       if(current.Node.GetType() != typeof(DataNode))
       {
     OperatorTree ot = GenerateNewUniformRandomTreeFull(maxdepth, currentdepth+1, memory, randomgenerator, treepossibilities);
     current.InsertLeft(ot);
     //If we didn't make a not node, attach things to the right.
     if(current.Node.GetType() == typeof(OperatorNode) && ((OperatorNode)current.Node).Operator.ToUpper() != "NOT")
       current.InsertRight(GenerateNewUniformRandomTreeFull(maxdepth, currentdepth+1, memory, randomgenerator, treepossibilities));
       }
       return current;
 }
Beispiel #2
0
 public Dilemma(double fitness, OperatorTree tree)
 {
     this.tree = tree;
       this.fitness = fitness;
       memory = new Queue<bool[]>();
       timesplayedipd = 0;
 }
Beispiel #3
0
 public Dilemma()
 {
     fitness = 0.0;
       tree = null;
       memory = new Queue<bool[]>();
       timesplayedipd = 0;
 }
Beispiel #4
0
 public OperatorTree(OTNode node)
 {
     this.node = node;
       this.right = null;
       this.left = null;
       this.depth = 1;
       this.parent = null;
       this.size = 1;
 }
Beispiel #5
0
 public OperatorTree(OperatorTree tree)
 {
     if (tree.Node != null)
     this.node = tree.node.Clone();
       else
     this.node = null;
       if (tree.Right != null)
       {
     this.right = tree.right.Clone();
     this.right.parent = this;
       }
       else
     this.right = null;
       if (tree.Left != null)
       {
     this.left = tree.left.Clone();
     this.left.parent = this;
       }
       else
     this.left = null;
       this.parent = null;
       this.depth = tree.depth;
       this.size = tree.size;
 }
Beispiel #6
0
 public void InsertLeft(OperatorTree tree)
 {
     //Assert that this OperatorTree's node is not Data and an operator node.
       //Operator nodes are only allowed as interior nodes,
       //Data nodes are only allowed as leaf nodes.
       if(this.node == null || (this.node != null && this.node.GetType() != typeof(OperatorNode)))
     throw new Exception("Can't attach subtree to node with data or null.");
       left = tree;
 }
Beispiel #7
0
 //Internal, uses this as current.
 private OperatorTree RetrieveTree(int index, OperatorTree current)
 {
     Stack<OperatorTree> nodes = new Stack<OperatorTree>();
       nodes.Push(current);
       OperatorTree currentNode = current;
       int currindex = 0;
       while(nodes.Count != 0 && currindex <= index)
       {
     currentNode = nodes.Pop();
     OperatorTree right = currentNode.Right;
     if(right != null)
       nodes.Push(right);
     OperatorTree left = currentNode.Left;
     if(left != null)
       nodes.Push(left);
     currindex++;
       }
       if(currentNode == null)
     throw new Exception("Tree at index not found.");
       return currentNode;
 }
Beispiel #8
0
 private void ReplaceTree(int index, OperatorTree current, OperatorTree newtree)
 {
     Stack<OperatorTree> nodes = new Stack<OperatorTree>();
       nodes.Push(current);
       OperatorTree currentNode = current;
       int currindex = 0;
       while (nodes.Count != 0 && currindex <= index)
       {
     currentNode = nodes.Pop();
     OperatorTree right = currentNode.Right;
     if (right != null)
       nodes.Push(right);
     OperatorTree left = currentNode.Left;
     if (left != null)
       nodes.Push(left);
     currindex++;
       }
       if (currentNode == null)
     throw new Exception("Tree at index not found.");
       if (index == 0 || currentNode.Parent == null)
     throw new Exception("Cannot replace root!");
       if (currentNode.Parent.Right != null && currentNode.Parent.Right.Equals(currentNode))
     currentNode.Parent.InsertRight(newtree);
       else if (currentNode.Parent.Left != null && currentNode.Parent.Left.Equals(currentNode))
     currentNode.Parent.InsertLeft(newtree);
 }
Beispiel #9
0
 //Replaces a subtree at pre order index, with newtree. Will not work if index = 0;
 public void ReplaceTree(int index, OperatorTree newtree)
 {
     ReplaceTree(index, this, newtree);
 }
Beispiel #10
0
 public Dilemma()
 {
     fitness = 0.0;
       tree = new OperatorTree(null);
 }
 //Generates a new randomly created tree (recursive). The first call should be with current depth 1.
 //This only has a max depth of D, not a mandatory depth of D.
 public OperatorTree GenerateNewUniformRandomTreeGrow(int maxdepth, int currentdepth, Queue<bool[]> memory, Random randomgenerator, string[] treepossibilities)
 {
     OperatorTree current = null;
       int choice = randomgenerator.Next(0, treepossibilities.Length + memory.Count);
       //If choice is a memory index take a memory index as next tree node
       if(choice >= treepossibilities.Length)
       {
     int memorylocation = choice - treepossibilities.Length;
     int player = randomgenerator.Next(0, 2);
     char playerchar = ' ';
     if(player == 0)
       playerchar = 'P';
     else
       playerchar = 'O';
     current = new OperatorTree(new DataNode() { Player = playerchar, Index = memorylocation });
       }
       //If we are at the max depth take a memory index as next tree node
       else if(maxdepth == currentdepth)
       {
     int memorylocation = randomgenerator.Next(0, memory.Count);
     int player = randomgenerator.Next(0, 2);
     char playerchar = ' ';
     if(player == 0)
       playerchar = 'P';
     else
       playerchar = 'O';
     current = new OperatorTree(new DataNode() { Player = playerchar, Index = memorylocation });
       }
       //Otherwise make next tree node an operator node
       else
       {
     current = new OperatorTree(new OperatorNode() { Operator = treepossibilities[choice] });
       }
       //If we didn't make a data node, we need to go deeper to give operators things to operate on
       if(current.Node.GetType() != typeof(DataNode))
       {
     current.InsertLeft(GenerateNewUniformRandomTreeGrow(maxdepth, currentdepth+1, memory, randomgenerator, treepossibilities));
     //If we didn't create a not, then attach things to the right.
     if(current.Node.GetType() == typeof(OperatorNode) && ((OperatorNode)current.Node).Operator.ToUpper() != "NOT")
       current.InsertRight(GenerateNewUniformRandomTreeGrow(maxdepth, currentdepth+1, memory, randomgenerator, treepossibilities));
       }
       return current;
 }
Beispiel #12
0
        //Generates a new randomly created tree (recursive). The first call should be with current depth 0.
        OperatorTree GenerateNewUniformRandomTree(int maxdepth, int currentdepth)
        {
            OperatorTree current = null;
              int choice = RandomGenerator.Next(0, treepossibilities.Length);
              if(treepossibilities[choice] == "DATA" || maxdepth == currentdepth)
              {
            int memorylocation = RandomGenerator.Next(0, memory.Count);
            int player = RandomGenerator.Next(0, 2);
            char playerchar = ' ';
            if(player == 0)
              playerchar = 'P';
            else
              playerchar = 'O';
            current = new OperatorTree(new DataNode() { Player = playerchar, Index = memorylocation });
              }
              else
              {
            current = new OperatorTree(new OperatorNode() { Operator = treepossibilities[choice] });
              }

              if(current.Node.GetType() != typeof(DataNode))
              {
            current.InsertLeft(GenerateNewUniformRandomTree(maxdepth, currentdepth+1));
            if(current.Node.GetType() == typeof(OperatorNode) && ((OperatorNode)current.Node).Operator.ToUpper() != "NOT")
              current.InsertRight(GenerateNewUniformRandomTree(maxdepth, currentdepth+1));
              }
              return current;
        }
Beispiel #13
0
 public Dilemma(double fitness, OperatorTree tree)
 {
     this.tree = tree;
       this.fitness = fitness;
 }
Beispiel #14
0
 public void InsertRight(OperatorTree tree)
 {
     //Make same assertion as above
       if(this.node == null || (this.node != null && this.node.GetType() != typeof(OperatorNode)))
     throw new Exception("Can't attach subtree to node with data or null.");
       if(this.node.GetType() == typeof(OperatorNode) && ((OperatorNode)this.node).Operator.ToUpper() == "NOT")
     throw new Exception("Can't attach things to the right child of a NOT node.");
       right = tree;
 }
Beispiel #15
0
 //Clones an operator tree (shallow copy)
 public OperatorTree Clone()
 {
     OperatorTree ot = new OperatorTree(this.node.Clone());
       if (this.right != null)
       {
       ot.right = this.right.Clone();
       ot.right.parent = this;
       }
       if (this.left != null)
       {
       ot.left = this.left.Clone();
       ot.left.parent = this;
       }
       ot.depth = this.depth;
       ot.size = this.size;
       return ot;
 }
Beispiel #16
0
 public OperatorTree(OTNode node)
 {
     this.node = node;
       this.right = null;
       this.left = null;
 }
Beispiel #17
0
 public Dilemma()
 {
     fitness = 0.0;
       tree = null;
       memory = new Queue<bool[]>();
 }