(I'm not sure anyone has ever gotten as excited as I get about writing really simple and really bad code)
My favorite biology book, "Out of Control: The New Biology of Machines, Social Systems and the Economic World", has a great section that describes the thermostat as the seed of Artificial Intelligence.
In some very, very narrow sense the thermostat was, well, not self-aware, but at least capable of continually responding in pre-defined ways to a constantly changing environment.
I've written a very simple program that uses a thermostat style principle to "shotgun" a solution to a problem.
The computer is tasked with finding a sequence of integers that add up to 6. However, the integers must be chosen at random.
Randomly chosen integers might add up to 6, but even more likely is that the numbers will jump straight from 4 to 7, and the computer (since only positive numbers can be chosen), will now be unable to find a sequence of numbers that add up to 6.
This is where the thermostat principle comes in.
A thermostat would turn the heating off when the temp reached, or jumped above, the target temperature. And so my program, when it adds a number that raises it above the goal number, will delete the last added number so that it is once again below the goal number. (Just like the thermostat temporarily turns the heating off, my program temporarily turns the adding off).
Small numbers result in shorter sequences since the computer is less likely to exceed the target number.
Large numbers introduce more volatility.
Here's the sequence when the computer randomly chooses numbers from the list [1,2,3] in order to find a combination that adds up to 6.
[1]
[1, 2]
[1, 2, 2]
[1, 2, 2, 3] (EXCESS 1)
[1, 2, 2]
[1, 2, 2, 2] (EXCESS 2)
[1, 2, 2]
[1, 2, 2, 3] (EXCESS 3)
[1, 2, 2]
[1, 2, 2, 1] (FOUND IT!!!)
We can see that three times the total exceeded 6 and the computer "turned off" the adding and removed an integer(s) until the total was below 6 and hence, again possible to reach by adding a number.
If we introduce bigger numbers, changing the list of potential numbers from [1,2,3] to [1,2,3,4,5] we should have greater volatility. I'm using volatility here to mean more excesses and more self-corrections.
And, voila!
[2]
[2, 5]
[2]
[2, 5]
[2]
[2, 3]
[2, 3, 2] (EXCESS)
[2, 3]
[2, 3, 3] (EXCESS)
[2, 3]
[2, 3, 5] (EXCESS)
[2, 3]
[2, 3, 2] (EXCESS)
[2, 3]
[2, 3, 3] (EXCESS)
[2, 3]
[2, 3, 4] (EXCESS)
[2, 3]
[2, 3, 2] (EXCESS)
[2, 3]
[2, 3, 3] (EXCESS)
[2, 3]
[2, 3, 5] (EXCESS)
[2, 3]
[2, 3, 2] (EXCESS)
[2, 3]
[2, 3, 5] (EXCESS)
[2, 3]
[2, 3, 2] (EXCESS)
[2, 3]
[2, 3, 2] (EXCESS)
[2, 3]
[2, 3, 4] (EXCESS)
[2, 3]
[2, 3, 3] (EXCESS)
[2, 3]
[2, 3, 3] (EXCESS)
[2, 3]
[2, 3, 4] (EXCESS)
[2, 3]
[2, 3, 2] (EXCESS)
[2, 3]
[2, 3, 4] (EXCESS)
[2, 3]
[2, 3, 1] (FOUND IT!!!!!)
WAY MORE VOLATILE!
But the goal of finding a combination of numbers that add up to 6 was still reached.
The code for the program is below.
#Goal: To create a shot-gun solution/combination of finding \
# numbers that add up to 6.
import random
the_list = [1,2,3,4,5]
def implement_shotgun():
answer_combo = []
while sum(answer_combo) != 6:
if sum(answer_combo) < 6:
r = random.randrange(5)
answer_combo.append(the_list[r])
print answer_combo
if sum(answer_combo) > 6:
answer_combo = answer_combo[:-1]
print answer_combo
implement_shotgun()
Thursday, August 12, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment