169 points · by signa11 · 3y ago · 47 comments
The original author of "wave function collapse" also has "convchain" [0] which is similarly "just" using Monte Carlo Markov Chains to sample the space. It's also a kind of "staple" algorithm for physicists, theoretical computer scientists, etc but I guess hasn't caught on as much as WFC.
Note that WFC has major problems when the tile set is too constrained. There's a SO question about it [1] and I also happen to have written a small article about it as well [2]().
[0] https://github.com/mxgmn/ConvChain
[1] https://stackoverflow.com/questions/72721299/why-can-the-wav...
[2] https://www.fxhash.xyz/article/lessons-learned-from-implemen...
(
) trigger warning, it's an NFT but please don't let that stop you from readingThis is not to say that people don't do this or make compromises like running it multiple times, if it fails, but Norvig's solution is doing proper search.
For those reasons I still liked the article.
But the idea of extracting constraints from a sample images and using those to generate variants is neat.
Here's a nice presentation from him explaining it: https://www.youtube.com/watch?v=0bcZb-SsnrA
The Wavefunction Collapse Algorithm Explained - https://news.ycombinator.com/item?id=18744696 - Dec 2018 (44 comments)
The Wavefunction Collapse Algorithm explained - https://news.ycombinator.com/item?id=18742295 - Dec 2018 (3 comments)
Infinite procedurally-generated city with the Wave Function Collapse algorithm - https://news.ycombinator.com/item?id=18443311 - Nov 2018 (141 comments)
Show HN: Wave function collapse algorithm - https://news.ycombinator.com/item?id=12612246 - Sept 2016 (122 comments)
[0] https://www.gamedeveloper.com/blogs/how-townscaper-works-a-s... [1] https://news.ycombinator.com/item?id=31799818
Unfortunately I could never really get it to be fast enough. There are an enormous number of tiles in StarCraft, 15,000+ for some tilesets, and people want to generate maps of up to 256x256 tiles. This is a huge state space and under these conditions the algorithm very often gets stuck in a local minima. I've tried so many different heuristics to get it to work but it doesn't work reliably enough to be a truly useful tool in this situation unfortunately.
There are many explanations out there about how this algorithm works, more so than many other algorithms. I think the reason for that is because from hearing the basic high level idea of this algorithm most people are able to then go and implement it and then that makes for an interesting blog post (I could do the same after implementing, I think). What almost all of these blog posts are missing is how to make it fast, and how to implement back tracking efficiently. I had to try a lot of things to get to where I am now and I don't feel like I've fully explored the space of possible implementations despite trying a lot of different approaches.
Here are some example generated maps: https://media.discordapp.net/attachments/583406212272619571/... https://i.imgur.com/LK5a6jU.jpg https://cdn.discordapp.com/attachments/583406212272619571/10... https://cdn.discordapp.com/attachments/583406212272619571/99... https://cdn.discordapp.com/attachments/583406212272619571/99...
The initial tile/pixel grid starts with the tiles/pixels being all possible values.
The algorithm selects a random tile with a preference for low-entropy tiles/pixels (i.e. with a low number of possible values). The chosen tile is the set to a value from its possible values ("superpositions").
The adjacent tiles/pixels are then updated ("collapsed") based on the rules/constraits of the system (e.g. if there is a wall to the left of the tile, there must be a wall on the right edge). This collapse is propagated until there are no more tiles/pixels to update. If a tile/pixel during the collapse has a single value then it is assigned that value.
The process repeats until all tiles/pixels have a single value.
There can be cases where a collapse reaches a state where it is in an invalid state or cannnot be collapsed further. Various implementations of the algorithm will then reject the generation and redo it from the beginning -- similar to the rejection sampling you mentioned.
It would be good to be able to
a) determine in bounded time whether any solution exists at all
b) use a deterministic procedure to find a solution
"Redo from the beginning" seems like cracking a cipher by brute force.
I can't prove it but I think this is roughly the same thing as the circuit satisfiability problem, which is np-complete. So, I think the best thing you can do there is a very large exponential time bound.
b) use a deterministic procedure to find a solution
You can solve this problem deterministically with depth first search. But I found that to be pretty slow and generate not very aesthetically interesting results most of the time.
As a calculation tool, like the algorithm in this thread, sure, but not as something that really happens.
[1]: https://en.wikipedia.org/wiki/Delayed-choice_quantum_eraser
https://en.wikipedia.org/wiki/Consistent_histories
That is, there is no collapse. QM is a description of the quantum level of the world relative to our fixed sized, such that it doesn't really make sense to ask "what is really happening" since "what is really happening" is relative to what we can measure and ask from a human perspective. The probabilisitic nature is the description we have to describe systems at the quantum scale since it doesn't really make sense to ask the question what a quantum system looks like at a quantum scale, since measurement devices are inherently classical.
I guess you can say it's sorta like taking some algorithm for some problem, caching all possible inputs, and thus knowing the full solution to that problem but not really knowing the "true" algorithm that has no memory space optimizations. Some might say the problem should have a more "real" solution, but the infinite cache solution might be all we have and it doesn't mean the solution is worthless.
While this may seam unsatisfactory to some, I think QM is a very consistent description to much physics, and alternatives like LQG are inherently problematic as it violates Lorentz invariance since it chooses a privileged reference frame (the reference frame with the smallest length metric). A lot of the hate comes from some physicists who want to introduce their own theories, want to full status and glory of being the next Einstein who disrupted physics, but will never address any criticism of their theories, berate others who work on QM or string theory for not wanting to work on their framework, and essentially saying those who are working on QM/theory don't deserve their jobs nor funding.