Wave function collapse: Difference between revisions

→‎{{header|J}}: another small optimization
(→‎{{header|J}}: small optimization)
(→‎{{header|J}}: another small optimization)
Line 9:
Implementation:<lang J>tiles=: 0,(|.@|:)^:(i.4)0,1 1 1,:0 1 0
wfc=: {{
adj=. y#.y|"1(y#:,i.y)+"1/0,<:3 3#:1 3 5 7
horz=. ({."1 -:"1/ {:"1) m NB. horizontal tile pairs
vert=. ({."2 -:"1/ {:"2) m NB. vertical tile pairs
id=. <@I.1,~=i.#tiles NB. identity constraint
north=. <@I.1,~vert NB. adj 1 constraint
south=. <@I.1,~|:vert NB. adj 7 constraint
west=. <@I.1,~horz NB. adj 3 constrint
east=. <@I.1,~|:horz NB. adj 5 constraint
allow=. id,north,west,east,:south
i=. (?#m) (?#i)} i=. ,y$_1
while. #todo=. I._1=i do.
Line 34 ⟶ 33:
<code>adj</code> indexes into <code>i</code> -- for each item in <code>i</code>, <code>adj</code> selects that item, the item "above" it, the item to the "left" of it, the item to the "right" of it and the item "below" it (with scare quotes because the tile represented by <code>i</code> "wraps around" on all sides). And, <code>allow</code> lists the allowable tiles corresponding to each of those <code>adj</code> constraints.
 
To build <code>allow</code> we first matched the left side of each tile with the right side of each tile (cartesian product) forming <code>horz</code> and similarly matched the tops and bottoms of the tiles forming <code>vert</code>. Then we built <code>id</code> which selects only the tile itself (or all tiles, for <code>i=_1</code>),build <code>north</code> which limits tiles based on the tile above it, and so onsimilarly for <code>west</code>, <code>east</code>, and <code>south</code> (when the adjacent tile is a <code>_1</code> tile, no limit is imposed).
 
Once we're set up, we drop into a loop: <code>todo</code> selects the unchosen tile locations, <code>wave</code>lists for each of the unchoosen tiles (for each <code>todo</code> value in <code>i</code> we select the tile and its adjacent locations and find the set intersection of all of those), <code>entropy</code> counts how many tiles are eligible for each of those location, and <code>min</code> is the smallest value in <code>entropy</code>. <code>ndx</code> is a randomly picked index into <code>todo</code> with minimal entropy and for that location we randomly pick one of the options and update <code>i</code> with it. (When there's only one option remaining, "randomly pick" here means we pick that option.)
6,962

edits