def run_solver_on(suite_path, solver_name, task_path, budget, store_answers, seed=None): """Run a solver.""" if seed is not None: borg.statistics.set_prng_seeds(seed) suite = borg.load_solvers(suite_path) with suite.domain.task_from_path(task_path) as task: with borg.accounting() as accountant: answer = suite.solvers[solver_name](task)(budget) succeeded = suite.domain.is_final(task, answer) cost = accountant.total.cpu_seconds logger.info( "%s %s in %.2f (of %.2f) on %s", solver_name, "succeeded" if succeeded else "failed", cost, budget, os.path.basename(task_path), ) if store_answers: return (task_path, solver_name, budget, cost, succeeded, answer) else: return (task_path, solver_name, budget, cost, succeeded, None)
def __init__(self, path): self.path = path self.support_paths = {} with open(path) as opb_file: self.header = instance.parse_opb_file_header(opb_file.readline()) (self.raw_M, self.raw_N, self.nonlinear) = self.header if self.nonlinear: linearizer = os.path.join(borg.defaults.solvers_root, "PBSimple/PBlinearize") (linearized, _) = cargo.check_call_capturing([linearizer, self.path]) (fd, self.linearized_path) = tempfile.mkstemp(suffix = ".opb") self.support_paths["linearized"] = self.linearized_path with os.fdopen(fd, "w") as linearized_file: linearized_file.write(linearized) logger.info("wrote linearized instance to %s", self.linearized_path) else: self.linearized_path = path with borg.accounting() as accountant: with open(self.linearized_path) as opb_file: self.opb = instance.parse_opb_file_linear(opb_file) logger.info("parsing took %.2f s", accountant.total.cpu_seconds)
def __init__(self, path, linearizer_path=None): self.path = path self.support_paths = {} with open(path) as opb_file: self.header = instance.parse_opb_file_header(opb_file.readline()) (self.raw_M, self.raw_N, self.nonlinear) = self.header if self.nonlinear: assert linearizer_path is not None (linearized, _) = borg.util.check_call_capturing([linearizer_path, self.path]) (fd, self.linearized_path) = tempfile.mkstemp(suffix=".opb") self._was_linearized = True self.support_paths["linearized"] = self.linearized_path with os.fdopen(fd, "w") as linearized_file: linearized_file.write(linearized) logger.info("wrote linearized instance to %s", self.linearized_path) else: self.linearized_path = path self._was_linearized = False with borg.accounting() as accountant: with open(self.linearized_path) as opb_file: self.opb = instance.parse_opb_file_linear(opb_file) logger.info("parsing took %.2f s", accountant.total.cpu_seconds)
def main( suite_path, solver_name, instance_path, specifics = None, budget = 1e6, max_length = None, seed = None, *args ): """Make a single solver run for ParamILS.""" suite = borg.load_solvers(suite_path) if seed is not None: borg.statistics.set_prng_seeds(seed) suite = borg.load_solvers(suite_path) solver = suite.solvers[solver_name].with_args(args) with suite.domain.task_from_path(instance_path) as task: with borg.accounting() as accountant: answer = solver(task)(budget) succeeded = suite.domain.is_final(task, answer) print \ "Result for ParamILS: {solved}, {run_time}, {run_length}, {best}, {seed}".format( solved = "SAT" if succeeded else "TIMEOUT", run_time = accountant.total.cpu_seconds, run_length = -1, best = -1, seed = seed, )
def main(suite_path, solver_name, instance_path, specifics=None, budget=1e6, max_length=None, seed=None, *args): """Make a single solver run for ParamILS.""" suite = borg.load_solvers(suite_path) if seed is not None: borg.statistics.set_prng_seeds(seed) suite = borg.load_solvers(suite_path) solver = suite.solvers[solver_name].with_args(args) with suite.domain.task_from_path(instance_path) as task: with borg.accounting() as accountant: answer = solver(task)(budget) succeeded = suite.domain.is_final(task, answer) print \ "Result for ParamILS: {solved}, {run_time}, {run_length}, {best}, {seed}".format( solved = "SAT" if succeeded else "TIMEOUT", run_time = accountant.total.cpu_seconds, run_length = -1, best = -1, seed = seed, )
def simulate_run(run, maker, train_data, test_data): """Simulate portfolio execution on a train/test split.""" split_id = uuid.uuid4() budget = test_data.common_budget #budget = test_data.common_budget / 4 suite = borg.fake.FakeSuite(test_data) solver = maker(suite, train_data, test_data) rows = [] for (i, instance_id) in enumerate(test_data.run_lists): logger.info("simulating run %i/%i on %s", i, len(test_data), instance_id) with suite.domain.task_from_path(instance_id) as instance: with borg.accounting() as accountant: answer = solver.start(instance).run_then_stop(budget) succeeded = suite.domain.is_final(instance, answer) cpu_cost = accountant.total.cpu_seconds logger.info( "%s %s on %s (%.2f CPU s)", maker.name, "succeeded" if succeeded else "failed", os.path.basename(instance), cpu_cost, ) success_str = "TRUE" if succeeded else "FALSE" rows.append([run["category"], maker.name, budget, cpu_cost, success_str, split_id]) return rows
def run_solver_on(suite_path, solver_name, task_path, budget, store_answers, seed = None): """Run a solver.""" if seed is not None: borg.statistics.set_prng_seeds(seed) suite = borg.load_solvers(suite_path) with suite.domain.task_from_path(task_path) as task: with borg.accounting() as accountant: answer = suite.solvers[solver_name](task)(budget) succeeded = suite.domain.is_final(task, answer) cost = accountant.total.cpu_seconds logger.info( "%s %s in %.2f (of %.2f) on %s", solver_name, "succeeded" if succeeded else "failed", cost, budget, os.path.basename(task_path), ) if store_answers: return (task_path, solver_name, budget, cost, succeeded, answer) else: return (task_path, solver_name, budget, cost, succeeded, None)
def __init__(self, path): self.path = path with borg.accounting() as accountant: with open(path) as task_file: self.instance = instance.parse_max_sat_file(task_file) logger.info("parsing took %.2f s", accountant.total.cpu_seconds)
def features_for_path(domain, task_path): logger.info("getting features of %s", os.path.basename(task_path)) with domain.task_from_path(task_path) as task: with borg.accounting() as accountant: (names, values) = domain.compute_features(task) return (["cpu_cost"] + list(names), [accountant.total.cpu_seconds] + list(values))
def __call__(self, task, suite, budget): """Run the portfolio.""" with borg.accounting() as accountant: # predict RTD weights if self._regress is None: initial_model = self._model else: (feature_names, feature_values) = suite.domain.compute_features(task) feature_dict = dict(zip(feature_names, feature_values)) feature_values_sorted = [ feature_dict[f] for f in sorted(feature_names) ] (predicted_weights, ) = numpy.log( self._regress.predict([task], [feature_values_sorted])) initial_model = self._model.with_weights(predicted_weights) # compute and execute a solver schedule plan = [] failures = [] for i in xrange(self._runs_limit): elapsed = accountant.total.cpu_seconds if budget.cpu_seconds <= elapsed: break if len(plan) == 0: model = initial_model.condition(failures) remaining = budget.cpu_seconds - elapsed remaining_b = int(numpy.ceil(remaining / model.interval)) plan = \ self._planner.plan( model.log_survival[..., :remaining_b], model.log_weights, ) (s, b) = plan.pop(0) remaining = budget.cpu_seconds - accountant.total.cpu_seconds duration = min(remaining, (b + 1) * model.interval) process = suite.solvers[self._solver_names[s]].start(task) answer = process.run_then_stop(duration) if suite.domain.is_final(task, answer): return answer else: failures.append((s, b)) return None
def __call__(self, task, budget, cores = 1): assert cores == 1 # obtain features with borg.accounting() as features_accountant: (_, features) = self._domain.compute_features(task) # select a solver scores = [(s, m.predict_proba([features])[0, -1]) for (s, m) in self._models.items()] (name, probability) = max(scores, key = lambda (_, p): p) # run the solver run_cpu_budget = borg.unicore_cpu_budget(budget - features_accountant.total) return self._domain.solvers[name](task)(run_cpu_budget)
def features_for_path(domain, task_path): # bring back relevant globals import os.path import borg logger = borg.get_logger(__name__, default_level="INFO") # collect features logger.info("getting features of %s", os.path.basename(task_path)) with domain.task_from_path(task_path) as task: with borg.accounting() as accountant: (names, values) = domain.compute_features(task) return (task_path, ["cpu_cost"] + list(names), [accountant.total.cpu_seconds] + list(values))
def __call__(self, task, suite, budget): """Run the portfolio.""" with borg.accounting() as accountant: # predict RTD weights if self._regress is None: initial_model = self._model else: (feature_names, feature_values) = suite.domain.compute_features(task) feature_dict = dict(zip(feature_names, feature_values)) feature_values_sorted = [feature_dict[f] for f in sorted(feature_names)] (predicted_weights,) = numpy.log(self._regress.predict([task], [feature_values_sorted])) initial_model = self._model.with_weights(predicted_weights) # compute and execute a solver schedule plan = [] failures = [] for i in xrange(self._runs_limit): elapsed = accountant.total.cpu_seconds if budget.cpu_seconds <= elapsed: break if len(plan) == 0: model = initial_model.condition(failures) remaining = budget.cpu_seconds - elapsed remaining_b = int(numpy.ceil(remaining / model.interval)) plan = \ self._planner.plan( model.log_survival[..., :remaining_b], model.log_weights, ) (s, b) = plan.pop(0) remaining = budget.cpu_seconds - accountant.total.cpu_seconds duration = min(remaining, (b + 1) * model.interval) process = suite.solvers[self._solver_names[s]].start(task) answer = process.run_then_stop(duration) if suite.domain.is_final(task, answer): return answer else: failures.append((s, b)) return None
def features_for_path(domain, task_path): # bring back relevant globals import os.path import borg logger = borg.get_logger(__name__, default_level = "INFO") # collect features logger.info("getting features of %s", os.path.basename(task_path)) with domain.task_from_path(task_path) as task: with borg.accounting() as accountant: (names, values) = domain.compute_features(task) return ( task_path, ["cpu_cost"] + list(names), [accountant.total.cpu_seconds] + list(values))
def run_validation(name, domain, train_paths, test_paths, budget, split): """Make a validation run.""" solver = borg.portfolios.named[name](domain, train_paths, 50.0, 42) # XXX successes = [] logger.info("running portfolio %s with per-task budget %.2f", name, budget) for test_path in test_paths: print test_path with domain.task_from_path(test_path) as test_task: cost_budget = borg.Cost(cpu_seconds = budget) with borg.accounting() as accountant: answer = solver(test_task, cost_budget) succeeded = domain.is_final(test_task, answer) cpu_cost = accountant.total.cpu_seconds if succeeded: successes.append(cpu_cost) logger.info( "%s %s on %s (%.2f CPU s)", name, "succeeded" if succeeded else "failed", os.path.basename(test_path), cpu_cost, ) rate = float(len(successes)) / len(test_paths) logger.info("method %s had final success rate %.2f", name, rate) return \ zip( itertools.repeat(name), itertools.repeat(budget), sorted(successes), numpy.arange(len(successes) + 1.0) / len(test_paths), itertools.repeat(split), )
def run_solver_on(solvers_path, solver_name, task_path, budget): """Run a solver.""" bundle = borg.load_solvers(solvers_path) with bundle.domain.task_from_path(task_path) as task: with borg.accounting() as accountant: answer = bundle.solvers[solver_name](task)(budget) succeeded = bundle.domain.is_final(task, answer) cost = accountant.total.cpu_seconds logger.info( "%s %s in %.2f (of %.2f) on %s", solver_name, "succeeded" if succeeded else "failed", cost, budget, os.path.basename(task_path), ) return (solver_name, budget, cost, succeeded, answer)
def simulate_run(run, maker, train_data, test_data): """Simulate portfolio execution on a train/test split.""" split_id = uuid.uuid4() budget = test_data.common_budget #budget = test_data.common_budget / 4 suite = borg.fake.FakeSuite(test_data) solver = maker(suite, train_data, test_data) rows = [] for (i, instance_id) in enumerate(test_data.run_lists): logger.info("simulating run %i/%i on %s", i, len(test_data), instance_id) with suite.domain.task_from_path(instance_id) as instance: with borg.accounting() as accountant: answer = solver.start(instance).run_then_stop(budget) succeeded = suite.domain.is_final(instance, answer) cpu_cost = accountant.total.cpu_seconds logger.info( "%s %s on %s (%.2f CPU s)", maker.name, "succeeded" if succeeded else "failed", os.path.basename(instance), cpu_cost, ) success_str = "TRUE" if succeeded else "FALSE" rows.append([ run["category"], maker.name, budget, cpu_cost, success_str, split_id ]) return rows
def simulate_run(run, maker, all_data, train_mask, test_mask, instances, independent, mixture): """Simulate portfolio execution on a train/test split.""" train_data = all_data.masked(train_mask) test_data = all_data.masked(test_mask) if instances is not None: ids = sorted(train_data.run_lists, key = lambda _: numpy.random.rand())[:instances] train_data = train_data.filter(*ids) if independent: train_data = train_data.collect_independent(mixture).only_nonempty() else: train_data = train_data.collect_systematic(mixture).only_nonempty() budget = test_data.common_budget #budget = test_data.common_budget / 2 # XXX suite = borg.fake.FakeSuite(test_data) if maker.subname == "preplanning-dir": model_kwargs = {"K": 64} if "set_alpha" in maker.variants: model_kwargs["alpha"] = 1e-2 else: model_kwargs = {} solver = maker(suite, train_data, model_kwargs = model_kwargs) successes = [] for (i, instance_id) in enumerate(test_data.run_lists): logger.info("simulating run %i/%i on %s", i, len(test_data), instance_id) with suite.domain.task_from_path(instance_id) as instance: with borg.accounting() as accountant: answer = solver.start(instance).run_then_stop(budget) succeeded = suite.domain.is_final(instance, answer) logger.info( "%s %s on %s (%.2f CPU s)", maker.name, "succeeded" if succeeded else "failed", os.path.basename(instance), accountant.total.cpu_seconds, ) if succeeded: successes.append(accountant.total.cpu_seconds) logger.info( "%s had %i successes over %i instances", maker.name, len(successes), len(test_data), ) description = "{0} ({1})".format(mixture, "Sep." if independent else "Sys.") return ( description, maker.name, instances, len(successes), numpy.mean(successes), numpy.median(successes), )
def simulate_run(run, maker, all_data, train_mask, test_mask, instances, independent, mixture): """Simulate portfolio execution on a train/test split.""" train_data = all_data.masked(train_mask) test_data = all_data.masked(test_mask) if instances is not None: ids = sorted(train_data.run_lists, key=lambda _: numpy.random.rand())[:instances] train_data = train_data.filter(*ids) if independent: train_data = train_data.collect_independent(mixture).only_nonempty() else: train_data = train_data.collect_systematic(mixture).only_nonempty() budget = test_data.common_budget #budget = test_data.common_budget / 2 # XXX suite = borg.fake.FakeSuite(test_data) if maker.subname == "preplanning-dir": model_kwargs = {"K": 64} if "set_alpha" in maker.variants: model_kwargs["alpha"] = 1e-2 else: model_kwargs = {} solver = maker(suite, train_data, model_kwargs=model_kwargs) successes = [] for (i, instance_id) in enumerate(test_data.run_lists): logger.info("simulating run %i/%i on %s", i, len(test_data), instance_id) with suite.domain.task_from_path(instance_id) as instance: with borg.accounting() as accountant: answer = solver.start(instance).run_then_stop(budget) succeeded = suite.domain.is_final(instance, answer) logger.info( "%s %s on %s (%.2f CPU s)", maker.name, "succeeded" if succeeded else "failed", os.path.basename(instance), accountant.total.cpu_seconds, ) if succeeded: successes.append(accountant.total.cpu_seconds) logger.info( "%s had %i successes over %i instances", maker.name, len(successes), len(test_data), ) description = "{0} ({1})".format(mixture, "Sep." if independent else "Sys.") return ( description, maker.name, instances, len(successes), numpy.mean(successes), numpy.median(successes), )
def __call__(self, task, bundle, budget, cores = 1): with borg.accounting(): return self._solve(task, bundle, budget, cores)