Exemple #1
0
def enable_output():
    """Set up competition-compliant output."""

    # configure the default global level
    borg.get_logger(level = borg.defaults.root_log_level)

    # set up output
    handler = logging.StreamHandler(sys.stdout)

    handler.setFormatter(CompetitionFormatter())
    handler.setLevel(logging.NOTSET)

    logging.root.addHandler(handler)
Exemple #2
0
def enable_output():
    """Set up competition-compliant output."""

    # configure the default global level
    borg.get_logger(level=borg.defaults.root_log_level)

    # set up output
    handler = logging.StreamHandler(sys.stdout)

    handler.setFormatter(CompetitionFormatter())
    handler.setLevel(logging.NOTSET)

    logging.root.addHandler(handler)
Exemple #3
0
def main(
    model_path,
    solvers_path,
    input_path,
    seed = 42,
    budget = 3600.0,
    cores = 1,
    speed = borg.defaults.machine_speed,
    quiet = False
    ):
    """Solve a problem instance."""

    # XXX hackish
    borg.defaults.machine_speed = speed

    try:
        # general setup
        enable_output()

        if not quiet:
            borg.get_logger("borg.solvers", level = "DETAIL")

        borg.statistics.set_prng_seeds(seed)

        # run the solver
        bundle = borg.load_solvers(solvers_path)

        logger.info("loaded portfolio model from %s", model_path)

        with open(model_path) as file:
            portfolio = pickle.load(file)

        logger.info("solving %s", input_path)

        with bundle.domain.task_from_path(input_path) as task:
            remaining = budget - borg.get_accountant().total.cpu_seconds
            answer = portfolio(task, bundle, borg.Cost(cpu_seconds = remaining), cores)

            return bundle.domain.show_answer(task, answer)
    except KeyboardInterrupt:
        print "\nc terminating on SIGINT"
Exemple #4
0
def main(model_path,
         solvers_path,
         input_path,
         seed=42,
         budget=3600.0,
         cores=1,
         speed=borg.defaults.machine_speed,
         quiet=False):
    """Solve a problem instance."""

    # XXX hackish
    borg.defaults.machine_speed = speed

    try:
        # general setup
        enable_output()

        if not quiet:
            borg.get_logger("borg.solvers", level="DETAIL")

        borg.statistics.set_prng_seeds(seed)

        # run the solver
        bundle = borg.load_solvers(solvers_path)

        logger.info("loaded portfolio model from %s", model_path)

        with open(model_path) as file:
            portfolio = pickle.load(file)

        logger.info("solving %s", input_path)

        with bundle.domain.task_from_path(input_path) as task:
            remaining = budget - borg.get_accountant().total.cpu_seconds
            answer = portfolio(task, bundle, borg.Cost(cpu_seconds=remaining),
                               cores)

            return bundle.domain.show_answer(task, answer)
    except KeyboardInterrupt:
        print "\nc terminating on SIGINT"
Exemple #5
0
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))
Exemple #6
0
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))
Exemple #7
0
def run_solver_on(suite_path, solver_name, task_path, budget, store_answers,
                  seed):
    """Run a solver."""

    # bring back relevant globals
    import os
    import borg

    logger = borg.get_logger(__name__, default_level="INFO")

    if seed is not None:
        borg.statistics.set_prng_seeds(seed)

    # run the solver
    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)
Exemple #8
0
def run_solver_on(suite_path, solver_name, task_path, budget, store_answers, seed):
    """Run a solver."""

    # bring back relevant globals
    import os
    import borg

    logger = borg.get_logger(__name__, default_level="INFO")

    if seed is not None:
        borg.statistics.set_prng_seeds(seed)

    # run the solver
    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)
"""@author: Bryan Silverthorn <*****@*****.**>"""

import os.path
import csv
import uuid
import numpy
import sklearn
import condor
import borg

logger = borg.get_logger(__name__, default_level="INFO")


def evaluate_split(run_data, alpha, split, train_mask, test_mask):
    """Evaluate a model on a train/test split."""

    training = run_data.masked(train_mask).collect_systematic([4])
    testing = run_data.masked(test_mask).collect_systematic([4])
    model = borg.models.MulEstimator(alpha=alpha)(training, 10, training)
    score = numpy.mean(borg.models.run_data_log_probabilities(model, testing))

    logger.info(
        "score at alpha = %.2f given %i runs from %i instances: %f",
        alpha,
        training.get_run_count(),
        len(training),
        score,
    )

    return [alpha, len(training), split, score]
Exemple #10
0
"""@author: Bryan Silverthorn <*****@*****.**>"""

import os
import pty
import subprocess
import borg

log = borg.get_logger(__name__)

def _child_preexec(environment):
    """Run in the child code prior to execution."""

    # update the environment
    for (key, value) in environment.iteritems():
        os.putenv(key, str(value))

    # start our own session
    os.setsid()

def spawn_pipe_session(arguments, environment = {}, cwd = None):
    """Spawn a subprocess in its own session."""

    popened = \
        subprocess.Popen(
            arguments,
            close_fds = True,
            stdin = subprocess.PIPE,
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE,
            preexec_fn = lambda: _child_preexec(environment),
            cwd = cwd,
Exemple #11
0
"""@author: Bryan Silverthorn <*****@*****.**>"""

import numpy
import borg

logger = borg.get_logger(__name__, default_level="DETAIL")


def read_median_cost(path):
    runs_data = numpy.recfromcsv(path, usemask=True)

    return numpy.median(runs_data["cost"])


@borg.annotations()
def main(local_path, train_path):
    """Compute the machine speed calibration ratio."""

    local_median = read_median_cost(local_path)
    train_median = read_median_cost(train_path)

    logger.info("local median run time is %.2f CPU seconds", local_median)
    logger.info("model median run time is %.2f CPU seconds", train_median)
    logger.info("local speed ratio is thus %f", local_median / train_median)


if __name__ == "__main__":
    borg.script(main)
Exemple #12
0
"""@author: Bryan Silverthorn <*****@*****.**>"""

import os.path
import csv
import itertools
import collections
import numpy
import borg

logger = borg.get_logger(__name__, default_level="INFO")


class RunRecord(object):
    """Record of a solver run."""

    def __init__(self, solver, budget, cost, success):
        """Initialize."""

        self.solver = solver
        self.budget = budget
        self.cost = cost
        self.success = success

    def __str__(self):
        return str((self.solver, self.budget, self.cost, self.success))

    def __repr__(self):
        return repr((self.solver, self.budget, self.cost, self.success))


class RunData(object):
Exemple #13
0
"""@author: Bryan Silverthorn <*****@*****.**>"""

import os
import select
import signal
import datetime
import collections
import borg

log = borg.get_logger(__name__)


class SessionTimeAccountant(object):
    """
    Track the total CPU (user) time for members of a session.

    Process accounting under Linux is a giant pain, especially without root
    access. In the general case, it is literally impossible (without patching
    the kernel or some such craziness). Whatever. We do our best. Slightly
    fancier schemes are available, but they're not much fancier---they're
    mostly good only at making it harder for processes to actively evade being
    charged. For primarily long-running processes that act in good faith, we
    should do ok.
    """
    def __init__(self, sid):
        """
        Initialize.
        """

        self.sid = sid
        self.charged = {}
Exemple #14
0
"""@author: Bryan Silverthorn <*****@*****.**>"""

import contextlib
import numpy
import borg

logger = borg.get_logger(__name__, default_level = "DETAIL")

class FakeSolverProcess(object):
    """Provide a solver interface to stored run data."""

    def __init__(self, run):
        """Initialize."""

        self._run = run
        self._elapsed = 0.0
        self._terminated = False

    def run_then_stop(self, budget):
        """Unpause the solver for the specified duration."""

        try:
            return self.run_then_pause(budget)
        finally:
            self.stop()

    def run_then_pause(self, budget):
        """Unpause the solver for the specified duration."""

        assert not self._terminated
Exemple #15
0
"""@author: Bryan Silverthorn <*****@*****.**>"""

import os.path
import uuid
import subprocess
import condor
import borg

logger = borg.get_logger(__name__, default_level = "DEBUG")

def ground_instance(asp_path, gringo_path, domain_path, ignore_errors, compat):
    """Ground an ASP instance using Gringo."""

    # prepare the gringo invocation
    (asp_path_base, _) = os.path.splitext(asp_path)
    verify_path = "{0}.verify".format(asp_path_base)
    command = [gringo_path, domain_path, asp_path]

    if compat:
        command.append("--compat")

    if os.path.exists(verify_path):
        command.append(verify_path)

        verified = " (and verified)"
    else:
        verified = ""

    logger.debug("running %s", command)

    # then ground the instance