diff --git a/bruteforce2.py b/bruteforce2.py index 9a1d534..07148b4 100644 --- a/bruteforce2.py +++ b/bruteforce2.py @@ -1,9 +1,7 @@ import sys -import subprocess import itertools -import shutil -from util import xmlfiletodict, dicttoxmlfile, get_pallet, get_articles, get_packlist_dict -from arrange_spread2 import arrange_in_layer, spread_articles, find_articles +from util import xmlfiletodict, get_pallet, get_articles +from arrange_spread2 import arrange_in_layer, spread_articles import cPickle from binascii import b2a_base64 import zlib @@ -54,8 +52,8 @@ def get_bit(num, pos): return num>>pos&1 def main(): - if len(sys.argv) != 3: - print "usage:", sys.argv[0], "order.xml packlist.xml" + if len(sys.argv) != 2: + print "usage:", sys.argv[0], "order.xml" exit(1) orderline = xmlfiletodict(sys.argv[1]) @@ -74,14 +72,20 @@ def main(): stuff1 = list() - #for order in itertools.product([True, False], repeat=10): - #for order in [[True]*12,]: - for order in itertools.product([0,1,2,3], repeat=8): + 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() layers = list() - it = get_layers(bins, pallet, get_bit(order[0], 0), get_bit(order[0], 1)) - #it = get_layers(bins, pallet, order[0], False) + 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)) layer, rest = it.next() if layer: layers.append(layer) @@ -91,8 +95,10 @@ def main(): fail = True for rot_article in order[1:]: try: - layer, rest = it.send((get_bit(rot_article,0), get_bit(rot_article,1))) - #layer, rest = it.send((rot_article, False)) + 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))) if layer: layers.append(layer) if rest: diff --git a/bruteforce3.py b/bruteforce3.py index 38d22df..9144d2a 100644 --- a/bruteforce3.py +++ b/bruteforce3.py @@ -1,18 +1,16 @@ import sys import subprocess import itertools -import shutil -from util import xmlfiletodict, dicttoxmlfile, get_pallet, get_articles, get_packlist_dict -from arrange_spread2 import arrange_in_layer, spread_articles, find_articles +from util import dicttoxmlfile, get_packlist_dict, dicttoxmlstring +from arrange_spread2 import arrange_in_layer import cPickle -import marshal from binascii import a2b_base64 import tempfile import os import zlib import fcntl -def evaluate_layers_rests(layers, rests, scores, pallet): +def evaluate_layers_rests(layers, rests, scores, pallet, result_max): rest_layers = list() # sort rests by space they cover and move them to the center of the pile # append them to the layer list @@ -58,37 +56,24 @@ def evaluate_layers_rests(layers, rests, scores, pallet): dicttoxmlfile(packlist, tmp) # ugly, ugly, ugly, ugly hack - dont copy this... - score = float(subprocess.check_output("../palletandtruckviewer-3.0/palletViewer -o " + score = float(subprocess.check_output(sys.argv[3]+" -o " +sys.argv[1]+" -p "+tmp - +" -s ../icra2011TestFiles/scoreAsPlannedConfig1.xml --headless | grep Score", shell=True).split(' ')[1].strip()) - if score > max(scores+[0]): - shutil.move(tmp, sys.argv[2]) - else: - os.remove(tmp) + +" -s "+sys.argv[4]+" --headless | grep Score", shell=True).split(' ')[1].strip()) + if score >= max(scores+[0]): + result_max[0] = dicttoxmlstring(packlist) + os.remove(tmp) scores.append(score) - """ - lock = open("score_max.lock", "w") - fcntl.lockf(lock, fcntl.LOCK_EX) - score_max_f = open("score_max", "w+") - score_max = score_max_f.read() - if not score_max: - score_max = 0.0 - if score > score_max: - shutil.move(tmp, sys.argv[2]) - score_max_f.write(str(score)) - else: - os.remove(tmp) - score_max_f.close() - lock.close() - scores.append(score) - """ - def main(): + if len(sys.argv) < 6: + print "usage:", sys.argv[0], "order.xml packlist.xml palletViewer scoring.xml LAYER [LAYER..]" + exit(1) + scores = list() - for arg in sys.argv[3:]: + result_max = [None] + for arg in sys.argv[5:]: layers, rests, pallet = cPickle.loads(zlib.decompress(a2b_base64(arg))) - evaluate_layers_rests(layers, rests, scores, pallet) + evaluate_layers_rests(layers, rests, scores, pallet, result_max) print max(scores) #print "max:", max(scores) @@ -98,5 +83,19 @@ def main(): #from math import sqrt #print "stddev:", sqrt(sum([(x-mean)**2 for x in scores])/len(scores)) + lock = open("score_max.lock", "w") + fcntl.lockf(lock, fcntl.LOCK_EX) + if os.path.isfile("score_max"): + with open("score_max", "r") as f: + score_max = float(f.read()) + else: + score_max = 0.0 + if max(scores) > score_max: + with open(sys.argv[2], "w+") as f: + f.write(result_max[0]) + with open("score_max", "w+") as f: + f.write(str(max(scores))) + lock.close() + if __name__ == "__main__": main() diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..be2aa12 --- /dev/null +++ b/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh -ex + +rm -f score_max + +VIEWER=../palletandtruckviewer-3.0/palletViewer +SCORING=../icra2011TestFiles/scoreAsPlannedConfig1.xml + +python bruteforce2.py $1 \ + | sort\ + | uniq\ + | xargs --max-procs=4 python bruteforce3.py $1 packlist.xml $VIEWER $SCORING