Beispiel #1
0
def advice_time_out():
    """ For every experiment, if the advice_id flag is set, we want to
    check whether certain advice_id's have timed out according to the
    experiment's own settings.
    """
    redis_db = Database()
    advice_db = Advice()
    experiment_ids = redis_db.get_experiment_ids()
    for experiment_id in experiment_ids:
        # Check experiment properties
        exp = Experiment(experiment_id)
        if exp.properties["advice_id"] == "True":
            # Get all the advices for this experiment
            # Check whether or not the date has exceeded the time-out rate
            delta_hours = int(exp.properties["delta_hours"])
            advices_retrieved = advice_db.advices.find({
                "date": {
                    "$lt": datetime.utcnow() - timedelta(hours=delta_hours)
                }
            })
            for adv in advices_retrieved:
                log = exp.get_by_advice_id(str(adv["_id"]))
                reward = ast.literal_eval(exp.properties["default_reward"])
                exp.run_reward_code(adv["context"], adv["action"], reward)
Beispiel #2
0
    def get(self, exp_id):  # Documentation needs update to advice_id
        """ Update the parameters and set a reward for a given experiment.

        For parameters, there are two options (next to the mandatory key 
        and exp_id). The first option is supplying all the information manually, 
        meaning that you supply the following parameters:
            -   Context
            -   Action
            -   Reward

        +----------------------------------------------------------------+
        | Example                                                        |
        +================================================================+
        |http://example.com/setreward/EXP_ID?key=KEY&context=CONTEXT     |
        |&action=ACTION&reward=REWARD                                    |
        +----------------------------------------------------------------+

        When you have toggled the Advice ID in the experiment properties (second option), 
        and have received an Advice ID from the getaction call, you only have
        to supply the following parameters:
            -   Advice ID
            -   Reward

        +----------------------------------------------------------------+
        | Example                                                        |
        +================================================================+
        |http://example.com/setreward/EXP_ID?key=KEY                     |
        |&advice_id=ADVICE_ID&reward=REWARD                              |
        +----------------------------------------------------------------+

        :param int exp_id: Experiment ID as specified in the url.
        :param string key: The key corresponding to the experiment.

        :param JSON context (optional): The context for the current update.
        :param JSON action (optional): The action for the current update.
        :param string advice_id (optional): The advice_id for the current \
        full update loop.
        :param JSON reward: The reward for the current update.
        :returns: A JSON indicating success.
        :raises 400: If key is not supplied.
        :raises 401: If the key or exp_id is invalid.
        """
        key = self.get_argument("key", default=False)

        if not key:
            raise ExceptionHandler(reason="Key not supplied.", status_code=400)

        __EXP__ = Experiment(exp_id, key)

        if __EXP__.is_valid():
            if self.get_argument("advice_id", default="") == "":
                context = json.loads(self.get_argument("context",
                                                       default="{}"))
                action = json.loads(self.get_argument("action", default="{}"))
            else:
                advice_id = self.get_argument("advice_id", default="")
                log = __EXP__.get_by_advice_id(advice_id)
                if log == False:
                    self.finish("Advice ID does not exist!")
                else:
                    context = log['context']
                    action = log['action']
            reward = json.loads(self.get_argument("reward", default="{}"))
            __EXP__.run_reward_code(context, action, reward)
            __EXP__.log_setreward_data(context, action, reward)

            if self.settings['debug']:
                self.write(
                    json.dumps({
                        'status': 'success',
                        'action': action,
                        'context': context,
                        'reward': reward
                    }))
            else:
                self.write(json.dumps({'status': 'success'}))
        else:
            raise ExceptionHandler(reason="Key or exp_id is invalid.",
                                   status_code=401)