finish level4/running-with-bunnies #4
@ -1,52 +1,53 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import itertools
|
|
||||||
|
|
||||||
def solution(times, times_limit):
|
def solution(times, times_limit):
|
||||||
# find shortest path
|
best_times = get_best_times(times)
|
||||||
nodes = len(times)
|
best_plan = get_best_plan(best_times, times_limit)
|
||||||
|
return best_plan
|
||||||
|
|
||||||
|
def get_best_times(times):
|
||||||
t = np.array(times)
|
t = np.array(times)
|
||||||
shortest = np.reshape(t, (nodes, 1, nodes))
|
spots = len(times)
|
||||||
one_step = np.reshape(t.T, (1, nodes, nodes))
|
bunny_count = spots-2
|
||||||
#print shortest
|
best_times = np.reshape(t, (spots, 1, spots))
|
||||||
#print one_step
|
one_step = np.reshape(t.T, (1, spots, spots))
|
||||||
infinite_time = False
|
infinite_time = False
|
||||||
while not infinite_time:
|
while not infinite_time:
|
||||||
temp = np.reshape(np.amin(shortest + one_step, axis=2), (nodes, 1, nodes))
|
temp = np.reshape(np.amin(best_times + one_step, axis=2), (spots, 1, spots))
|
||||||
#print temp
|
new_best_times = np.minimum(best_times, temp)
|
||||||
new_shortest = np.minimum(shortest, temp)
|
if np.array_equal(best_times, new_best_times):
|
||||||
#print new_shortest
|
best_times = new_best_times
|
||||||
if np.array_equal(shortest, new_shortest):
|
|
||||||
shortest = new_shortest
|
|
||||||
break;
|
break;
|
||||||
shortest = new_shortest
|
best_times = new_best_times
|
||||||
for i in range(nodes):
|
for i in range(spots):
|
||||||
if shortest[i][0][i] < 0:
|
if best_times[i][0][i] < 0:
|
||||||
infinite_time = True
|
infinite_time = True
|
||||||
break;
|
break;
|
||||||
if infinite_time:
|
if infinite_time:
|
||||||
return list(range(nodes-2))
|
return list(range(bunny_count))
|
||||||
shortest = np.reshape(shortest, (nodes, nodes))
|
best_times = np.reshape(best_times, t.shape)
|
||||||
#print shortest
|
return best_times
|
||||||
|
|
||||||
# calc time for all paths
|
def get_best_plan(best_times, times_limit):
|
||||||
best_plan = []
|
best_plan = []
|
||||||
for order in itertools.permutations(range(1, nodes)):
|
last_position = 0
|
||||||
|
bulkhead = len(best_times)-1
|
||||||
|
bunny_count = len(best_times)-2
|
||||||
|
for plan in get_plans(bunny_count):
|
||||||
estimated_time = 0
|
estimated_time = 0
|
||||||
order = (0,) + order
|
for bunny_id in plan:
|
||||||
bunnies = []
|
next_position = bunny_id+1
|
||||||
for i in range(nodes-1):
|
estimated_time += best_times[last_position][next_position]
|
||||||
start_node, end_node = order[i], order[i+1]
|
last_position = next_position
|
||||||
estimated_time += shortest[start_node][end_node]
|
estimated_time += best_times[last_position][bulkhead]
|
||||||
if end_node == nodes-1:
|
if estimated_time <= times_limit:
|
||||||
break;
|
best_plan = get_better_plan(best_plan, plan)
|
||||||
bunnies += [end_node-1]
|
|
||||||
if estimated_time <= times_limit and len(bunnies) > 0:
|
|
||||||
bunnies.sort()
|
|
||||||
best_plan = get_better_plan(best_plan, bunnies)
|
|
||||||
#print best_plan
|
|
||||||
|
|
||||||
return best_plan
|
return best_plan
|
||||||
|
|
||||||
|
# TODO: implement get_plans
|
||||||
|
def get_plans(bunny_count):
|
||||||
|
return [[1], [0], [1, 2], [0, 1, 2], [0, 1]]
|
||||||
|
|
||||||
def get_better_plan(a, b):
|
def get_better_plan(a, b):
|
||||||
if len(a) > len(b):
|
if len(a) > len(b):
|
||||||
return a
|
return a
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user