Wednesday, January 15, 2014

Tuesday, January 7, 2014

Pattern Matching Constants in CLIPS

In CLIPS, there are three basic templates for matching a constant in a rule pattern. The constant, in this case 387, can be placed directly in the pattern as a literal constraint:
(defrule rule-387
   (data (value 387))
   =>)
A predicate constraint can be used to evaluate an expression testing for equality:
(defrule rule-387
   (data (value ?x&:(eq ?x 387)))
   =>)
A test conditional element can be used to evaluate an expression testing for equality:
(defrule rule-387
   (data (value ?x))
   (test (eq ?x 387))
   =>)
An empirical analysis can be performed to determine which template is most efficient as the number of rules and distinct constants increase. A common set of constructs will be used with each group of rules to trigger pattern matching as a single fact is repeatedly modified:
(deftemplate data
   (slot index)
   (slot value))

(deffacts start
   (data (index 1000000) (value 100000)))

(defrule loop
   (declare (salience 1))
   ?f <- (data (index ?i&~0))
   =>
   (modify ?f (index (- ?i 1)) (value (- ?i 1))))
Using CLIPS 6.24 to run the common rules in conjunction with groups of 1, 100, 400, 700, and 1000 rules using each of the three templates produces the following results.



The literal constraint is the most efficient, which is what you’d intuitively expect after an examination of the code. It directly compares a value in a fact to a value stored in the pattern network.

The predicate constraint and test conditional element are less efficient as they require a function evaluation. The predicate constraint is slightly more efficient because it’s evaluated at an earlier stage of pattern matching than the test conditional element. If all of the referenced variables in a predicate constraint are bound within the pattern, then the evaluation can be performed in the pattern network. Test conditional elements and expressions containing variables bound in other patterns are evaluated in the join network, which is primarily used to unify variable bindings across multiple patterns.

CLIPS 6.30 provides significantly better performance for literal constraints:



In situations where multiple patterns satisfy the criteria for sharing, literal constraints are not evaluated one at a time. Instead the next node in the pattern network is determined using a hashing algorithm which requires just a single computation regardless of the number of literal constraints. So for this particular benchmark, the execution time does not increase for literal constraints as the number of rules and distinct literals is increased.

Bottom line: regardless of which version of CLIPS you’re using, use literal constraints rather than predicate constraints or test conditional elements.

The code for these benchmarks is available here.

Saturday, December 28, 2013

Life is a Highway



“It doesn’t matter where you start the road is just as long / It’s not the destination it’s the journey makes you strong” (Gill, Janis and Schlitz, Don. "Uphill All the Way”. Buffalo Zone. Sweethearts of the Rodeo).

Friday, December 20, 2013

Tuesday, December 17, 2013

Chinese Marketing



I have no idea what “this kind of beer” is, but apparently it’s very popular here.

Friday, December 6, 2013

Wednesday, December 4, 2013

Designed by Apple

Apple’s Designed by Apple video is a tad pretentious, but I largely agree with its theme. Good design, however intangible, adds value to a product whether it’s greater productivity from software seamlessly integrated with hardware or the delight of using a device that just works. I can’t tell you how much the comfort in a comfortable chair is worth, but I know it’s worth something.

To its credit, Apple has a pretty good track record of designing great products, but sometimes they completely blow it.

Take the Airport Time Capsule.

Seriously, take it; I don't want mine anymore. In fact, I wish Time Capsule had a feature that allowed me to go back in time and reconsider purchasing it.

After a recent upgrade mishap, I had to restore my computer’s data from my Time Capsule (which is basically a WiFi base station combined with a hard drive that allows you to wirelessly backup your computer’s data using Time Machine, Apple’s incremental backup software). After letting the process run overnight, this was the amount of progress that had been made:



Seriously? Five days to restore around 300 GB of data? And to make things even worse, there was another 200 GB of data that needed to be separately restored from Time Capsule. Even after a trip to the Apple Store for advice, it took another four full days to completely recover everything.

Five wasted days, all because of poor design decisions.

Apple’s biggest mistake was making this device in the first place. Wireless backup for the amount of data on a computer is just not fast enough to be practical without annoying drawbacks. Even Apple recommends that the initial backup of your computer be done using an ethernet cable because it “may take overnight or longer, depending on how much data you have.”

For the price of a 3 TB Time Capsule you can instead buy unobtrusive 1 TB USB powered hard drives, one each for up to four computers. Time Machine works significantly faster when directly connected to an external hard drive. So much faster, that after my own experience I draw a blank thinking of a situation where I’d recommend Time Capsule.

I thought I’d set mine up efficiently when I originally bought it. It was directly connected via an ethernet cable to my iMac, which I use daily for most of my work, and I planned on backing up my MacBook, which I use infrequently, connecting via WiFi.

I was wrong. Even though there was a direct connection from the iMac to Time Capsule, it was using a much slower WiFi connection to transfer data. Eventually I was able to configure Time Capsule to use the ethernet cable, but that was after four days of waiting for restores to complete.

It should have just worked. I don’t care how Time Capsule is configured, if it’s connected by a cable with a faster transfer rate, it needs to use the damn cable.

Time Capsule is both a WiFi base station and a backup device. If it fails as a WiFi base station, I can easily replace it with another WiFi base station. But if it fails as a backup device, I have no recourse. I can’t easily replace the data stored on the device.

The design of the device should have made creation and recovery of a backups fast and dead simple. Zero configuration. If that meant fewer options in configuring it as a WiFi base station, that’s the call that should have been made. Instead, I had to browse internet forums looking for hints on how to correctly configure the device. Even after managing to get my data restored, I still have no idea how to configure it properly for the manner I originally intended to use it.

Finally, even the smallest details matter in design. Time Machine warns you if it hasn’t been able to perform a backup for an extended period of time. It should also warn you if it detects a Time Capsule connected by an ethernet cable that it’s not correctly configured to use. I doubt that’s technically complicated, but even if it is, here’s something that’s even simpler. During the four days I was waiting for my data to be restored, this message should have been displayed:
Restoring “Giganto” via WiFi on the disk “Giganto”
Just two small words would have helped me to figure out why something that should just work wasn’t working.

Here’s what Steve Jobs had to say about design:
“That's not what we think design is. It’s not just what it looks like and feels like. Design is how it works.” — New York Times, The Guts of a New Machine, 2003
Apple, the way your Time Capsule works sucks.