From d7627a92944e4386351021a10e1b2f393adf900b Mon Sep 17 00:00:00 2001 From: josch Date: Mon, 14 May 2012 23:40:48 +0200 Subject: [PATCH] new product generator --- bruteforce2.py | 41 +++++++++++++++-------------------------- util.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/bruteforce2.py b/bruteforce2.py index 07148b4..d3d1fdf 100644 --- a/bruteforce2.py +++ b/bruteforce2.py @@ -1,6 +1,6 @@ import sys 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 import cPickle 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): 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" @@ -68,46 +71,32 @@ def main(): else: bins[article['Article']['Height']] = [article] - scores = list() - - 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): + product_it = product_varlength(4) + while True: rests = list() layers = list() - - if rot_pallet_only: - it = get_layers(bins, pallet, False, order[0]) - else: - it = get_layers(bins, pallet, get_bit(order[0], 0), get_bit(order[0], 1)) + try: + rot_article, rot_pallet = get_bitmask(product_it.send(True), 2) # start a new combination + except TypeError: + rot_article, rot_pallet = get_bitmask(product_it.next(), 2) # can't send to a just-started generator + 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) - fail = True - for rot_article in order[1:]: + while True: try: - if rot_pallet_only: - layer, rest = it.send((False, rot_article)) - else: - layer, rest = it.send((get_bit(rot_article,0), get_bit(rot_article,1))) + layer, rest = it.send(get_bitmask(product_it.send(False), 2)) if layer: layers.append(layer) if rest: rests.append(rest) except StopIteration: - fail = False break - if fail: - raise Exception("finished early") print b2a_base64(zlib.compress(cPickle.dumps((layers, rests, pallet)))), if __name__ == "__main__": main() diff --git a/util.py b/util.py index 4ea5f6b..c0e22df 100644 --- a/util.py +++ b/util.py @@ -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__": + 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/palDay1R1Order.xml') root = tree.getroot()