Puzzle cube

Like many geeks, I enjoy unique puzzles, especially when the solution is mathematically interesting. (Puzzles with nicely-defined moves / positions / candidate solutions, and which require more thought than dexterity are the best.) I have a small collection of interesting physical puzzles (which might make an interesting article someday.)

A while ago, my wife found a wooden puzzle cube.  Figuring that it was inexpensive, somewhat educational, more or less nontoxic, and might keep me from bothering her for a few hours while she studied for her Pharmacy exams, she bought it for me. (Guys never really do grow up…)

The cube in a solved state.

The premise is pretty simple. Twenty-seven wooden blocks are joined together by elastic thread. At sixteen points in the chain, the thread makes a right angle. When twisted into the right configuration, the blocks can be formed into a 3x3x3 cube. This is more difficult than it seems: there are some four billion possible combinations, most of which seem to be impossible, since the blocks would overlap. At least one solution must work, since the cube was initially in solved form when bought.

I played with the cube for a while, eventually solving it again, and decided it would make an interesting problem for a genetic algorithm.

The cube in an unsolved state.

Two things are needed to make a genetic algorithm (GA) work: a way to express the solution as a bitstream (or string using a fixed alphabet of characters), and a method of evaluating a candidate solution (bitstream or string).  With these two pieces, the genetic operators of crossover and mutation (with possible others added) can be repeatedly applied to a population of solutions. Artificial selection using the fitness function is used to choose the next population.

Since at each point in the cube, four directions are possible, I settled on expressing candidate solutions as strings of characters — either N, S, E, or W.  I arbitrarily chose “North” to represent “up” (positive Y direction) wherever possible and “away” (positive Z direction) when “up” was not possible. Every string of these directions would represent a valid — if not viable — candidate solution. (The distinction is central to how Genetic Algorithms work; each candidate solution, even completely randomly-generated ones, must be valid. That is, it must be a string of the right length, drawn from the acceptable alphabet — in this case, N, S, E, or W. The solution may make no sense at all — with blocks overlapping multiple times and still not fitting into the desired 3x3x3 square — but it will be able to be evaluated, since any possible solution is “gramatically” correct.)

Candidate solutions are graded on the size of the rectangular prism “bounding box” needed to contain the resulting configuration, with smaller numbers being better. (The ideal 3x3x3 nonoverlapping solution would have a fitness of 27.)  The problem of overlap was handled by penalizing solutions which overlapped — in this case, ten blocks per overlap was chosen as an arbitrary penalty.)

I first wrote a fitness evaluation routine, and tested it on a known-good solution. It returned the expected value of 27, so I set up a naive random-string-generator algorithm which simply tried random strings, keeping the best-so-far solution. It came up with reasonably good guesses in a short amount of time, but took 21.17 hours to come up with an optimal, fitness-27 solution: SNNESESSNWWWSEESN.

The results for a random search. After more than 21 hours, it finally found it...

Next, I tried a genetic algorithm approach. An initial population of sequence strings was generated and evaluated. (The fitness function used was the same as before — the size of the bounding box needed to contain the result, plus ten times the number of overlapping cubes.) The fitness results were used to select individuals to be used to create the next generation. Uniform crossover and uniform random mutation were applied. It took a bit of tweaking to find optimal values (more on that later), but one GA run found a correct solution (although a different solution than the random one: SSNWSWSNNWEESEWSS) in 20.45 seconds (the 40th generation with a population size of 1000). A slightly different run found a third optimal solution — NWSSNNNESSWWNSENW — in 7.16 seconds (14 generations).

Optimal solution found in 14 generations (with population size 1,000). Only 15,000 candidate solutions (out of over four billion) were searched!

Those results represent GA runs where an optimal solution is found, so they’re a bit optimistic. Using the GA settings that I’ve come across so far, I’ve found that the GA tends to either find an optimal solution relatively quickly, or doesn’t find one within quite some time (if ever).

I’m currently working towards characterizing the effects of varying the mutation rate and fitness / selection criteria on the overall probability of the GA finding an optimal solution. For now, though, restarting randomized runs of the GA after a hundred or so generations pass without a solution seems to do the trick; a solution is usually found sometime in the first five or ten minutes — still a speedup of over a hundred times compared to the blind random search procedure.

Myth confirmed: Darwinian evolution does work — at least in silico.

 

 

 

Posted in BASIC, Coding, Math, Science | Tagged , , , , , | Leave a comment

Full of hot air

There is a definite trend in electronics away from the older, larger through-hole components in favor of smaller, denser (and often more complex) surface-mount components. For now, many microcontrollers and support chips are available in through-hole DIP form, suitable for breadboarding or soldering using through-hole PC boards.

The writing is on the wall, though: to work with smaller components and/or more complex components with higher pin count, surface-mount techniques and equipment are needed. For one thing, FPGAs are difficult or impossible to find in DIP form factor; the only options are to use dev boards for everything (expensive and unwieldy for finished products) or to be able to work with QFN and other surface-mount packages.

I had recently come across an old parallel-port scanner being given away at a yard sale, and picked it up. My wife thought I was crazy — but scanners need stepper motors to work, and stepper motors are useful to have and normally cost actual money. I figured if nothing else, I could always just take out the stepper and throw away the rest. I hate to see technology go to waste, though, so I resolved to salvage whatever I could from the scanner. I got the stepper motor, a few gears, a lens assembly, a cold cathode light tube and its easy-to-use high voltage supply — but the rest of the components were mostly surface mount.

I’d been thinking of getting a hot-air rework station for a while, and this made the decision for me. So many devices are thrown away or sold for next to nothing at thrift stores and flea markets. Rather than seeing these components go into the Dumpster, why not build a stock of SMT components and teach myself surface-mount techniques at the same time? I ordered a nice-but-not-too-expensive Aoyue 852A++ rework station with a vacuum pick-up tool, and set the circuit boards from the scanner aside.

Last week, the rework station arrived. I tried it out at work (we’re seriously considering getting one for a device we’re developing). Here is a video showing how relatively easy it is for a hot-air-rework newbie to recover small components (most likely logic drivers and a couple of capacitors) from a board…

Now for the next steps: classifying and reusing the recovered components in new circuits!

Posted in Electronics, Tools | Tagged , , , , | Comments Off on Full of hot air

BINAC

While walking home one day, I took a small detour to check out an interesting-looking Geocache. When I arrived at the coordinates, I found a historical marker:

 

BINAC -- possibly the world's first true computer -- was built here! (Click for larger.)

BINAC, the world’s first “commercial, electronic, stored-program, digital computer” was built and tested here in North Philadelphia. (Its inventors, Presper Eckert and John Mauchly, were also the creators of ENIAC, at the nearby University of Pennsylvania.)

That’s a lot of qualifiers for a historical marker, but nearly everything that would even remotely be called a “computer” these days is an “electronic, digital, stored-program computer.” The vast majority of them also happen to be commercial. In a real sense, BINAC was one of the first “real” computers — and probably the first one that really started to resemble the core architecture of modern computers.

 

 

Posted in Digital, Nostalgia | Leave a comment

WhirrrrrCLUNK

Dear City of Philadelphia,

I like classic technology as much as you do. Probably more, in fact. But even I know how unreliable older technology can sometimes be, especially when it most likely hasn’t been replaced in a long time — because it most likely hasn’t been produced in a long time.

Classic technology can be a way to remember the best parts of our past, and can even provide entertainment (and draw tourism dollars, like vintage steam-engine railroads can.) But when the technology in question is part of our infrastructure, perhaps a bit of preemptive upgrading is called for.

Specifically, when you walk past a traffic light timer and it makes a “whirrrrCLUNK” sound while changing from one cycle to another, this means that inside that box is a set of electromechanical contacts, a motor, and a gear train probably old enough and curmudgeonly enough to run for Congress. Motors, gears and contacts — having moving parts — wear out. Most likely (hopefully) the light is designed to fail into one valid position or the other — or at worst, an all-red condition — but this kind of design is why “stuck” lights happen. The whole thing could be replaced, at low cost, with a microcontroller and FET board. Heck, making a working traffic light system out of an Arduino and custom shield would be a suitable project for an undergraduate course in power electronics.

Let’s swap these things out and donate them to the “Please Touch” museum and local high-school classrooms so kids can see what vintage electromechanical technology looks like. Designed properly, a digital solution (as no doubt is used in new installations) would not only be much more reliable, but could allow for timing variations based on the time of day or day of the week, traffic flow, and weather conditions. Given an Internet connection (conveniently available with another popular shield, in the case of an Arduino solution), it could even coordinate with other nearby traffic lights and help smooth the flow of traffic.

But this is Philadelphia. We can’t even keep people from smoking in our subway stations. I can’t imagine traffic light reliability is high on anyone’s agenda. Maybe someday.

Sincerely,
Your friendly neighborhood Paleotechnologist

 

Posted in Arduino, Digital, Digital Citizenship | Leave a comment