-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbruteforce2.py
More file actions
197 lines (174 loc) · 7.21 KB
/
Copy pathbruteforce2.py
File metadata and controls
197 lines (174 loc) · 7.21 KB
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
#!/usr/bin/env python
#
# Copyright 2012 Johannes 'josch' Schauer <j.schauer@email.de>
#
# This file is part of Sisyphus.
#
# Sisyphus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Sisyphus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Sisyphus. If not, see <http://www.gnu.org/licenses/>.
import sys
import itertools
from util import xmlfiletodict, get_pallet, get_articles, product_varlength, starmap
from arrange_spread2 import arrange_in_layer, spread_articles
import cPickle
from binascii import b2a_base64
import zlib
import os
import random
def rotate(node):
if node is None:
return
if node['article']:
# exchange x and y coordinate
node['article']['PlacePosition']['X'], node['article']['PlacePosition']['Y'] = node['article']['PlacePosition']['Y'], node['article']['PlacePosition']['X']
# rotate article
node['article']['Orientation'] = node['article']['Orientation']%2+1
rotate(node['right'])
rotate(node['down'])
def get_layers(bins, pallet, rot_article=False, rot_pallet=False):
for abin in bins:
bins[abin] = sorted(bins[abin], key=lambda article: article['Article']['Length']*article['Article']['Width'], reverse=True)
plength, pwidth = (pallet['Dimensions']['Length'], pallet['Dimensions']['Width'])
if rot_pallet:
root, layer, rest = arrange_in_layer(bins[abin], pwidth, plength, rot_article=rot_article)
else:
root, layer, rest = arrange_in_layer(bins[abin], plength, pwidth, rot_article=rot_article)
while layer:
spread_articles(root)
if rot_pallet:
rotate(root)
occupied_area = 0
for article in layer:
length, width = article['Article']['Length'], article['Article']['Width']
occupied_area += length*width
# print "layer occupation:", occupied_area/float(plength*pwidth)
if occupied_area/float(plength*pwidth) <= 0.7:
rot_article, rot_pallet = (yield None, layer)
else:
rot_article, rot_pallet = (yield layer, None)
if rot_pallet:
root, layer, rest = arrange_in_layer(rest, pwidth, plength, rot_article=rot_article)
else:
root, layer, rest = arrange_in_layer(rest, plength, pwidth, rot_article=rot_article)
def get_bit(num, pos):
return num>>pos&1
def get_bitmask(num, length):
return tuple(( bool(num>>pos&1) for pos in xrange(length-1,-1,-1) ))
def main():
if len(sys.argv) != 2:
print "usage:", sys.argv[0], "order.xml"
exit(1)
orderline = xmlfiletodict(sys.argv[1])
pallet = get_pallet(orderline)
articles = get_articles(orderline)
bins = dict()
for article in articles:
abin = bins.get(article['Article']['Height'])
if abin:
abin.append(article)
else:
bins[article['Article']['Height']] = [article]
if os.environ.get("rot_article"):
try_rot_article = bool(int(os.environ["rot_article"]))
else:
try_rot_article = True
if os.environ.get("rot_pallet"):
try_rot_pallet = bool(int(os.environ["rot_pallet"]))
else:
try_rot_pallet = True
if os.environ.get("rot_article_default"):
rot_article_default = bool(int(os.environ["rot_article_default"]))
else:
rot_article_default = False
if os.environ.get("rot_pallet_default"):
rot_pallet_default = bool(int(os.environ["rot_pallet_default"]))
else:
rot_pallet_default = False
if os.environ.get("iterations"):
max_iter = int(os.environ["iterations"])
else:
max_iter = -1
if os.environ.get("randomize"):
try_random = bool(int(os.environ["randomize"]))
else:
try_random = False
if try_rot_article and try_rot_pallet:
if try_random:
product_it = starmap(random.randint, itertools.repeat((0,3)))
else:
product_it = product_varlength(4)
elif try_rot_article or try_rot_pallet:
if try_random:
product_it = starmap(random.randint, itertools.repeat((0,1)))
else:
product_it = product_varlength(2)
i = 0
while True:
rests = list()
layers = list()
try:
if try_rot_article and try_rot_pallet:
rot_article, rot_pallet = get_bitmask(product_it.send(True), 2)
elif try_rot_article and not try_rot_pallet:
rot_article = get_bitmask(product_it.send(True), 1)[0]
rot_pallet = rot_pallet_default
elif not try_rot_article and try_rot_pallet:
rot_article = rot_article_default
rot_pallet = get_bitmask(product_it.send(True), 1)[0]
else:
rot_article = rot_article_default
rot_pallet = rot_pallet_default
except TypeError:
if try_rot_article and try_rot_pallet:
rot_article, rot_pallet = get_bitmask(product_it.next(), 2)
elif try_rot_article and not try_rot_pallet:
rot_article = get_bitmask(product_it.next(), 1)[0]
rot_pallet = rot_pallet_default
elif not try_rot_article and try_rot_pallet:
rot_article = rot_article_default
rot_pallet = get_bitmask(product_it.next(), 1)[0]
else:
rot_article = rot_article_default
rot_pallet = rot_pallet_default
except StopIteration:
break # generator empty
it = get_layers(bins, pallet, rot_article, rot_pallet)
layer, rest = it.next()
if layer:
layers.append(layer)
if rest:
rests.append(rest)
while True:
try:
if try_rot_article and try_rot_pallet:
layer, rest = it.send(get_bitmask(product_it.send(False), 2))
elif try_rot_article and not try_rot_pallet:
layer, rest = it.send((get_bitmask(product_it.send(False), 1)[0], rot_pallet_default))
elif not try_rot_article and try_rot_pallet:
layer, rest = it.send((rot_article_default, get_bitmask(product_it.send(False), 1)[0]))
else:
layer, rest = it.send((rot_article_default, rot_pallet_default))
if layer:
layers.append(layer)
if rest:
rests.append(rest)
except StopIteration:
break
print b2a_base64(zlib.compress(cPickle.dumps((layers, rests, pallet)))),
if not try_rot_article and not try_rot_pallet:
break # only one iteration if both are deactivated
i+=1
if max_iter != -1 and i >= max_iter:
break
if __name__ == "__main__":
main()