Skip to content

Commit

Permalink
Merged in tKe/tradedangerous/run-improvements (pull request #116)
Browse files Browse the repository at this point in the history
Destination-based pruning and loop option
  • Loading branch information
kfsone committed Mar 29, 2015
2 parents f3b504f + 029dcd3 commit 154c952
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion commands/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
metavar='SYSTEM',
default=None,
),
ParseArgument('--loop',
help='Return to the starting station.',
action='store_true',
default=False,
),
),
ParseArgument('--via',
help='Require specified systems/stations to be en-route.',
Expand Down Expand Up @@ -734,6 +739,9 @@ def validateRunArguments(tdb, cmdenv, calc):
if cmdenv.insurance >= (cmdenv.credits + arbitraryInsuranceBuffer):
raise CommandLineError("Insurance leaves no margin for trade")

if cmdenv.loop and cmdenv.unique:
raise CommandLineError("Cannot use --unique and --loop together")

checkOrigins(tdb, cmdenv, calc)
checkDestinations(tdb, cmdenv, calc)

Expand Down Expand Up @@ -788,7 +796,7 @@ def validateRunArguments(tdb, cmdenv, calc):
]
cmdenv.destinations = [
dest for dest in cmdenv.destinations
if destination.system in viaSystems
if dest.system in viaSystems
]
cmdenv.destSystems = [
dest.system for dest in cmdenv.destinations
Expand Down Expand Up @@ -1012,6 +1020,7 @@ def run(results, cmdenv, tdb):

pruneMod = cmdenv.pruneScores / 100

loopRoutes = []
for hopNo in range(numHops):
restrictTo = None
if hopNo == lastHop and stopStations:
Expand All @@ -1031,6 +1040,32 @@ def run(results, cmdenv, tdb):
if cmdenv.maxRoutes and len(routes) > cmdenv.maxRoutes:
routes = routes[:cmdenv.maxRoutes]

if cmdenv.destPlace or cmdenv.loop:
remainingDistance = (numHops - hopNo) * cmdenv.maxJumpsPer * cmdenv.maxLyPer
remainingDistanceSq = remainingDistance * remainingDistance

def canReachStopStation(r):
if cmdenv.loop:
stopSystems = {r.firstSystem}
else:
stopSystems = {stopStation.system
for stopStation in stopStations}
reachableSystems = [stopSystem.name()
for stopSystem in stopSystems
if r.lastSystem.distToSq(stopSystem) <= remainingDistanceSq]
if len(reachableSystems):
cmdenv.DEBUG1("Route {} can still reach: {}", r.str(), ', '.join(reachableSystems))
return True
else:
cmdenv.DEBUG1("Route {} too far from all end stations", r.str())
return False

preCrop = len(routes)
routes[:] = [x for x in routes if canReachStopStation(x)]
pruned = preCrop - len(routes)
if pruned:
cmdenv.NOTE("Pruned {} origins too far from any end stations", pruned)

if cmdenv.progress:
print("* Hop {:3n}: {:.>10n} origins".format(hopNo+1, len(routes)))
elif cmdenv.debug:
Expand Down Expand Up @@ -1103,6 +1138,18 @@ def run(results, cmdenv, tdb):
routes = routes[:1]
break

if cmdenv.loop:
for route in routes:
if route.lastStation == route.firstStation:
loopRoutes.append(route)

if cmdenv.loop:
routes = loopRoutes

# normalise the scores for fairness...
for route in routes:
route.score /= len(route.hops)

if not routes:
raise NoDataError(
"No profitable trades matched your critera, "
Expand Down

0 comments on commit 154c952

Please sign in to comment.