Friday, December 23, 2011

Design Patterns Refresher

The most common design patterns from "Head First Design Patterns" by Eric Freeman and Elisabeth Freeman - the best book ever about design patterns:

Encapsulate algorithms. Allows object's behaviour to be changed at runtime by substituting default algorithms. 

Notify dependents of state changes. A very common pattern, often part of the language, see events and listeners in C#.

Use and extend behaviour of the object being wrapped, while acting as that object to the external world. A decorator object is created with an instance of an object of the same type. When a method is invoked, it calls the embedded object's method, modifies the result, and returns it.

Simple Factory
Encapsulate object creation. Useful when you need to decide at runtime which object to create.

Factory Method
Same purpose as Simple Factory, but more abstract: moves actual object creation to other factory objects. 

Indispensable when there is one of something in the system, like one database, or one set of configuration data. Implemented as a sealed class with private constructor and a static method that returns the only instance.

Encapsulate actions. Every action object has "execute" method. Very useful for a processing system that needs to queue commands and then execute them in order. 

Provide a converter that allows clients to use the interface they expect with an object that has a different interface.

Provide a simplified interface. Used to hide complexity of the underlying system from the external world.

Template Method
Provides extension points or hooks by defining steps of an algorithm and allowing or forcing subclasses to provide implementation for some of them. This pattern is often used in frameworks.

Provides a uniform way of accessing sequentially elements in an aggregate object of any type. Typically found in collections.

Organize objects into tree structures so a group of them can be treated the same way as a single element. Each object in the hierarchy is a component that can have sub-components. User interfaces are usually built as composites.

Encapsulate behaviours of an object in state objects. Current state object can request transition to another state.

Provide a surrogate object that controls access to an object that is remote, needs to be secured or is expensive to create.

Saturday, December 17, 2011

Forgetting is the key to AI

I think that making computer programs fortget most of what they receive is the key to building Artificial Intelligence. Why? Because having to much data creates three problems:

1. Space needed to store it.
2. Speed of retrieval.
3. Classification: which piece of data is important?

Selective forgetting would solve these problems.

Spaced repetition algorithm should be used backwards to achieve that goal: when the AI program sees something for the first time, it keeps it until the next day. If it sees it again the next day, it keeps it for 6 more days. If by then it sees it again close to the 6th day, then it keeps it for another 15 days,  and so on. When something stops occurring, it is gradually forgotten. So, the AI program keeps less data, so the information can be retrieved faster, and the data kept is important in understanding the environment, because it keeps coming.

This mechanism works ok with the AI program learning about normal situations. It may stumble upon extraordinary, life-threatening situations, which need to be remembered separately from the spaced repetition algorithm.