-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.py
70 lines (43 loc) · 1.44 KB
/
common.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
import random
import time
from collections import deque, namedtuple
import pybp
from constants import BIN_HEIGHT, BIN_WIDTH
def perm_by_height_(xy):
xy.sort(key=lambda x: (-x[1], x[0]))
return xy
def perm_by_width_(xy):
xy.sort(key=lambda x: (-x[0], x[1]))
return xy
def perm_by_area_(xy):
xy.sort(key=lambda x: (-x[0] * x[1], -x[1], x[0]))
return xy
def perm_by_height(xy):
return sorted(xy, key=lambda x: (-x[1], x[0]))
def perm_by_width(xy):
return sorted(xy, key=lambda x: (-x[0], x[1]))
def perm_by_area(xy):
return sorted(xy, key=lambda x: (-x[0] * x[1], -x[1], x[0]))
def bp_shelf(xy):
return pybp.get_bin_out_fast_3(xy, False, BIN_WIDTH, BIN_HEIGHT)[1]
def bp_max_rect(xy):
return pybp.get_bin_out_fast_4(xy, False, BIN_WIDTH, BIN_HEIGHT)[1]
def transpose(arr):
return [list(i) for i in zip(*arr)]
class Timer():
def __init__(self, n=10):
self.queue = deque([], n)
def step(self):
self.queue.append(time.time())
def fps(self):
assert len(self.queue) > 1
return (len(self.queue) - 1) / (self.queue[-1] - self.queue[0])
class Solution(namedtuple('Solution', ['plan', 'bins'])):
def shuffle(self):
i = list(range(len(self.bins)))
random.shuffle(i)
self.bins[:] = [self.bins[i] for i in i]
self.plan[:] = [self.plan[i] for i in i]
@property
def cost(self):
return sum(len(i) for i in self.bins)