Esempio n. 1
0
            raise

    def __repr__(self):
        return "Agent({_coconut_format_0})".format(_coconut_format_0=(self.name))

    def get_defaults(self):
        """Get a dictionary of all default values to assign."""
        defaults = {}
        if self.default is not self.NO_DEFAULT:
            defaults[self.name] = deepcopy(self.default)
        for name, val in self.extra_defaults.items():
            defaults[name] = deepcopy(val)
        return defaults


_coconut_call_set_names(Agent)
def agent(name_or_agent_func=None, **kwargs):
    """Decorator for easily constructing agents.

    If a string is passed to the decorator it will use that as the name,
    otherwise the name is inferred from the name of the function.

    Examples:

        @agent()  # or just @agent
        def x(env) =
            ...

        @agent("x")
        def x_agent(env) =
            ...
Esempio n. 2
0
    @_coconut_tco
    def resolve_against(self, other, **kwargs):
        """Attempt to perform a resolution against other else None."""
        if isinstance(other, (Not, Or, Eq)):
            return _coconut_tail_call(other.resolve_against, self, **kwargs)
        elif (self.find_unification)(Not(other).simplify(**kwargs)) is not None:
            return bot
        else:
            return None

    def admits_empty_universe(self):
        """Determines if self allows for the possibility of an empty universe."""
        return True


_coconut_call_set_names(Expr)
class Top(Expr):
    """True"""
    __slots__ = ()

    @_coconut_tco
    def __eq__(self, other):
        return _coconut_tail_call(isinstance, other, Top)

    def __repr__(self):
        return "top"

    def __str__(self):
        return top_sym

    def __bool__(self):
Esempio n. 3
0
            reload(mixture_example)
            assert_improving(mixture_example.bb.get_data(print_data=True))
            assert mixture_example.loss == want
            assert 0 <= mixture_example.loss < 85
            assert (len)((set)((map)(
                _coconut_base_compose(_coconut.operator.itemgetter(
                    ("memo")), (_coconut.operator.itemgetter(("alg")), 0)),
                mixture_example.bb.get_data()["examples"]))) > 1
            assert mixture_example.bb.num_examples == NUM_TRIALS

    def test_json(self):
        print("\ntest json:")
        with using(json_data):
            from bbopt.examples import json_example
            assert round(json_example.y, 5) == 6

            results = call_bbopt(json_file)
            want = min(get_nums(results, numtype=float))
            assert os.path.exists(json_data)

            reload(json_example)
            assert_improving(json_example.bb.get_data(print_data=True))
            assert json_example.y == want
            assert json_example.bb.num_examples == NUM_TRIALS


_coconut_call_set_names(TestExamples)
if __name__ == "__main__":
    unittest.main()
Esempio n. 4
0

def assert_dict_or_callable_or_hashable(name, obj):
    """Assert obj is hashable, or for dicts apply recursively to values."""
    if isinstance(obj, dict):
        for val in obj.values():
            assert_dict_or_callable_or_hashable(name, val)
    elif not callable(obj):
        assert is_hashable(
            obj), "Constant " + name + " contains unhashable values"


# Tests:


class TestConstants(unittest.TestCase):
    def test_immutable(self):
        for name, value in vars(constants).items():
            if not name.startswith("__"):
                assert not isinstance(
                    value,
                    list), "Constant " + name + " should be tuple, not list"
                assert not isinstance(
                    value,
                    set), "Constant " + name + " should be frozenset, not set"
                if "sentinel" not in name.lower():
                    assert_dict_or_callable_or_hashable(name, value)


_coconut_call_set_names(TestConstants)
Esempio n. 5
0
    def __gt__(self, other):
        # type: (...) -> int
        return str(self) > str(other)

    def __ge__(self, other):
        # type: (...) -> int
        return str(self) >= str(other)

    def __le__(self, other):
        # type: (...) -> int
        return str(self) <= str(other)


# Derived Classes:

_coconut_call_set_names(Vars)


class LowercasePropositions(Vars):
    a, b, c = props("a b c")
    d, e, f = props("d e f")
    g, h, i = props("g h i")
    j, k, l = props("j k l")
    m, n, o = props("m n o")
    p, q, r = props("p q r")
    s, t, u = props("s t u")
    v, w, x = props("v w x")
    y, z = props("y z")


_coconut_call_set_names(LowercasePropositions)
Esempio n. 6
0
        data_points, losses = split_examples(new_examples, self.params)
        self.result = self.optimizer.tell(data_points, losses)

        current_point = self.optimizer.ask()
        self.current_values = make_values(self.params, current_point)

    @property
    def space(self):
        """The space over which optimization was performed."""
        return self.optimizer.space

    @property
    def model(self):
        """Get the most recently fit model."""
        return self.optimizer.models[-1]


# Registered names:

_coconut_call_set_names(SkoptBackend)
SkoptBackend.register()
SkoptBackend.register_alias("skopt")

SkoptBackend.register_alg("gaussian_process", base_estimator="GP")
SkoptBackend.register_alg("random_forest", base_estimator="RF")
SkoptBackend.register_alg("extra_trees", base_estimator="ET")
SkoptBackend.register_alg("gradient_boosted_regression_trees",
                          base_estimator="GBRT")

SkoptBackend.register_meta_for_all_algs("any_skopt")
Esempio n. 7
0
File: pysot.py Progetto: evhub/bbopt
_coconut_sys.path.pop(0)

# Compiled Coconut: -----------------------------------------------------------

sys = _coconut_sys

import numpy as np

# patch in abc.ABC if it doesn't already exist
import abc
if not hasattr(abc, "ABC"):

    class ABC(_coconut.object):
        __metaclass__ = abc.ABCMeta

    _coconut_call_set_names(ABC)
    abc.ABC = ABC

from pySOT.optimization_problems.optimization_problem import OptimizationProblem
from pySOT.experimental_design import ExperimentalDesign
from pySOT.experimental_design import LatinHypercube
from pySOT.experimental_design import SymmetricLatinHypercube
from pySOT.experimental_design import TwoFactorial
from pySOT.surrogate import RBFInterpolant
from pySOT.surrogate import GPRegressor
from pySOT.surrogate.kernels import CubicKernel
from pySOT.surrogate.kernels import LinearKernel
from pySOT.surrogate.tails import ConstantTail
from pySOT.surrogate.tails import LinearTail
from pySOT.strategy import SRBFStrategy
from pySOT.strategy import EIStrategy
Esempio n. 8
0
File: util.py Progetto: evhub/bbopt
        cls.register_meta(alg_name, cls.registered_algs, meta_alg)

    @staticmethod
    def register_meta(alg_name, algs, meta_alg=constants.default_alg_sentinel):
        """Register an algorithm that defers to run_meta."""
        meta_registry.register(alg_name, (algs, meta_alg))

    @staticmethod
    def register_param_func(func_name, handler, placeholder_generator,
                            support_check_func):
        """Register a new parameter definition function. See bbopt.params for examples."""
        param_processor.register(func_name, handler, placeholder_generator,
                                 support_check_func)


_coconut_call_set_names(Backend)


class StandardBackend(Backend):
    """Base class for standard BBopt backends."""
    def __init__(self, examples, params, *args, **kwargs):
        """Implement __init__ using setup_backend and tell_examples."""
        self.init_fallback_backend()

        if not params:
            self.current_values = {}
            return

        self.setup_backend(params, *args, **kwargs)

        if examples:
Esempio n. 9
0
                        if not _coconut_match_kwargs:
                            _coconut_match_set_name_val = _coconut_match_temp_0
                            _coconut_match_check_2 = True
                    if _coconut_match_check_2:
                        if _coconut_match_set_name_val is not _coconut_sentinel:
                            val = _coconut_match_temp_0
                        if _coconut_match_set_name_loss is not _coconut_sentinel:
                            loss = _coconut_match_args[1][0]
                        if _coconut_match_set_name_N is not _coconut_sentinel:
                            N = _coconut_match_args[1][1]
                    if not _coconut_match_check_2:
                        raise _coconut_FunctionMatchError('ns = marginals |> starmap$(def (val, (loss, N)) -> N) |> np.asarray', _coconut_match_args)
                    return N
                ns = (np.asarray)((starmap)(_coconut_lambda_2, marginals))
                zs /= np.sqrt(ns)
            else:
                assert self.bandit_alg == "boltzmann", "invalid boltzmann bandit algorithm: {_coconut_format_0}".format(_coconut_format_0=(self.bandit_alg))

            best_i = np.argmax(xs + zs)
            return marginals[best_i][0]


# Registered names:

_coconut_call_set_names(BanditBackend)
BanditBackend.register()

BanditBackend.register_alg("epsilon_greedy", bandit_alg="greedy")
BanditBackend.register_alg("boltzmann_exploration", bandit_alg="boltzmann")
BanditBackend.register_alg("boltzmann_gumbel_exploration", bandit_alg="boltzmann_gumbel")
Esempio n. 10
0
                        _coconut_format_0=(name), _coconut_format_1=(i)),
                    len(sampling_population), **proc_kwargs)
                sample.append(sampling_population.pop(ind))
        return sample

    def shuffled(self, name, population, **kwargs):
        """Create a new parameter with the given name modeled by
        random.shuffle(population) except returned instead of modified in place."""
        return self.sample(name, population, len(population), **kwargs)

    def shuffle(self, name, population, **kwargs):
        """Create a new parameter with the given name modeled by random.shuffle(population)."""
        population[:] = self.shuffled(name, population, **kwargs)

    def stdnormal(self, name, **kwargs):
        """Equivalent to bb.normalvariate(name, 0, 1)."""
        return self.normalvariate(name, 0, 1, **kwargs)


# Array-based random functions:

    def rand(self, name, *shape, **kwargs):
        """Create a new array parameter for the given name and shape modeled by np.random.rand."""
        return array_param(self.random, name, shape, kwargs)

    def randn(self, name, *shape, **kwargs):
        """Create a new array parameter for the given name and shape modeled by np.random.randn."""
        return array_param(self.stdnormal, name, shape, kwargs)

_coconut_call_set_names(BlackBoxOptimizer)
Esempio n. 11
0
        """Get all items in the registry as (name, value) pairs."""
        self.run_all_gens()
        _coconut_yield_from_7 = _coconut.iter(self.registered.items())
        while True:
            try:
                yield _coconut.next(_coconut_yield_from_7)
            except _coconut.StopIteration as _coconut_yield_err_3:
                _coconut_yield_from_6 = _coconut_yield_err_3.args[
                    0] if _coconut.len(_coconut_yield_err_3.args) > 0 else None
                break

        _coconut_yield_from_6

    def asdict(self):
        """Convert registry to dictionary."""
        self.run_all_gens()
        return self.registered


# Registries:

_coconut_call_set_names(Registry)
backend_registry = Registry("backend")

alg_registry = Registry("algorithm")

meta_registry = Registry("meta algorithm")

alg_registry.shouldnt_conflict_with(meta_registry)
meta_registry.shouldnt_conflict_with(alg_registry)
Esempio n. 12
0
        except constants.erroring_backend_errs:
            if not self.remove_erroring_algs:
                raise
            self.reselect_backend()
        return self.param(name, func, *args, **kwargs)

    @classmethod
    def register_safe_alg_for(cls, base_alg, new_alg_name=None, fallback_alg=None):
        """Register a version of base_alg that defaults to the fallback if base_alg fails."""
        if new_alg_name is None:
            new_alg_name = "safe_" + base_alg
        if fallback_alg is None:
            fallback_alg = constants.safe_fallback_alg
        cls.register_alg(new_alg_name, distribution=((base_alg, float("inf")), (fallback_alg, 1)), remove_erroring_algs=True)


# Registered names:

_coconut_call_set_names(MixtureBackend)
MixtureBackend.register()
MixtureBackend.register_alg("epsilon_max_greedy", distribution="epsilon_max_greedy")

MixtureBackend.register_safe_alg_for("gaussian_process")
MixtureBackend.register_safe_alg_for("random_forest")
MixtureBackend.register_safe_alg_for("extra_trees")
MixtureBackend.register_safe_alg_for("gradient_boosted_regression_trees")

# we register meta alg mixtures here
MixtureBackend.register_meta("tpe_or_gp", ("tree_structured_parzen_estimator", "safe_gaussian_process"))
MixtureBackend.register_meta("any_fast", ("tree_structured_parzen_estimator", "safe_random_forest", "safe_extra_trees", "safe_gradient_boosted_regression_trees"))
Esempio n. 13
0
    return newsubs


class counter(_coconut.object):
    def __init__(self, num=-1):
        self.num = num

    def inc(self):
        assert not self.done(), self.num
        self.num -= 1

    def done(self):
        return self.num == 0


_coconut_call_set_names(counter)


@_coconut_tco
def sub_once(obj, subs):
    """Performs one substitution of subs into obj."""
    return _coconut_tail_call(obj.substitute, subs, counter=counter(1))


def can_sub(kwargs):
    """Determines if the counter in kwargs allows for another sub."""
    try:
        return not kwargs["counter"].done()
    except KeyError:
        return True
Esempio n. 14
0
    def standardize_kwargs(self, kwargs):
        """Standardizes param keyword args."""
        return (fmap)(lambda k, v: denumpy_all((k, v)), kwargs)

    def choose_default_placeholder(self, name, func, *args, **kwargs):
        """Choose a default placeholder_when_missing value for the given parameter."""
        if func not in self.placeholder_funcs:
            raise ValueError(
                "unknown parameter definition function {_coconut_format_0} (register with bbopt.params.param_processor.register)"
                .format(_coconut_format_0=(func)))
        return self.placeholder_funcs[func](*args)


# Registration:

_coconut_call_set_names(ParamProcessor)
param_processor = ParamProcessor()

param_processor.register("randrange", handle_randrange, placeholder_randrange,
                         support_check_randrange)
param_processor.register("choice", handle_choice, placeholder_choice,
                         support_check_choice)
param_processor.register("uniform", handle_uniform, placeholder_uniform,
                         support_check_uniform)
param_processor.register("triangular", handle_triangular,
                         placeholder_triangular, support_check_triangular)
param_processor.register("betavariate", handle_betavariate,
                         placeholder_betavariate, support_check_betavariate)
param_processor.register("expovariate", handle_expovariate,
                         placeholder_expovariate, support_check_expovariate)
param_processor.register("gammavariate", handle_gammavariate,
Esempio n. 15
0
        self.trials.refresh()

        # run one iteration of hyperparameter optimization, with values saved
        #  to the self.set_current_values callback passed to Domain
        next(self.fmin_iter)

        assert self.current_values is not None, self.current_values
        assert set(self.current_values.keys()) == set(
            self.params), self.current_values

    def set_current_values(self, values):
        """Callback to set the values for this run."""
        assert isinstance(values, dict), values
        self.current_values = values
        return {"status": STATUS_RUNNING}


# Registered names:

_coconut_call_set_names(HyperoptBackend)
HyperoptBackend.register()

HyperoptBackend.register_alg("tree_structured_parzen_estimator",
                             algo=tpe.suggest)
HyperoptBackend.register_alg("annealing", algo=anneal.suggest)
if sys.version_info >= (3, ):
    from hyperopt import atpe
    HyperoptBackend.register_alg("adaptive_tpe", algo=atpe.suggest)

HyperoptBackend.register_meta_for_all_algs("any_hyperopt")
Esempio n. 16
0
    def append(self, obj):
        self.new_list.append(obj)
        if obj not in self.old_list:
            self.old_list.append(obj)

    def __setitem__(self, index, obj):
        self.new_list[index] = obj
        if obj not in self.old_list:
            self.old_list.append(obj)

    def __repr__(self):
        return "ListProxy(\n\tself.old_list={_coconut_format_0},\n\tself.new_list={_coconut_format_1},\n)".format(_coconut_format_0=(self.old_list), _coconut_format_1=(self.new_list))


_coconut_call_set_names(ListProxy)
class DictProxy(_coconut.object):
    """Behaves like new_dict, but adds new keys to old_dict."""

    def __init__(self, old_dict, new_dict):
        self.old_dict = old_dict
        self.new_dict = new_dict

    def __iter__(self):
        _coconut_yield_from_3 = _coconut.iter(self.new_dict)
        while True:
            try:
                yield _coconut.next(_coconut_yield_from_3)
            except _coconut.StopIteration as _coconut_yield_err_1:
                _coconut_yield_from_2 = _coconut_yield_err_1.args[0] if _coconut.len(_coconut_yield_err_1.args) > 0 else None
                break
Esempio n. 17
0
import random

from bbopt.backends.util import Backend


# Backend:

class RandomBackend(Backend):
    """The random backend chooses parameter values randomly."""
    backend_name = "random"
    random_functions = {"randrange": random.randrange, "choice": random.choice, "uniform": random.uniform, "triangular": random.triangular, "betavariate": random.betavariate, "expovariate": random.expovariate, "gammavariate": random.gammavariate, "normalvariate": random.gauss, "vonmisesvariate": random.vonmisesvariate, "paretovariate": random.paretovariate, "weibullvariate": random.weibullvariate}

    @override
    def param(self, name, func, *args, **kwargs):
        if func not in self.random_functions:
            raise ValueError("unknown random function {_coconut_format_0}".format(_coconut_format_0=(name)))
        return self.random_functions[func](*args)

    @override
    def attempt_update(self, examples, params):
        """The random backend requires no modifications to be updated with new parameters."""
        return True


# Registered names:

_coconut_call_set_names(RandomBackend)
RandomBackend.register()
RandomBackend.register_alg("random")
Esempio n. 18
0
from bask import Optimizer

from bbopt.backends.skopt import SkoptBackend
from bbopt.backends.skopt import create_dimensions
from bbopt.backends.skopt import guess_n_initial_points

# Backend:


class BaskBackend(SkoptBackend):
    """The bask backend uses bayes-skopt for black box optimization."""
    backend_name = "bayes-skopt"

    @override
    def setup_backend(self, params, n_initial_points=None, **options):
        """Special method to initialize the backend from params."""
        self.params = params
        if n_initial_points is None:
            n_initial_points = guess_n_initial_points(params)
        self.optimizer = Optimizer(create_dimensions(params),
                                   n_initial_points=n_initial_points,
                                   **options)


# Registered names:

_coconut_call_set_names(BaskBackend)
BaskBackend.register()
BaskBackend.register_alias("bask")
BaskBackend.register_alg("bask_gaussian_process")
Esempio n. 19
0
        # since we're serving, ignore params and just extract the best example
        self.current_values = best_example(examples)["values"]

        # set new allow_missing_data and call init_fallback_backend if necessary
        self.allow_missing_data = allow_missing_data
        if not self.fallback_backend and self.allow_missing_data:
            self.init_fallback_backend()

        return True

    @override
    def fallback_func(self, name, func, *args, **kwargs):
        if self.allow_missing_data:
            return super().fallback_func(name, func, *args, **kwargs)
        else:
            raise ValueError(
                "missing data for parameter {_coconut_format_0} while serving and no guess"
                .format(_coconut_format_0=(name)))


# Registered names:

_coconut_call_set_names(ServingBackend)
ServingBackend.register()

# allow_missing_data=False not included to help bb._backend_store
ServingBackend.register_alg(None)
ServingBackend.register_alg("serving")

ServingBackend.register_alg("max_greedy", allow_missing_data=True)
Esempio n. 20
0
    equality = (call)(Eq, term + equals + term)
    atom = pred | prop | equality

    expr = Forward()
    quant_sep = dot | comma
    exists = ((call)(exists_handle, exists_op + var + Optional(colon + prop) + quant_sep + expr))
    forall = ((call)(forall_handle, forall_op + var + Optional(colon + prop) + quant_sep + expr))
    quant = exists | forall

    base_expr = top_lit | bot_lit | quant | atom | lparen - expr - rparen
    not_expr = quant | ((call)(Not, not_op + base_expr)) | base_expr | quant
    and_expr = quant | ((call)(And, not_expr + OneOrMore(and_op - not_expr))) | not_expr
    or_expr = quant | ((call)(Or, and_expr + OneOrMore(or_op - and_expr))) | and_expr
    expr <<= quant | ((call)(Implies, or_expr + OneOrMore(imp_op - or_expr))) | or_expr

    formula = stringStart + expr + stringEnd

_coconut_call_set_names(Grammar)
for varname, val in vars(Grammar).items():
    if isinstance(val, ParserElement):
        setattr(Grammar, varname, val.setName(varname))


# Endpoint:

def expr(formula):
    """Parses the given formula into an expression."""
    result = parse(Grammar.formula, formula)
    assert len(result) == 1, results
    return result[0]