Notes on a New World Order

Would you like fries with your Universe?

risk-calculator.py
2008-11-12 18:22

Comments

[Game of World Domination] battle probability calculator. A python script to calculate the success of conquering a territory.

Download

#!/usr/bin/env python

from __future__ import division
import sys

def cross(*args):
    if not args:
        yield []
        return
    for i in args[0]:
        for n in cross(*(args[1:])):
            yield tuple([i] + list(n))

# http://linux.derkeiler.com/Mailing-Lists/Fedora/2005-11/4432.html
def combinations_r( list_of_items, number_of_picks ):
    k = number_of_picks
    n = len(list_of_items)
    if k==0 or n==0:
        return
    b = [0]*k # make list [0,0,0,....,0] - of length k
    bdone = [0]*k
    while True:
        choice = [list_of_items[i] for i in b]
        yield tuple(choice)
        # Modify b in place (an addition operation in base-n)
        for i in range(k):
            b[i] += 1
            if b[i] < n:
                break # break our of for loop, no need to carry
            b[i] = 0 # carry
        if b == bdone:
            break # break out of while loop, no more choices left
    return

RANGE = range(1,7)

DIE_PROB = 1/max(RANGE)

SINGLE_DIE = list((x,) for x in RANGE)
TWO_DICE = list(combinations_r(RANGE, 2))
THREE_DICE = list(combinations_r(RANGE, 3))

dice_rolls = {
    1: SINGLE_DIE,
    2: TWO_DICE,
    3: THREE_DICE,
}

def risk_wins(attack, defense):
    at_victory = 0
    de_victory = 0
    for at,de in zip( sorted(attack, lambda x, y: y-x),
                      sorted(defense, lambda x, y: y-x) ):
        if at > de:
            at_victory += 1
        else:
            de_victory += 1
    return at_victory, de_victory


def make_probability(nattack, ndefense):
    attacker = dice_rolls[nattack]
    defender = dice_rolls[ndefense]

    probs = {
        (2, 0): 0,
        (1, 0): 0,
        (1, 1): 0,
        (0, 1): 0,
        (0, 2): 0,
    }
    values = list(cross(attacker, defender))
    for at_roll, de_roll in values:
        at, de = risk_wins(at_roll, de_roll)
        probs[(at,de)] += 1

    l = len(values)
    for k in probs:
        probs[k] /= l

    return probs

def calculate_round(nattack, ndefense):
    probs = make_probability(nattack, ndefense)
    diffs = {}
    for k, v in probs.items():
        at, de = k
        diffs[(-de, -at)] = v
    return diffs

def decision_sum(decision):
    ats, des = 0, 0
    cum_prob = 1.0
    for (at, de), prob in decision:
        ats += at
        des += de
        cum_prob *= prob
    return ats, des, cum_prob

def make_decisions(nattack, ndefense, iterations):
    decisions = []
    from pprint import pprint
    for i in range(iterations):
        if not decisions:
            decisions = [ [(o, p)] for o, p in calculate_round(min(nattack-1, 3), min(ndefense, 2)).items() if p != 0.0]
            continue
        for decision in decisions[:]:
            at, de, _ = decision_sum(decision)
            try:
                round = calculate_round(min(nattack+at-1, 3), min(ndefense+de, 2))
            except (KeyError, AssertionError):
                continue
            for outcome, prob in round.items():
                if prob == 0.0:
                    continue
                try:
                    decisions.remove(decision)
                except:
                    pass
                decisions.append(decision + [(outcome,prob)])
    return decisions

def calculate_outcomes(nattack, ndefense, iterations):
    decisions = make_decisions(nattack, ndefense, iterations)
    results = []
    for decision in decisions:
        ats, des, cum_prob = decision_sum(decision)
        if cum_prob == 0.0:
            continue
        results.append( (nattack+ats, ndefense+des, cum_prob) )
    assert str(sum(prob for na, nd, prob in results)) == "1.0"
    return results

def print_outcomes(results, n):
    winp = 0.0
    losep = 0.0
    draws = {}
    for nat, nde, prob in results:
        if nat == 1:
            losep += prob
        elif nde == 0:
            winp += prob
        else:
            if (nat, nde) not in draws:
                draws[(nat,nde)] = 0
            draws[(nat,nde)] += prob
    draws = [ (nat, nde, prob) for (nat, nde), prob in draws.items() ]

    print "Chance of victory in %d battles:               %3.2f%%" % (n, winp*100)
    print "Chance of defeat (1 remaining) in %d battles:  %3.2f%%" % (n, losep*100)
    for at, de, prob in draws:
        print "%3.2f%% chance of:" % (prob*100)
        print "\tAttacker remaining: %d" % at
        print "\tDefender remaining: %d" % de

def print_result(probs):
    for result, prob in probs.items():
        if prob == 0.0:
            continue
        at, de = result
        print "Attacker Loses: %d, Defender Loses: %d (probability %1.2f)" % (de, at, prob)

def testmain():
    for at, de in cross(range(1,4),range(1,3)):
        print "------------------------------------------------------------------------"
        print "  Attacker: %d dice        Defender: %d dice" % (at, de)
        print "------------------------------------------------------------------------"
        print_result(make_probability(at, de))

def main():
    at, de, n = int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3])
    print "Attacking Units: %d\t\tDefending Units: %d\t\t# of Battles: %d" % (at, de, n)
    print_outcomes(calculate_outcomes(at, de, n), n)

if __name__=='__main__':
    sys.exit(main())

On Open Minds and the Value of Information
2008-10-29 00:00

Comments

I came upon a thought recently, while wandering through my brain, and I feel like I should write it down to clarify and crystallize it. It's not fully formed, and is just a notion; I have no evidence to back it up.

Phrased as a question, it looks something like this:

Do skeptics value information less than the more open-minded?

To elaborate, I wonder if the nature of skepticism is to create a value judgment about information in general — that information is, by itself, worth very little, any one piece of information is only of trivial value. Then, I posit, it might be the case that the open-minded place a higher value on information; any piece of information is valuable and important in isolation. The open-minded put a value on knowing, regardless of what is known.

I ask these questions, and wonder about these things, primarily because it seems to me skeptics reject information too easily. I'm not saying that skeptics should reject information that is incorrect, or wrong, or <insert-reason-here>; It seems to me the default assumption of skeptics (that assertions are false) leads to the undesirable quality that skeptics say, "Prove it to me," rather than, "I must investigate."

Again, this is not a fully formed idea, but a sketch — a fragment. Do skeptics (especially hard-line skeptics) value information differently than more traditional viewpoints might suggest?

Possibly Asked Questions
2008-10-06 10:46

Comments

What "New World Order" is this? And why do I have to take notes?

I suppose I should explain myself, or at least my nomenclature — it's not entirely clear what I mean.

From the beginning this website was a phoenix, rising from the proverbial ashes (of a hard disk failure). A site reborn, with new found purpose. (Or lack of purpose.)

I set out to create a website like used to be the norm. Well written (alright, who am I kidding — written with capitalization and spelling), eclectic, but above all, interesting. I set out to create something that was more artifice than diary, more journalistic than blogging.

I have so far failed, by my own admission (as well as anyone else who's nice enough to tell me the truth), to keep the promise to which the name would hold me. it's a grandiose promise of ideal rhetoric and deep thought, with not a little cynicism and practicality thrown in. ("Would you like fries with your universe?" is not exactly the next great rallying cry.)

So, what is this "New World Order" I'm alluding to? The one my vision of which I wish to share, that holds such promise and hope?

I don't know what it looks like, or who to get there, but the feel of it is what I long for so deeply.

Ever since I read William Gibson's Neuromancer I've had a longing for a world with that kind of freedom. While Gibson's work is often seen as a critique of contemporary issues, it has sparked something deep within many to bring parts of his vision into reality. (Namely among technologists to bring the technology he portrays from speculative fiction into hard science.)

What I long for is not the dystopic social structure, the rule of corporations, or even the widespread use of cybernetics and the mutability of our human existence. Instead I long for a time when technology is really just a part of the human experience, considered part of our humanity rather than separate and opposite.

This really is the romantic in me (in the rather outmoded sense of the word) trying to reconcile the aesthetic and emotional impact and power of technology with humanity. As a technologist (of a sort), I cannot help but see the discontinuities between human and machine. In fact, it is often my task to break down those discontinuities, to smooth over technology so it is ready for "the masses."

I see the same problems and misunderstandings all the time, the same discontinuities between humanity and machine. Between the human experience we know so well and the machine experience we are only beginning to understand. And I cannot help but to think there must be a better way. that the fluid relationship shown in William Gibson's work can exist — perhaps even must emerge from the progression of technology — I want it, but we do not know if it is fantasy or future yet.

I want to find that relationship, that fluidity with on of humanity's few unique gifts — the objects of our creation. We need to bring our creative power back to ourselves, to reincorporate that which has for so long been associated only with unfeeling, callous, cold, industrial dominance, and corporate greed.

Computers and computing have opened the eyes of many to what might be possible, given them a glimpse of the freedom and power that lies at the heart of the ability to bend the world, the very laws of nature to out will. But it is only the beginning.

Without diving past the unfathomable Singularity, I don't know what will happen, whether we can build th world I long for so desperately. I do know that I do not fear the Singularity, nor the reckless advance of technology. We must be wary of the existential risks such power holds, but even so it seems humanity's only hope for a truly New World Order — not an utopia, but a substantively different world — lies in the hands of technologists.

We must not only be the change we wish to see in the world, we must build the world to our vision of how it ought to be. This is not a single task, but a lifestyle, a purpose for being — the resurgence of the DIY ethic speaks to the hunger to shape our world.

We must make our New World Order, it will not come by revolution.

Wait... what?

The name of the site notwithstanding, this is a place for me to put interesting things. The nominal plan was to have articles and essays, along with maybe a bit of fiction, and some technical crunch because I am a technologist.

My idea was that the site become a sort of idea repository, not necessarily a complete picture, just fragments in which you might glimpse the whole (like a hologram).

Well, what happened?

Not much. I'm busy, it happens. I haven't felt motivated to write in a long time. I've got lots of projects that don't really go anywhere, left half-unfinished as a reminder that I'm not nearly as awesome as I'd like to be.

I'll get back to some of it, eventually. Until then, there's not much to see here. — There is something though, and as I find time and interest I'll write it up and let it out into the wild internets.

So you say you want a Revolution...
2008-09-25 00:00

Comments

With all the fuss and politicking around the new (September 2008) rapidly approaching presidential election, many people are feeling like new leadership isn't going to fix anything. The problems we face seem endemic to the whole system of government we have.

(At least that's the explicit frustration I've heard from one person I know, and many other people I talk to seem to agree. But I don't really get out much so this opinion may be based on the intelligent-technical-college-student culture and not something more general. — he, at least my biases are fairly obvious.)

Anyway, it seems like the current governmental strucutre is brokne or at the very least malfunctioning. If, for a moment, we assume that is true, and some other system would be superior, then we have a few things we need to know:

  • What system is superior, and how?
  • In what manner can the new system be instituted, or what manner can it not be instituted?

To answer the first question requires answering a lot of other questions, at the very least, identifying the superior system.

The second question is somewhat easier to break apart because we only need to know about the current system; though, it is possible the superior system's specifics will inform the precise answer (or the optimal answer).

The tried and true, old school, tested and respected method of changing out governmental systems is, of course, revolution.

But revolution is the brute force approach, it's hard, messy, and inelegant — if romantic. It also doesn't work very well "at scale."

The United States military is one the most dominating forces in the world. When both sides of a revolution had muskets, it's one thing. When one side has hunting rifles and the other side has air support — The side the the air support wins.

So straight revolution is out, unless the military is leading the way, in which case, by historial example, things have probably Gone Horribly Wrong.

The next most obvious thing to do is to have the system change itself. Revolution from within, so to speak. This, of course, relies on the system being somewhat functional, and not broken in the very particular manner of trying to keep the power it has at all costs. That last bit, unfortunately, is usually a symptom of a system that needs changing.

If you go the route of infiltrating the system until you have enough control to enact the changes you want — you've just created a totalitarian state, just like the fascist leader of the nineteen thirties and forties. This may be acceptable if there is a single restructuring and the totalitarian control goes away; but again, history tells us that doesn't actually happen.

We (the proto-revolutionaries) could try to make the current system obsolete. We provide a compelling alternative to everything the system currently provides. Then the system gets replaced effectively, but not on paper. Eventually, when everyone realized the old system is not longer necessary the old system can be removed.

Again, there are a host of problems with this plan, the the least of which is the issue of providing services in competition with your government's while not being a recognized sovereign power.

The last option, the easiest of all, is to simply write about the change,s what you want, what needs to happen. Nothing will actually change, but you do get the melancholy satisfaction of saying "I told you so," when someone complains about the system.

I don't think there's an easy answer to find for changing the status quo. You need to look at your agenda, timetable, and resources to find the best option. If you've figured out the superior system — go to it, maybe we can avoid an ungraceful all from out decaying superpower status. Instead, what?

Go buck the system, start a revolution; but remember you New World Order is standing ont he shoulds of the giant before you.

And have a plan!

Google App Engine Pylons Authentication Decorator
2008-05-15 00:00

Comments

I've recently been playing with Google's latest strange (ad)venture, Google App Engine. I've been addicted to python for a while, and have been playing with some of the web frameworks like TurboGears and pylons. So I jumped on the opportunity to write something I wouldn't have to worry about getting slashdotted for. That is, if I could get slashdotted (or dugg, or whatever).

In any case, it's a interesting framework, the datastore is different from anything you've probably worked with before, but is definitely worth a look, if only to expand your mind.

But, I just recently started writing an application with the App Engine SDK using pylons, and had a need to restrict access to certain pages of the application to logged in, or administrative users. Google's user API allows this fairly easily, but I wanted something nice and simple, so I wrote a decorator for the controller actions.

  1. from decorator import decorator
  2. import google.appengine.api.users as users
  3.  
  4. @decorator
  5. def reqire_user(f, *args, **kws):
  6. if users.get_current_user() is None:
  7. redirect_to(users.create_login_url(request.path_info))
  8. else:
  9. return f(*args, **kws)
  10.  
  11. @decorator
  12. def require_admin_user(f, *args, **kws):
  13. if users.get_current_user() is None:
  14. redirect_to(users.create_login_url(request.path_info))
  15. elif not users.is_current_user_admin():
  16. return render('/need_admin.mako')
  17. else:
  18. return f(*args, **kws)

Free to use, put it in the public domain, no need to credit or anything. Just thought I'd throw this out there for all those people who are new to python and might not think of this solution first.