Monday, 2 March 2015

Why is Minimax called Minimax?

On my first read through of the assignment, I skimmed through the Minimax part and didn't stop to consider why Minimax is named as it is. I didn't put any thought into this as my first reaction to the assignment is to design both of the Tippy classes, state and move.

Once I had an up and running game of Tippy that works perfectly with my random strategy, I had to deal with the looming task of programming Minimax. My first instinct was to have my suggest_move method call the method Minimax on each of the new states after applying each possible move. Minimax would then recursively check each possible game state and return 1, 0 or -1 depending on whether it was a winning or losing position. 

I had a lot of trouble with dealing with the 1s and negative 1s. I wasn't doing a good job of keeping track of which player I was dealing with or which player was next player of the state and kept on getting my 1s and -1s mixed up. I had a lot of tests that I'd run to check to make sure that my code returned -1 for a SubtractSquareState where there was 2 coins left, since thats a very obvious losing position. However the choosing the best move process I had a lot of trouble with. 

My solution was to comment out all my existing code and write a new iteration of it but without any 1s or -1s. Instead I would assign each state a str, either 'p1' or 'p2' depending which player had the winning position. If neither player had a winning position, it would return 'tied', which acted the same way as 0.

For the first time my Minimax code worked! However there was an absence of 1s, 0s and -1s. I felt pretty good about it as it had passed the Tippy test, picking the centre spot on a 3X3 grid on the first turn. However I started talking about Minimax with another student that takes computer science during our mutual philosophy class and he told me all about why Minimax was called Minimax. It's an algorithm, a pretty famous one at that, that Maximizes your score while Minimizes your opponents. Thus the naming is pretty clever, and this algorithmic solution can be generalized to non-zero sum games. This made me reconsider my code which doesn't minimize or maximize, it just picks the state that the concerned player wins in.

In order to try and redo my code, I commented it out and rejigged my suggest_move to call on my new method Maximize, which then called on my second new function Minimize, which called on Maximize etc. I thought this approach could work until I began to actually code it. I realized that both of these functions had almost identical code, and could easily be made into a single function. However once I amalgamated the two of them I began to run into my previously mentioned problems. Only after painstakingly drawing out trees of states and noting which player is next_player and whom I should be concerned with did my algorithm finally take shape.

I'm glad that I finally figured out why this strategy was called Minimax, and that it definitely saved me some marks here and there for a quite unorthodox solution to this problem. In the end my Minimax code is nice and straight forward, I would even call it elegant in some places. Recursion really takes a lot of onus off of a lot of unnecessary computation. 

No comments:

Post a Comment