From cd3d56411312c5c7fa5a37875aecce7054edfa0b Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Tue, 21 Jan 2020 15:05:33 -0500 Subject: [PATCH] Start VRP. --- vrp/vrp.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 vrp/vrp.py diff --git a/vrp/vrp.py b/vrp/vrp.py new file mode 100644 index 0000000..cc2d381 --- /dev/null +++ b/vrp/vrp.py @@ -0,0 +1,76 @@ +import math + + +class Customer(object): + def __init__(self, index, demand, x, y): + self.index = index + self.demand = demand + self.x = x + self.y = y + + def __str__(self): + m = "C({}, {})".format(self.index, self.demand) + return m + + +class Vrp(object): + + def __init__(self, input_data): + lines = input_data.split('\n') + parts = lines[0].split() + self.customer_count = int(parts[0]) + self.vehicle_count = int(parts[1]) + self.vehicle_capacity = int(parts[2]) + + customers = [] + for i in range(1, self.customer_count + 1): + line = lines[i] + parts = line.split() + demand = int(parts[0]) + x, y = map(float, parts[1:]) + c = Customer(i - 1, demand, x, y) + customers.append(c) + + self.depot = customers[0] + self.customers = customers[1:] + + def plot(self): + try: + import matplotlib.pyplot as plt + except ModuleNotFoundError: + return + + for c in self.customers: + plt.plot(c.x, c.y, 'rx') + plt.text(c.x, c.y, f' {c}') + + d = self.depot + plt.plot(d.x, d.y, 'bo') + + plt.show() + + def solve_trivial(self): + pass + + def to_output(self): + return "" + outputData = '%.2f' % obj + ' ' + str(0) + '\n' + for v in range(0, vehicle_count): + outputData += str(depot.index) + ' ' + ' '.join([str(customer.index) for customer in vehicle_tours[v]]) + ' ' + str(depot.index) + '\n' + + return outputData + + +def solve_it(input_data): + vrp = Vrp(input_data) + vrp.plot() + print(vrp.customer_count) + + return vrp.to_output() + + +if __name__ == "__main__": + file_location = "data/vrp_16_3_1" + with open(file_location, 'r') as f: + print(solve_it(f.read())) +