From b33b3f186b3a49d8beebca5bf4c90962a29f702a Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Sat, 17 Jan 2015 16:41:32 -0800 Subject: [PATCH] Added --prune-score and --prune-hops to RUN 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. --- CHANGES.txt | 5 ++ README.txt | 133 +++++++++++++++++++++++++------------------- commands/run_cmd.py | 41 ++++++++++++-- scripts/config.sh | 5 ++ scripts/tdrun | 14 +++-- 5 files changed, 130 insertions(+), 68 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 55f99449..3bf86cf7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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: diff --git a/README.txt b/README.txt index 45a95e8a..c75bc9c6 100644 --- a/README.txt +++ b/README.txt @@ -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 Lets you specify the starting station @@ -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 @@ -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 @@ -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 diff --git a/commands/run_cmd.py b/commands/run_cmd.py index aa925955..4e328f5a 100644 --- a/commands/run_cmd.py +++ b/commands/run_cmd.py @@ -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', + ), ] ###################################################################### @@ -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 ###################################################################### @@ -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: diff --git a/scripts/config.sh b/scripts/config.sh index 58800d4d..c7126fdd 100644 --- a/scripts/config.sh +++ b/scripts/config.sh @@ -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. diff --git a/scripts/tdrun b/scripts/tdrun index a61d39da..725b6414 100755 --- a/scripts/tdrun +++ b/scripts/tdrun @@ -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"