new product generator

main
josch 12 years ago
parent addccfef86
commit d7627a9294

@ -1,6 +1,6 @@
import sys import sys
import itertools import itertools
from util import xmlfiletodict, get_pallet, get_articles from util import xmlfiletodict, get_pallet, get_articles, product_varlength
from arrange_spread2 import arrange_in_layer, spread_articles from arrange_spread2 import arrange_in_layer, spread_articles
import cPickle import cPickle
from binascii import b2a_base64 from binascii import b2a_base64
@ -51,6 +51,9 @@ def get_layers(bins, pallet, rot_article=False, rot_pallet=False):
def get_bit(num, pos): def get_bit(num, pos):
return num>>pos&1 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(): def main():
if len(sys.argv) != 2: if len(sys.argv) != 2:
print "usage:", sys.argv[0], "order.xml" print "usage:", sys.argv[0], "order.xml"
@ -68,46 +71,32 @@ def main():
else: else:
bins[article['Article']['Height']] = [article] bins[article['Article']['Height']] = [article]
scores = list() product_it = product_varlength(4)
while True:
stuff1 = list()
rot_pallet_only = False
if rot_pallet_only:
combos = [True, False]
else:
combos = [0,1,2,3]
for order in itertools.product(combos, repeat=5):
rests = list() rests = list()
layers = list() layers = list()
try:
if rot_pallet_only: rot_article, rot_pallet = get_bitmask(product_it.send(True), 2) # start a new combination
it = get_layers(bins, pallet, False, order[0]) except TypeError:
else: rot_article, rot_pallet = get_bitmask(product_it.next(), 2) # can't send to a just-started generator
it = get_layers(bins, pallet, get_bit(order[0], 0), get_bit(order[0], 1)) except StopIteration:
break # generator empty
it = get_layers(bins, pallet, rot_article, rot_pallet)
layer, rest = it.next() layer, rest = it.next()
if layer: if layer:
layers.append(layer) layers.append(layer)
if rest: if rest:
rests.append(rest) rests.append(rest)
fail = True while True:
for rot_article in order[1:]:
try: try:
if rot_pallet_only: layer, rest = it.send(get_bitmask(product_it.send(False), 2))
layer, rest = it.send((False, rot_article))
else:
layer, rest = it.send((get_bit(rot_article,0), get_bit(rot_article,1)))
if layer: if layer:
layers.append(layer) layers.append(layer)
if rest: if rest:
rests.append(rest) rests.append(rest)
except StopIteration: except StopIteration:
fail = False
break break
if fail:
raise Exception("finished early")
print b2a_base64(zlib.compress(cPickle.dumps((layers, rests, pallet)))), print b2a_base64(zlib.compress(cPickle.dumps((layers, rests, pallet)))),
if __name__ == "__main__": if __name__ == "__main__":
main() main()

@ -117,7 +117,38 @@ def get_packlist_dict(pallet, articles):
} }
} }
def product_varlength(branch_factor):
root = {"value": None, "parent": None, "children": []}
current = root
while True:
if not current["children"]:
current["children"] = [{"value":val, "parent":current, "children":[]}
for val in range(branch_factor)]
current = current["children"][0]
if (yield current["value"]):
while True:
if current["parent"]:
current["parent"]["children"].pop(0)
else:
return
if current["parent"]["children"]:
current = root
break
else:
current = current["parent"]
if __name__ == "__main__": if __name__ == "__main__":
it = product_var_repeat(3)
while True:
try:
foo = it.send(True) # start a new combination
except TypeError:
foo = it.next() # can't send to a just-started generator
except StopIteration:
break # generator empty
print foo, it.send(False), it.send(False)
tree = ElementTree.parse('../icra2011TestFiles/GT/gt_d1r1.wpacklist.xml') tree = ElementTree.parse('../icra2011TestFiles/GT/gt_d1r1.wpacklist.xml')
#tree = ElementTree.parse('../icra2011TestFiles/palDay1R1Order.xml') #tree = ElementTree.parse('../icra2011TestFiles/palDay1R1Order.xml')
root = tree.getroot() root = tree.getroot()

Loading…
Cancel
Save