Right now, I'm working on understanding the effectiveness of Pruning. This is something I'm not sure about. As mentioned previously, I decided to try using cProfile to obtain all the function calls.
I've just completed it and I'm finding that compared to my original minimax, it's significantly faster for Tippy, but not so much for SubtractSquareState, since I already do some pruning (return win if I just get a winning state; this is a no-brainer form of pruning... Not complete pruning tho).
I also "fixed" that just now to do a better testing. My fixed version basically goes through everything...
I'm testing for subtract square where I seed it with current sum of 29.
StrategyMinimax: 612235 function calls (572894 primitive calls) in 6.180 seconds
Wow... out of those, if we look at the recursive calls only:
ncalls tottime percall cumtime percall filename:lineno(function)
39342/1 0.973 0.000 6.180 6.180 strategy_minimax.py:66(_suggest_move_helper)
1 0.000 0.000 6.180 6.180 strategy_minimax.py:46(suggest_move)
FYI, _suggest_move_helper was the helper function that gets called recursively.
Now, I want to compare this to pruning... which calls move_score... I should change that too... fixed.
StrategyMinimaxPrune: 865 function calls (814 primitive calls) in 0.010 seconds
ncalls tottime percall cumtime percall filename:lineno(function)
52/1 0.002 0.000 0.010 0.010 strategy_minimax_prune.py:69(_suggest_move_helper)
1 0.000 0.000 0.020 0.020 strategy_minimax_memoize.py:53(suggest_move)
No wonder it's so much faster... but now, I want to CHECK how many times it should be checking... To do that... I added a function called tree_form to game_state_tree. I only show the docstring below since I'm not sure if code sharing is allowed... So, the idea was to use this new thing to visualize the tree with current node 29 and to "prune" by hand... but then you see that 39342/1 function calls... what does that mean? That should be the number of nodes right? since it's called at EVERY node... so... This might be hard to SEE. Kind of funny side note is that code is often more accurate than my doctests -_-; haha. Anyhow, I decided to check pruning on just current number 10.
Actually surprisingly simple and my calculation says 16... but my code says less??? Right... my hand checking ability sucks... Retry... got 9... awesome. =) Partial analysis shown below (fyi, I traverse the nodes in my minimaxPrune in the same order as shown in the tree):
For further checking, I will probably use a print statement showing cur_val to check that only the "expected" states are being checked. Doing that now... checked awesome. Repeat for 1 other case... Or not. I'm actually confident it should work. I'll just think about whether there are any special boundary cases I need to worry about.
Just as a side note, I want to add that just by returning when you see a WIN get's you what in a two state (-1, 1) game. I'll leave out the detailed analysis but it should be somewhat obvious.
No comments:
Post a Comment