Wednesday, 4 March 2015

Week 8: Review on Week 7

 Hello readers!

I can't believe it's been 2 weeks since Reading Week has passed! A lot of new material was covered in lecture, but my brain is still in vacation mode...

For this week, I'll be discussing the topics recently covered in lecture as well as my progression in Assignment 2.

A quick update on my progress in assignment 2, it's been tough for me and my partner to figure out minimax and rough_outcome. Aside from those, everything was doable, unfortunately minimax and rough_outcome make up nearly 50% of the grade. The methods for these are fairly unique and I have a general idea of what is supposed to happen - but I can't seem to find a way to properly implement it. Minimax essentially takes all possible gamestates in the game of Tippy and tries to make the best moves for the computer so that it never loses a game. I had a couple of questions in mind that I have asked and have received responses to - but they haven't really answered my questions - How to implement them. These include multiplying by -1, alternating moves and calling on methods such as apply_move from TippyGameState into minimax. Rough outcome takes all game states and decides a winner based on the available moves left. Once again, we knew the idea but unfortunately we can't implement it for now.

BT Nodes and Binary Search Trees (BSTs) were the topics covered in Week 7. It took me awhile to get the feel for implementing Trees, so it naturally took me a while to get used to the topics covered in Week 7 because they build upon Trees.

Essentially, BT Nodes are Trees that have 2 children that are named left and right as well as data that 'connects' those children together. The class is listed below with the __init__ method:


 
Note that the default parameters for left and right are of type None - this is a crucial concept for implementing the base case for methods in Class BTNode.

Also note that the body in __init__ is the same as the following:






An example of what I mean by 'connecting' these two children together can relate to ANY basic mathematical operators that can join 2 numbers together, which in this case is the left and right child.

We were given a handout for the method 'evaluate' in class BTNode and we were asked questions that could possibly lead us into implementing it.




Like with most recursion functions, we find a base case first and foremost.

If we look at the first example of the docstring, we note that there are no left or right children. At that point we don't need to evaluate anything, so we can simply just return b.data. This can be our base case.



Our generic case will be when there is an interior node with two children - specifically the two children that are each leaves with float data as specified by the docstring. Naturally, I found that it made sense to use the b.left, followed by the operator at b.data and then b.right to evaluate the expression.

The python built in function eval helps use use mathematical operations on strings and return a non-string value.





 Using this we can form a statement to complete the generic case using recursion on the left child and the right child.





Note that going to the left tree followed by the node at b.data and then the right tree is of 'inorder' form to evaluate an expression of a tree.

There are also other ways to evaluate trees such as 'preorder' which evaluates the node itself at b.data followed by the left and right tree respectively. 'Postorder' evaluates the left tree, the right tree and the node itself. But 'inorder' serves our best interests at this time.

BSTs (Binary Search Trees) is a built upon the idea of the left and right children of BTs, but for BSTs, all nodes that are less than the current value are in the left subtree while all the nodes greater than the current value are in the right subtree. This leads to more efficiency. How you say? Suppose that we want a specific value of a tree and we know that specific value is less than the current value of the tree. With BSTs, we would only need to check the left subtree to find that value instead of going through every single node in the right tree as well.


No comments:

Post a Comment