-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.py
579 lines (423 loc) · 14.3 KB
/
algorithm.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File name: algorithm.py
# Description: Python implementation of the Polyomino contraction algorithm
# Author: irreq
# Date: 23/04/2023
import numpy as np
import math
"""Documentation
This program provides an algorithm for contracting polyominos with neighbor conservation.
The pseudo code works as following:
1. Identify contractable parts
2. Find allowed contractions and rotations
3. Find contraction with lowest resulting general distance
4. Contract and repeat the process
This is a naive implementation that will take first best contraction and contract. It does not
have the capacity to find moves that will generate more efficients folds in the future. Therefore,
try enabling and disabling `ALLOW_DIAGONAL_NEIGHBORS` to see if it contracts more efficient.
"""
TARGET = 1 # What to fold, as binary
ALLOW_DIAGONAL_NEIGHBORS = True
def get_nearby(x: int, y: int, allow_diagonal=ALLOW_DIAGONAL_NEIGHBORS) -> tuple[tuple]:
"""
Return a tuple of four tuples, where each inner tuple contains two integer values
representing the coordinates of a nearby point relative to the input point (x, y).
Parameters:
x (int): The x-coordinate of the input point.
y (int): The y-coordinate of the input point.
Returns:
tuple of tuples: A tuple containing four tuples, where each inner tuple contains two
integer values representing the coordinates of a nearby point relative to the input point.
The nearby points are defined as:
- (x, y+1) which is one unit above the input point
- (x-1, y) which is one unit to the left of the input point
- (x+1, y) which is one unit to the right of the input point
- (x, y-1) which is one unit below the input point
Example:
>>> get_nearby(0, 0)
((0, 1), (-1, 0), (1, 0), (0, -1))
"""
if allow_diagonal:
return (
(x-1, y+1), (x, y+1), (x+1, y+1),
(x-1, y), (x+1, y),
(x-1, y-1), (x, y-1), (x+1, y-1)
)
else:
return (
(x, y+1),
(x-1, y), (x+1, y),
(x, y-1),
)
def rotate_points(points: list[tuple], origin: tuple, angle: float) -> list[tuple]:
"""
Rotate a list of points around a given origin point by a specified angle in degrees.
Parameters:
points (list of tuples): A list of tuples, where each tuple represents a point to be rotated.
origin (tuple): A tuple representing the origin point around which the points should be rotated.
angle (float): The angle (in degrees) by which the points should be rotated.
Returns:
list of tuples: A list of tuples representing the rotated points.
Example:
>>> rotate_points([(1, 1), (2, 2), (3, 3)], (0, 0), 45)
[(1, 1), (0, 2), (-1, 3)]
"""
radians = math.radians(angle)
cos_val = math.cos(radians)
sin_val = math.sin(radians)
# translate points so that a is at the origin
translated_points = [(p[0]-origin[0], p[1]-origin[1]) for p in points]
# rotate points
rotated_points = [(p[0]*cos_val - p[1]*sin_val, p[0]*sin_val + p[1]*cos_val) for p in translated_points]
# translate points back to their original position
final_points = [(int(round(p[0]+origin[0],1)), int(round(p[1]+origin[1], 1))) for p in rotated_points]
return final_points
def mean_distance(points: list[tuple]) -> float:
"""
Calculate the mean distance between all pairs of points in a given list.
Parameters:
points (list of tuples): A list of tuples representing the points for which the mean distance should be calculated.
Returns:
float: The mean distance between all pairs of points in the given list.
Example:
>>> mean_distance([(1, 1), (2, 2), (3, 3)])
1.6329931620475584
"""
n = len(points)
total_distance = 0
for i in range(n):
for j in range(i+1, n):
distance = math.sqrt((points[i][0] - points[j][0])**2 + (points[i][1] - points[j][1])**2)
total_distance += distance
mean_distance = total_distance / (n*(n-1)/2)
return mean_distance
def find_movements(points: list[tuple]) -> list[tuple]:
free = []
def is_shape(enumerations) -> bool:
"""
Determines if a given polyomino shape (represented as a set of positions) matches any of the given enumerations.
Parameters:
enumerations (list of sets): A list of sets, each representing a possible polyomino shape as a set of positions.
Returns:
bool: True if the given shape matches any of the enumerations, False otherwise.
"""
is_found = False
for enum in enumerations:
tmp = True
for pos in enum:
if pos not in points:
tmp = False
break
if tmp == True:
is_found = True
break
return is_found
for (x, y) in points:
# Filter immovable parts
if is_shape(
(
# ((x-1,y), (x+1,y)),
# ((x, y+1),
# (x, y-1)),
((x-1, y),
(x-1,y-1), (x,y-1)),
(
(x-1,y-1), (x, y-1), (x+1,y-1)),
( (x+1,y),
(x, y-1), (x+1,y-1)
),
(
(x+1,y+1),
(x+1,y),
(x+1,y-1)
),
(
(x,y+1), (x+1,y+1),
(x+1,y)
),
(
(x-1,y+1), (x, y+1), (x+1,y+1)
),
(
(x-1,y+1), (x, y+1),
(x-1,y)
),
(
(x-1,y+1),
(x-1,y),
(x-1,y-1)
)
)
):
continue
free.append((x,y))
return free
def type_finder(points: list[tuple]):
free = find_movements(points)
movable = []
for point in free:
x, y = point
connections = 0
for nearby_pos in get_nearby(x, y):
if nearby_pos in points:
connections += 1
if connections > 1:
movable.append(point)
return movable
def get_width(shape):
return len(shape[0])
def get_height(shape):
return len(shape)
def get_positions(shape):
for y in range(get_height(shape)):
for x in range(get_width(shape)):
yield (x, y)
def get_value_at(x: int, y: int, shape):
return shape[y][x]
def invert_map(dictionary):
return {value:key for key, value in dictionary.items()}
def create_shape_map(shape):
shape_map = {}
index = 1
for (x, y) in get_positions(shape):
if get_value_at(x, y, shape) == TARGET:
shape_map[(x, y)] = index
index += 1
return shape_map
def calculate_score(structure):
return mean_distance(list(structure.values()))
def convert_position_to_id(position, structure):
for index, pos in structure.items():
if pos == position:
return index
def movement_finder(moveable, structure):
to_contract = {}
for index in moveable:
x, y = structure[index]
joints = {}
for possible in get_nearby(x, y):
if possible in structure.values():
xp, yp = possible
transforms = []
for neighbor in get_nearby(xp, yp):
if not neighbor in structure.values():
transforms.append(neighbor)
if transforms != []:
joints[convert_position_to_id(possible, structure)] = transforms
if joints != {}:
to_contract[index] = joints
return to_contract
def find_moving_tiles(index, joint_index, original_relation):
contracting = [index]
visited = [index, joint_index]
current_indexes = [index]
while current_indexes != []:
i = current_indexes.pop()
for neighbor_index in original_relation[i]:
if neighbor_index not in visited:
current_indexes.append(neighbor_index)
contracting.append(neighbor_index)
visited.append(neighbor_index)
return contracting
def get_neighbors_new(index, new_shape):
x, y = new_shape[index]
d = [possible for possible in get_nearby(x, y) if possible in new_shape.values()]
return [index for index, pos in new_shape.items() if pos in d]
def is_allowed_update(new_shape, original_relation):
for index, neighbors in original_relation.items():
if index not in new_shape:
return False
x, y = new_shape[index]
d = [possible for possible in get_nearby(x, y) if possible in new_shape.values()]
new_neighbors = [index for index, pos in new_shape.items() if pos in d]
for neighbor in neighbors:
if neighbor not in new_neighbors:
return False
return True
def possible_solutions(index, joint_index, new_pos, structure, original_relation):
contracting = find_moving_tiles(index, joint_index, original_relation)
to_move = {}
point = structure[index]
dx, dy = new_pos[0] - point[0], new_pos[1] - point[1]
to_rotate_current = []
to_rotate_new = []
for i in contracting:
x, y = structure[i]
to_rotate_current.append((x, y))
to_rotate_new.append((x+dx, y+dy))
def can_rotate(rotated, to_rotate):
can_rotate = True
for rotated_point in rotated:
x, y = rotated_point
if rotated_point in structure.values():
if (x, y) not in to_rotate:
can_rotate = False
break
return can_rotate
def perform_rotation(to_move, rotated):
new_grid = structure.copy()
new_grid = invert_map(new_grid)
for ide, current, rotatedt in zip(contracting, to_rotate_current, rotated):
new_grid[current] = 0
new_grid[rotatedt] = ide
new_grid = {v: k for k, v in new_grid.items() if v != 0}
return new_grid
possible_grids = []
for angle in (0, 90, 180, 270):
# Order is a must
rotated_current = rotate_points(to_rotate_current, point, angle)
# Check rotation on current position
if can_rotate(rotated_current, to_rotate_current):
new_grid = perform_rotation(to_move, rotated_current)
if is_allowed_update(new_grid, original_relation):
possible_grids.append(new_grid)
rotated_new = rotate_points(to_rotate_new, new_pos, angle)
# Check rotation on proposed position
if can_rotate(rotated_new, to_rotate_current):
new_grid = perform_rotation(to_move, rotated_new)
if is_allowed_update(new_grid, original_relation):
possible_grids.append(new_grid)
return possible_grids
def convert_poly_to_shape(structure):
positions = list(structure.values())
min_x, max_x = positions[0][0], positions[0][0]
min_y, max_y = positions[0][1], positions[0][1]
# Calculate width and height for array
for x, y in positions:
if x < min_x:
min_x = x
if x > max_x:
max_x = x
if y < min_y:
min_y = y
if y > max_y:
max_y = y
max_x -= min_x
max_y -= min_y
shape = np.zeros((max_y+1, max_x+1))
for index, (x, y) in structure.items():
shape[y-min_y][x-min_x] = index
return shape
def solver(shape: list[list[int]]) -> list[list[int]]:
shape_map = create_shape_map(shape)
structure = invert_map(shape_map)
def get_neighbors(index):
x, y = structure[index]
neighbors = []
for position in get_nearby(x, y):
if position in shape_map:
neighbors.append(shape_map[position])
return neighbors
# How the polyomino is attached
original_relation = {index:get_neighbors(index) for index in structure}
points = list(structure.values())
# Find which parts can be modified
moveable = [shape_map[move] for move in type_finder(points)]
original_mapping = convert_poly_to_shape(structure.copy())
score = calculate_score(structure) + 10
times = 0
while True:
to_contract = movement_finder(moveable, structure)
solutions = {}
global_solution = []
for movable in to_contract:
joints = to_contract[movable]
for joint in joints:
for new_pos in joints[joint]:
solutions = possible_solutions(movable, joint, new_pos, structure, original_relation)
result = {mean_distance(list(solution.values())):solution for solution in solutions}
current_score = sorted(result.keys())[0]
best = result[current_score]
if current_score < score:
global_solution = best
score = current_score
# Converged
if global_solution == []:
break
else:
structure = global_solution
times += 1
result_mapping = convert_poly_to_shape(structure)
print(f"Converged after {times} contractions")
return result_mapping, original_mapping
if __name__ == "__main__":
grid = np.array([
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
])
# grid = np.array([
# [1,0,1,1],
# [1,0,1,0],
# [1,1,1,0],
# [1,0,1,0],
# [1,0,1,0],
# [1,0,1,0],
# [1,0,1,0],
# ])
# grid = np.array([
# [1,0,1,1,0,1,1,1,0,1],
# [1,0,1,0,0,1,1,0,0,1],
# [1,1,1,0,0,1,1,0,0,1],
# [1,0,1,0,0,1,1,0,0,1],
# [1,0,1,0,0,1,1,0,0,1],
# [1,0,1,0,0,1,1,0,0,1],
# [1,0,1,1,1,1,1,0,0,1],
# [1,0,1,0,0,1,1,0,0,1],
# [1,0,1,0,0,1,1,0,0,1],
# [1,0,1,0,0,1,1,0,0,1],
# [1,0,1,0,0,1,1,0,0,1],
# ])
grid = np.array([
[1,0,0,0,0,0],
[1,0,0,0,1,1],
[1,0,0,0,1,1],
[1,0,0,0,0,0],
[1,0,0,0,0,0],
[1,0,0,0,0,0],
[1,1,1,1,1,1],
])
solution, original = solver(grid)
print("\nOriginal:")
print(original)
print("\nSolution:")
print(solution)
def get_width(shape: list[int]) -> int:
return len(shape[0])
def get_height(shape: list[int]) -> int:
return len(shape)
def get_points(shape: list[int]) -> list[tuple]:
points = []
for x, y in get_positions(shape):
if shape[y][x] != 0:
points.append((x, y))
return points
original_value = mean_distance(get_points(original))
new_value = mean_distance(get_points(solution))
percentage_smaller = ((original_value - new_value) / original_value) * 100
print(f"\nThe new layout is {round(percentage_smaller, 2)}% contracted")