Ford Circles

I picked up a copy of The Math Book after reading the review on John D. Cook's blog.

So far it has been pretty interesting and I've was plesently surprised with how much of the material I am familar with, despite the fact that I haven't studied math beyond the courses that are part of the engineering curriculum at my university.

The best part, though, has been the occasions when I find something that I can play with a little bit on my own. A good example is Ford Circles, which with a little time in NodeBox I had my own renderings:

Ford Circles

Here's the code:

from __future__ import division

scale(1000, 1000)

def circle(x, y, r):
    oval(x * 1000, y * 1000, 2 * r, 2 * r)

def ford(h, k):
    r = 1 / (2 * k**2)
    x, y = h/k, r
    print x, y, r
    circle(x, y, r)


for h in range(0, 200):
    for k in range(1, 200):
        ford(h, k)

... and here's a bigger rendering, if you're interested.

Lockhart's Lament

A Mathematician's Lament is an essay I read a while ago that was written by Paul Lockhart, a math teacher. It expresses, in ways far more articulate than I ever could, some of the things that are wrong with the way people are taught mathematics: Namely that no-one actually does any mathematics proper until their undergrad and even then only if you are studying one of the few disciplines that requires real math courses.

Check it out. It's a good read.

Guns, Bullets and Uncharted

I've been playing Uncharted: Drake's Fortune recently and so far it's a really fun game... but, the balance of the weapons has been really irking me.

You Start with a 9mm...

You start out with a generic 9mm pistol which seems to take 3-5 body shots to kill a bad guy with. Yikes! Meaning that instead of doing what actual people who shoot at people (cops, soldiers, etc.) are trained to do (center of mass shooting) you end up peering around corners trying to get head shots while your enemies shoot you in the face (thankfully your character appears to be even more nigh invulnerable than the bad guys).

Then You Get an AK-47

That iconic Soviet assault rifle, which holds more bullets than your 9mm, still takes 3-5 body shots to kill a bad guy and will run out of ammo a lot faster if you carelessly hold down the trigger.

For some insane reason, it's actually harder to get head shots with the AK than with the 9mm, so I found myself fighting with the pistol because I would at least have a chance of taking out a bad guy with a single shot rather than spraying bullets all over the place (that's right, the shoulder mounted rifle is less accurate than your pistol because... it's crappy and Soviet *shrug*).

... And Finally a One Shot Kill

... and it's a Desert Eagle .50AE (well actually the first one hit kill you come across a .44 Magnum but I have more rant material for the Desert Eagle and in terms of ballistics they are largely equivalent). This is the classic shooting game, hit them in the knee and they fly back a meter or two an fall the ground dead style of one-hit-kill weapon. Fun, but seriously, why a Desert Eagle?

Yes, I know that both Bullet Tooth Tony and The Agents use them, thus, they must be totally bad ass, but seriously they're hand guns.

I get that, when you're making a game, you want a power curve on your weapons so that as the game progresses your character gets progressively more kick-ass weapons; however, why on Earth is a pistol the first weapon you encounter that can reasonably knock someone on their ass the first time you shoot them.

... And Order is Restored... Sort Of

A little deeper in you get a SVD (Dragunova) which is a sniper rifle of Soviet origin, which also has the ability to deliver one-hit-kills. So at least one rifle is able to out perform the Desert Eagle, but neither the AK-47 nor the M4 (which is black, futuristic looking and American, thus, better than the Soviet piece of shit (... or the the AK at least)) manage to do so, each requiring multiple body shots to disable your opponents.

Pretty Graphs

I pulled together the ballistics data from the Wikipedia for the:

... and plotted muzzle energy, muzzle velocity and projectile mass.

The muzzle energy of various rounds.

The muzzle energy of (in Joules) 9mm, .50AE, 7.62x39mm and 7.62x54mmR rounds.

The muzzle velocity of various rounds.

The muzzle velocity (in meters per second) of 9mm, .50AE, 7.62x39mm and 7.62x54mmR rounds.

The projectile mass of various rounds.

The projectile mass (in grams) of 9mm, .50AE, 7.62x39mm and 7.62x54mmR rounds.

Discussion

You'll notice that the SVD delivers substantially more energy (3939J) than the runner up, the AK-47 (2010J), 96% more in fact. The AK-47 in turn delivers 21% more energy than the next runner up, the Desert Eagle (1666J). Admittedly, the Desert Eagle has a massive round (massing in at 21.1 grams) which does endow it with favourable penetrative characteristics, but the only round which it clearly out performs is the 9mm.

Consequently, why the world of Uncharted seems to treat the Desert Eagle as largely the equal of the SVD and the AK-47 as the equivalent (... or lesser if you consider the fact that you can't hit a damn thing with it) of the 9mm is frankly mystifying. Further exacerbated by the fact that late in the game you begin to encounter "snipers" that use Desert Eagle with laser sights to one-shot-kill you if you stand in place too long whereas the guys will assault rifles can't seem to think of anything better to do than spray bullets randomly.

In Conclusion

My crazy mental model of guns, is that a body shot will pretty much disable anybody, regardless of what you use to shoot them. So seeing the bad guys limp around like I kicked them in the shins after pegging them 2-3 times with an AK, is just kind of crazy. Admittedly, having every gun dole out one-hit-kills could make the game too easy (if the one-hit-kills only apply to your enemies) or too hard (if it applies to your character as well), inject too much realism and make you resort to stealth when you just want to race in and kill people.

The FBI Handgun Wounding Factors and Effectiveness is an interesting read and discusses the use of handguns in combat from the perspective of people who actually rely on them (namely law enforcement officers). One of the interesting observations (when discussing the tactical realities of law enforcement) notes that due to the difficulty of making effective use of handguns in combat:

[...] no law enforcement officer should ever plan to meet an expected attack armed only with a handgun.

A world in which game designers read some Box O' Truth and therefore knew that "rifles are rifles and pistols are pistols" would be a scary place indeed, but at least the uber-weapons would stop being handguns.

Fun Fact

In the 2008-2009 skills competition, Zdeno Chára won the hardest shot competition with a shot registering 105.4 miles per hour. Given the mass of a hockey puck ranges between 156 and 170 grams, the winning puck had approx 173-189J of kinetic energy. Which is just 1 - 10% shy of the muzzle energy of a .22 Long Rifle. Which, I suppose, explains why NHL goal tenders wear so much equipment.

Lorem Ipsum to Hebrew (redux)

The previous post: Lorem Ipsum to Hebrew gets a little side tracked in the technical details. There's a program to evolve text using mutation-selection cycles and a program to chart the results, but it mostly misses the point.

Questions for Evolutionists asks:

How can mutations (recombining of the genetic code) create any new, improved varieties?
(Recombining English letters will never produce Chinese books.)

This question has two problems. First, it presents a straw man: nobody contends that mutations alone result in the awe inspiring adaptations possessed by the life all around us. The vast majority of mutations are silent-site mutations and have no effect on the phenotype of the resulting creature, because the mutation occurs in a non-coding section and is never expressed. The majority of the rest of mutations are deleterious and give you cancer or Down's syndrome or something equally nasty. Only a tiny fraction of mutations have any beneficial effect on those carrying the mutation.

Mutation alone would never have produced the staggering variety of life that populates the Earth. Mutation combined with selection on the other hand can produce staggering effect on a population. Mutation introduces novel varieties into a population with each generation. While some mutants will be more fit than than their parents most will not be and some will be decidedly less so. The most fit individuals will found the next generation and beneficial mutations will slowly accumulate generation after generation producing the adaptations observed in current populations.

Second, conceptualizing mutation as moving letters around, is a complete underrepresentation of the ways in which our genome's can be mis-copied. There can be deletions, replacements, duplications of single bases or entire sections of DNA. Additionally, it is at the completely wrong level of granularity. Mutation would not occur on the letters themselves but on the bytes representing them. With only 256 bytes to choose from you can represent either Macbeth or the I Ching with ease. Likewise, using only C, G, A and T you can just as easily spell E. Coli and H. Sapiens and you can almost spell Human immunodeficiency virus, but you would be missing the U.

Conclusion

So how can mutation create new and improved variants? When acting alone, mutation would only ever improve an individual through luck. It is after all a random process and would mostly only introduce deleterious changes. When combined with selection on the other hand, together mutation and selection can produce amazing adaptations by allowing beneficial mutations to accumulate over generations.

Lorem Ipsum to Hebrew

Questions for Evolutionists asks:

How can mutations (recombining of the genetic code) create any new, improved varieties?
(Recombining English letters will never produce Chinese books.)

Lorem Ipsum to "ןגכה9'kpx%y'ךbעX]kגkVגגהiEג9*Qעfעt9fכ9עגכf' qFxה%עגה9תגכג9X' k-?%עגכהןגכה9'" in only 118 generations. Sure it's garbled gobbledygook but if you have any familiarity with Hebrew you should be able to spot kaf, ayin and tav among others. In this string there is a total of 33 Hebrew characters ranging from aleph to tav.

evolution.py is the program which produced this particular piece of gibberish. It models evolution by natural selection by successively evolving a population of phrases for two hundred generations. In the case of this particular string, fitness was calculated as a function of the number of Hebrew characters contained by an individual.

Method

Each population starts as a single copy of Lorem Ipsum. It is allowed to reproduce without the influence of any kind of selection until the population reaches 1024 individuals. At which point only the 1024 fittest individuals will be allowed to be part of the next generation.

Generation n + 1 is composed of the daughters of generation. Each individual produces two offspring and dies (binary fusion) each daughter is an inexact copy of their mother: 1 in 10 bases are replaced by a point mutation. Daughters are then decoded from utf-8 to unicode and then encoded back to utf-8, errors are ignored. The genome of the mother is recycled until a full 128 bytes are copied to the daugther's genome. Once 2 daughter cells are generated for each mother, the 1024 fittest cells are selected to be the mothers for the next generation.

Three different fitness criteria were used in three different evolutions: # Hebrew characters, # Latin Uppercase characters, # Latin Lowercase characters.

Results

In each of the following figures the absolute fitness of an individual (in terms of the # of relevant characters) is shown on the Y axis while the generation is shown on the X axis. The green dots are the most fit individuals of their generation while the blue dots are the least fit.

Scatter plot of population fitness.

Figure 1: Evolution of Lorem Ipsum when selecting for # Hebrew characters

Scatter plot of population fitness.

Figure 2: Evolution of Lorem Ipsum when selecting for Uppercase Latin

Scatter plot of population fitness.

Figure 3: Evolution of Lorem Ipsum when selecting for Lowercase Latin

Discussion

In the three figures there are a couple of important similarities:

  1. Maximum fitness reaches a fitness plateau before it reaches the theoretical maximum in any of the experiments (128 in the case of either Latin experiment or 64 in the case of Hebrew).
  2. Minimum fitness drops off for the first 10 generations in the two Latin experiments.
  3. In two of the three experiments minimum fitness after 100 generations greatly exceeds initial fitness of the population.

Plateau

This is an example of the mutation-selection balance. Essentially, after a certain point the mutation rate is introducing deleterious mutations (replacing characters relevant to fitness with characters that are not) as fast as selection removes them from the population. Thus the population's fitness reaches a plateau.

The Hebrew experiment was far more susceptible to this effect than either of the Latin experiments because each Hebrew character is represented by two bytes rather than one, the chance of a beneficial mutation is lower and the chance of a deleterious mutation is higher.

Minimum Fitness Dropoff

Lorem Ipsum is a text that is written using Latin characters as such the fitness of the founding individual is greater than zero. Since the population starts with a single individual which divides until it reaches the maximum population of 1024, individuals are not under selection for the first ~10 generations and thus deleterious mutations may accumulate without facing negative selective pressure.

Improved Minimum Fitness Over Time

Lorem Ipsum is a text mostly composed of lowercase Latin characters, as such it is not overly surprising that it shows almost no improvement over time.

On the other hand, the other two experiments show vast improvements in both minimum and maximum fitness over time. In fact the Hebrew experiment starts out with an individual of fitness 0 and results in a population of median fitness ~20 a mere 100 generations later. The Latin Uppercase experiment starts with an individual of fitness ~5 and results in a population with a median fitness of ~85 after 50 generations.

Conclusions

This experiment offers example of several interesting evolutionary effect. We've seen the mutation selection balance in play. Mutations introducing novel variants into the population. The steady increase of fitness over multiple generations. Increase in complexity of the organisms under study (Hebrew characters are represented using two bytes while Latin characters only use one).

Maybe rearranging the letters of the poems of Shakespear can never produce the I Ching, but rearranging the bytes most certainly could.