-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowsolver.py
More file actions
executable file
·54 lines (46 loc) · 2.1 KB
/
flowsolver.py
File metadata and controls
executable file
·54 lines (46 loc) · 2.1 KB
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
#! /usr/bin/python3
from scipy import spatial
from tqdm import tqdm
from car import Car
from ride import Ride
from score import Score
from solvers.basesolver import BaseSolver
class FlowRide(Ride):
def __init__(self, rid, x1, y1, x2, y2, step_min, step_max):
super().__init__(rid, x1, y1, x2, y2, step_min, step_max)
self.flow = 0
def compute_flow(self, kdtree, radius):
self.flow = len(kdtree.query_ball_point((self.x2, self.y2), radius))
class FlowSolver(BaseSolver):
def __init__(self):
super().__init__()
def get_solution(self, rides_list, rows, columns, vehicles, rides, bonus, steps, args):
cars = [Car() for _ in range(vehicles)]
score = Score()
rides_instance_list = [FlowRide(rid, *data) for rid, data in enumerate(rides_list)]
departures = [(r.x1, r.y1) for r in rides_instance_list]
kdtree = spatial.KDTree(departures)
flow_bar = tqdm(rides_instance_list) if args.progress else rides_instance_list
for r in flow_bar:
r.compute_flow(kdtree, 20)
rides_sorted = sorted(rides_instance_list, key=lambda ride: (ride.step_min, ride.flow))
rides_sorted = tqdm(rides_sorted) if args.progress else rides_sorted
for r in rides_sorted:
candidates = [c for c in cars if c.can_finish_in_time(r, steps)]
cars_with_bonus = [c for c in candidates if c.can_start_on_time(r)]
if cars_with_bonus:
best_car = min(cars_with_bonus, key=lambda c: c.wait_time(r))
score.bonus_score += bonus
score.raw_score += r.distance()
score.wait_time += best_car.wait_time(r)
score.assigned += 1
best_car.assign(r)
elif candidates:
best_car = min(candidates, key=lambda c: c.distance_to_ride_start(r))
score.raw_score += r.distance()
score.assigned += 1
best_car.assign(r)
else:
score.unassigned += 1
rides_solution = [c.assigned_rides for c in cars]
return rides_solution, score