def testReset(self): Warehouse.clear() # New warehouse configuration with reset() that combines clear() and init(racks, bins) try: # test reset allowing warehouse reconfiguration w = Warehouse.reset(5, 5) self.assertEqual(len(w.racks), 5, 'racks SBE 5 but is {}'.format(len(w.racks))) self.assertEqual((w.dock.lat, w.dock.long), (0, 7), \ 'dock lat/long SBE (0, 7), is {}'.format((w.dock.lat, w.dock.long))) except Exception as e: print(e) sys.exit(-1)
def testExceptions(self): Warehouse.clear() w = Warehouse(5, 5) try: # confirm 2nd instantiation w/o clear() raises exception Warehouse(2, 2) raise RuntimeError( 'second and subsequent instantiations of warehouse should raise exception' ) except ValueError as v: print(v) sys.exit(-1) except Exception as e: pass # these should raise exceptions try: w.racks = 1 raise RuntimeError('setting racks should raise exception') except ValueError as v: print(v) sys.exit(-1) except Exception as e: pass try: w.dock_lat_long = (0, 0) raise RuntimeError('setting dock_lat_long should raise exception') except ValueError as v: print(v) sys.exit(-1) except Exception as e: pass
def testUpdateStockSingle(self): Warehouse.clear() w = Warehouse(5, 5) b = w.racks[0].bins_a[1] w.update_stock(1, 40, b.location) _, q = w.get_stock_qty(1) assert q == 40, 'item 1 qty SBE {} but is {}'.format(40, q)
def testStockingRackBins(self): Inventory.clear() Warehouse.clear() wh = Warehouse(5, 5) for i, (r, s, bn) in enumerate( product(range(1, 5 + 1, 1), list('ab'), range(1, 5 + 1, 1))): if s == 'a': b = wh.racks[r - 1].bins_a[bn] else: # s == 'b' b = wh.racks[r - 1].bins_b[bn] wh.update_stock(i + 1, (i + 1) * 10, b.location) _, q = wh.get_stock_qty(i + 1) self.assertEqual( q, (i + 1) * 10, 'inventory qty for item {} item SBE {}, is {}'.format( i, (i + 1) * 10, q))
def testInit(self): Warehouse.clear() w = Warehouse(5, 5) self.assertEqual(len(w.racks), 5, 'w should have 5 racks, is {}'.format(w.racks)) self.assertEqual(w.racks_bins, (5, 5), 'racks_bin SBE (5, 50, is {}'.format(w.racks_bins)) self.assertEqual((w.dock.lat, w.dock.long), (0, 7), \ 'dock_lat_long SBE (0, 7), is {}'.format((w.dock.lat, w.dock.long))) self.assertEqual( w.__repr__(), 'racks: 5, dock (lat, long): (0, 7), inventory has 0 item_no, 0 quantities', 'repr should not be "{}"'.format(w.__repr__()))
class Test(unittest.TestCase): Warehouse.clear() wh = Warehouse(5, 5) rack_count, bin_count = wh.racks_bins bins_total = rack_count * bin_count * 2 inv = Inventory() num_items = bins_total o = None order_lines = 5 setup_calls = 0 def setUp(self): Test.setup_calls += 1 # print('setup call number {}'.format(Test.setup_calls)) np.random.seed(42) Test.wh = Test.wh.reset(5, 5) self.wh = Test.wh Test.inv.clear() rsb = [(r, s, b) for r, s, b in product(range(1, 5 + 1, 1), list('ab'), range(1, 5 + 1, 1))] np.random.seed(42) np.random.shuffle(rsb) for i, (r, s, bn) in enumerate(rsb): if s == 'a': b = self.wh.racks[r - 1].bins_a[bn] else: # s == 'b' b = self.wh.racks[r - 1].bins_b[bn] self.wh.update_stock(i + 1, (i + 1) * 10, b.location) np.random.seed(42) Test.o = Order() np.random.seed(42) for i, q in zip(np.random.choice(range(1, Test.num_items + 1), size=Test.order_lines, replace=False), np.random.randint(1, Test.num_items + 1, size=Test.order_lines)): Test.o.add_line(item_no=int(i), qty=int(q)) racks = self.wh.racks b = racks[0].bins_a[1] self.assertEqual(b.item, 26, 'rack 1, bin @ {}, item SBE 26, is {}'.format(b.location, b.item)) # print(b) b = racks[1].bins_a[1] self.assertEqual(b.item, 41, 'rack 1, bin @ {}, item SBE 41, is {}'.format(b.location, b.item)) # print(b) b = racks[1].bins_b[1] self.assertEqual(b.item, 20, 'rack 1, bin @ {}, item SBE 29, is {}'.format(b.location, b.item)) # print(b) def tearDown(self): pass def testInit(self): # print('testInit') pr = PickRoute(Test.wh, self.o) self.assertEqual(len(pr.wh.racks), 5, 'warehouse should have 5 racks, is {}'.format(len(pr.wh.racks))) self.assertEqual(len(self.o.lines), Test.order_lines, 'order lines SBE {}, is {}'.format(Test.order_lines, len(self.o.lines))) self.assertEqual(len(pr.pick_bins), Test.order_lines, \ 'order pick_bins SBE {}, is {}'.format(Test.order_lines, len(pr.pick_bins))) def testBinsDistance(self): # print('testBinsDistance') b1 = Bin(rack_no=1, side='b', bin_no=1) b2 = Bin(rack_no=1, side='a', bin_no=5) pr = PickRoute(Test.wh, self.o) d = pr.bin_to_bin_distance(b1, b2) print('from {} to {} dist: {}'.format(b1.location, b2.location, d)) #expected = 16 #self.assertEqual(d, expected, '{} to {} SBE [], is {}'\ # .format(b1.location, b2.location, expected, d)) b1 = Bin(rack_no=1, side='b', bin_no=1) b2 = Bin(rack_no=2, side='a', bin_no=1) pr = PickRoute(Test.wh, self.o) d = pr.bin_to_bin_distance(b1, b2) print('from {} to {} dist: {}'.format(b1.location, b2.location, d)) #expected = 16 #self.assertEqual(d, expected, '{} to {} SBE [], is {}'\ # .format(b1.location, b2.location, expected, d)) b1 = Bin(rack_no=1, side='a', bin_no=4) b2 = Bin(rack_no=4, side='b', bin_no=5) pr = PickRoute(Test.wh, self.o) d = pr.bin_to_bin_distance(b1, b2) print('from {} to {} dist: {}'.format(b1.location, b2.location, d)) #expected = 16 #self.assertEqual(d, expected, '{} to {} SBE [], is {}'\ # .format(b1.location, b2.location, expected, d)) b1 = Bin(rack_no=3, side='a', bin_no=0) b2 = Bin(rack_no=4, side='b', bin_no=5) pr = PickRoute(Test.wh, self.o) d = pr.bin_to_bin_distance(b1, b2) print('from dock {} to {} dist: {}'.format(b1.location, b2.location, d)) #expected = 16 #self.assertEqual(d, expected, '{} to {} SBE [], is {}'\ # .format(b1.location, b2.location, expected, d)) def testRoute(self): # print('testRoute') np.random.seed(42) pr = PickRoute(Test.wh, self.o) expected_steps = 46 if pr.route_distance != expected_steps: print('route distance {:d} SBE {} {}'.format(pr.route_distance, expected_steps, 'maybe next time?' \ if pr.route_distance != expected_steps \ else '')) # print(os.path.curdir) # I'm missing a random.seed somewhere self.assertEqual(pr.route_distance, expected_steps, 'route_distince SBE {}, is {}'\ .format(expected_steps, pr.route_distance))
def __init__(self, args, CXPB=0.7): restore = args.restore if len(restore) > 0: if not os.path.isfile(restore): print('restore file {} does not exist'.format(restore)) sys.exit(2) else: self.RESTORE = True else: self.RESTORE = False self.verbose = args.verbose racks = args.racks bins = args.bins self.NGENS = args.gens if self.verbose: print("Verbose mode on") if racks > 0: self.NUM_RACKS = racks if self.verbose: print("{} racks".format(racks)) else: raise RuntimeError('must be int > 0 number of racks') if bins > 0: self.NUM_RACK_SIDE_BINS = bins if self.verbose: print("{} bins".format(bins)) else: raise RuntimeError( 'must be int > 0 number of bins on each side of rack') self.NUM_BINS = self.NUM_RACKS * self.NUM_RACK_SIDE_BINS * 2 # two sides to a rack self.INDXS = np.arange(self.NUM_BINS) self.NUM_ORDERS = self.NUM_BINS * 10 self.ORDER_LINES = 15 self.CXPB = CXPB self.MUTPB = 1.0 - CXPB self.wh = Warehouse(self.NUM_RACKS, self.NUM_RACK_SIDE_BINS) self.rsb = [(r, s, b) for r, s, b in product( range(self.NUM_RACKS), list('ab'), range(1, self.NUM_RACK_SIDE_BINS + 1, 1))] self.idxs = np.arange(self.NUM_BINS) self.orders = [] seed(42) for _ in range(self.NUM_ORDERS): o = Order() o_items = choice(range(1, self.NUM_BINS + 1), size=self.ORDER_LINES, replace=False) for itm in o_items: o.add_line(item_no=int(itm), qty=10) self.orders.append(o) if self.RESTORE: with open(restore, 'rb') as infile: self.pop = pickle.load(infile) plen = len(self.pop[0]) # reset pop_id and fitness = 0 self.pop = [ Individual(shape=(plen, ), buffer=p) for p in self.pop ] pass # for debugging else: self.pop = list() for _ in range(1, self.NUM_BINS + 1): # individulas contain a shuffled list of item_no # bins contain a fixed qty of an item_no selected from a shuffled list # the fitness attribute will be set to number of steps required to fulfill orders from items when so distributed in wh self.pop.append(Individual(shape=(self.NUM_BINS, )))