As we've been recently learning about recursion and game strategy (evolving from the random strategy to something more intuitive), I'm a bit amazed at just how relevant and simplistic it is. The thought of being able to solve almost any problem using computation and the fact that we can use it to being to solve something like Tic Tac Toe or more complex games with our relative limited knowledge is quite surprising.
I have been interested in Chess since I was in Grade 11, and Chess is a HUGELY complex a game, that is easy to learn, difficult to master. It amazes me that any player above 2000+ ELO rating can easily see what I see (1736 ELO) in just a few seconds while seeing over 5 moves ahead. A 2600+ player can play around 10 chess games blindfolded simultaneously and can memorize more than 100,000 different positions he/she has played.
The ingenuity of ranking a position based on a numeric value is great way of breaking a problem and I'm a bit surprised at just how applicable the very essence of computing is and that it can be used to solve a wide array of problems. The computer does not care about the position it's not, just the move that will lead it to the minimized loss or maximized win. It puts shame to the idea of creativity, creativity does not exist as if there is a "creative" way of doing something (a way that may seem quite novel), the computer will find it and elect it if it is the best MOVE possible, in essence, the computer is the most creative thing there is, as contradictory as it may sound. (A machine being creative?)
Of course the way we are doing it is an exponential problem, and practically impossible to solve many games that are of higher complexity (Go and Chess). In fact, Chess is widely regarded as being unsolvable:
http://en.wikipedia.org/wiki/Shannon_number
The number of possible move states our minimax algorithm would have to look for is more than the number of atoms in the observable universe, but the fact that we can begin to "tackle" the problem and emulate what other chess engines do (Stockfish) is quite astonishing. In an interesting end-off, in an algorithm that was coded using A* search and other mechanisms played Super Mario. Although playing Pacman very strangely and taking advantages of multiple glitches in the game code, it completed it, but when it came to Tetris it struggled to complete all the possibilities quick enough to play logically. When it was a single move from losing, the computer did something unexpected. It paused the game forever. The creator of the algorithm (Tom Seven), phrased it like this "As soon as he unpauses the game, he will lose, and really the only winning move is to not play at all", and it's true, the only winning move in any of those games, is to not play at all.
Tuesday, 24 February 2015
Thursday, 12 February 2015
Learning OO programming in a day
Since I come from a background of AngularJS and PHP web development, I have almost no experience designing object-oriented program, skipped CSC108, and haven't taken a single comp sci course in my life. Although PHP does have a an Object-Oriented side to it, I've never used it much before learning Python's OO. I had a few books on Python and decided to read a few to get a solid understanding of OO as I pretty much had no idea what was happening in class.
I learnt that OO in Python is very similar to OO in PHP and an "emulation" of AngularJS. First, Python supports multi-inheritance while PHP does not. AngularJS does not really use the concept of "objects" much and uses the MVC model. I quickly caught the concept of OO in Python after reviewing and learnt that Python calls a "parent" object to create a new object. This parent object contains callable methods such as init and new, but can re-defined by Python:
Python:
class X:
def __init__(self, x):
self.x = x
self.y = x + 1
PHP:
class X {
public function __construct(x) {
$this->x = x;
$this->y = x + 1;
}
}
As you can see, PHP uses the term this instead of self. Also, PHP does not need to pass self into the parameter while Python separates these two processes in that you must include self inside the parameters of the function. Furthermore, PHP immediately declares a function as public, private, or protected, while Python uses @property to emulate this. By learning Python OO, I was able to apply it immediately into PHP and boost my knowledge it. It also helped reinforce the concept of MVC in AngularJS, the same code in AngularJS:
var myMod = angular.module('myMod', []).
config(function($provide) { //$provide is an injectable parameter in the config attribute
$provide.provider('exampleProvide', function() {
this.$get = function(x) {
return x + 1
};
});
});
myMod.controller('x', ['$scope', 'exampleProvide', function($scope, exampleProvide) {
//Dependency injection from $scope and the provider function that will be used to reference a service from the creation of the module
//Set up initial scopes from the ng-init module
$scope.xInit = function(x) {
$scope.x = x;
}
$scope.y = exampleProvide($scope.x);
}]);
AngularJS uses dependency injection to allow functions into a controller or module. The controller can be viewed as a function in Python. The ng-init module defines a directive (a function that sends code) to receive a parameter and define it as x, ($scope is a service that manipulates the "Pyhton shell" (HTML) of the code)
I'm glad I was able to learn OO and apply to lots of my code in PHP and AngularJS. It especially helps as I'm currently Node.js and MonogoDB and most of them deal with objects encoded into JSON.
I learnt that OO in Python is very similar to OO in PHP and an "emulation" of AngularJS. First, Python supports multi-inheritance while PHP does not. AngularJS does not really use the concept of "objects" much and uses the MVC model. I quickly caught the concept of OO in Python after reviewing and learnt that Python calls a "parent" object to create a new object. This parent object contains callable methods such as init and new, but can re-defined by Python:
Python:
class X:
def __init__(self, x):
self.x = x
self.y = x + 1
PHP:
class X {
public function __construct(x) {
$this->x = x;
$this->y = x + 1;
}
}
As you can see, PHP uses the term this instead of self. Also, PHP does not need to pass self into the parameter while Python separates these two processes in that you must include self inside the parameters of the function. Furthermore, PHP immediately declares a function as public, private, or protected, while Python uses @property to emulate this. By learning Python OO, I was able to apply it immediately into PHP and boost my knowledge it. It also helped reinforce the concept of MVC in AngularJS, the same code in AngularJS:
var myMod = angular.module('myMod', []).
config(function($provide) { //$provide is an injectable parameter in the config attribute
$provide.provider('exampleProvide', function() {
this.$get = function(x) {
return x + 1
};
});
});
myMod.controller('x', ['$scope', 'exampleProvide', function($scope, exampleProvide) {
//Dependency injection from $scope and the provider function that will be used to reference a service from the creation of the module
//Set up initial scopes from the ng-init module
$scope.xInit = function(x) {
$scope.x = x;
}
$scope.y = exampleProvide($scope.x);
}]);
AngularJS uses dependency injection to allow functions into a controller or module. The controller can be viewed as a function in Python. The ng-init module defines a directive (a function that sends code) to receive a parameter and define it as x, ($scope is a service that manipulates the "Pyhton shell" (HTML) of the code)
I'm glad I was able to learn OO and apply to lots of my code in PHP and AngularJS. It especially helps as I'm currently Node.js and MonogoDB and most of them deal with objects encoded into JSON.
Wednesday, 4 February 2015
Object Oriented Programming Overview
Object Oriented Programming is a different approach at programming where the application or code is made up of many parts (objects) and its built piece by piece to create a functional program. It uses the concept of treating parts of the code as "objects" in which these objects contain certain "attributes" and when completely put together can make up the functional program allowing for a logical and simple structure, flexibility, and readability. Object Oriented programming is more read-friendly to humans and has multiple support inside the coding language to create it.
Learning about object-oriented programming has really changed the way I code and likely the way many people code when it was first introduced. The most striking difference is the mindset "adopted" when coding in OO as compared to procedural programming. The most prominent difference is in PHP from my experience, where one learns coding it from a procedural point of view and then learns the OO approach. A example in pseudo PHP code, extends means A extends B, A is a child of B. $this is a the keyword for the python equivalent self and does not need to be passed in the function argument as PHP already does this automatically:
As you can see, with the procedural approach, we made it a bit more intuitive than normal. We created function that preform specific tasks such as eat an orange, or peel a banana. We are under the assumption that peeling an orange and a banana require two difference types of actions and thus require different code for both of the, but eating them is universal. We started by creating a value for an orange and a banana, then peeled them and got a new value for them and assigned that to them, then we ate them and assigned the new value to the orange and the banana after they had been eaten. Essentially we were creating a value, and creating a new modified value and assigning it to said previous reference and did this a bit like issuing commands to a command line and getting the wanted final result. It is logical to read from up to down and you can follow it, but it's not very intuitive or easy to read when you get to more complex code. Let's take a look at the OO approach
The OO approach does things differently. We can see that the only thing that seperates an orange and a banana (other than the real world physical difference) is the way they are peeled. Otherwise, they all share common characteristics, the eat method and the peel method. They are also both FRUITS, and this is where OO becomes to be relevant. Since both the orange and the apple need to be eaten, and since there is no difference in code between eating an orange and eating a banana, we create the super class "Fruit", a class that will not only show the similarities of an orange and a banana, but also act as a general class for ANY "fruit" we create, whether it be a grape, or a pineapple. We can't implement a peel method in fruit (although we could do something like declare that all child fruits (oranges, bananas) must have a eat method in them, this can be done through abstract classes and I have shown below what it might look like along with improvements to the OO code:
Learning about object-oriented programming has really changed the way I code and likely the way many people code when it was first introduced. The most striking difference is the mindset "adopted" when coding in OO as compared to procedural programming. The most prominent difference is in PHP from my experience, where one learns coding it from a procedural point of view and then learns the OO approach. A example in pseudo PHP code, extends means A extends B, A is a child of B. $this is a the keyword for the python equivalent self and does not need to be passed in the function argument as PHP already does this automatically:
As you can see, with the procedural approach, we made it a bit more intuitive than normal. We created function that preform specific tasks such as eat an orange, or peel a banana. We are under the assumption that peeling an orange and a banana require two difference types of actions and thus require different code for both of the, but eating them is universal. We started by creating a value for an orange and a banana, then peeled them and got a new value for them and assigned that to them, then we ate them and assigned the new value to the orange and the banana after they had been eaten. Essentially we were creating a value, and creating a new modified value and assigning it to said previous reference and did this a bit like issuing commands to a command line and getting the wanted final result. It is logical to read from up to down and you can follow it, but it's not very intuitive or easy to read when you get to more complex code. Let's take a look at the OO approach
The OO approach does things differently. We can see that the only thing that seperates an orange and a banana (other than the real world physical difference) is the way they are peeled. Otherwise, they all share common characteristics, the eat method and the peel method. They are also both FRUITS, and this is where OO becomes to be relevant. Since both the orange and the apple need to be eaten, and since there is no difference in code between eating an orange and eating a banana, we create the super class "Fruit", a class that will not only show the similarities of an orange and a banana, but also act as a general class for ANY "fruit" we create, whether it be a grape, or a pineapple. We can't implement a peel method in fruit (although we could do something like declare that all child fruits (oranges, bananas) must have a eat method in them, this can be done through abstract classes and I have shown below what it might look like along with improvements to the OO code:
Now this code shows completely what we want to achieve through OO. Before, we were doing almost the same thing as procedural, just changing the functions under this fancy "class". But now we are treating our code as objects and directly manipulating them instead of returning some value. We know that every fruit has to have some "peel" function, so we declared it as an abstract function which pretty much means, I don't know how to peel said fruit, but I know that if you are going to call yourself a fruit, you should be able to peel it, enforcing that every fruit that is a fruit (child of class Fruit), must have a function that peels it.
Now, our peel function is immediate and directly manipulates the fruit object, instead of returning a value like it used to. If we are eating the fruit it should be something like this:
$orange = 'not eaten';
$orange->eat_fruit();
print($orange);
'Eaten'; //Although this would be a JSON call
As you can see, we are treating our code as objects, directly manipulating it, taking advantage that it has many shared features of other parts of our application and combining them into one universal category to allow us to give them special attributes to them. Oranges, Apples, Grapes are all fruits and all need to be eaten the same way, but not all peeled the same way, some of them (Grapes) don't need to be peeled while some (pineapples) have a radically different way of being prepared/peeled. So the object hierarchy is something like:
Fruits -> (Oranges, Bananas, Grapes, Pineapples)
And maybe more intuitively:
Fruits -> (Oranges, Bananas, Grapes, Berries -> (Blueberries, Blackberries, Raspberries), Pineapples)
All these share common attributes (functions or methods as seen in the code) but almost all have something specific to them. A few examples could be if we introduced a variable or methods called color or calories, sugar_content, create_juice. Not many people create juice purely from bananas, but a create_juice method for oranges makes much more sense. The power in OO is creating this logical and easy to understand structure while maintaining machine logic and flexibility to control the code more. When you see the procedural code, you may or may not know what an orange can do, what it can't or what it needs to do (especially since in code, variable terms aren't nearly as simple as an orange or an apple, but in object oriented code, you know what it cant do or can do, and you also know what role it plays in the code. Like a house:
House -> (Kitchen -> (Sink, Stove, Cabinet -> (Forks, Knives), Fridge -> (Lettuce, Tomatoes)), Living_Room -> (TV -> (Screen, Remote Control), Couch, Lights -> (Methods: Turn On, Turn Off, Replace)), Garage -> (Garage Door (Methods: Open, Close))
Forum -> (Topic -> Array of Comments -> (each comment as variables and methods: comment_text, author_username, insert_reply, delete))
When correctly utilized OO is a logical and intuitive way to approach a large problem and break it down into smaller pieces and solve them to create a functional solution, something that would just be too complicated to do in procedural as the problem scales up. After all, it is a progammer's job to take a impossible large task and divide it into possible smaller tasks.
Subscribe to:
Posts (Atom)


