def OptimalEnergy(intersections, routes, number, vehicle, console): '''Get optimal route from pre-generated routes''' t0 = time.time() calcRoutes = [] for route, distance in routes: energy = 0 t = 0 #can't use time, because of module time for int1, int2, int3 in zip(route[:-2], route[1:-1], route[2:]): energy += intersections[int1][int2][int3].energy t += intersections[int1][int2][int3].time energy += intersections[route[-2]][route[-1]].micropath.energy t += intersections[route[-2]][route[-1]].micropath.time calcRoutes.append((route, energy, t, distance)) calcRoutes.sort(key=lambda a: a[1]) #sort by energy usage top = calcRoutes[:min(number*5, len(calcRoutes))] #get best routes, more than you need to filter near-duplicates newTop = util.filterRoutes(top, f=3) #take out very similar routes of greater energy newTop = newTop[:min(len(newTop), number)] #take only what you need output = [] for route, energy, t, distance in newTop: path = [intersections[route[0]][route[1]][route[2]][0]] for int1, int2, int3 in zip(route[:-2], route[1:-1], route[2:]): path.extend(intersections[int1][int2][int3][1:-1]) output.append(Longpath(path, vehicle, ('energy',energy), ('distance',distance), ('time',t))) console.add('Optimal Energy', error=': '+str(round(time.time()-t0, 3))) return output
def OptimalEnergy(intersections, routes, number, vehicle, console): '''Get optimal route from pre-generated routes''' t0 = time.time() calcRoutes = [] for route, distance in routes: energy = 0 t = 0 #can't use time, because of module time for int1, int2, int3 in zip(route[:-2], route[1:-1], route[2:]): energy += intersections[int1][int2][int3].energy t += intersections[int1][int2][int3].time energy += intersections[route[-2]][route[-1]].micropath.energy t += intersections[route[-2]][route[-1]].micropath.time calcRoutes.append((route, energy, t, distance)) calcRoutes.sort(key=lambda a: a[1]) #sort by energy usage top = calcRoutes[:min( number * 5, len(calcRoutes) )] #get best routes, more than you need to filter near-duplicates newTop = util.filterRoutes( top, f=3) #take out very similar routes of greater energy newTop = newTop[:min(len(newTop), number)] #take only what you need output = [] for route, energy, t, distance in newTop: path = [intersections[route[0]][route[1]][route[2]][0]] for int1, int2, int3 in zip(route[:-2], route[1:-1], route[2:]): path.extend(intersections[int1][int2][int3][1:-1]) output.append( Longpath(path, vehicle, ('energy', energy), ('distance', distance), ('time', t))) console.add('Optimal Energy', error=': ' + str(round(time.time() - t0, 3))) return output
def OptimalEnergyInitial(intersections, routes, number, console): '''Get optimal route using pre-generated routes.''' t0 = time.time() #get list of most promising routes output = [] for route, distance in routes: total = 0 for index, inter in enumerate(route[:-1]): total += intersections[inter][route[index + 1]].energy output.append((route, total)) considered = sorted(output, key=lambda a: a[1]) considered = considered[:min(len(output), 100)] print len(considered) output = util.filterRoutes(considered, f=2) print len(output) console.add('Initial Energy', error=': ' + str(time.time() - t0)) output = output[:min(len(output), max(number * 3, 15))] print len(output) return output
def OptimalEnergyInitial(intersections, routes, number, console): '''Get optimal route using pre-generated routes.''' t0 = time.time() #get list of most promising routes output = [] for route, distance in routes: total = 0 for index, inter in enumerate(route[:-1]): total += intersections[inter][route[index+1]].energy output.append((route, total)) considered = sorted(output, key=lambda a: a[1]) considered = considered[:min(len(output), 100)] print len(considered) output = util.filterRoutes(considered, f=2) print len(output) console.add('Initial Energy', error=': '+str(time.time()-t0)) output = output[:min(len(output), max(number*3, 15))] print len(output) return output