Optimize interrogation, add max_eq solution

This commit is contained in:
Orien Vandenbergh 2016-08-30 10:56:01 -06:00
parent 87f62712ea
commit 43241be2f3
3 changed files with 62 additions and 28 deletions

View File

@ -0,0 +1,44 @@
#!/usr/bin/env python
# Foreach minion, x[0] = time, x[1]/x[2] = probability of response.
inputs = [ [[5, 1, 5], [10, 1, 2]], [[390, 185, 624], [686, 351, 947], [276, 1023, 1024], [199, 148, 250]] ]
solutions = [ [1,0], [2,3,0,1] ]
minions = inputs
def answer(minions):
"""Calculate the optimal interrogation order, providing the answer in the shortest time"""
""" T[1] + (1-P[1])*T[2] + (1-P[1])(1-P[2])*T[3]"""
optimal = [ [], 0 ]
for i,minion in enumerate(minions):
optimal = goDeeper(optimal,[i],minions,1-probabilityOf(minion),minion[0])
return optimal[0]
def goDeeper(optimal,used,minions,pn,t):
"""My recursion function to evaluate all the combinations of interrogation orders"""
for i, minion in enumerate(minions):
if i not in used:
p = pn*(1-probabilityOf(minion))
time = t + (pn*minion[0])
curr = used+[i]
if len(curr) == len(minions):
if len(optimal[0])>0:
if time < optimal[1]:
optimal = [ curr, time ]
else:
optimal = [ curr, time ]
else:
optimal = goDeeper(optimal,curr,minions,p,time)
return optimal
def probabilityOf(minion):
return ((1.0*minion[1])/minion[2])
for i in range(0,len(solutions)):
print answer(inputs[i])
print solutions[i]

View File

@ -7,36 +7,14 @@ solutions = [ [1,0], [2,3,0,1] ]
minions = inputs
def answer(minions):
"""Calculate the optimal interrogation order, providing the answer in the shortest time"""
""" T[1] + (1-P[1])*T[2] + (1-P[1])(1-P[2])*T[3]"""
orderable = map(order,range(0,len(minions)),minions)
return map(index,sorted(orderable,key=lambda minion: minion[1]))
optimal = [ [], 0 ]
def order(i,minion):
return [i,minion[0]/(1.0*minion[1]/minion[2])]
for i,minion in enumerate(minions):
optimal = goDeeper(optimal,[i],minions,1-probabilityOf(minion),minion[0])
return optimal[0]
def goDeeper(optimal,used,minions,pn,t):
"""My recursion function to evaluate all the combinations of interrogation orders"""
for i, minion in enumerate(minions):
if i not in used:
p = pn*(1-probabilityOf(minion))
time = t + (pn*minion[0])
curr = used+[i]
if len(curr) == len(minions):
if len(optimal[0])>0:
if time < optimal[1]:
optimal = [ curr, time ]
else:
optimal = [ curr, time ]
else:
optimal = goDeeper(optimal,curr,minions,p,time)
return optimal
def probabilityOf(minion):
return ((1.0*minion[1])/minion[2])
def index(minion):
return minion[0]
for i in range(0,len(solutions)):
print answer(inputs[i])

View File

@ -0,0 +1,12 @@
#!/usr/bin/env python
samples = [ [[1,4,1],3], [[1,2],1] ]
def answer(x):
"""Find the number of cars that can be made an equal weight via bunny redistribution between cars"""
remainder = sum(x)%(len(x))
return len(x) if (remainder==0) else len(x)-1
for sample in samples:
print answer(sample[0])
print sample[1]