Heuristics, Undecidability and Simulation
When the exact answer is out of reach
Some problems have no efficient algorithm. Nobody knows a fast way to solve them, and past a certain input size the exact answer will not arrive while it is still of any use. When that happens the practical step is to stop asking for the best answer and start asking for a good one.A heuristic is what you reach for instead. It hands back the guarantee of an optimal solution, and it is chosen precisely when the methods that would provide that guarantee are impractical.
Decidable and undecidable
A decision problem is one whose answer is yes or no. Is this number even? Is this word in the dictionary? Does this list contain a duplicate?A decision problem is decidable when some algorithm exists that always answers it correctly, on every input it could be handed. Deciding whether a number is even is decidable, and so is deciding whether a list of a million names contains a duplicate.
A problem is undecidable when no such algorithm exists.
This is not a gap in anyone's knowledge that better work will close, and it is not a job waiting for a cleverer programmer. Alan Turing, considered by many to be the father of computer science, proved that undecidable problems exist. The shortfall is permanent.
Simulation
A computer simulation is a mathematical model of a real-world or physical system, run on a computer so that the system's behavior or outcome can be predicted.A simulation lets a phenomenon be investigated without the constraints of the real world, so that inferences about the real thing can be drawn from how the model behaves.
Simulations earn their place when the real event is impractical to experiment on: too big, too small, too fast, too slow, too expensive or too dangerous.
A car maker can drive a new bumper design into a wall at fifty miles an hour inside a model as often as it likes, for the price of the electricity. Doing it for real costs a car every time. A galaxy takes billions of years to form, which rules out watching one no matter what the budget is.
| Property of a simulation | What it means |
|---|---|
| It is an abstraction | building one involves removing specific details or simplifying what the real system does |
| It can carry bias | bias can come from which parts of the real world are put in or left out, and more runs will never reveal it |
| It supports hypotheses | a model can be run again and again, so hypotheses about the real system can be formed and refined cheaply |
| It can model variability | a real system does not repeat itself exactly, and random number generators can give a model the same kind of variation |
The program below is a small simulation of a cup of tea cooling in a room. Its state is two values: the temperature
temp and the minute count minute. Each minute the tea loses a fifth of the gap between its temperature and the room's, rounded down to a whole degree. Run it, then change it and run it again:• Change
room to 35, for a hot day.• Add
temp = temp - 2; on a new line just below while (temp > drinkAt) {, for someone blowing on the tea.• Change 90 to 100, for tea poured straight from the kettle.
let temp = 90;
let room = 22;
let drinkAt = 50;
let minute = 0;
while (temp > drinkAt) {
temp = temp - Math.floor((temp - room) / 5);
minute += 1;
console.log("After minute", minute, "the tea is at", temp);
}
console.log("Cool enough to drink after", minute, "minutes");
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.