# qnikst/qnikst.github.com

2013-09-09.0

1 parent 144eba4 commit 42ede3f60d73f637d18a5a21fe93911314064d62 committed Sep 9, 2013
Showing with 67 additions and 39 deletions.
1. +15 −9 posts/2013-09-08-pathfinding.html
2. +20 −10 posts/2013-09-08-pathfinding.lhs
24 posts/2013-09-08-pathfinding.html
 @@ -36,6 +36,10 @@

Basic pathfinding
September 8, 2013

+
2013-09-09: fixes by @qrilka +
+

In this post I’ll review basic approach to pathfinding using functional approach. It possible that this post doesn’t contain the best approach and all other solutions are welcome.

@@ -49,7 +53,7 @@ > import Data.Set (Set) > import qualified Data.Set as Set > import Data.Function -

We will solve simple task of finding the shortest path from one location to another (from 5 to 1). As a first step lets introduce some basic types. Most of them was introduced like types:

+

We will solve simple task of finding the shortest path from one location to another (from 5 to 1). As a first step lets introduce some basic types. Most of them are just type synonyms:

> type Loc     = Int                      -- Location
> type Dist    = Int                      -- Distance
> type History = [Loc]                    -- History
@@ -68,19 +72,21 @@

Basic algorithm

-

The basic idea of our solution is: ‘we need to create a list of the locations that is reachable from initial descrination sorted by distance’. You can this about this solution as we move in all possible directions at the same time. Then we can filter out final destination and find history related to the correct solution.

-

So algorithm will look like:

+

The basic idea of our solution is: ‘we need to create a list of the locations that is reachable from initial location sorted by distance’. You can this about this solution like we move in all possible directions at the same time. Then we can filter out final destination and find history related to the correct solution.

+

Terms: frontier - a list of possible locations with distance from the starting point, sorted by ascending distance.

+
> type Frontier0 = [(Loc, Dist)]
+

So algorithm of generating result list will look like:

-
1. create frontier from the start position.
2. -
3. take the first location from the frontier. It will be our next current location.
4. -
5. p1 <- get list of the locations reachage from the current location.
6. -
7. p2 <- add current distance to the distance from the current location to destination
8. -
9. add p2 to the frontier
10. +
11. Add starting point with zero distance to the frontier.
12. +
13. Take the first location from the frontier. It will be our next current location.
14. +
15. p1 <- get list of the locations reachable from the current location in one step.
16. +
17. p2 <- sum distance to the current distance with the distance from the current location to each possible location from p1. Now we have a list of possible locations with distances from the starting point.
18. +
19. Add list of the new possible locations (p2) to the frontier.
20. goto 1.

This algorithm will create a list of the possible destinations from the current point. The key of the algorithm is that frontier should be sorted by distance, so every time we will take closest position. It will guarantee that our algorithm will not diverge.

As you’ll see the solution will be a direct translation of literal description to the code. And that is a very nice property of haskell.

-

Here is some images describing process:

+

Here are few images describing process:

30 posts/2013-09-08-pathfinding.lhs
 @@ -1,3 +1,5 @@ +
2013-09-09: fixes by @qrilka
Here is an image of the current state after step 0.
 @@ -15,7 +15,11 @@ http://qnikst.github.com/posts/2013-09-08-pathfinding.html 2013-09-08T00:00:00Z 2013-09-08T00:00:00Z - In this post I’ll review basic approach to pathfinding using functional approach. It possible that this post doesn’t contain the best approach and all other solutions are welcome.

2013-09-09: fixes by @qrilka + + +

In this post I’ll review basic approach to pathfinding using functional approach. It possible that this post doesn’t contain the best approach and all other solutions are welcome.

@@ -28,7 +32,7 @@ > import Data.Set (Set) > import qualified Data.Set as Set > import Data.Function -

We will solve simple task of finding the shortest path from one location to another (from 5 to 1). As a first step lets introduce some basic types. Most of them was introduced like types:

+

We will solve simple task of finding the shortest path from one location to another (from 5 to 1). As a first step lets introduce some basic types. Most of them are just type synonyms:

> type Loc     = Int                      -- Location
> type Dist    = Int                      -- Distance
> type History = [Loc]                    -- History
@@ -47,19 +51,21 @@

Basic algorithm

-

The basic idea of our solution is: ‘we need to create a list of the locations that is reachable from initial descrination sorted by distance’. You can this about this solution as we move in all possible directions at the same time. Then we can filter out final destination and find history related to the correct solution.

-

So algorithm will look like:

+

The basic idea of our solution is: ‘we need to create a list of the locations that is reachable from initial location sorted by distance’. You can this about this solution like we move in all possible directions at the same time. Then we can filter out final destination and find history related to the correct solution.

+

Terms: frontier - a list of possible locations with distance from the starting point, sorted by ascending distance.

+
> type Frontier0 = [(Loc, Dist)]
+

So algorithm of generating result list will look like:

-
1. create frontier from the start position.
2. -
3. take the first location from the frontier. It will be our next current location.
4. -
5. p1 <- get list of the locations reachage from the current location.
6. -
7. p2 <- add current distance to the distance from the current location to destination
8. -
9. add p2 to the frontier
10. +
11. Add starting point with zero distance to the frontier.
12. +
13. Take the first location from the frontier. It will be our next current location.
14. +
15. p1 <- get list of the locations reachable from the current location in one step.
16. +
17. p2 <- sum distance to the current distance with the distance from the current location to each possible location from p1. Now we have a list of possible locations with distances from the starting point.
18. +
19. Add list of the new possible locations (p2) to the frontier.
20. goto 1.

This algorithm will create a list of the possible destinations from the current point. The key of the algorithm is that frontier should be sorted by distance, so every time we will take closest position. It will guarantee that our algorithm will not diverge.

As you’ll see the solution will be a direct translation of literal description to the code. And that is a very nice property of haskell.

-

Here is some images describing process:

+

Here are few images describing process:

 @@ -15,7 +15,11 @@ http://qnikst.github.com/posts/2013-09-08-pathfinding.html 2013-09-08T00:00:00Z 2013-09-08T00:00:00Z - In this post I’ll review basic approach to pathfinding using functional approach. It possible that this post doesn’t contain the best approach and all other solutions are welcome.

2013-09-09: fixes by @qrilka + + +

In this post I’ll review basic approach to pathfinding using functional approach. It possible that this post doesn’t contain the best approach and all other solutions are welcome.

@@ -28,7 +32,7 @@ > import Data.Set (Set) > import qualified Data.Set as Set > import Data.Function -

We will solve simple task of finding the shortest path from one location to another (from 5 to 1). As a first step lets introduce some basic types. Most of them was introduced like types:

+

We will solve simple task of finding the shortest path from one location to another (from 5 to 1). As a first step lets introduce some basic types. Most of them are just type synonyms:

> type Loc     = Int                      -- Location
> type Dist    = Int                      -- Distance
> type History = [Loc]                    -- History
@@ -47,19 +51,21 @@

Basic algorithm

-

The basic idea of our solution is: ‘we need to create a list of the locations that is reachable from initial descrination sorted by distance’. You can this about this solution as we move in all possible directions at the same time. Then we can filter out final destination and find history related to the correct solution.

-

So algorithm will look like:

+

The basic idea of our solution is: ‘we need to create a list of the locations that is reachable from initial location sorted by distance’. You can this about this solution like we move in all possible directions at the same time. Then we can filter out final destination and find history related to the correct solution.

+

Terms: frontier - a list of possible locations with distance from the starting point, sorted by ascending distance.

+
> type Frontier0 = [(Loc, Dist)]
+

So algorithm of generating result list will look like:

-
1. create frontier from the start position.
2. -
3. take the first location from the frontier. It will be our next current location.
4. -
5. p1 <- get list of the locations reachage from the current location.
6. -
7. p2 <- add current distance to the distance from the current location to destination
8. -
9. add p2 to the frontier
10. +
11. Add starting point with zero distance to the frontier.
12. +
13. Take the first location from the frontier. It will be our next current location.
14. +
15. p1 <- get list of the locations reachable from the current location in one step.
16. +
17. p2 <- sum distance to the current distance with the distance from the current location to each possible location from p1. Now we have a list of possible locations with distances from the starting point.
18. +
19. Add list of the new possible locations (p2) to the frontier.
20. goto 1.

This algorithm will create a list of the possible destinations from the current point. The key of the algorithm is that frontier should be sorted by distance, so every time we will take closest position. It will guarantee that our algorithm will not diverge.

As you’ll see the solution will be a direct translation of literal description to the code. And that is a very nice property of haskell.

-

Here is some images describing process:

+

Here are few images describing process: