-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
55 lines (48 loc) · 1.45 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import math
import io
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Driver:
def __init__(self, distance=0.0, route=[]):
self.distanceTravelled = distance
self.route = route
class Load:
def __init__(self, id, pickup, dropoff):
self.id = id
self.pickup = pickup
self.dropoff = dropoff
self.assigned = None
self.delivery_distance = distanceBetweenPoints(pickup, dropoff)
def distanceBetweenPoints(p1, p2):
xDiff = p1.x - p2.x
yDiff = p1.y - p2.y
return math.sqrt(xDiff*xDiff + yDiff*yDiff)
def loadProblemFromFile(filePath):
f = open(filePath, "r")
problemStr = f.read()
f.close()
return loadProblemFromProblemStr(problemStr)
def getPointFromPointStr(pointStr):
pointStr = pointStr.replace("(","").replace(")","")
splits = pointStr.split(",")
return Point(float(splits[0]), float(splits[1]))
def loadProblemFromProblemStr(problemStr):
loads = []
buf = io.StringIO(problemStr)
gotHeader = False
while True:
line = buf.readline()
if not gotHeader:
gotHeader = True
continue
if len(line) == 0:
break
line = line.replace("\n", "")
splits = line.split()
id = splits[0]
pickup = getPointFromPointStr(splits[1])
dropoff = getPointFromPointStr(splits[2])
loads.append(Load(id, pickup, dropoff))
return loads