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.
Grouping photos by time: within an hour = same event. A timeline from 9am to 5pm with photos of a park at 9:02, 9:15 and 9:31, lunch at 10:20 and 10:41, a museum at 12:55 pm and 1:10 pm, and dinner at 4:40 pm. Event 1 holds the park and lunch photos, event 2 the museum photos, event 3 the dinner photo. The park photos end at 9:31 and lunch starts at 10:20, 49 minutes later, so two separate outings are filed as one event.

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 simulationWhat it means
It is an abstractionbuilding one involves removing specific details or simplifying what the real system does
It can carry biasbias can come from which parts of the real world are put in or left out, and more runs will never reveal it
It supports hypothesesa model can be run again and again, so hypotheses about the real system can be formed and refined cheaply
It can model variabilitya 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.