예제 #1
0
파일: utils_test.py 프로젝트: Recmo/MOE
 def test_make_bandit_historical_info_from_params_variance_passed_through(self):
     """Test that the variance of a given sample arm got passed through."""
     historical_info = self.three_arms_with_variance_no_unsampled_arm_test_case
     T.assert_dicts_equal(
             _make_bandit_historical_info_from_params(self.make_params_from_bandit_historical_info(historical_info)).json_payload(),
             historical_info.json_payload()
             )
예제 #2
0
    def bandit_epsilon_view(self):
        """Endpoint for bandit_epsilon POST requests.

        .. http:post:: /bandit/epsilon

           Predict the optimal arm from a set of arms, given historical data.

           :input: :class:`moe.views.schemas.rest.bandit_epsilon.BanditEpsilonRequest`
           :output: :class:`moe.views.schemas.bandit_pretty_view.BanditResponse`

           :status 200: returns a response
           :status 500: server error

        """
        params = self.get_params_from_request()

        subtype = params.get('subtype')
        historical_info = _make_bandit_historical_info_from_params(params)

        bandit_class = EPSILON_SUBTYPES_TO_BANDIT_METHODS[subtype].bandit_class(historical_info=historical_info, **params.get('hyperparameter_info'))
        arms_to_allocations = bandit_class.allocate_arms()

        return self.form_response({
                'endpoint': self._route_name,
                'arm_allocations': arms_to_allocations,
                'winner': bandit_class.choose_arm(arms_to_allocations),
                })
예제 #3
0
    def bandit_epsilon_view(self):
        """Endpoint for bandit_epsilon POST requests.

        .. http:post:: /bandit/epsilon

           Predict the optimal arm from a set of arms, given historical data.

           :input: :class:`moe.views.schemas.rest.bandit_epsilon.BanditEpsilonRequest`
           :output: :class:`moe.views.schemas.bandit_pretty_view.BanditResponse`

           :status 200: returns a response
           :status 500: server error

        """
        params = self.get_params_from_request()

        subtype = params.get('subtype')
        historical_info = _make_bandit_historical_info_from_params(params)

        bandit_class = EPSILON_SUBTYPES_TO_BANDIT_METHODS[
            subtype].bandit_class(historical_info=historical_info,
                                  **params.get('hyperparameter_info'))
        arms_to_allocations = bandit_class.allocate_arms()

        return self.form_response({
            'endpoint':
            self._route_name,
            'arm_allocations':
            arms_to_allocations,
            'winner':
            bandit_class.choose_arm(arms_to_allocations),
        })
예제 #4
0
 def test_make_bandit_historical_info_from_params_variance_passed_through(
         self):
     """Test that the variance of a given sample arm got passed through."""
     historical_info = self.three_arms_with_variance_no_unsampled_arm_test_case
     assert _make_bandit_historical_info_from_params(
         self.make_params_from_bandit_historical_info(historical_info)
     ).json_payload() == historical_info.json_payload()
예제 #5
0
 def test_make_bandit_historical_info_from_params_make_bernoulli_arms(self):
     """Test that the function can make historical infos with Bernoulli arms."""
     historical_info = self.three_arms_with_variance_no_unsampled_arm_test_case
     for historical_info in self.bernoulli_historical_infos_to_test:
         assert _make_bandit_historical_info_from_params(
             self.make_params_from_bandit_historical_info(historical_info),
             BernoulliArm).json_payload() == historical_info.json_payload()
예제 #6
0
파일: utils_test.py 프로젝트: Recmo/MOE
 def test_make_bandit_historical_info_from_params_make_bernoulli_arms(self):
     """Test that the function can make historical infos with Bernoulli arms."""
     historical_info = self.three_arms_with_variance_no_unsampled_arm_test_case
     for historical_info in self.bernoulli_historical_infos_to_test:
         T.assert_dicts_equal(
                 _make_bandit_historical_info_from_params(self.make_params_from_bandit_historical_info(historical_info), BernoulliArm).json_payload(),
                 historical_info.json_payload()
                 )
예제 #7
0
def run_example(
        verbose=True,
        testapp=None,
        bandit_bla_kwargs=None,
        bandit_epsilon_kwargs=None,
        bandit_ucb_kwargs=None,
        **kwargs
):
    """Run the bandit example.

    :param verbose: Whether to print information to the screen [True]
    :type verbose: bool
    :param testapp: Whether to use a supplied test pyramid application or a rest server [None]
    :type testapp: Pyramid test application
    :param bandit_bla_kwargs: Optional kwargs to pass to bandit_bla endpoint
    :type bandit_bla_kwargs: dict
    :param bandit_epsilon_kwargs: Optional kwargs to pass to bandit_epsilon endpoint
    :type bandit_epsilon_kwargs: dict
    :param bandit_ucb_kwargs: Optional kwargs to pass to bandit_ucb endpoint
    :type bandit_ucb_kwargs: dict
    :param kwargs: Optional kwargs to pass to all endpoints
    :type kwargs: dict

    """
    # Set and combine all optional kwargs
    # Note that the more specific kwargs take precedence (and will override general kwargs)
    bandit_kwargs = {}
    if bandit_bla_kwargs is None:
        bandit_bla_kwargs = {}
    bandit_kwargs[BANDIT_BLA_ROUTE_NAME] = dict(kwargs.items() + bandit_bla_kwargs.items())

    if bandit_epsilon_kwargs is None:
        bandit_epsilon_kwargs = {}
    bandit_kwargs[BANDIT_EPSILON_ROUTE_NAME] = dict(kwargs.items() + bandit_epsilon_kwargs.items())

    if bandit_ucb_kwargs is None:
        bandit_ucb_kwargs = {}
    bandit_kwargs[BANDIT_UCB_ROUTE_NAME] = dict(kwargs.items() + bandit_ucb_kwargs.items())

    # A BernoulliArm has payoff 1 for a success and 0 for a failure.
    # See :class:`~moe.bandit.data_containers.BernoulliArm` for more details.
    historical_info = _make_bandit_historical_info_from_params(
            {
                "historical_info": {
                    "arms_sampled": {
                        "arm1": {"win": 20, "loss": 0, "total": 25},
                        "arm2": {"win": 20, "loss": 0, "total": 30},
                        "arm3": {"win": 0, "loss": 0, "total": 0},
                    }
                },
            },
            arm_type=BernoulliArm
            )

    # Run all multi-armed bandit strategies we have implemented. See :doc:`bandit` for more details on multi-armed bandits.
    # We have implemented 3 bandit types: BLA (Bayesian Learning Optimization), Epsilon, and UCB (Upper Confidence Bound).
    for type in BANDIT_ROUTE_NAMES:
        if verbose:
            print "Running Bandit: {0:s}...".format(type)
        # Each bandit type has different subtypes. If a user does not specify a subtype, we use the default subtype.
        # For example, the bandit type Epsilon has two subtypes: epsilon-first and epsilon-greedy.
        # See :class:`~moe.bandit.epsilon.epsilon_first.EpsilonFirst` and :class:`~moe.bandit.epsilon.epsilon_greedy.EpsilonGreedy` for more details.
        for subtype in BANDIT_ROUTE_NAMES_TO_SUBTYPES[type]:
            if verbose:
                print "Running subtype: {0:s}...".format(subtype)
            bandit_kwargs[type]['subtype'] = subtype
            # Compute and return arm allocations given the sample history of bandit arms.
            # For example, the allocations {arm1: 0.3, arm2: 0.7} means
            # if we have 10 arm pulls, we should pull arm1 3 times and arm2 7 times.
            # See :func:`moe.bandit.bandit_interface.BanditInterface.allocate_arms` for more details.
            arm_allocations = bandit(historical_info, type=type, testapp=testapp, **bandit_kwargs[type])
            if verbose:
                print "Arm allocations {0:s}".format(str(arm_allocations))
예제 #8
0
def run_example(verbose=True,
                testapp=None,
                bandit_bla_kwargs=None,
                bandit_epsilon_kwargs=None,
                bandit_ucb_kwargs=None,
                **kwargs):
    """Run the bandit example.

    :param verbose: Whether to print information to the screen [True]
    :type verbose: bool
    :param testapp: Whether to use a supplied test pyramid application or a rest server [None]
    :type testapp: Pyramid test application
    :param bandit_bla_kwargs: Optional kwargs to pass to bandit_bla endpoint
    :type bandit_bla_kwargs: dict
    :param bandit_epsilon_kwargs: Optional kwargs to pass to bandit_epsilon endpoint
    :type bandit_epsilon_kwargs: dict
    :param bandit_ucb_kwargs: Optional kwargs to pass to bandit_ucb endpoint
    :type bandit_ucb_kwargs: dict
    :param kwargs: Optional kwargs to pass to all endpoints
    :type kwargs: dict

    """
    # Set and combine all optional kwargs
    # Note that the more specific kwargs take precedence (and will override general kwargs)
    bandit_kwargs = {}
    if bandit_bla_kwargs is None:
        bandit_bla_kwargs = {}
    bandit_kwargs[BANDIT_BLA_ROUTE_NAME] = dict(kwargs.items() +
                                                bandit_bla_kwargs.items())

    if bandit_epsilon_kwargs is None:
        bandit_epsilon_kwargs = {}
    bandit_kwargs[BANDIT_EPSILON_ROUTE_NAME] = dict(
        kwargs.items() + bandit_epsilon_kwargs.items())

    if bandit_ucb_kwargs is None:
        bandit_ucb_kwargs = {}
    bandit_kwargs[BANDIT_UCB_ROUTE_NAME] = dict(kwargs.items() +
                                                bandit_ucb_kwargs.items())

    # A BernoulliArm has payoff 1 for a success and 0 for a failure.
    # See :class:`~moe.bandit.data_containers.BernoulliArm` for more details.
    historical_info = _make_bandit_historical_info_from_params(
        {
            "historical_info": {
                "arms_sampled": {
                    "arm1": {
                        "win": 20,
                        "loss": 0,
                        "total": 25
                    },
                    "arm2": {
                        "win": 20,
                        "loss": 0,
                        "total": 30
                    },
                    "arm3": {
                        "win": 0,
                        "loss": 0,
                        "total": 0
                    },
                }
            },
        },
        arm_type=BernoulliArm)

    # Run all multi-armed bandit strategies we have implemented. See :doc:`bandit` for more details on multi-armed bandits.
    # We have implemented 3 bandit types: BLA (Bayesian Learning Optimization), Epsilon, and UCB (Upper Confidence Bound).
    for type in BANDIT_ROUTE_NAMES:
        if verbose:
            print "Running Bandit: {0:s}...".format(type)
        # Each bandit type has different subtypes. If a user does not specify a subtype, we use the default subtype.
        # For example, the bandit type Epsilon has two subtypes: epsilon-first and epsilon-greedy.
        # See :class:`~moe.bandit.epsilon.epsilon_first.EpsilonFirst` and :class:`~moe.bandit.epsilon.epsilon_greedy.EpsilonGreedy` for more details.
        for subtype in BANDIT_ROUTE_NAMES_TO_SUBTYPES[type]:
            if verbose:
                print "Running subtype: {0:s}...".format(subtype)
            bandit_kwargs[type]['subtype'] = subtype
            # Compute and return arm allocations given the sample history of bandit arms.
            # For example, the allocations {arm1: 0.3, arm2: 0.7} means
            # if we have 10 arm pulls, we should pull arm1 3 times and arm2 7 times.
            # See :func:`moe.bandit.bandit_interface.BanditInterface.allocate_arms` for more details.
            arm_allocations = bandit(historical_info,
                                     type=type,
                                     testapp=testapp,
                                     **bandit_kwargs[type])
            if verbose:
                print "Arm allocations {0:s}".format(str(arm_allocations))