Skip to content

Commit

Permalink
Added --prune-score and --prune-hops to RUN
Browse files Browse the repository at this point in the history
These allow you to eliminate candidate routes early on in the
"run" command based on their performance compared to the current
leader.

For example

  --prune-score 22.5 --prune-hops 3

says that from the 3rd hop, begin eliminate routes which have
scored under 22.5% of the current best candidate. This can
significantly improve the time to calculate long runs.

But if the early hops are all poor performers it can keep you from
seeing a gold mine a few hops away that requires you to take a few
low-profit hits first.

E.g.

  -1-> 50cr/ton -2-> 90cr/ton -3-> 50cr/ton -4-> 50cr/ton
  -1-> 10cr/ton -2-> 50cr/ton -3-> 20cr/ton -4-> 900cr/ton

"--prune-score 50 --prune-hops 4"

would cause you to miss the second option.
  • Loading branch information
kfsone committed Jan 18, 2015
1 parent 7b329a9 commit b33b3f1
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 68 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014
==============================================================================

[wip]
. (kfsone) Added '--prune-score' and '--prune-hops' to run; these let
you discard routes that are under-performing early on which
can make calculating longer runs more efficient.

v6.7.0 Jan 17 2015
. (kfsone) ".prices" import automatically creates local placeholders
for unknown stations when using "-i", e.g:
Expand Down
133 changes: 76 additions & 57 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,51 @@ RUN sub-command:
This command provides the primary trade run calculator functionality (it provides
the functionality of the older TradeDangerous versions prior to 3.1)

Ship/Trade options:
--capacity N
--cap N
Maximum items you can carry on each hop.

--credits N
--cr N
How many credits to start with
e.g.
--credits 20000

--ly-per N.NN
--ly N.NN
Maximum distance your ship can jump between systems at full capacity.
NOTE: You can increase your range by selling your weapons.
e.g.
--ly-per 19.1
--ly-per 3

--empty-ly N.NN
--emp N.NN
DEFAULT: same as --ly-per
How far your ship can jump when empty (used by --start-jumps)

--limit N DEFAULT: 0
If set, limits the maximum number of units of any cargo
item you will buy on any trade hop, incase you want to
hedge your bets or be a good economy citizen.
e.g.
--capacity 16 --limit 8

--insurance N DEFAULT: 0
--ins N
How many credits to hold back for insurance purposes
e.g.
--insurance 1000
--ins 5000

--margin N.NN DEFAULT: 0.01
At the end of each hop, reduce the profit by this much (0.02 = 2%),
to allow a margin of error in the accuracy of prices.
e.g.
--margin 0 (no margin)
--margin 0.01 (1% margin)

Route options:
--from <station or system>
Lets you specify the starting station
Expand Down Expand Up @@ -238,6 +283,21 @@ RUN sub-command:
--via Enterprise
--via Chango

--hops N
DEFAULT: 2
Maximum number of hops (number of cargo pickups)
e.g.
--hops 8

--jumps-per N
--jum N
DEFAULT: 2
Limit the number of systems jumped to between each station
e.g.
-jumps-per 5


Filter options:
--max-days-old N.NN
-MD N.NN
Filters out price data that exceeds a specified age in days
Expand Down Expand Up @@ -278,22 +338,22 @@ RUN sub-command:
--ls-max 10000
--ls-m 32000

--unique
--uni
Only show routes which do not visit any station twice

--hops N
DEFAULT: 2
Maximum number of hops (number of cargo pickups)
--prune-score N.NN
DEFAULT: 0
After a number of hops (controlled by --prune-hops), eliminate
candidate routes which have under-performed the leading candidate.
NOTE: This can speed up long run calculations, but it can also
cause you to miss gold-mines that are a just a few hops away...
e.g.
--hops 8
--prune-score 12.5 (prune routes scoring less than 10% of the leader)

--jumps-per N
--jum N
DEFAULT: 2
Limit the number of systems jumped to between each station
--prune-hops N
DEFAULT: 3
Being applying "--prune-score" from this hop onward. Set 0 to disable.
NOTE: This can speed up long run calculations, but it can also
cause you to miss gold-mines that are a just a few hops away...
e.g.
-jumps-per 5
--prune-hop 4 --prune-score 22.5

--avoid ITEM/SYSTEM/STATION
--avoid AVOID,AVOID,...,AVOID
Expand All @@ -307,50 +367,9 @@ RUN sub-command:
--avoid prise
--av gold,aulin,enterprise,anderson

Ship/Trade options:
--capacity N
--cap N
Maximum items you can carry on each hop.

--credits N
--cr N
How many credits to start with
e.g.
--credits 20000

--ly-per N.NN
--ly N.NN
Maximum distance your ship can jump between systems at full capacity.
NOTE: You can increase your range by selling your weapons.
e.g.
--ly-per 19.1
--ly-per 3

--empty-ly N.NN
--emp N.NN
DEFAULT: same as --ly-per
How far your ship can jump when empty (used by --start-jumps)

--limit N DEFAULT: 0
If set, limits the maximum number of units of any cargo
item you will buy on any trade hop, incase you want to
hedge your bets or be a good economy citizen.
e.g.
--capacity 16 --limit 8

--insurance N DEFAULT: 0
--ins N
How many credits to hold back for insurance purposes
e.g.
--insurance 1000
--ins 5000

--margin N.NN DEFAULT: 0.01
At the end of each hop, reduce the profit by this much (0.02 = 2%),
to allow a margin of error in the accuracy of prices.
e.g.
--margin 0 (no margin)
--margin 0.01 (1% margin)
--unique
--uni
Only show routes which do not visit any station twice

Other options:
--routes N DEFAULT: 1
Expand Down
41 changes: 36 additions & 5 deletions commands/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@
default=False,
dest='x52pro',
),
ParseArgument('--prune-score',
help='From the 3rd hop on, only consider routes which have ' \
'at least this percentage of the current best route''s score.',
dest='pruneScores',
type=float,
default=0,
),
ParseArgument('--prune-hops',
help='Changes which hop --prune-score takes effect from.',
default=3,
type=int,
dest='pruneHops',
),
]

######################################################################
Expand Down Expand Up @@ -584,6 +597,13 @@ def validateRunArguments(tdb, cmdenv):
if cmdenv.mfd:
cmdenv.mfd.display("Loading Trades")

if cmdenv.pruneScores and cmdenv.pruneHops:
if cmdenv.pruneScores > 100:
raise CommandLineError("--prune-score value percentage exceed 100.")
if cmdenv.pruneHops < 3:
raise CommandLineError("--prune-hops must 3 or more.")
else:
cmdenv.pruneScores = cmdenv.pruneHops = 0

######################################################################

Expand Down Expand Up @@ -668,18 +688,29 @@ def run(results, cmdenv, tdb):
results.summary = ResultRow()
results.summary.exception = ""

for hopNo in range(numHops):
if not cmdenv.quiet and not cmdenv.debug:
print("* Hop {:3n}: {:.>10n} routes".format(hopNo+1, len(routes)), end='\r')
elif cmdenv.debug:
cmdenv.DEBUG0("Hop {}...", hopNo+1)
pruneMod = cmdenv.pruneScores / 100

for hopNo in range(numHops):
restrictTo = None
if hopNo == lastHop and stopStations:
restrictTo = set(stopStations)
elif len(viaSet) > cmdenv.adhocHops:
restrictTo = viaSet

if pruneMod and hopNo + 1 >= cmdenv.pruneHops and len(routes) > 10:
routes.sort()
bestScore, worstScore = routes[0].score, routes[-1].score
threshold = bestScore * pruneMod
oldLen = len(routes)
while routes[-1].score < threshold:
routes.pop()
cmdenv.NOTE("Pruned {} routes".format(oldLen - len(routes)))

if not cmdenv.quiet and not cmdenv.debug:
print("* Hop {:3n}: {:.>10n} routes".format(hopNo+1, len(routes)), end='\r')
elif cmdenv.debug:
cmdenv.DEBUG0("Hop {}...", hopNo+1)

newRoutes = calc.getBestHops(routes, restrictTo=restrictTo)
if not newRoutes and hopNo > 0:
if restrictTo:
Expand Down
5 changes: 5 additions & 0 deletions scripts/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ CR=270742
# Set the position of the update window relative to top-right of screen
UPD_ARGS="-wx=-40 -wy=40"

# After a given number of hops, discard candidates that have scored less
# than the given percentage of the best candidate.
PRUNE_HOPS=3 # after 3 hops
PRUNE_SCORE=20 # percentage

# You can only have one set of variables "live" at a time, comment out
# old ships while you are not flying them, then you can just uncomment
# them later on.
Expand Down
14 changes: 8 additions & 6 deletions scripts/tdrun
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ then
fi

cmd="${TRADEPY} run -vv \
--ly ${MAXLY} \
--empty ${EMPTYLY} \
--cap ${CAP} \
--jumps ${JUMPS} \
--cr ${CR} \
--from \"${origin}\" \
--ly=${MAXLY} \
--empty=${EMPTYLY} \
--cap=${CAP} \
--jumps=${JUMPS} \
--cr=${CR} \
--from=\"${origin}\" \
--prune-score=${PRUNESCORE:-5} \
--prune-hops=${PRUNEHOPS:-4} \
$@"
echo \$ $cmd
eval "$cmd"
Expand Down

0 comments on commit b33b3f1

Please sign in to comment.