Sign in to follow this  

optimize a path?

This topic is 4415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a basic path finding algorythm, and it generates path like the one on the left, blue is wall, green is path found and red is area that was scanned for path but didn't end being part of the path, you can see the final path isn't what would be expected, i illustrated how the path should look on the right side of the picture: I guess i need to optimize the path somehow, could someone point me to a algorythm to do this?

Share this post


Link to post
Share on other sites
Yeah, that looks like a depth-first algorithm, which is inefficient and doesn't guarantee a shortest path. Breadth-first and A* will fare better in finding an optimized path in the first place. For path optimization on a continuous euclidean state space, BTW, google "string pulling".

Share this post


Link to post
Share on other sites
but this is "Breadth-first" algorythm (i think), this is what i use:


function TForm1.findPathDepth(start, endp: Tpoint): boolean;
var
x, y: integer;
begin
result:= false;

// is this the end of the path?
if ((start.x = endp.x) and (start.y = endp.y)) then begin
path[pathlen]:= start;
pathlen:= pathlen +1;
result:= true; exit;
end;

// you can't go through walls you know
if map[start.y, start.x] or map[endp.y, endp.x] <> 0 then begin
result:= false; exit;
end;

// oh no, outside of world?
if ((start.x < 0) or (start.y < 0)) or ((start.x > mwidth - 1) or (start.y > mheight - 1)) then begin
result:= false; exit;
end;

// already cheched this block?
if ((map[start.y, start.x] = 1) or (mapBeen[start.y][start.x] = true)) then begin
result:= false; exit;
end;

// this block was processed
mapBeen[start.y, start.x]:= true;

// noteslist.lines.add(format('SEARCHING: , %d %d', [start.x, start.y]));

// search for neighbour cubes
for y:= -1 to 1 do
for x:= -1 to 1 do
begin
if (y = 0) and (x = 0) then continue; // don't search same cell again

if (mapBeen[start.y + y, start.x + x] = false)
and (map[start.y + y, start.x + x] = 0) then begin

// found a node
if findPathDepth(Point(start.x + x, start.y + y), endp) = true then begin
path[pathlen]:= start;
pathlen:= pathlen +1;
result:= true; exit;
end;

end;
end;
end;

Share this post


Link to post
Share on other sites
Nope, that's depth-first (look at the name of the function). Also note that DF is usually implemented via recursion, as this function is, while BF is usually implemented via iteration (often with a priority queue).

Share this post


Link to post
Share on other sites
i haven't found a decent fast example of A* or breadth yet, and i think this depth first works pretty well, all i need to do is optimise the path.

Share this post


Link to post
Share on other sites
No, depth-first doesn't work well. Its complexity balloons up in larger state spaces, and there's no good way to optimize the paths it produces. Take the time to actually learn how A* works, and then code it yourself. You can't always count on other people writing code for you.

Share this post


Link to post
Share on other sites
but i only asked for a algorythm to optimize these paths, i'd really like to learn A* but it is impossible to search for it on google (doesn't search for the star character).

Share this post


Link to post
Share on other sites

This topic is 4415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this