Solving a Logic Maze: Shortest Path to the Goal

  • Context: Graduate 
  • Thread starter Thread starter madameChing
  • Start date Start date
  • Tags Tags
    Logic Path
Click For Summary
SUMMARY

The discussion focuses on solving a logic maze using a solver program that requires defining sorts, functions, and clauses to find the shortest path from a starting position to a goal. The grid is represented with blocked squares, and the solver must navigate through unblocked cells. Key functions include defining positions, obstacles, and the goal, while the algorithm proposed involves iteratively updating cell values based on adjacent cells until the goal is reached. Optimizations for speed include reevaluating only changed cells and strategically assessing quadrants with fewer barriers.

PREREQUISITES
  • Understanding of logic programming concepts
  • Familiarity with grid-based pathfinding algorithms
  • Knowledge of defining sorts and functions in logic solvers
  • Experience with optimization techniques in search algorithms
NEXT STEPS
  • Implement a grid-based pathfinding algorithm using A* or Dijkstra's algorithm
  • Explore logic programming languages such as Prolog for defining sorts and predicates
  • Research optimization techniques for pathfinding, including barrier analysis and quadrant division
  • Learn about heuristic methods to improve search efficiency in logic mazes
USEFUL FOR

Logic programmers, algorithm developers, and anyone interested in optimizing pathfinding solutions in grid-based environments.

madameChing
Messages
2
Reaction score
0
Hi guys,

I'm new to logic and have a logic maze to solve.
basically there is a "solver" program which takes in a set of predicates which must be satisfied to obtain a solution. here's a reproduction of the question below:

The routes to the goal in this case are easy to see. The problem for the solver is to find the shortest route. Of course, you can solve the problem yourself; it is less easy to represent it to the automatic solver.

Here is a simple grid, with some small squares blocked out:
Code:
        a     b     c     d     e     f

     +-----+-----+-----+-----+-----+-----+
     |     |     |     |  o  |     |*****|
  A  |     |     |     | [:] |     |*****|
     |     |     |     | / \ |     |*****|
     +-----+-----+-----+-----+-----+-----+
     |*****|     |*****|     |     |     |
  B  |*****|     |*****|     |     |     |
     |*****|     |*****|     |     |     |
     +-----+-----+-----+-----+-----+-----+
     |     |     |     |*****|     |     |
  C  |     |     |     |*****|     |     |
     |     |     |     |*****|     |     |
     +-----+-----+-----+-----+-----+-----+
     |     |*****|*****|     |*****|     |
  D  |     |*****|*****| [G] |*****|     |
     |     |*****|*****|     |*****|     |
     +-----+-----+-----+-----+-----+-----+
     |     |*****|     |     |*****|     |
  E  |     |*****|     |     |*****|     |
     |     |*****|     |     |*****|     |
     +-----+-----+-----+-----+-----+-----+
     |     |     |     |     |     |     |
  F  |     |     |     |     |     |     |
     |     |     |     |     |     |     |
     +-----+-----+-----+-----+-----+-----+

There is a robot in square Ad, and he needs to find his way to the goal (G) in square Dd. At each step he can move to any adjacent square (North, South, East or West) but must avoid the blocked ones.

Find the shortest path he can follow.

--------
There are 3 types of things to define: sorts, functions and the clauses (predicates that must be satisfied).

I have defined my sorts as such:
#sorts
row enum: A,B,C,D,E,F.

col enum:a,b,c,d,e,f.

dir enum: up,down,left,right.

---
then the functions:

pos: row,col -> int {partial all_different}

obs: row,col -> bool {print: none}

goal: row,col -> bool {print:none}

start: row,col -> bool {print:none}

next: row,col,dir -> bool{partial print:none}

---
note: partial means that the function may be undefined for certain arguments
all_different means no two arguments give the same value.

---
clauses - this is where I am stuck.
I want to tell the solver that there is a next cell if it is not an obstacle, it is not out of range and the goal has not been reached.

The solution should look something like:

Code:
path | a  b  c  d  e  f  
    -----+------------------
       A |          0        
       B |          1  2  3  
       C |                4  
       D |          11    5  
       E |          10    6  
       F |          9  8  7

So far, I have define some clauses :

pos(A,d) = 0 .x=B, y=d -> goal(x,y).

%define obstacles
obs(A,f).
obs(B,a).
obs(B,c).
obs(C,d).
obs(D,b).
obs(D,c).
obs(D,e).
obs(E,b).
obs(E,e).

%there does not exist a pos(x,y) at an obstacle
EST(x), EST(y), EST(pos(x,y)) -> NOT(obs(x,y)).

%this assigns a number to every cell except the obstacles (which is not exactly what i want of course).

---
sorry that it is a long post. any ideas/hints/tips very welcome and appreciated.

thanks!
 
Last edited:
Physics news on Phys.org
Wrap your preformatted text in [ code ] ... [ /code ] tags so it shows up right.
 
Thanks for the tip Hurkyl!
much more comprehensible now. didn't know know to.
 
-I’m unfamiliar with the language or logic scheme you are using, but
-Here is the simplest algorithm for determining the path

Create an array: a grid a-f x A-F
Fill is array with a number > 6*6 = 36 ( assume least optimal )
Place a zero at the robots position.

Look at every cell in this array and…
If that cell has an adjacent cell to it ( which is not blocked or off the edge )
And the number in the adjacent cell is > the number in the current cell +1
Then assign that cell the number in the current cell +1
Repeat this until the goal is reached OR no cells are changed.

(Thinning it out to look like the solution)
Do not display any cell which does not have a higher neighbor OR is >= the number in the ‘goal’ unless it is the goal.

-If you want to tell the “solver” that there is a next cell it is as you stated, the adjacent cell is only a possibility if its not an obstacle, its not out of range, and the goal has not been reached. But there are few things you can do to directly eliminate any other possibilities without trying them.

-Note that the above method finds equivalent paths not shown in your solution, is this still correct?

To improve things quite a bit in terms of speed you can:
Only reevaluate cells adjacent to cells that have changed.
Make a pass though beforehand and add barriers to any cells with three adjacent barriers, repeat.
Simultaneously derive a path from the goal to the robot until it intersects the robot goal path.
Divide the board into quadrants around the goal, sum the barriers in each quadrant and have the search look in quadrants with the fewest barriers first.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
5
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K