Exemple #1
0
        // Return a copy of a Tree must provide initial root node
        public Tree copyTree(Node _root)
        {
            Tree copy = new Tree(board, individual);

            copy.root = root.copy(_root);
            copy.setRoot(copy.getRoot());
            return(copy);
        }
Exemple #2
0
        public Node copy(Node rootSource)
        {
            Node root;

            if (rootSource is Terminal)
            {
                root = new Terminal((Terminal)rootSource);
            }
            else
            { //if(rootSource instanceof Function)
                root          = new Function((Function)rootSource);
                root.children = new Node[root.getNumChildren()];
            }
            for (int i = 0; i < root.getNumChildren(); i++)
            {
                root.children[i] = rootSource.copy(rootSource.children[i]);
            }
            return(root);
        }
        public Individual crossover2(Individual otherIndividual)
        {
            /*
             * crossover function
             * clone both parent (the clones will be the new children after the crossover operation)
             * randomly choose a node from each cloned parent
             * swap the nodes
             */
            int       randNum;
            const int MIN = 2;
            // Node objects to temporary hold the returned references
            Node swap1 = null;
            Node swap2 = null;

            // randomize a number ranging from 2....nodesAmount of first parent
            randNum = rnd.Next(this.getStrategyRoot().countNodes() - MIN + 1) + MIN;
            // paint the selected nodes at the original first parent
            this.getStrategyRoot().getNode(this.getStrategyRoot(), randNum).paintNode(Color.Red);
            // get the first random node reference
            swap1 = this.getStrategyRoot().getNode(this.getStrategyRoot(), randNum);
            // randomize a number ranging from 2....nodesAmount of second parent
            randNum = rnd.Next(otherIndividual.getStrategyRoot().countNodes() - MIN + 1) + MIN;
            // paint the selected nodes at the original second parent
            otherIndividual.getStrategyRoot().getNode(otherIndividual.getStrategyRoot(), randNum).paintNode(Color.Red);
            // get the second random node reference
            swap2 = otherIndividual.getStrategyRoot().getNode(otherIndividual.getStrategyRoot(), randNum);

            // paint the swapped nodes
            swap1.paintNode(Color.Green);
            swap2.paintNode(Color.Green);

            // make the reference swaps
            this.getStrategyRoot().swapNodes(swap1, swap2.copy(swap2));

            // combine the parents name to create a new name
            this.setPlayerName(otherIndividual.getPlayerName());
            return(this);
        }
Exemple #4
0
        public int Move()
        {
            int  max      = -10;
            Node bestNode = new Node();

            for (int x = 0; x <= 8; x++)
            {
                Node n = new Node();
                n.copy(rootNode);
                if (n.board.SetMove(x) == true)
                {
                    rootNode.AddChildren(n);
                    n.moveBox = x;
                    int val = minimaxAB(n, true, 100, -10, 10);
                    if (val >= max)
                    {
                        max      = val;
                        bestNode = n;
                    }
                }
            }
            return(bestNode.moveBox);
        }
Exemple #5
0
        private int minimaxAB(Node node, bool min, int depth, int alpha, int beta)
        {
            if (boardPoint(node) != -2)
            { //|| depth == 0) {
              //            if (depth <= 0) {
              //                Console.Write("Depth Reached");
              //            }

                node.point = boardPoint(node);
                //node.Print();
                //Console.Writeln(nodeCount++ + " node.point: " + node.point + " Alpha: " + alpha + " Beta: " + beta);

                return(boardPoint(node));
            }
            else
            {
                if (min == true)
                {
                    for (int x = 0; x <= 8; x++)
                    {
                        Node n = new Node();
                        n.copy(node);
                        if (n.board.SetMove(x) == true)
                        {
                            node.AddChildren(n);
                            n.moveBox = x;
                            //Console.Writeln("In min:"+ min);
                            int val = minimaxAB(n, false, depth - 1, alpha, beta);

                            if (val < beta)
                            {
                                beta           = val;
                                n.parent.point = val;
                            }
                        }
                    }
                    //Console.Writeln("Out min:"+ min);
                    return(beta);
                }

                if (min == false)
                {
                    for (int x = 0; x <= 8; x++)
                    {
                        Node n = new Node();
                        n.copy(node);
                        if (n.board.SetMove(x) == true)
                        {
                            node.AddChildren(n);
                            n.moveBox = x;
                            //Console.Writeln("In min:"+ min);
                            int val = minimaxAB(n, true, depth - 1, alpha, beta);

                            if (val > alpha)
                            {
                                alpha          = val;
                                n.parent.point = val;
                            }
                        }
                    }
                    //Console.Writeln("Out min:"+ min);
                    return(alpha);
                }
            }
            return(-100);
        }