A* Admissible Heuristic for die rolling on grid

前端 未结 5 1453
盖世英雄少女心
盖世英雄少女心 2020-12-23 14:21

I need some help finding a good heuristic for the following problem:

You are given an R-by-C

5条回答
  •  眼角桃花
    2020-12-23 14:37

    Main Edit 3: Proof that the optimal admissible heuristic should be based on 3.5m

    The average cost of travelling along the board has to approach 3.5m over the long run where m is the Manhattan distance. Therefore the best admissible heuristic should be 3.5m plus or minus some small constant.

    The reason for this is that whenever you move in a direction, x, say, from face x1, the next move in the same direction, to face x2 has to satisfy x1 + x2 = 7. This is because any moves in the perpendicular direction leave the orientation of face x2 the same. Think about rotating a die left to right -- the front and back faces stay the same no matter how many rotations you do. Conversely if you rotate a die front to back, the left and right faces stay the same.

    It's easiest to see this with some examples (all starting in the configuration pictured in the question)

       6
    2453
    1
    

    here you can see that we start with y1=1, and however many times we move in the x-direction afterwards, the next move in the y-direction has to be y2=6, so y1+y2=7. (Also in the x-direction, there is a simple pairing of 2+5 = 7 and 4+3 = 7).

    Another example is

      35
     26
    14
    

    In this example we start with x1=1, and however many times we move in the y-direction afterwards, the next move in the x-direction has to be x2=6. (Also, we see pairings of 4+3=7 in the y-direction, 2+5=7 in the x-direction. And we know in this case the next move in the x-direction has to be 4, and the next move in the y-direction has to be 1.)

    This all assumes it's never worth backtracking, but hopefully this can be taken as read.

    The original post below just fills in some details of how the estimate of 3.5m should be adjusted to take account of the ability for it to be beaten over the short term.

    As a side-note, as I just commented on the OP, A* search might not be required at all. It ought to make sense to simply choose a path made out of 4-long horizontal pieces and 4-long vertical pieces, say, which are optimal. And then make up the remainder with a search or a lookup table based on orientation and x-y offset. (But the question asks for an admissible heuristic so I'm going to leave my answer.)

    Main Edit 2: summarize original empirical work, taking account of comments below

    In the long term, as explained above, your average cost per move is 3.5. This can also can be seen empirically in the exploration of the data below.

    This gives a naive estimate of 3.5m where m is the Manhattan distance. However this is an over-estimate, because in the short term it is possible to do better than the average. A good hypothesis for this is to explore how we can avoid using any faces larger than 3.

    • If we start with face 1, we can use faces 2 and 3 on our first 2 moves, going 2 moves better than 3.5m predicts.
    • If we start with face 2, we can use faces 1 and 3 on our first 2 moves, going 3 moves better than 3.5m predicts.
    • If we start with face 3, we can use faces 1 and 2 on our first 2 moves, going 4 moves better than 3.5m predicts.
    • If we start with face 4,5, or 6, we can use faces 1, 2 and 3 on our first 3 moves, going 4.5 moves better than 3.5m predicts.

    This hypothesis can be confirmed empirically by simply running the script below for every starting possibility of the die, as suggested by BlueRaja - Danny Pflughoeft. So a simple admissible statistic is 3.5m - k, where k = max(f+1, 4.5), and f is the starting face. But this is a bit klunky, giving negative numbers for small values of m. It's easy to write a programmatic version that takes account of whether you have just 1 or 2 or 3 moves to go, see below

        static double Adm(int x, int y, int face /* start face */, out int m)
        {
            double adm = 0;
            m = Math.Abs(x) + Math.Abs(y);
            if (m >= 1) {
                if (face == 1)
                    adm += 2;
                else
                    adm += 1;
                m--;
            }
            if (m >= 1) {
                if (face <= 2)
                    adm += 3;
                else
                    adm += 2;
                m--;
            }
            if (m >= 1 && face >=4) {
                // 4,5,6: we can still use a 3 without backtracking
                adm += 3;
                m--;
            }
    
            adm += 3.5 * m;
    
            return adm;
        }
    

    Running this across a search space with |x|,|y| <= 100, this function underestimates the actual cost by between 0 and 6, with a median of 0.5 or 1.5 depending on the start face.

    Main Edit 1: original post

    My basic thought was that it would be good to explore the data. So I had a go at Dijkstra's algorithm to see what the space of solutions looks like. What I found is supportive of what's been said already. Some factor times the Manhattan distance is appropriate, but there may be some justification for a higher factor than 1.5. This is nicely indicated by the shape of a contour plot of cost against deviation from initial x y position.

    cost against deviation from initial x y position

    Here's a wire frame plot -- to be honest this is just for eye candy.

    enter image description here

    What's interesting is that if you add another column to your data for the manhattan distance (man) and regress the cost (v) against the manhattan distance in R, you get the following

    Coefficients:
              Estimate Std. Error  t value Pr(>|t|)    
    (Intercept) -0.6408087  0.0113650   -56.38   <2e-16
    df$man       3.4991861  0.0001047 33421.66   <2e-16
    

    I.e. it's telling you that for every move you make horizontally or vertically, your cost is 3.4991861, or v close to 3.5. That just happens to be the average of 1 to 6, so my intuition is that the data is telling us that on average, it's most efficient to use all the faces of the die equally over a long distance. Over short distances you can be more optimal.

    I tried 3.5man - k as an estimate, with k = 2.5. This seemed to work ok. When I subtracted the actual cost from this I got -0.5 as the highest value.

    > summary(df$est - df$v)
      Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
     -6.500  -2.500  -2.000  -1.777  -1.000  -0.500 
    

    However A* search has to work for all configurations including those after the start where the die is not in the original configuration, so the constant k can't be as low as 2.5 in general. It either needs to be raised, e.g. to 4, or be dependent on the configuration of the die, as suggested in another answer.

    It's quite possible that I've made some horrible mistake in all of this so I've put the code below. Like I said, I think the approach of generating the data and investigating it is sound even if my results aren't.

    Here are some lines of the result file first.

    17,-100,410

    17,-99,406

    17,-98,403

    17,-97,399

    17,-96,396

    C# code

    class Die
    {
        int top;
        int bottom;
        int front;
        int back;
        int left;
        int right;
    
        public int Top { get { return top; } }
        public int Bottom { get { return bottom; } }
        public int Front { get { return front; } }
        public int Back { get { return back; } }
        public int Left { get { return left; } }
        public int Right { get { return right; } }
    
        public Die(int top, int bot, int fro, int bac, int lef, int rig)
        {
            this.top = top;
            bottom = bot;
            front = fro;
            back = bac;
            left = lef;
            right = rig;
        }
    
        public Die RotateLeft()
        {
            return new Die(
                   top: right,
                   rig: bottom,
                   bot: left,
                   lef: top,
                   fro: front,
                   bac: back
                );
        }
    
        public Die RotateRight()
        {
            return new Die(
                   rig: top,
                   top: left,
                   lef: bottom,
                   bot: right,
                   fro: front,
                   bac: back
                );
        }
    
        public Die RotateUp()
        {
            return new Die(
                    top: front,
                    fro: bottom,
                    bot: back,
                    bac: top,
                    lef: left,
                    rig: right
                 );
        }
    
        public Die RotateDown()
        {
            return new Die(
                    fro: top,
                    top: back,
                    bac: bottom,
                    bot: front,
                    lef: left,
                    rig: right
                );
        }
    }
    
    
    
    class DieXY
    
    {
    
        public Die Die { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
    
        public DieXY(Die die, int x, int y) { Die = die; X = x; Y = y; }
    
        public override int GetHashCode() { return Die.Top + Die.Bottom*6 + Die.Front*6^2 + Die.Back*6^3 + Die.Left*6^4 + Die.Right*6^5 + X*6^6 + Y*6^8; }
        public override bool Equals(object obj)
        {
            DieXY die = (DieXY)obj;
            return die != null
                && die.Die.Top == Die.Top && die.Die.Bottom == Die.Bottom
                && die.Die.Front == Die.Front && die.Die.Back == Die.Back
                && die.Die.Left == Die.Left && die.Die.Right == Die.Right 
                && die.X == X && die.Y == Y;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary dict = new Dictionary();
            int n = 100;
            int sofar = -1;
            DieXY root = new DieXY(new Die(1, 6, 2, 5, 4, 3), 0, 0);
            Queue> queue = new Queue>();
            queue.Enqueue(new Tuple(root,0));
            while (queue.Count > 0)
            {
                Tuple curr = queue.Dequeue();
                DieXY dieXY = curr.Item1;
                Die die = dieXY.Die;
                int x = dieXY.X;
                int y = dieXY.Y;
                if (Math.Max(x,y) > sofar)
                {
                    sofar = Math.Max(x, y);
                    Console.WriteLine("{0}", sofar);
                }
                int score = curr.Item2;
                if (Math.Abs(x) <= n && Math.Abs(y) <= n)
                {
                    int existingScore = 0;
                    if (!dict.TryGetValue(dieXY, out existingScore)
                        || score < existingScore)
                    {
                        dict[dieXY] = score;
                        Die newDie = null;
                        newDie = die.RotateLeft();
                        queue.Enqueue(new Tuple(new DieXY(newDie, x - 1, y), score + newDie.Top));
                        newDie = die.RotateRight();
                        queue.Enqueue(new Tuple(new DieXY(newDie, x + 1, y), score + newDie.Top));
                        newDie = die.RotateUp();
                        queue.Enqueue(new Tuple(new DieXY(newDie, x, y + 1), score + newDie.Top));
                        newDie = die.RotateDown();
                        queue.Enqueue(new Tuple(new DieXY(newDie, x, y - 1), score + newDie.Top));
                    }
                }
            }
    
            int[,] scores = new int[2*n+1,2*n+1];
            for (int aX = 0; aX < 2 * n + 1; aX++)
                for (int aY = 0; aY < 2 * n + 1; aY++)
                    scores[aX, aY] = int.MaxValue;
            foreach (KeyValuePair curr in dict)
            {
                int aX = curr.Key.X + n;
                int aY = curr.Key.Y + n;
                if (curr.Value < scores[aX, aY])
                {
                    scores[aX, aY] = curr.Value;
                }
            }
    
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("out.csv"))
            {
                file.WriteLine("x,y,v");
                for (int aX = 0; aX < 2*n+1; aX++)
                {
                    int x = aX - n;
                    for (int aY = 0; aY < 2 * n + 1; aY++)
                    {
                        int y = aY - n;
                        file.WriteLine("{0},{1},{2}", x, y, scores[aX, aY]);
                    }
                }
            }
    
            Console.WriteLine("Written file");
            Console.ReadKey();
        }
    }
    

    R code below

    library(lattice)
    df = read.csv("out.csv")
    df=transform(df, man=abs(x)+abs(y))
    
    v50=df[abs(df$x)<=50 & abs(df$y)<=50,]
    with(v50, wireframe(v ~ x*y))
    with(v50, contourplot(v ~ x*y))
    
    summary(lm(df$v ~ df$man))
    
    df$est = df$man * 3.5 - 2.5
    summary(df$est - df$v)
    

提交回复
热议问题