After learning, and re-learning, that the best way to write a sophisticated program is to do extensive algorithmic planning before even looking at an IDE I have decided to take a novel approach to Assignment 3: Writing unit tests before implementing my classes.
This does a few things. First off, it familiarizes me with how class will be structured before I start implementing it. I get to think about what kinds of properties and methods it needs, but I am still a safety-layer away from actual implementation code. I am therefore thinking about what my class needs to do, as well as (to a helpful degree) how it will do it in a way that baby steps me towards filling in the details. Then, when I actually begin to implement my class, I will be productively haunted by the test cases so that I proceed gently and carefully, bearing in mind the totality of what criteria my code must meet for the client. Rushing to patch up a bug with code that creates three more bugs should be more difficult.
Second off, when my class is fully implemented I will have test cases ready to go. This allows me to test my class while implementation details are still fresh in memory, instead of diverting attention to creating Unit Tests (which may reveal things that cause me to actually redesign my class) and then trying to find bugs in code that I have a vaguer recollection of. Furthermore, with general tests cases in place, I can change code to address bugs while still having firm criteria in place for when my class is back to regular performance. At this point I can even design more savvy test cases to find specific bugs, while remaining assured that I know what my code must do before it gets shipped.
Thursday, 26 March 2015
Monday, 23 March 2015
Slaying The Foe Of Minimax
*Official "revisitation" post is on April 2nd
Finally! After multiple revisitations, I finally managed to crack Minimax. Shipping the non-functional code for assignment 2 was a disappointing experience, albeit slightly relieving because I could now justify my withdrawal from the agony of trying to get it to work like the doc string says.
The problem? My get_score function did not handle recursive calls in a way that actually fit GameState's properties. I split Minimax into two functions: suggest_move to return a move to the game state, and get_score to see what the best move(s) were for the computer. Most of the heavy lifting was done in get_score, which would check the GameState it was passed first and (pending a non-terminal state) would proceed recursively until it found finished games. Where I ran into trouble, and where I will guess many others did too, is with how many levels down (to analogize possible GameStates to a tree) the function checked.
If get_score begins by asking "Can I win?" of the current GameState, it is left with a response that satisfies a base case. The game is either over (leading to a win, lose or tie), or the game is not over and the possible subsequent game states must be assesed from the frame of reference of the same player. The answer may seem simple: Because of our game's zero-sum nature, we assume that the score of Minimax's opponent can simply be multiplied by minus one; but this does not really inform us of what the possibilities are for the computer. Supposing, at level 2, the game was still incomplete and the game is winnable for the opponent. This does not mean that the game is not winnable for Minimax! The only case we care about at this level, is if the computer has won or tied the game.
What I had to do was go two layers below any game state passed into get_score, to get to the next state where the computer was the state.next_player. This subsequent GameState could be asked, again, "Can I win?" and the framework would therefore be set for a recursive algorithm. Each recursive call would therefore not only check the GameStates one move away, but also the ones two moves away. The next recursive call would start where the last one left off, resetting the state.next_player to the computer.
Finally! After multiple revisitations, I finally managed to crack Minimax. Shipping the non-functional code for assignment 2 was a disappointing experience, albeit slightly relieving because I could now justify my withdrawal from the agony of trying to get it to work like the doc string says.
The problem? My get_score function did not handle recursive calls in a way that actually fit GameState's properties. I split Minimax into two functions: suggest_move to return a move to the game state, and get_score to see what the best move(s) were for the computer. Most of the heavy lifting was done in get_score, which would check the GameState it was passed first and (pending a non-terminal state) would proceed recursively until it found finished games. Where I ran into trouble, and where I will guess many others did too, is with how many levels down (to analogize possible GameStates to a tree) the function checked.
If get_score begins by asking "Can I win?" of the current GameState, it is left with a response that satisfies a base case. The game is either over (leading to a win, lose or tie), or the game is not over and the possible subsequent game states must be assesed from the frame of reference of the same player. The answer may seem simple: Because of our game's zero-sum nature, we assume that the score of Minimax's opponent can simply be multiplied by minus one; but this does not really inform us of what the possibilities are for the computer. Supposing, at level 2, the game was still incomplete and the game is winnable for the opponent. This does not mean that the game is not winnable for Minimax! The only case we care about at this level, is if the computer has won or tied the game.
What I had to do was go two layers below any game state passed into get_score, to get to the next state where the computer was the state.next_player. This subsequent GameState could be asked, again, "Can I win?" and the framework would therefore be set for a recursive algorithm. Each recursive call would therefore not only check the GameStates one move away, but also the ones two moves away. The next recursive call would start where the last one left off, resetting the state.next_player to the computer.
Friday, 13 March 2015
Creativity In Programming
While working through lab 7, I had the benefit of experiencing a brief "aha" moment that I am less familiar with in my humanities experience. Sitting with a problem, deliberating to the point of diminishing returns, taking breaks, and finally having the solution "emerge from the ether" has an inherent pleasure that makes me begin to understand how it is that so many accomplished scientists have forgone much of 21st century Western leisure in exchange for vocational commitment.
In computer science, this takes an interesting form: Elegant solutions to seemingly unsolvable problems. These elegant solutions, when combined, give us the sophisticated functionality that takes from Abstract Data Structures right through to cars that can drive themselves. In the case of linked lists, the example I am writing about is fairly simple: How it is that one programs into a data structure the capacity to add or remove data elements. We take the apparent simplicity of this problem for granted, because (as introductory programmers) we are so used to having this problem handled for us in pre-implemented data structures. However, when creating data structures from scratch one has to do the creative heavy lifting oneself.
The elegance lies in a simple trick: Point the prior element's "next" property past the current element to delete the current element. Garbage collection handles the rest. These "out of the ether" solutions take a creative form, and are incredibly powerful. Another one, discussed last class, allows us to delete elements from a Binary Search Tree: Either replace the deleted element with the rightmost element in the left sub-tree, or the leftmost element in the right sub-tree. This is another clever solution that allows data structures to perform in intuitive ways for clients, and allows them the benefit of taking the creativity on the back-end for granted. The extent to which our clients can do this can be framed as a measure of our success as developers.
Sunday, 8 March 2015
Thoughts on Artificial Intelligence
So far in the course, our strategy classes have acquired an increasing degree of intelligence: In assignment 1, we used a computer strategy that simply picked a random move for the computer. In assignment 2, we were required to create a strategy that led the computer to winnable game states.
While planning the Minimax class, I was struck by a relation to a Cognitive Science course I took with Professor Vervaeke. While the Touring Test is often considered to be criteria by which we can judge a computer to have "human-like" intelligence, it neglects key components of how it is that we achieve intelligence which may be essential to what intelligent behaviour we can actually perform. Here is one distinction between computer algorithms and human intelligence: While the algorithms of a class such as Minimax must compute all possibilities until they arrive at one that they are programmed to look for, the human mind is somehow able to automatically discount an enormous amount of data "out there" without having to compute it at all. Take, for example, a chess game: The brain does not need to process the combinatorially explosive range of possible sequences before it picks a favourable one. The ability to pick courses of action that cohere with underlying imperatives and incentives comes with an equal ability to discount courses of action that are irrelevant to said motives: Professor Vervaeke calls this "relevance realization".
While relevance realization is a "how" rather than a "what" (an implementation detail, rather than an output), its mimicry is not just essential to an actual working model of the human mind. It may actually be essential to mimicking the mind's output as well. While a computer can be programmed to handle domain-specific tasks such as chess, navigation and so forth, the brain (and consequently the mind) is a machine capable of incredible degrees of plasticity. It can compensate for functional damage by constructing new pathways, it can master novel challenges through repetition, and it carefully yet brilliantly controls what information is processed both consciously and subconsciously and what is simply not computed at all. All of this is taking place in an incredibly dynamic internal and external environment, while factoring in quantities of data that would be impossible to consider at the conscious level. Consider the amount of data required to simply make you flinch out of the way of a projectile object: Trajectory, distance, the object itself, sorrounding circumstances (would you flinch into oncoming traffic?), and so forth.
The brain does not respond to its circumstances in a static manner, either: While a computer can be programmed to avoid projectile objects, if you were to place it next to an open flame it may very well avoid an incoming object by jumping into a fiery dissolution. To alter that unintended consequence, programmers and engineers would have to manually add additional behaviour, and the cycle would repeat itself. Following this developmental order, it may be reasonable to suppose that human-like intelligence will never really be repliated: The best we can do is produce robots that are indistinguishable from humans in very specific performance domains.
What makes a human brain, or even a bird's brain, so impressively intelligent should begin to jump out at you: It obviously transcends the specific algorithms by which life forms accomplish discerete tasks. The transcience of these algorithms points to an underlying imperative, by which an organism constantly devises new sequences of behaviour, metabolism, thought (in the case of humans), and so forth. The imperative to survive and reproduce has granted us, via millions of years of natural selection and spontaneous mutation, an incredible array of adaptive faculties and new ones that can evolve in the course of a life time (for example, skill mastery).
Whether it is possible to program a machine to have such an imperative, by which it can be let loose in the world only to self-organize and co-develop new forms of intelligence alongside a dynamic environment, is beyond me. Logically, nothing seems out of bounds here. As a materialist, who long ago reliquished belief in philsophically replete notions of immortal souls and "free will", I don't see any inherent contradiction. A computer, being made of matter in the same way that a brain is, shouldn't be limited in any fundamental regard. The question is whether the Darwinian imperative of life can be packaged in an algorithm to give AI an evolutionary starting point, and if not, how that imperative could somehow be installed in a machine while avoiding an infinite regress of pre-programmed algorithms
While relevance realization is a "how" rather than a "what" (an implementation detail, rather than an output), its mimicry is not just essential to an actual working model of the human mind. It may actually be essential to mimicking the mind's output as well. While a computer can be programmed to handle domain-specific tasks such as chess, navigation and so forth, the brain (and consequently the mind) is a machine capable of incredible degrees of plasticity. It can compensate for functional damage by constructing new pathways, it can master novel challenges through repetition, and it carefully yet brilliantly controls what information is processed both consciously and subconsciously and what is simply not computed at all. All of this is taking place in an incredibly dynamic internal and external environment, while factoring in quantities of data that would be impossible to consider at the conscious level. Consider the amount of data required to simply make you flinch out of the way of a projectile object: Trajectory, distance, the object itself, sorrounding circumstances (would you flinch into oncoming traffic?), and so forth.
The brain does not respond to its circumstances in a static manner, either: While a computer can be programmed to avoid projectile objects, if you were to place it next to an open flame it may very well avoid an incoming object by jumping into a fiery dissolution. To alter that unintended consequence, programmers and engineers would have to manually add additional behaviour, and the cycle would repeat itself. Following this developmental order, it may be reasonable to suppose that human-like intelligence will never really be repliated: The best we can do is produce robots that are indistinguishable from humans in very specific performance domains.
What makes a human brain, or even a bird's brain, so impressively intelligent should begin to jump out at you: It obviously transcends the specific algorithms by which life forms accomplish discerete tasks. The transcience of these algorithms points to an underlying imperative, by which an organism constantly devises new sequences of behaviour, metabolism, thought (in the case of humans), and so forth. The imperative to survive and reproduce has granted us, via millions of years of natural selection and spontaneous mutation, an incredible array of adaptive faculties and new ones that can evolve in the course of a life time (for example, skill mastery).
Whether it is possible to program a machine to have such an imperative, by which it can be let loose in the world only to self-organize and co-develop new forms of intelligence alongside a dynamic environment, is beyond me. Logically, nothing seems out of bounds here. As a materialist, who long ago reliquished belief in philsophically replete notions of immortal souls and "free will", I don't see any inherent contradiction. A computer, being made of matter in the same way that a brain is, shouldn't be limited in any fundamental regard. The question is whether the Darwinian imperative of life can be packaged in an algorithm to give AI an evolutionary starting point, and if not, how that imperative could somehow be installed in a machine while avoiding an infinite regress of pre-programmed algorithms
Subscribe to:
Posts (Atom)