Monday, 2 March 2015

A Late Night With Recursion

Recursion is a tricky beast to handle. 

I ran into an interesting problem with my recursive Minimax while coding over the weekend. A bunch of friends of mine that also take CSC148 got together over food and started discussing Minimax and the ways of creating it. After a few minutes we all had a very good idea about how to implement it and went our separate ways to actually write the few lines of code to get it to work.

Later that night at an ungodly hour, our mutual Facebook Chat exploded with us all complaining about the same error. So we met up again in my room and proceeded to try to explain to the others why the errors were happening. When logic didn't work, we resorted to volume, until my don told us that it was 3am and we were way out of line. 

Eventually we stripped as much code away from minimax until all it did was run through every game state, and the same error occurred. It only went down until it hit a leaf, and then returned a NoneType error. We were all perplex. After drawing out way too many diagrams explaining the situation, we realized that when the move was executed, it changed the original game state. So once the base-case was hit and it recursively bumped up to it's parent, instead of seeing the correct game state, the game state was one of a finished game. Thus there were no possible moves however it was inside a part of the code that was only reachable if there were possible moves. Thus a None move was returned, creating a GameState of None which effectively broke our code. 

Finally after much pacing and frustration, we tried the built in Copy class in python. Every time we applied a move to the state, we'd actually apply it to copy.copy(state). This still didn't fix our problem and we went back to the drawing board. This was until my very quiet friend who had sat through this entire process piped up from the corner of the room, "have you tried deep copy yet?" We all looked at him as if he was clinically insane. 

"What's a deep copy?" 

"I literally have no idea, it looks complicated and stuff. But it could work?"

Hopefully my buddy entered copy.deep_copy(state) and proceeded to run out code that had become littered with print statements, used for the endless debugging we were stuck in. Nothing had changed. We decided we needed to unwind so we left our residence and began to make a trek to our local 24 hour restaurant to enjoy some much needed food and beverages. Half way there we got a phone call from our same quiet friend who had decided to stay behind. Instead of beginning the conversation normally, he immediately reamed us out from being idiots and explained that we mistyped the deep copy code. Apparently it is actually copy.deepcopy(state), and once run it fixed the very problem that we had been pulling our hair out because of. 

Nothing needed to be said amongst my group of friends, we looked at each other and turned on our heel to return to our warm room after braving the cold for no good reason. Once back we all dispersed to various corners of my room in an effort to beat each other to write the code for a working Minimax first. This turned out to be very profitable but quite anticlimactic, since in order to prove one's code works... they had to run Minimax. Running minimax for the first time on untested code is quite a trying experience because it takes literally 8 minutes and if you got it wrong... sucks to suck. 

Not surprisingly my quiet friend won, after taking much longer than anyone else to write the code, but actually getting it correct on his first time running it. 

What we ended up learning the hard way is the copy.deepcopy(s) is a great friend to have while doing recursion on an object in another class that is modified at each level of the reclusive call. 

No comments:

Post a Comment