-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalgo.py
194 lines (143 loc) · 4.41 KB
/
algo.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Fast Optimizing Rectangle Packing
Python 2.7.6
Author paper
Date 2014-08
Site https://github.com/paper
Reference http://www.99css.com/archives/977
http://www.aaai.org/Papers/ICAPS/2003/ICAPS03-029.pdf
http://www.google.com
'''
import copy
def _clone(r):
return copy.deepcopy(r)
# 从大到小
def sortByHeight(r):
result = []
r = _clone(r)
def sortCmp(a, b):
if a['h'] < b['h']:
return 1
if a['h'] > b['h']:
return -1
return 0
result = sorted(r, sortCmp);
return result
#end sortByHeight
def FORP(r):
r2 = []
length = len(r)
def letsgo():
for i, v in enumerate(r):
w, h = v[0], v[1]
r2.append( { 'w':w, 'h':h, 'p':i } )
sampleArr = sortByHeight(r2)
maxHeightRect = sampleArr[0]
boxInitHeight = maxHeightRect['h']
boxHeightStep = 16
sampleMax = 10
nice = {'u':0}
i = 0
while i < sampleMax:
niceTemp = baseAlgo(sampleArr, boxInitHeight + boxHeightStep * i)
print u"样本%d: 利用率:%.3f, 宽度:%d, 高度:%d" % (i, niceTemp['u'], niceTemp['w'], niceTemp['h'])
if niceTemp['u'] > nice['u']:
nice = niceTemp
if nice['u'] == 1:
break
i+=1
return nice
# end letsgo
def baseAlgo(r, height=0):
cloneArr = _clone(r)
MAXW, MAXH, MINH = 10000, 0, 0
totalArea, totalWidth, totalHeight = 0, 0, 0
space = 1
for i, v in enumerate(cloneArr):
w = v['w'] + space
h = v['h'] + space
cloneArr[i]['w'] = w
cloneArr[i]['h'] = h
totalArea += w * h
totalWidth += w
totalHeight += h
maxHeightRect = cloneArr[0]
MAXH = max(height, maxHeightRect['h'])
MINH = cloneArr[-1]['h']
box = { 'w':maxHeightRect['w'], 'h':MAXH }
boxIn = [ {'w':maxHeightRect['w'], 'h':maxHeightRect['h'], 'p':maxHeightRect['p'], 'x':0, 'y':0} ]
xWrap = []
xStep = 1
def checkTempBoxInRectLine(rect, x, tempBoxInRectLine):
key = False
def tempBoxInRectLineSort(a, b):
if a[0] < b[0]:
return -1
if a[0] > b[0]:
return 1
return 0
tempBoxInRectLine = sorted( tempBoxInRectLine, tempBoxInRectLineSort )
for i ,v in enumerate(tempBoxInRectLine):
w = rect['w']
h = rect['h']
p = rect['p']
x = x
y = v[1]
if y + h > MAXH:
continue
if tempBoxInRectLine[i+1] and (tempBoxInRectLine[i+1][0] - y) < h:
continue
boxInCover = []
for v in boxIn:
boxInCover.append(x >= v['x'] + v['w'] or y >= v['y'] + v['h'] or x + w <= v['x'] or y + h <= v['y'])
if all( boxInCover ):
boxIn.append( {'w':w, 'h':h, 'p':p, 'x':x, 'y':y} )
if x + w > box['w']:
box['w'] = x + w
key = True
break
return key
#end checkTempBoxInRectLine
for i in range(1, length):
temp = cloneArr[i]
temp_w = temp['w']
temp_h = temp['h']
temp_p = temp['p']
for x in range(0, MAXW, xStep):
if x in xWrap:
continue
if len(boxIn) == length:
break
if x == box['w']:
boxIn.append( {'w':temp_w, 'h':temp_h, 'p':temp_p, 'x':x, 'y':0} )
box['w'] += temp_w
break
tempBoxInRect = [];
for v in boxIn:
if x >= v['x'] and x < ( v['w'] + v['x'] ):
tempBoxInRect.append(v)
tempBoxInRectTotalHeight = 0;
tempBoxInRectLine = [ [0, 0], [MAXH, MAXH] ]
for v in tempBoxInRect:
tempBoxInRectTotalHeight += v['h']
tempBoxInRectLine.append( [ v['y'], v['y'] + v['h'] ] )
if tempBoxInRectTotalHeight + MINH > MAXH:
xWrap.append(x)
continue
if checkTempBoxInRectLine( temp, x, tempBoxInRectLine):
break
return {
's' : totalArea,
'w' : box['w'] - space,
'h' : box['h'] - space,
'r' : boxIn,
'u' : round( float(totalArea)/(box['w'] * box['h']), 3),
'space' : space
}
# end baseAlgo
return letsgo()
# end FORP
# ------------- test --------------
# print FORP([ [1,1], [2,2], [3,3], [4,4], [5,5] ])