I got paid minimum wage to solve an impossible problem using C++ (and accidentally learned why most algorithms make life worse)
https://tiespetersen.substack.com/p/i-got-paid-minimum-wage-to-solveI was sweeping floors at a supermarket and decided to over-engineer it.
Instead of just… sweeping… I turned the supermarket into a grid graph and wrote a C++ optimizer using simulated annealing to find the “optimal” sweeping path.
It worked perfectly.
It also produced a path that no human could ever walk without losing their sanity. Way too many turns.
Turns out optimizing for distance gives you a solution that’s technically correct and practically useless.
Adding a penalty each time it made a sharp turn made it actually walkable:
But, this led me down a rabbit hole about how many systems optimize the wrong thing (social media, recommender systems, even LLMs).
If you like algorithms, overthinking, or watching optimization go wrong, you might enjoy this little experiment. More visualizations and gifs included!
216
u/ZMeson Embedded Developer Jan 07 '26
This needs to be a lesson for management and their KPIs.
87
u/unknownmat Jan 07 '26
Pretty amusing, thanks for sharing.
Unrelated to C++ but this is kind of a penny-wise and pound-foolish situation that often comes up in trying to optimize real-life situations. If you are a robot with six-sigma reliability, then even small improvements can add up to significant savings given enough repetition. However, for a human, all it would take would be one or two mistakes (e.g. forgetting where you left off when you went out to lunch) to completely dominate the supposed savings of a more efficient path. Even your "friendlier" path is not really something a human could reasonably follow - it requires you to remember which bits you've covered and somehow avoiding unnecessary overlap. I suspect that rewarding locality higher than penalizing sharp turns might result in a more humanly usable solution.
55
u/def-pri-pub Jan 07 '26
In the future, you're not going to be making minimum wage. You'll be well above it.
The fact that you explore these things, write about them (very well), and do this outside your normal coursework is very good. Keep pushing.
35
u/BigJhonny Jan 07 '26
This reminds me of the time I worked as a student assistant and I had to sort 300 unordered student files in alphabetical order. I did it with merge sort, which led to me spreading out all the files over the whole hallway at the start and always merging two stacks together one after the other. Which looked very stupid from the outside. But I and my supervisor were surprised, how quickly I was done. But I was very close to running out of memory (hallway size).
8
u/perspectiveiskey Jan 07 '26
I mean this is the alignment problem in a nutshell, but you are mistaken in thinking they optimize the wrong thing. They optimize something you actually don't care for, and that is raking in money. Not your well being.
2
u/johannes1971 Jan 07 '26 edited Jan 07 '26
If you have this kind of skill, why are you sweeping floors in a supermarket?
Actually, scratch that: is this the AH in Oegstgeest by any chance?
2
u/Tamitami Jan 07 '26
I mean everything is nice what you described (in the blog post and the reddit post). I had the same thoughts when doing lawn mowering and how to optimize the paths. But could you please share your code? The algos in this case are what's the interesting thing :)
10
u/FluffyNevyn Jan 07 '26
I too write software. And all I can say to this is "KPIs". The most useless invention by people who cannot code but want to at least attempt to measure how efficient the department is operating. KPIs. Optimizing for the wrong thing, 99.99% of the time.
6
u/thefeedling Jan 07 '26
Hard to believe someone like you gets paid a minimum wage, even as a student! Nevertheless, great read
2
u/Professional_End1897 Jan 07 '26
Thank you for sharing. I enjoyed this read.
5
u/InfernoGems Jan 07 '26
Nice article :)
I’ve recently been looking into different algorithms for solving optimization problems, and came across simulated annealing too.
Even though I now understand the algorithm, I’m still surprised by how well it works, given its simplicity.
Your point about how LLMs optimize the wrong thing might be right. They’re purely optimizing the function that returns a probability distribution of the next token.
But that doesn’t incorporate whether the user actually had their problem solved for example, or whether what was written is truthful.
If those can somehow be incorporated in the error function, LLMs could be improved. But can truthfulness or usefulness be turned into a differentiable metric, so that gradient descent can be applied?
Anyway, excuse the ramblings, optimization is exciting :)
4
u/windozeFanboi Jan 07 '26
Engagement Optimized Match Making in modern games is meant to maximize your time spent in the game instead of fun.
It's not wrong per se, it's just not why you as a gamer want to actually play a game. The company made the right decision if they get you to spend more time and thus more opportunity to spend money.
3
u/Sopel97 Jan 07 '26
Ultimately, every state space simplification comes with a cost in accuracy. It's deceiving, you think you're making the problem simpler, but in the end you end up solving a completely useless one.
5
u/LongestNamesPossible Jan 07 '26
I'm very tired of nonsense hyperbolic titles like this.
2
u/These-Maintenance250 Jan 07 '26
a maze solving robot competition participant found out the same thing and won the world championship
2
u/Th1088 Jan 07 '26
I wonder if choosing a larger grid size would work as well as the turn penalty. Seems like that would prevent a lot of the tight 90/180 degree turns. A very interesting read!
2
u/kernel_task Big Data | C++23 | Folly | Exceptions Jan 07 '26
Super impressive, dude. I’m sure you’ll go far.
2
u/kaveman909 Jan 08 '26
The real optimization is figuring out which tiles you can skip mopping entirely and have management never find out. 1/2? 1/3? I think you have your next research project. Worst case: fired. Bast case: ... I don't know.
2
u/yuehuang Jan 08 '26
Sometimes making 3 right turns is faster than making 1 left turn. It was my first example where simple isn't always best.
1
u/Ameisen vemips, avr, rendering, systems Jan 08 '26
Interestingly, if you were to change it to bias for maximizing horizontal movement, you'd also be optimizing for cache locality...
1
u/die_liebe Jan 08 '26
At first I thought that you were just looking for Euler paths (Like Euler trying to sweep all the bridges in Koenigsberg), but I see the task is harder because the bridges are so wide that they have to be walked over twice. Still I wonder if you could Euler's algorithm for finding Euler paths as starting point.
Nice, entertaining post.
1
u/AssemblerGuy Jan 08 '26
You're figured out mathematical optimization, basically.
This isn't a C++ matter, it's a math matter. With optimization, you can not only do (soft) penalties, but also (hard) constraints.
If you can formulate your question as an already solved type of optimization problem (e.g. least-squares, or more general as a convex optimization problem), you have essentially solved the problem as a computer will give you very good answers.
1
u/Kautsu-Gamer Jan 08 '26
You modeled the problem wrong due wrong assumptions. For human long straight path is cheaper than same distance in shorter legs as the walking reserves energy from one step to next until you stop or change direction.
1
u/matracuca Jan 08 '26
clickbait at its finest
1
u/Responsible_Bat_9956 Jan 09 '26
ooooo interesting!
1
u/Strange_Water5256 Jan 10 '26
That's amazing, I also didn't think about it.
1
u/Professional_End1897 Jan 10 '26
btw, there's a free even coming up about how to move to Ghost. you're on Substack but I don't subscribe to Substack for political reasons. https://luma.com/mp1i5386
1
u/Significant_Fix2408 Jan 11 '26
Fantastic read. I'd argue that there is one component that almost all engineers recognise and also (at the very least subconsciously) take into account: Complexity.
It's one of the very foundational principles in problem solving (e.g. KISS). But unfortunately it is very difficult to quantify and highly subjective (the mythos of model interpretability is an interesting read).
In this case taking turns or breaking locality can both be used as proxy for complexity
1
u/pebms Jan 08 '26
>why most algorithms make life worse
Yea. That is clickbait. You used an algorithm with a particular objective function and you dislike the solution it produced because humans have to do it? Ergo most algorithms make life worse? Non sequitur.
What if a mechanical robot has to do it? Then, indeed the TSP route which minimizes distance could be optimal from an energy utilization POV if changing directions is not tough for mechanical devices. Chip designers need to use algorithms that minimize weighted average distances to decide locations of different modules on their chip given that energy and cooling systems are at a premium.
0
u/tartaruga232 MSVC user, r/cpp_modules Jan 08 '26
Ugh. White on black again. No thanks.
5
u/[deleted] Jan 07 '26
[removed] — view removed comment