Beispiel #1
0
        /********
         * Add a new child based on removing the pieces from (x1, y1) and (x2, y2)
         */
        public void AddChild(int x1, int y1, int x2, int y2, List <BoxOnBoard> children)
        {
            BoxOnBoard board = Clone(x1, y1, x2, y2, length);

            board.values[x1, y1] = 0;
            board.values[x2, y2] = 0;
            children.Add(board);
        }
Beispiel #2
0
        /*********
         * Boards are equal when they have the same colors in the same locations.
         */
        public override bool Equals(object obj)
        {
            BoxOnBoard other = (BoxOnBoard)obj;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (values[i, j] != other.values[i, j])
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Beispiel #3
0
        public static void BoxOnMain()
        {
            int TRIALS = 1;
            int EXP    = 10;

            // Choose here the setup algorithm you wish to use
            BoxOnSetup setup = BoxOnSetup.TILES;

            // Choose here the search algorithm for the solver
            Search search = Search.BFS;

            Random random = new Random();

            // Easy output for copying into a spreadsheet
            Console.WriteLine("solved\tdead\tavelen\tsconn\tfconn");

            // Run the specified number of experiments within the number
            // of specified trials
            for (int t = 0; t < TRIALS; t++)
            {
                int    count  = 0;
                int    lensum = 0;
                int    dead   = 0;
                double sconn  = 0;
                double fconn  = 0;
                int    max    = 0;

                Parallel.For(0, EXP, i =>
                             //for (int i = 0; i < EXP; i++)
                {
                    // Total count of boards, for HashSet later
                    int bcount = 0;

                    // Store all boards seen in found
                    HashSet <BoxOnBoard> found = new HashSet <BoxOnBoard>();

                    // Keep track of board states to explore in frontier
                    // Sort them by heuristic plus current path length for A*
                    Queue <BoxOnBoard> frontier = new Queue <BoxOnBoard>();

                    // Create a new board and place it in the frontier
                    BoxOnBoard start = new BoxOnBoard(random, 6, 6, 6, setup);
                    //Console.WriteLine(start);
                    //Console.WriteLine("Starting:");
                    //Console.WriteLine(start + "\n");
                    frontier.Enqueue(start);

                    // Keep searching the frontier until it is empty or
                    // a solution is found
                    bool solved     = false;
                    int maxLenLocal = 0;
                    while (frontier.Count > 0)
                    {
                        // Take the next promising board state
                        BoxOnBoard board = frontier.Dequeue();
                        found.Add(board);

                        // Find the children of the current board
                        List <BoxOnBoard> children = board.GetChildren();
                        //Console.WriteLine("numChildren:" + children.Count);
                        List <BoxOnBoard> stuff = new List <BoxOnBoard>();
                        if (search == Search.BFS)
                        {
                            stuff = children;
                        }
                        else  // Pick a child randomly
                        {
                            if (children.Count > 0)
                            {
                                int which = random.Next(0, children.Count);
                                //Console.WriteLine("adding child" + which);

                                stuff.Add(children[which]);
                            }
                        }
                        //Console.WriteLine(board.Path());
                        if (board.length > maxLenLocal)
                        {
                            Console.WriteLine(board.length + "," + frontier.Count);
                            maxLenLocal = board.length;
                        }
                        foreach (BoxOnBoard b in stuff)
                        {
                            // Did you find a solution?
                            if (b.Solved())
                            {
                                // Yay! Record statistics
                                solved = true;
                                Console.WriteLine("SOLUTION!!!!");
                                Console.WriteLine(b.Path());

                                frontier.Clear();
                                lock (random)
                                {
                                    lensum += b.length;
                                    count++;

                                    if (b.length > max)
                                    {
                                        //Console.WriteLine("SOLUTION!!!!");
                                        //Console.WriteLine(b.Path());
                                        max = b.length;
                                    }
                                }
                                break;
                            }
                            else
                            {
                            }

                            // If you have never seen this board before
                            // Add it to the frontier
                            if (!found.Contains(b) && !frontier.Contains(b))
                            {
                                bcount++;
                                frontier.Enqueue(b);
                            }
                            //else if (found.Contains(b))
                            //{
                            //    Console.WriteLine("found before!");
                            //}
                            //else if (frontier.Contains(b))
                            //{
                            //    Console.WriteLine("in frontier!");
                            //}
                        }
                    }

                    // Record when no children of initial state could be found
                    if (!solved)
                    {
                        if (found.Count == 1)
                        {
                            lock (random)
                            {
                                dead++;
                            }
                        }
                    }
                });

                Console.WriteLine(((float)count / EXP) +
                                  "\t" + ((float)dead / EXP) +
                                  "\t" + ((float)lensum / count) +
                                  "\t" + sconn / count +
                                  "\t" + fconn / (EXP - count));
            }
        }