Haha.. I'm done my assignment but I'm still not happy about the result of optimization. I guess I don't let go when I bite. As someone else was saying, cracking 4x4 tippy might be a fun task.
Although in class, Diane emphasized the big picture and the O(g(n)), I think at this point of the Tippy assignment, it might be worth considering the details.
One thing about minimax is that the overall speed of minimax depends on the speed of the game. For example, we will be calling apply_move very often and also be checking whether the game has ended. To that effect, it's useful to be able to perform all of these tasks faster. Suppose minimax goes through N iterations of suggest_move_helper. Then the overall speed boost the USER gets is N*improvement_time.
In practical terms, I don't care what N is as long as N*time_per_N < thresh-hold.
Anyhow, I have a new idea about how to make TippyGameState faster. So, if you read my following post, I mentioned that my game was stored as a nested list, which looking back was a horrible idea. It's one of the first structures that came to mind. Another structure was a dictionary whose elements are "p1", "p2", "p0" and the values are the locations of the possible moves, but I gave up on this approach since I thought that when I look for the value at some location x,y, that the nested list would be faster since I need to iterate through "all" the elements to see if something is in "p1", "p2" or "p0".
But now, I know hashing and that finding an element in "hashed space" only requires O(1).
This means now I know that the following is fast:
D = {"p1":set((x,y), (x2,y2)), "p2":set((x3,y3), (x4,y4)), "p0":set((x5,y5), (x6,y6))}
When you are looking for (x2, y2), you need to ask whether its in D['p1'], D['p2'], or D['p0'].
Overall, this only requires "3 lookups". (for is this value 'p1', you only need "1 lookup" and this is something that is probably often done).
Also, when you look for the possible moves, you just need to get D['p0'].
And thinking of implementing symmetry (about p1 and p2), you just need to swap D['p1'] and D['p2'].
In my implementation, it's sometimes useful to know from time to time what moves have been made by 'p1' or 'p2'... Now, this is also super fast.
Hashing is ingenious.
I've been a frequent reader of your slog and I have to thank you for all your posts! Your conceptual posts and lab answers were a great help for me to understand and complete my own work!
ReplyDeleteThis comment has been removed by the author.
ReplyDelete