-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaveFunctionCollapse.py
439 lines (376 loc) · 16.4 KB
/
WaveFunctionCollapse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
from __future__ import annotations
import itertools
import os
from concurrent.futures import ProcessPoolExecutor, Future
from copy import deepcopy
from typing import Tuple, Callable, Iterator, Dict, Set, List
import numpy as np
from glm import ivec3
from numpy.random import Generator
from ordered_set import OrderedSet
from termcolor import cprint
import Adjacency
import globals
import vectorTools
from Adjacency import StructureRotation, StructureAdjacency
from gdpc.src.gdpc import Box
class WaveFunctionCollapse:
# Implementation of Wave Function Collapse inspired by https://github.com/ScholliYT/MGAIA-Minecraft
stateSpaceBox: Box
stateSpace: Dict[ivec3, OrderedSet[StructureRotation]]
lockedTiles: Dict[ivec3, bool]
structureWeights: Dict[str, float]
defaultAdjacencies: Dict[str, StructureAdjacency]
defaultDomain: OrderedSet[StructureRotation]
rng: Generator
validationFunction: Callable[[WaveFunctionCollapse], bool] | None
workList: Dict[ivec3, OrderedSet[StructureRotation]]
foundBuildings: List[Set[ivec3]]
def __init__(
self,
volumeGrid: Box,
structureWeights: Dict[str, float],
initFunction: Callable[[WaveFunctionCollapse], None] | None = None,
validationFunction: Callable[[WaveFunctionCollapse], bool] | None = None,
rngSeed: int | None = None,
):
self.stateSpaceBox = volumeGrid
if not volumeGrid.size >= ivec3(1, 1, 1):
raise ValueError('State space size should be at least (1, 1, 1)')
self.stateSpace = dict()
self.lockedTiles = dict()
self.structureWeights = structureWeights
# Setup default list of adjacencies and cell domain
self.defaultAdjacencies = self.createDefaultAdjacencies()
self.defaultDomain = self.createDefaultDomain()
self.setupRNG(rngSeed)
self.initFunction = initFunction
self.validationFunction = validationFunction
self.workList = dict()
self.firstPositions = set()
self.foundBuildings = []
self.initStateSpaceWithDefaultDomain()
if initFunction:
initFunction(self)
for index in self.stateSpace:
if not self.stateSpaceBox.contains(index):
raise IndexError(f'Index {index} not contained in state space box {self.stateSpaceBox}')
def setupRNG(self, rngSeed: int | None = None):
self.rng = np.random.default_rng(seed=rngSeed)
def createDefaultAdjacencies(self) -> Dict[str, StructureAdjacency]:
return Adjacency.omitAdjacenciesWithZeroWeight(
globals.defaultAdjacencies,
self.structureWeights
)
def createDefaultDomain(self) -> OrderedSet[StructureRotation]:
return OrderedSet(
Adjacency.StructureRotation(structureName, rotation)
for structureName, rotation in itertools.product(self.defaultAdjacencies.keys(), range(4))
)
def initStateSpaceWithDefaultDomain(self):
for index in self.cellCoordinates:
self.stateSpace[index] = deepcopy(self.defaultDomain)
self.lockedTiles[index] = False
@property
def cellCoordinates(self) -> Iterator[ivec3]:
return vectorTools.boxPositions(self.stateSpaceBox)
@property
def uncollapsedCellIndicies(self) -> Iterator[ivec3]:
for cellIndex, cellState in self.stateSpace.items():
if len(cellState) > 1 and self.lockedTiles[cellIndex] is False:
yield cellIndex
def getCellIndicesWithEntropy(self, entropy: int = 1) -> Iterator[ivec3]:
for cellIndex, cellState in self.stateSpace.items():
if len(cellState) == entropy and self.lockedTiles[cellIndex] is False:
yield cellIndex
@property
def lowestEntropy(self) -> int:
minEntrophy = len(self.defaultDomain) + 1
for cellIndex, cellState in self.stateSpace.items():
if self.lockedTiles[cellIndex] is True:
continue
entrophySize = len(cellState)
if entrophySize == 0:
return entrophySize
if entrophySize < minEntrophy and entrophySize != 1:
minEntrophy = entrophySize
return minEntrophy
def getStructureWeights(self, structureRotations: OrderedSet[StructureRotation]):
structureWeights = np.array([
self.structureWeights[structureRotation.structureName] for structureRotation in structureRotations
], dtype=float)
return structureWeights / sum(structureWeights)
def getRandomStateFromSuperposition(self, cellSuperPosition: OrderedSet[StructureRotation]) -> StructureRotation:
assert len(cellSuperPosition) > 1
# noinspection PyTypeChecker
return self.rng.choice(cellSuperPosition, p=self.getStructureWeights(cellSuperPosition))
@property
def randomUncollapsedCellIndex(self) -> ivec3:
# noinspection PyTypeChecker
return ivec3(self.rng.choice(list(self.uncollapsedCellIndicies)))
@property
def randomCollapsedCellIndex(self) -> ivec3:
# noinspection PyTypeChecker
return ivec3(self.rng.choice(list(self.getCellIndicesWithEntropy(entropy=1))))
@property
def isCollapsed(self) -> bool:
return len(list(self.getCellIndicesWithEntropy(entropy=1))) == len(list(filter(
lambda item: item[1] is False and item[0] in self.stateSpace, self.lockedTiles.items()
)))
def collapse(self) -> bool:
while not self.isCollapsed:
minEntropy = self.lowestEntropy
if minEntropy == 0:
return False
nextCellsToCollapse = list(self.getCellIndicesWithEntropy(minEntropy))
assert len(nextCellsToCollapse) > 0
# noinspection PyTypeChecker
nextCellIndex = ivec3(self.rng.choice(nextCellsToCollapse))
if len(self.firstPositions) > 0:
nextCellIndex = self.firstPositions.pop()
if len(self.stateSpace[nextCellIndex]) == 1:
continue
nextCellState = self.stateSpace[nextCellIndex]
collapsedState = self.getRandomStateFromSuperposition(nextCellState)
self.collapseCellToState(nextCellIndex, collapsedState)
if self.validationFunction:
return self.validationFunction(self)
return True
def collapseRandomCell(self):
cellCellIndex = self.randomUncollapsedCellIndex
nextCellState = self.stateSpace[cellCellIndex]
collapsedState = self.getRandomStateFromSuperposition(nextCellState)
self.collapseCellToState(cellCellIndex, collapsedState)
def collapseCellToState(self, cellIndex: ivec3, structureToCollapse: StructureRotation):
self.workList[cellIndex] = OrderedSet([structureToCollapse])
while len(self.workList) > 0:
if self.lowestEntropy == 0:
self.workList.clear()
break
taskCellIndex, remainingStates = self.workList.popitem()
newTasks = self.propagate(
cellIndex=taskCellIndex,
remainingStates=remainingStates,
)
self.workList.update(newTasks)
assert self.stateSpace[cellIndex] in (OrderedSet(), OrderedSet([structureToCollapse])), \
f'Cell should have been set to {structureToCollapse} or {OrderedSet()} but is {self.stateSpace[cellIndex]}'
def propagate(
self,
cellIndex: ivec3,
remainingStates: OrderedSet[StructureRotation],
) -> Dict[ivec3, OrderedSet[StructureRotation]]:
nextTasks: Dict[ivec3, OrderedSet[StructureRotation]] = dict()
if not remainingStates.issubset(self.stateSpace[cellIndex]):
raise Exception(
f'{cellIndex} tried to colapse a state to values not available in current superposition: '
f'{remainingStates} ⊄ {self.stateSpace[cellIndex]}'
)
if set(remainingStates) == set(self.stateSpace[cellIndex]):
# No change in states. No need to propagate further.
return nextTasks
if not self.lockedTiles[cellIndex]:
# Update cell to new collapsed state
self.stateSpace[cellIndex] = remainingStates
xForward, axis = Adjacency.getPositionFromAxis('xForward', cellIndex)
if cellIndex.x < self.stateSpaceBox.last.x and xForward not in self.workList:
nextTasks.update(self.computeNeighbourStatesIntersection(
xForward,
cellIndex,
axis,
))
zForward, axis = Adjacency.getPositionFromAxis('zForward', cellIndex)
if cellIndex.z < self.stateSpaceBox.last.z and zForward not in self.workList:
nextTasks.update(self.computeNeighbourStatesIntersection(
zForward,
cellIndex,
axis,
))
xBackward, axis = Adjacency.getPositionFromAxis('xBackward', cellIndex)
if cellIndex.x > self.stateSpaceBox.begin.x and xBackward not in self.workList:
nextTasks.update(self.computeNeighbourStatesIntersection(
xBackward,
cellIndex,
axis,
))
zBackward, axis = Adjacency.getPositionFromAxis('zBackward', cellIndex)
if cellIndex.z > self.stateSpaceBox.begin.z and zBackward not in self.workList:
nextTasks.update(self.computeNeighbourStatesIntersection(
zBackward,
cellIndex,
axis,
))
yBackward, axis = Adjacency.getPositionFromAxis('yBackward', cellIndex)
if cellIndex.y > self.stateSpaceBox.begin.y and yBackward not in self.workList:
nextTasks.update(self.computeNeighbourStatesIntersection(
yBackward,
cellIndex,
axis,
))
yForward, axis = Adjacency.getPositionFromAxis('yForward', cellIndex)
if cellIndex.y < self.stateSpaceBox.last.y and yForward not in self.workList:
nextTasks.update(self.computeNeighbourStatesIntersection(
yForward,
cellIndex,
axis,
))
return nextTasks
def computeNeighbourStatesIntersection(
self,
neighbourCellIndex: ivec3,
cellIndex: ivec3,
axis: str,
) -> Dict[ivec3, OrderedSet[StructureRotation]]:
return {
neighbourCellIndex: self.stateSpace[neighbourCellIndex].intersection(self.computeNeighbourStates(
cellIndex,
axis,
))
}
def computeNeighbourStates(
self,
cellIndex: ivec3,
axis: str,
) -> Set[StructureRotation]:
allowedStates: Set[StructureRotation] = set()
for s in self.stateSpace[cellIndex]:
allowedStates.update(self.defaultAdjacencies[s.structureName].adjacentStructures(
axis,
s.rotation
))
return allowedStates
@property
def structuresUsed(self) -> Iterator[StructureRotation]:
for cellState in self.stateSpace.values():
if cellState[0].structureName not in globals.structureFolders:
raise Exception(f'Structure file {cellState[0].structureName} not found in {globals.structureFolders}')
yield cellState[0]
def scanForBuildings(self) -> List[Set[ivec3]]:
buildings: List[Set[ivec3]] = []
newBuilding: Set[ivec3] = set()
cellsVisited: Set[ivec3] = set()
stateSpaceKeys: List[ivec3] = list(self.stateSpace.keys())
while len(cellsVisited) != len(stateSpaceKeys):
randomIndex = self.randomCollapsedCellIndex
if randomIndex in cellsVisited:
continue
if self.stateSpace[randomIndex][0].structureName.endswith('air'):
cellsVisited.add(randomIndex)
continue
scanWorkList: List[ivec3] = [randomIndex]
while len(scanWorkList) > 0:
cellIndex = scanWorkList.pop()
if cellIndex in cellsVisited:
continue
if cellIndex in self.lockedTiles and self.lockedTiles[cellIndex] is True:
newBuilding.add(cellIndex)
cellsVisited.add(cellIndex)
continue
cellState: StructureRotation = self.stateSpace[cellIndex][0]
if cellState.structureName.endswith('air'):
raise Exception(f'Wall leak found at {cellIndex} {cellState} in building {newBuilding}')
openPositions: Set[ivec3] = self.defaultAdjacencies[cellState.structureName].getNonWallPositions(
cellState.rotation,
cellIndex,
stateSpaceKeys,
)
newBuilding.add(cellIndex)
newBuilding.update(openPositions)
scanWorkList.extend(openPositions)
cellsVisited.add(cellIndex)
buildings.append(newBuilding.copy())
newBuilding.clear()
return buildings
def startWFCInstance(
attempt: int,
volumeGrid: Box,
structureWeights: Dict[str, float],
initFunction: Callable[[WaveFunctionCollapse], None] | None,
validationFunction: Callable[[WaveFunctionCollapse], bool] | None,
) -> Tuple[bool, WaveFunctionCollapse, int]:
rngSeed = (globals.rngSeed + attempt) if globals.rngSeed is not None else None
wfc = WaveFunctionCollapse(
volumeGrid=volumeGrid,
initFunction=initFunction,
validationFunction=validationFunction,
rngSeed=rngSeed,
structureWeights=structureWeights,
)
print(f'Starting WFC collapse attempt {attempt}')
isCollapsed = wfc.collapse()
return isCollapsed, wfc, attempt
def startMultiThreadedWFC(
volumeGrid: Box,
structureWeights: Dict[str, float],
initFunction: Callable[[WaveFunctionCollapse], None] | None,
validationFunction: Callable[[WaveFunctionCollapse], bool] | None,
onResolve: Callable[[WaveFunctionCollapse], None],
maxAttempts: int = 10000,
) -> WaveFunctionCollapse:
executor = ProcessPoolExecutor()
wfcResult: WaveFunctionCollapse | None = None
attempt = 1
def createFuture():
nonlocal attempt
if attempt >= maxAttempts:
executor.shutdown(wait=False, cancel_futures=True)
raise Exception(f'WFC did not collapse after {maxAttempts} retries.')
try:
future: Future = executor.submit(
startWFCInstance,
attempt,
volumeGrid,
structureWeights,
initFunction,
validationFunction,
)
future.add_done_callback(futureCallback)
attempt += 1
except RuntimeError:
print('Shutting down remaining attempts…')
def futureCallback(f: Future):
nonlocal wfcResult
if wfcResult:
f.cancel()
return
newWfcResultIsCollapsed, wfc, lastAttempt = f.result()
if newWfcResultIsCollapsed:
executor.shutdown(wait=False, cancel_futures=True)
wfcResult = wfc
cprint(f'WFC attempt {lastAttempt} HAS collapsed!', 'green')
return
cprint(f'WFC attempt {lastAttempt} did NOT collapse', 'yellow')
# Create a new future after a previous future has not resulted in a collapsed state.
createFuture()
for initialAttempt in range(1, min(maxAttempts, os.cpu_count() + 1)):
createFuture()
while wfcResult is None:
pass
executor.shutdown(wait=False, cancel_futures=True)
onResolve(wfcResult)
return wfcResult
def startSingleThreadedWFC(
volumeGrid: Box,
structureWeights: Dict[str, float],
initFunction: Callable[[WaveFunctionCollapse], None] | None,
validationFunction: Callable[[WaveFunctionCollapse], bool] | None,
onResolve: Callable[[WaveFunctionCollapse], None],
maxAttempts: int = 10000,
) -> WaveFunctionCollapse:
attempt = 0
isCollapsed = False
wfc = None
while not isCollapsed:
attempt += 1
isCollapsed, wfc, _ = startWFCInstance(
attempt=attempt,
volumeGrid=volumeGrid,
structureWeights=structureWeights,
initFunction=initFunction,
validationFunction=validationFunction,
)
if attempt > maxAttempts:
raise Exception(f'WFC did not collapse after {maxAttempts} retries.')
cprint(f'WFC collapsed after {attempt} attempts', 'green')
onResolve(wfc)
return wfc