Saturday, February 04, 2012

Movies: The Dangerous Method

"The Dangerous Method" is a movie about Carl Gustav Jung, and his complicated relationships with two people: Sigmund Freud, his colleague and mentor, and Sabina Spielrein, his patient and lover. It was not a bad movie, but I expected more of it.

While it touched upon Freud and Jung's friendship and rivalry, their diverging methods of psychoanalysis, and their eventual breakup, its primary focus was Jung's affair with Sabina. Not that it was a bad choice, given how vivid is the character of the crazy and brilliant, but mostly brilliant Sabina. Like most people in this story, she was a real, historical person, a young Russian Jewish woman who met Jung when she was mentally ill, was enormously helped by psychoanalysis, and later became an innovative psychiatrist herself. Keira Knightley's acting is a bit overdone in the "crazy Sabina" scenes, though it's amazing how such a beautiful actress contorts her face and body into such ugly poses. Overall I think she was great in this role.

Speaking of actors that were not so good in their roles: professor Aragorn! And not just because of his Lord of the Rings fame. Viggo Mortensen as Sigmund Freud just didn't compute. Every time I saw him on screen, it pulled me out of the story. Even with the "authentic" makeup, hair, and beard, Viggo looks nothing like Freud, at least in the pictures I've seen. His acting wasn't convincing either. He played Freud with all the same cliches that denote a sophisticated man of a bygone era: slow, drawn-out sentences, half-closed eyelids, a pipe permanently stuck in his teeth. It's like I've seen this character on the screen hundreds of times, and I don't even watch that many movies.

For a movie about the birth of psychoanalysis, it oddly lacks intellectual content. Considering how Jung's ideas about archetypes and collective unconscious captivated the public (even if they later fell out of use; even so, writers and artists still derive inspiration from them), they get surprisingly little mention in the movie. There is a scene that suggests that Jung's notions of anima and animus were planted in his mind by Sabina: in one of their conversations she says there is something male in every woman, and something female in every man. And so the movie removes the last reason for a viewer to believe that Jung was famous for anything other than his polyamorous lifestyle, or wacky paranormal beliefs (he thought he was capable of premonition). He is a vivid, conflicted and sympathetic character, but it's not clear what he was notable for.

A movie about Jung that didn't concern itself with any of his intellectual contributions, but only with his romance with a patient, was like... why, it's like a fantasy novel that focused primarily on an exchange rate between Elvish gold and dollar.

Then again, I shouldn't criticize the movie too much. We are talking here about historical figures that achieved a mythical power in and of themselves, and their lives cannot be squeezed into two hours of screen time. To tell a story about them requires ruthless picking and choosing.

Saturday, January 21, 2012

Sometimes a problem pounces upon your brain...

Sometimes a mathematics / computer science problem pounces upon your brain, and takes possession of it for half the day -- at least until you figure out that its general case does have a solution. Then you finally escape its clutches, feeling that you haven't accomplished much; but you've brushed off some dust of that old math/CS knowledge that sometimes gets asked at software development job interviews. So it's not all a waste.

The problem was posted on Facebook by a friend who encountered it in his engineering work.

Suppose you have tasks A, B, and C. Task A occurs nA times in a given time period, task B occurs nB times, and task C occurs nC times. Let's say nA is 7, nB is 2, and nC is 1. The time between any two tasks is the same. Let's call it a unit time interval. These tasks repeat endlessly in a loop. The problem is to arrange the tasks in such a way that the number of time intervals between any two tasks of the same type will be minimized.

It may seem simple, but... it's not.

Ideally, any two A-tasks will be separated by at most ceil((nA + nB + nC) / nA) intervals, any two B-tasks are separated by at most ceil((nA + nB + nC) / nB), and any two C-tasks are separated by at most ceil((nA + nB + nC) / nC) intervals, where ceil() is ceiling, a function that rounds up a number to the nearest integer larger than it.

I came up with an example (or a counterexample, if you will) where it is impossible to find a schedule that satisfies those constraints.

Say task A occurs 19 times in a given time period, task B 13 times, and task C 3 times. Then nA + nB + nC = 35. ceil(35/19) = 2, so A-tasks should be separated by no more than 2 intervals. B-tasks need to be no more than ceil(35/13) = 3 intervals apart. Now, if you put a C-task between two A-tasks, then B-constraint will be violated. Any segment A-A is surrounded by B's, i.e. it will be a subset of B-A-A-B, and those two B's are 3 intervals apart. Put a C in the middle, and B-tasks will be 4 intervals apart. That's greater than ceil(35/13) = 3. If you put a C between A and B-tasks, then A-constraint will be violated, because you'll have a sequence A-C-B, so an A following that sequence will be 3 intervals away from the first A. That's greater than ceil(35/19) = 2.

Of course, there are many combinations of frequencies for which it IS possible to find an optimal schedule, for example 19-11-5. Also, if the numbers are multiples of other numbers, this problem may have additional properties that may make it solvable. Then again, engineers usually don't care about exact solutions, rather than "good enough" approximate solutions. If you require that the number of intervals between two tasks of the same type exceed the optimal only a certain percentage of times, the problem becomes complex enough to be a fodder for master's thesis. I wouldn't be surprised if it has been already solved in the academic literature. I just don't know of a good enough way to search for papers or theses that may have been published on this topic.

Further thoughts: you could map this problem to a graph theory problem. Each task can be a node in a graph, and you would need to find a shortest route for visiting all the nodes -- "shortest" as determined by the costs required to visit each node. This would be the traveling salesman problem, which is NP-complete. But this proves nothing, because this particular problem might map to a special instance of traveling salesman problem that's solvable in polynomial time.

Sunday, January 01, 2012

My drafts are a continuum

I don't know if I finished the first draft of my novel. At the beginning of last year I said I was going to, but I don't know. That's because I'm no longer sure what should count as a first draft. The zeroth draft was clear. I had a beginning and an ending, but the middle lacked some chunks. Now all the parts are in place, and in the right order. Finding the right order was nontrivial. Maybe normal writers write a story in a linear fashion, as it develops in time, but that's not me. I write chunks and scenes, and wrack my brain about how to make them fit together. Which one should come before another one? The story can be told in any of the million ways.

I can't help but see stories from a software engineering perspective, not something that develops in time, but as a modular structure that occupies (mental) space. The logical way the modules fit together may not necessarily be the temporal way.

I finally worked out the order in which to string the scenes together. But I also determined I need more scenes with certain characters, because they are more instrumental to the story than I thought before. So can I really say I have Draft 1, or is still Draft 0.5? Or perhaps novel drafts should not be versioned like software releases?

I also have several short (or "short") stories I am working on, all but the newest one completed, and in various states of rewriting. One of them underwent five rewrites -- five completely different variations on the same theme with the same characters, converging on the same finale -- and this is still not the end. I need to figure out how to make the final version shorter. Might I be overthinking this just a tad?

That's the state of my writing for 2011. As far as my programming goes (and I can only speak of spare time projects, not anything I do at work), I'll spare the gentle reader an account of all false starts and stops on my coding endeavors throughout the year. It suffices to say that for the last month I've been trying to get an application I wrote to work on a Windows-hosted website. It is supposed to automate certain tasks on which I spend a lot of time, so it is important to me. To make a long story short, I found out that there are certain incompatibilities between the development environment on my laptop, and my Windows website environment, that can make it impossible to host that application. There are a couple of ways to deal with this, and I don't know which is more viable. It can also be said that when it comes to web hosting, I have champagne tastes on beer budget.

Saturday, December 17, 2011

Take the stairs, and other impossible health advice

A fire alarm rings at work. You leave your desk and properly evacuate to the parking garage. It turns out to be a drill, and 10 minutes later you are allowed back. You get a brilliant idea: you'll use this disruption to do something good for you health. You'll take the stairs up to the third floor where your office is. And maybe you should try walking up and down the stairs a few times during the day, so your butt won't meld with the chair. You don't quite remember where the stairs are in this building -- you only took them once -- but a building deputy showed them to you.

You go up to the third floor. The stairwell door won't open. It's locked. You swear the last time you tried it, it was unlocked.

You go up to the 4th (top) floor, then to the 2nd floor, and all the stairwell doors are locked. You get down to the first floor, and the door is unlocked, but it opens into a small dead-end hallway with several other nondescript, identical, unlabeled doors. You try one after another, until you find one that opens into a longer hallway, which leads you to the elevators... just in time before your claustrophobia flares up.

This is what happens in corporate America when you try to do something good for your health. It's like that in pretty much any office building. In some buildings, I heard, people are so discouraged from using the stairs, that an alarm would go off if you open the stairwell door! The stairs are to be used strictly as a fire exit.

And yet there is no shortage of health experts who tell us to incorporate small acts of fitness into our daily life, first and foremost by taking the stairs. Also, ride your bicycle to work. Uh-huh. And play Russian roulette with the cars whizzing by, and arrive to your cubicle sweaty, and delight coworkers with your post-workout aroma. None of those experts must have ever worked in corporate America, or lived in a suburb. Next thing they'll tell us to eat cake. :-)

Sunday, December 11, 2011

How come xkcd hasn't addressed this?

I often hear that engineers are literal. This typically includes software developers. I hear this even from people who work with engineers all the time -- you'd think they would take a more nuanced view. But perhaps not many people understand what "literal" means. I'm not even talking about the common, oft-ridiculed use of "literally" to mean "figuratively" -- as when somebody tells you "I literally died laughing" (and they don't have chunks of rotten flesh falling off, so they're not Undead).

Figurative, after all, is the opposite of literal. You can also say that the opposite of literal thinking is metaphorical thinking. Apparently most people don't understand how much certain intellectual activity, such as science and engineering, relies on metaphor. As an example, read this article in Wikipedia about Aspect-oriented programming, and show me even one paragraph in it that does NOT contain a metaphor.

The point is: software design, like any design, models real world problems in some kind of abstract conceptual framework. This is only possible if you think in metaphors.

I really think there should be an xkcd cartoon on this.

Saturday, December 03, 2011

When a children's store practices tough love

Some stores must be really, really sick of people stealing their shopping carts. So sick that they put the cart before the horse's customer's safety... or maybe just don't think how anti-theft devices can put customers at risk.

A case in point is an incident my significant other, Ray, termed "The Great Magnetic Shopping Cart Caper". It occurred in Babies'R'Us parking lot in Austin. Here is the email he sent to Babies-R-Us Management.



I would like to humbly express my intense displeasure at the experience of "discovery" I had with the "new" shopping cart system you have deployed at store # 7047, on November 25, 2011 at 2:35 in the afternoon. I began by having a pleasant shopping experience. On this day (the day after Thanksgiving) I parked my car right next to the Babies-R-Us, in the giant, parking area shared with Babies'R-Us's neighbor "Bed Bath and Beyond". I bought $164.51 worth of merchandise in the store, one of the items being a heavy metal "superyard" play-yard enclosure for my 5 month old son. I was able to fit this box perfectly fine in the shopping cart. The shopping cart rolled fine and smooth with my happily purchased merchandise in the store and out the door.

I was happily pushing along my Babies-R-Us shopping cart (full of merchandise) out into the beginning of the parking lot, weaving through the busy high traffic area, where there are cars moving in both directions. You have to get through this narrow car path to get to the parking rows on the other side.

To my sudden and almost fatal surprise, halfway there my cart suddenly locked up, and due to my decent momentum (I'm a man) and being caught off-guard I almost turned over the whole shopping cart and its entire contents on to the pavement.

Very luckily for me, the cars in front and behind me were paying attention and not fiddling with their cell phones, because they screeched their brakes and avoided running into me.

Aerial photograph of Babies'R'Us and Bed, Bath & Beyond parking lot
Aerial photograph of Babies'R'Us and Bed, Bath & Beyond parking lot where the incident occurred. Click for a bigger version.


Embarrassingly, I tried moving the cart back-and-forth, side-to-side to get it to move again. Nothing budged. I tried looking down into the wheels, thinking that possibly a rock/piece-of-tape/etc got caught up in the wheel or something.... I found nothing that would explain the wheels not moving.

Since I realized I was blocking all the traffic of the other customers in their cars trying to get my Babies-R-Us cart to move, I resorted to picking up all of my merchandise, including the heavy metal-based item (again, I'm a strong man) and put them down on the nearest curb.

I tried one last time to move the cart - seeing no motion, I drug the empty cart (leaving a big black skid mark from the locked wheel) over to the Babies-R-Us storefront sidewalk.

AND THEN I NOTICED a small sign INSIDE the cart (which was blocked from my reading from the merchandise I purchased), which states that the cart has a new "feature" that will lock the wheel up when trying to go beyond certain lines on the sides of the Babies-R-Us building.

(1) That would have been good to know BEFORE I parked my car, especially since after realizing the smaller area WITHIN the "painted cart-stop lines" out of the whole shopping center's shared parking area was totally full of cars, I would have not shopped at Babies-R-Us that day, and driven on.

(2) Imagine if I wasn't just "some guy" alone with a bunch of paid-for merchandise, trying to push it to my car? Imagine I was instead a mom, cart loaded full of merchandise AND a kid or baby, and the wheel unexpectedly locked on her while she was moving along? Imagine her cart falling over with a baby in it. Imagine a nearby car preoccupied on their phone while a family pushing a Babies-R-Us cart suddenly locked up in front of the car?

I THINK YOU'LL AGREE that the SAFETY of your customers is MORE IMPORTANT than the "prevention of shopping carts moving off too far".

The store immediately neighboring the Babies-R-Us, "Bed Bath and Beyond" had one of their store associates retrieving their carts. He saw my dilemma unfold, and he was so kind as to bring me a "Bed Bath and Beyond" shopping cart in which I put my Babies-R-Us merchandise, an "old-fashioned" cart that pushed... all the way to my car in the parking lot.

After I got the stuff in my car, I walked back into the Babies-R-Us store, and I went to the Customer Service desk.

I tried asking if I could speak with the store manager to explain my experience, but I was told by the associates there that the store manager wasn't available. I told the experience to them. They suggested I describe the complaint online.

I hope my time and effort in trying to help you listen to the safety of your customers does get read, and this letter is not in vain,

<name>

Hoping to remain a happy Babies-R-Us customer



So there you are. I guess the children's store is showing by example how to practice "tough love". After all, setting consequences is a common parenting advice. Only in this case it is, step outside the line, get a jolt of reality. No matter if it sends you under the wheels of oncoming car.

=======

UPDATE: A few days after submitting his feedback via Babies'R'Us website, Ray got a canned response from Babies'R'Us customer service, saying that to get help with his problem, he has to write a (paper) letter and mail it to corporate headquarters. The customer service representative did not acknowledge his problem, and did not forward it to supervisors, or to anyone who could have done anything about it.

Saturday, November 26, 2011

Girls hack Arduino

In all my programming life I never tried communicating in code with real-world objects. But now there is something to make it easy (or at least doable): Arduino. Sharon Cichelli demonstrated it at the All Girl Hack Night in October of 2011. Arduino is a microcontroller that lets you program blinky LED lights, or things that make buzzing alarm sounds, or all sorts of other electronic. It also accepts input from buttons or sensors, or anything that translates a physical action into electric pulse. You can plug things into one of its pins (there are 13 of them, if I recall), and have it "read" electric pulses from those pins, or "write" to them.

"Read" and "write" probably shouldn't be in quotes, because they are the same programming concepts as reading and writing to/from any input/output device. So, to switch on an LED light that's plugged into pin 13, you would digitalWrite(13, HIGH). As with all things digital, the function digitalWrite only recognizes on/off, or in this case, HIGH/LOW values. HIGH means the device plugged into the pin is on, LOW -- off. Simple, right? These basics allow one to sit down and program a primitive blinky light in minutes. The programming language is fairly high level. Its function names, as one can see, resemble human language -- this isn't assembly, where you push bits around with three-letter commands. It enables you to quickly do simple demos, like Sharon Cichelli did. Just as you digitalWrite to a pin to turn a device on, you digitalRead from a pin to recognize, say, a button press. Again, you pass to it the number of a pin into which the button is plugged in.

You write the code in an IDE, and with one mouse click upload it to an Arduino connected to your computer via USB.

Sharon Cichelli dictates the code that Anne types up on the laptop

Sharon Cichelli demonstrates Arduino programming



Arduino programming seems encouragingly simple at first, but as with everything, once you get deeper into it, you discover complexities. In fact, one's lack of electronics background might put brakes on the progress. Having been told that a LED light is plugged into a pin number 13, you (and by you, I mean me) might notice that the light actually has two wires, both plugged into tiny slots. Why are there two wires, and which of them is # 13, you ask. Oh, it's because the light needs to be grounded. The other wire is plugged into the grounding pin, you can ignore it for the purposes of your program. Yes, grounding is something you should have remembered from physics classes, but didn't. Then you digitalWrite to it, and it still doesn't light up -- that's because something is wrong with the hardware, and the circuit does not close as expected. Maybe the light isn't plugged in firmly enough. So, there are two dimensions to debugging Arduino code: a programmatic and an electrical dimension. As if catching just the software bugs wasn't tricky in itself!

Sharon couldn't help but demonstrate troubleshooting techniques, as things malfunctioned. Pins "floated". Still we had enough time to progress to a more advanced part of the lesson: Pulse Width Modulation. If you can only send "on" or "off" signals to a LED, how would you dim the lights, or produce nice, slow fade on / fade off effects? The fading or increase in brightness is caused by flickering very fast between on and off positions. If the light spends more time in the "off" position than "on", it appears to fade; if in on position, it grows brighter. If we graph HIGH and LOW signals versus time, where every HIGH signal is a vertical bar, we'll see that the "width" of the HIGH signal decreases (increases) over time. So that, simply put, is Pulse Width Modulation. Sharon pair-programmed with someone (sitting another person down at her laptop's keyboard, and dictating code to her) to implement the slow fade.

Besides blinking and buzzing things, you can connect Arduino to vibrating things. Erm. I mean, like a mat under a cat food bowl. A vibrating mat would scare off a cat that is afraid of vibration. This was the idea one of the girls had. How do you make a cat lose weight, if you also have a normal-weight cat, and each time one sees the other eating, she will eat too? The goal is to make the fat cat to eat less frequently than the normal-weight cat. So, this girl got an idea to put a vibrating mat under the cats' food bowl, so that when the cat steps on it, her weight would send an input to Arduino, which would trigger the vibration, scaring the cat off. The smaller cat's weight would not trigger this response. And the fat cat's weight would only do it at certain times but not others, otherwise the pudgy feline would never eat.

And if you don't have problematic cats, there is still a wide variety of applications for Halloween costumes, such as blinky eyeballs like Sharon made for her own Halloween costume).