Start VRP.
parent
3461738861
commit
cd3d564113
|
@ -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()))
|
||||
|
Loading…
Reference in New Issue