From 2dd76439095206dd070f8232fba5894e654d0186 Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Wed, 16 Jun 2021 11:32:05 -0500 Subject: [PATCH] Add command-line parameter for time limit You can now pass an optional time limit to the script, which causes the solver to return the best solution it has once the duration has elapsed. --- Bone Market Solver.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Bone Market Solver.py b/Bone Market Solver.py index cc42b13..ba05d40 100644 --- a/Bone Market Solver.py +++ b/Bone Market Solver.py @@ -1146,7 +1146,7 @@ class Fluctuation(enum.Enum): AMALGAMY = 2 -def Solve(bone_market_fluctuations, zoological_mania, desired_buyer = None, maximum_cost = cp_model.INT32_MAX, maximum_exhaustion = cp_model.INT32_MAX, stdscr = None): +def Solve(bone_market_fluctuations, zoological_mania, desired_buyer = None, maximum_cost = cp_model.INT32_MAX, maximum_exhaustion = cp_model.INT32_MAX, time_limit = float('inf'), stdscr = None): model = cp_model.CpModel() actions = {} @@ -2083,6 +2083,7 @@ def Solve(bone_market_fluctuations, zoological_mania, desired_buyer = None, maxi solver = cp_model.CpSolver() solver.parameters.num_search_workers = os.cpu_count() + solver.parameters.max_time_in_seconds = time_limit # There's no window in verbose mode if stdscr is None: @@ -2169,10 +2170,17 @@ def main(): help='whether the solver should output search progress rather than showing intermediate solutions', dest='verbose' ) + parser.add_argument( + '-t', '--time-limit', + default=float('inf'), + type=float, + help='maximum number of seconds that solver runs for', + dest='time_limit' + ) args = parser.parse_args() - arguments = (args.bone_market_fluctuations, args.zoological_mania, args.desired_buyer, args.maximum_cost, args.maximum_exhaustion) + arguments = (args.bone_market_fluctuations, args.zoological_mania, args.desired_buyer, args.maximum_cost, args.maximum_exhaustion, args.time_limit) if not args.verbose: def WrappedSolve(stdscr, arguments):