I'm trying to implement a 8 puzzle program using the manhattan heuristic using OOP in matlab, with each node represented by an object "state". However, i'm having lost of speed problems implementing an object array for queue. After lots of trial and error, this is the fastest i could go (i hate using global objects but it seems that any attempts at passing the array or using an object for it slows it down tremendously):
.classdef state < handle
. properties
. position; %3x3 array
. moves;
. blank;
. parent;
. prevmove;
. mdist;
. end
....
queue is preallocated
queue = repmat(state,1,5000);
.function newfront = q(item,front)
.global queue;
.if isempty(front)
. newfront = 1;
. queue(1) = item;
.else
. for i = front:-1:0
. if i == 0
. break;
. end
. a = item.mdist;
. b = queue(i).mdist;
. if (a < b)
. break;
. else
. queue(i+1) = queue(i);
. end
. end
. newfront = front + 1;
. queue(i+1) = item;
.end
.end
Using MATLAB profiler:
eightpuzzle 1 67.941 s 2.550 s
eightpuzzle>q 4722 59.657 s 59.657 s
....
118 queue(i+1) = queue(i); 2583916 29.064 s 48.7%
120 end 2583916 16.202 s 27.2%
114 b = queue(i).mdist; 2587318 4.357 s 7.3%
113 a = item.mdist; 2587318 2.783 s 4.7%
115 if (a < b) 2587318 2.721 s 4.6%
is there anyway to make it run faster? interesting thing to note was that by using
a = item.mdist;
b = queue(i).mdist;
if (a < b)
...
instead of
if (item.mdist < queue(i).mdist)
overall runtime was already halved.
Thanks!