Example #1
0
    def setUp(self):
        app = flask.Flask('test_app')
        flask_mab.BanditMiddleware(app)
        self.bandit_a = makeBandit("EpsilonGreedyBandit",epsilon=1.0)
        self.bandit_b = makeBandit("NaiveStochasticBandit")
        app.add_bandit('color_button', self.bandit_a)
        app.add_bandit('color_bg', self.bandit_b)
        app.debug = True

        @app.route("/")
        def root():
            return "Hello"

        @app.route("/show_btn_decorated")
        @choose_arm("color_button")
        @choose_arm("color_bg")
        def assign_arm_decorated(color_button, color_bg):
            assert(color_button)
            assert(color_bg)
            return flask.make_response("assigned an arm")

        @app.route("/reward_decorated")
        @reward_endpt("color_button",1.0)
        @reward_endpt("color_bg", 1.0)
        def reward_decorated():
            return flask.make_response("awarded the arm")

        self.app = app
        self.app.config["MAB_DEBUG_HEADERS"] = True
        self.app_client = app.test_client()
Example #2
0
    def banditFactory(self, app):
        """Use a dictionary to look for different app setups.  Key is element name,
        list of tuples represent bandits (name, banditObj)
        """

        bandit_setups = {
            "test_app" :  [('color_button', makeBandit("EpsilonGreedyBandit",epsilon=0.1))],
            "some_other_app": [('bg_image', makeBandit("EpsilonGreedyBandit",epsilon=0.1))]
        }

        setup = bandit_setups.get(app.import_name, [])
        return setup.pop()
Example #3
0
    def banditFactory(self, app):
        """Use a dictionary to look for different app setups.  Key is element name,
        list of tuples represent bandits (name, banditObj)
        """

        bandit_setups = {
            "test_app":
            [('color_button', makeBandit("EpsilonGreedyBandit", epsilon=0.1))],
            "some_other_app":
            [('bg_image', makeBandit("EpsilonGreedyBandit", epsilon=0.1))]
        }

        setup = bandit_setups.get(app.import_name, [])
        return setup.pop()
Example #4
0
    def test_proper_configuration(self):
        app = flask.Flask(__name__)
        BanditMiddleware().init_app(app)

        app.add_bandit("some_bandit", makeBandit("EpsilonGreedyBandit", epsilon=1.0))
        app.register_blueprint(self.bp)
        app_client = app.test_client()

        rv = app_client.get("/")
        assert parse_cookie(rv.headers["Set-Cookie"])["MAB"]
        print parse_cookie(rv.headers["Set-Cookie"])["MAB"]

        assert "X-MAB-Debug" in rv.headers.keys()
        chosen_arm = self.get_arm(rv.headers)["some_bandit"]
        assert app.extensions["mab"].bandits["some_bandit"][chosen_arm]["pulls"] > 0
        assert json.loads(parse_cookie(rv.headers["Set-Cookie"])["MAB"])["some_bandit"] == chosen_arm

        app_client.get("/reward")
        assert app.extensions["mab"].bandits["some_bandit"][chosen_arm]["reward"] > 0
Example #5
0
    def test_proper_configuration(self):
        app = flask.Flask(__name__)
        BanditMiddleware().init_app(app)

        app.add_bandit("some_bandit",
                       makeBandit("EpsilonGreedyBandit", epsilon=1.0))
        app.register_blueprint(self.bp)
        app_client = app.test_client()

        rv = app_client.get("/")
        assert parse_cookie(rv.headers["Set-Cookie"])["MAB"]
        print parse_cookie(rv.headers["Set-Cookie"])["MAB"]

        assert "X-MAB-Debug" in rv.headers.keys()
        chosen_arm = self.get_arm(rv.headers)["some_bandit"]
        assert app.extensions['mab'].bandits["some_bandit"][chosen_arm][
            "pulls"] > 0
        assert json.loads(parse_cookie(
            rv.headers["Set-Cookie"])["MAB"])["some_bandit"] == chosen_arm

        app_client.get("/reward")
        assert app.extensions['mab'].bandits["some_bandit"][chosen_arm][
            "reward"] > 0
Example #6
0
    def setUp(self):
        banditStorage = flask_mab.storage.JSONBanditStorage('./bandits.json')
        app = flask.Flask('test_app')
        flask_mab.BanditMiddleware(app)
        app.add_bandit('color_button',makeBandit("EpsilonGreedyBandit",epsilon=0.1))
        app.debug = True

        @app.route("/")
        def root():
            return "Hello"

        @app.route("/show_btn_decorated")
        @choose_arm("color_button")
        def assign_arm_decorated(color_button):
            return flask.make_response("assigned an arm")

        @app.route("/reward_decorated")
        @reward_endpt("color_button",1.0)
        def reward_decorated():
            return flask.make_response("awarded the arm")

        self.app = app
        self.app.config["MAB_DEBUG_HEADERS"] = True
        self.app_client = app.test_client()
Example #7
0
 def setUp(self):
     self.bandit = makeBandit("EpsilonGreedyBandit",epsilon=0.5)
Example #8
0
 def setUp(self):
     self.bandit = makeBandit("NaiveStochasticBandit")