Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python idastar vs astar solving 8 puzzle

I started to refresh my ai knowledge so i implemented some pathfind algorithms to solve 8-Puzzle.

  • A*
  • IDA*
  • BFS
  • IDFS

I was wondering why my implementation of IDA* has a longer path. It should be optimal like A*.

% python puzzle8.py -a idastar -d hard
IDASTAR - RESULT in 161.6099:
1 | 2 | 3
4 | 5 | 6
7 | 8 | N

cost: 0 total_cost: 121
...
nodes 28

% python puzzle8.py -a astar -d hard
Max nodes 665 loops 1085
ASTAR - RESULT in 0.3148:
1 | 2 | 3
4 | 5 | 6
7 | 8 | N

cost: 0 total_cost: 115
...
nodes 24

Code is on gist https://gist.github.com/1629405

Update:

Code is now pointing to a working version.

% python puzzle8.py -a idastar -d hard
IDASTAR - RESULT in 234.4490: 

1 | 2 | 3
4 | 5 | 6
7 | 8 | N
...
nodes 24

But i'm still wondering why IDA* takes so much longer under python than A*.

Update 2:

Code is changed prints now visited nodes.

IDASTAR creates 4184368 ASTAR 1748 nodes.

like image 549
delijati Avatar asked Jan 17 '12 23:01

delijati


1 Answers

Because your implementation of IDASTAR increments the limit by 10 with each iteration, which only guarantees that your solution will be no more than 9 more than optimal. Change the increment to 1, and you should get an optimal result (but take longer to do so).

like image 97
Scott Hunter Avatar answered Sep 22 '22 10:09

Scott Hunter