Question

I find a method implement get successor for node in A* in c# first the user will enter the start position and the goal position in 10x10 grid the method get successor will take the goal and then generate the successor for the goal and so on till it's reach the start my question now that the method takes the diagonal successor "corners", I want to get only up, down, left and right nodes as successor for the node. this the method

public ArrayList GetSuccessors()
    {
        ArrayList successors = new ArrayList ();

        for (int xd=-1;xd<=1;xd++)
        {
            for (int yd=-1;yd<=1;yd++)
            {

                if (Map.getMap (x+xd,y+yd) !=-1)
                {
                    Node n = new Node (this,this._goalNode ,Map.getMap (x+xd,y+yd) ,x+xd,y+yd);
                    if (!n.isMatch (this.parentNode) && !n.isMatch (this))
                        successors.Add (n);

                }
            }
        }
        return successors;
    }

assume that I enter x=0 , y=0 as start x = 5 , y = 2 as goal

it will start with goal position it's will take x= 4 , y=1 as first successor which is diagonal node !!

I know that the problem is on the loop condition, How I need to modify the condition to take only up down left right successors. don't worry about getMab it's only return if the node has 1 or -1 if it's an obstacle.

thank you

Was it helpful?

Solution

Your for loops currently iterate over all 9 possible actions (moving in the 8 possible directions and staying put). Add an if statement inside your second for loop that will ignore the moves that you don't want. Something like this:

for (int xd=-1;xd<=1;xd++)
{
    for (int yd=-1;yd<=1;yd++)
    {
        // you want only up, down, left, or right moves
        // those are if one of xd or yd == 0
        if ((xd != 0 && yd != 0) || (xd == 0 && yd == 0))
            continue;
        if (Map.getMap (x+xd,y+yd) !=-1)
        {
            Node n = new Node (this,this._goalNode ,Map.getMap (x+xd,y+yd) ,x+xd,y+yd);
            if (!n.isMatch (this.parentNode) && !n.isMatch (this))
                successors.Add (n);

        }
    }
}

OTHER TIPS

Your nested loops are going to take every combination of -1,0,1 for both the x and y coordinate, which includes diagonal values (and the current node at (0,0), which should also be avoided. It may be easier to just manually check the 4 cardinal directions you want rather than try to loop.

To do that, just take your inner if block and repeat it for the 4 combinations of xd and yd you want (0,1), (1,0), (-1,0), and (0,-1). You could refactor this block in to a function to shorten the code up so you do not repeat code so much.

            if (Map.getMap (x+xd,y+yd) !=-1)
            {
                Node n = new Node (this,this._goalNode ,Map.getMap (x+xd,y+yd) ,x+xd,y+yd);
                if (!n.isMatch (this.parentNode) && !n.isMatch (this))
                    successors.Add (n);

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top