def test_profit_multiple_deliveries(self): depot = (0, 0) trucks = [cvrp.Truck(truck_id="t1", capacity=10, per_mile_cost=0, max_dist=100)] deliveries = [cvrp.Delivery(delivery_id="d1", location=(0, 1), profit=10, size=1), cvrp.Delivery(delivery_id="d2", location=(0, 2), profit=10, size=1)] opt = cvrp.CVRP(depot_location=depot, trucks=trucks, deliveries=deliveries) opt.init_all() opt.solve() try: self.assertEqual(opt.mip_result.fun, -20) except AssertionError: debug_model(opt) raise
def test_max_dist_skip_furthest(self): # Skip 2nd, farther away, delivery depot = (0, 0) trucks = [cvrp.Truck(truck_id="t1", capacity=100, per_mile_cost=0, max_dist=2)] deliveries = [cvrp.Delivery(delivery_id="d1", location=(0, 1), profit=10, size=1), cvrp.Delivery(delivery_id="d2", location=(2, 0), profit=9, size=1)] opt = cvrp.CVRP(depot_location=depot, trucks=trucks, deliveries=deliveries) opt.init_all() opt.solve() try: self.assertEqual(opt.mip_result.fun, -10) except AssertionError: debug_model(opt) raise
def test_capacity_basic(self): # Can't fit both, so pick the more profitable delivery (10 > 9). depot = (0, 0) trucks = [cvrp.Truck(truck_id="t1", capacity=1, per_mile_cost=0, max_dist=100)] deliveries = [cvrp.Delivery(delivery_id="d1", location=(0, 1), profit=9, size=1), cvrp.Delivery(delivery_id="d2", location=(1, 0), profit=10, size=1)] opt = cvrp.CVRP(depot_location=depot, trucks=trucks, deliveries=deliveries) opt.init_all() opt.solve() try: self.assertEqual(opt.mip_result.fun, -10) except AssertionError: debug_model(opt) raise
def callback(ch, method, properties, body): print(" [x] Received %r" % body) print("Decoding...") str_obj = body.decode("utf-8") print(f"Decoded object: {str_obj}") data = json.loads(str_obj) print("JSON data object:") print(json.dumps(data, sort_keys=True, indent=4)) # TODO: Get depot and truck data print("Constructing model...") depot = (0, 0) trucks = [ cvrp.Truck(truck_id="t1", capacity=10, per_mile_cost=0, max_dist=100) ] # TODO: Add size to message payload deliveries = [ cvrp.Delivery(delivery_id=f"d{i}", location=(line_item["SiteLatitude"], line_item["SiteLongitude"]), profit=line_item["Profit"], size=line_item["Weight"]) for i, line_item in enumerate(data) ] print(f"Deliveries: {deliveries}") opt = cvrp.CVRP(depot_location=depot, trucks=trucks, deliveries=deliveries) opt.init_all() print("Solving...") opt.solve() print(f"Done solving. Result: {opt.mip_result}")
def test_per_mile_cost_basic(self): # 1 mile out, 1 mile back at cost of 1 per mile and 10 profit. depot = (0, 0) trucks = [cvrp.Truck(truck_id="t1", capacity=10, per_mile_cost=1, max_dist=100)] deliveries = [cvrp.Delivery(delivery_id="d1", location=(0, 1), profit=10, size=1)] opt = cvrp.CVRP(depot_location=depot, trucks=trucks, deliveries=deliveries) opt.init_all() opt.solve() try: self.assertEqual(opt.mip_result.fun, -8) except AssertionError: debug_model(opt) raise
def test_per_mile_cost_negative_distances(self): # Should have the exact same profit as the basic example, just with negative locations. depot = (0, 0) trucks = [cvrp.Truck(truck_id="t1", capacity=10, per_mile_cost=1, max_dist=100)] deliveries = [cvrp.Delivery(delivery_id="d1", location=(0, -1), profit=10, size=1)] opt = cvrp.CVRP(depot_location=depot, trucks=trucks, deliveries=deliveries) opt.init_all() opt.solve() try: self.assertEqual(opt.mip_result.fun, -8) except AssertionError: debug_model(opt) raise
def test_many_nodes(self): # Test with 15 nodes to ensure branch and bound doesn't fail depot = (0, 0) trucks = [cvrp.Truck(truck_id="t1", capacity=100, per_mile_cost=0, max_dist=100)] deliveries = [cvrp.Delivery(delivery_id="d1", location=(0, 1), profit=10, size=1), cvrp.Delivery(delivery_id="d2", location=(0, 2), profit=10, size=1), cvrp.Delivery(delivery_id="d3", location=(0, 3), profit=10, size=1), cvrp.Delivery(delivery_id="d4", location=(0, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d5", location=(1, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d6", location=(2, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d7", location=(3, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d8", location=(4, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d9", location=(4, 3), profit=10, size=1), cvrp.Delivery(delivery_id="d10", location=(4, 2), profit=10, size=1), cvrp.Delivery(delivery_id="d11", location=(4, 1), profit=10, size=1), cvrp.Delivery(delivery_id="d12", location=(4, 0), profit=10, size=1), cvrp.Delivery(delivery_id="d13", location=(3, 0), profit=10, size=1), cvrp.Delivery(delivery_id="d14", location=(2, 0), profit=10, size=1), cvrp.Delivery(delivery_id="d15", location=(1, 0), profit=10, size=1)] opt = cvrp.CVRP(depot_location=depot, trucks=trucks, deliveries=deliveries) opt.init_all() opt.solve() try: self.assertEqual(opt.mip_result.fun, -150) except AssertionError: debug_model(opt) raise
import cvrp import time depot = (0, 0) trucks = [cvrp.Truck(truck_id="t1", capacity=100, per_mile_cost=0, max_dist=100)] deliveries = [cvrp.Delivery(delivery_id="d1", location=(0, 1), profit=10, size=1), cvrp.Delivery(delivery_id="d2", location=(0, 2), profit=10, size=1), cvrp.Delivery(delivery_id="d3", location=(0, 3), profit=10, size=1), cvrp.Delivery(delivery_id="d4", location=(0, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d5", location=(1, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d6", location=(2, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d7", location=(3, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d8", location=(4, 4), profit=10, size=1), cvrp.Delivery(delivery_id="d9", location=(4, 3), profit=10, size=1), cvrp.Delivery(delivery_id="d10", location=(4, 2), profit=10, size=1), cvrp.Delivery(delivery_id="d11", location=(4, 1), profit=10, size=1), cvrp.Delivery(delivery_id="d12", location=(4, 0), profit=10, size=1), cvrp.Delivery(delivery_id="d13", location=(3, 0), profit=10, size=1), cvrp.Delivery(delivery_id="d14", location=(2, 0), profit=10, size=1), cvrp.Delivery(delivery_id="d15", location=(1, 0), profit=10, size=1)] opt = cvrp.CVRP(depot_location=depot, trucks=trucks, deliveries=deliveries) opt.init_all() times = [] n = 10 for _ in range(n): start = time.time() opt.solve() end = time.time() times.append(end - start) print(f"After {n} iterations, average runtime of {sum(times) / len(times)}s")