class Experiment(): def __init__(self, exp_id, key = "notUsedForLoopBack"): self.db = Database() self.advice_db = Advice() self.exp_id = exp_id # sets the experimentID self.properties = self.db.get_one_experiment(self.exp_id) self.key = key self.valid = False # should be taken from Redis def is_valid(self): """Checks wheter the exp_id and key match for the current experiment. Input arguments: none Returns: A boolean: true if a valid key is provided (a prime), false otherwise. """ key = self.db.experiment_properties("exp:%s:properties" % (self.exp_id), "key") if key == self.key: self.valid = True return self.valid def run_action_code(self, context, action={}): self.action = action self.context = context code = self.db.experiment_properties("exp:%s:properties" % (self.exp_id), "getAction") #logging.debug(code) exec(code) return self.action def run_reward_code(self, context, action, reward): self.context = context self.action = action self.reward = reward code = self.db.experiment_properties("exp:%s:properties" % (self.exp_id), "setReward") exec(code) return True def log_data(self, value): self.advice_db.log_row(value) return True def set_theta(self, values, context = None, action=None, all_action=False, all_context=False, name="theta"): key = "exp:%s:" % (self.exp_id) +name return self.db.set_theta(values, key, context, action, all_action, all_context) def get_theta(self, context = None, action=None, all_action=False, all_context=False, all_float=True, name="theta"): key = "exp:%s:" % (self.exp_id) +name return self.db.get_theta(key, context, action, all_action, all_context, all_float) def is_prime(self, n): if n < 2: return False for number in islice(count(2), int(sqrt(n)-1)): if not n%number: return False return True
class Experiment(): def __init__(self, exp_id, key): self.db = Database() self.advice_db = Advice() self.exp_id = exp_id # sets the experimentID self.properties = self.db.get_one_experiment(self.exp_id) self.key = key self.valid = False # should be taken from Redis self.log_advice_bool = (self.properties['adviceID'] == 'true') # should be taken from Redis def is_valid(self): """Checks wheter the exp_id and key match for the current experiment. Input arguments: none Returns: A boolean: true if a valid key is provided, false otherwise. """ # After database has a decent is_valid function, change to self.db.is_valid(exp_id) self.valid = True return self.valid def advice(self): return self.log_advice_bool def gen_advice_id(self, context, action): return self.advice_db.log_advice(context, action) def get_by_advice_id(self, _id): return self.advice_db.get_advice(_id) ### THESE NEED CHANGING FOR THE NEW LOCATION def run_action_code(self, context, action): #or make it get_action_code and return the code instead self.action = action self.context = context code = self.db.experiment_properties("exp:%s:properties" % (self.exp_id), "getAction") exec(code) return self.action def run_reward_code(self, context): self.context = context code = self.db.experiment_properties("exp:%s:properties" % (self.exp_id), "setReward") exec(code) return True ### END OF CHANGE def set_theta(self, values, context = None, action=None, all_action=False, all_context=False, name="theta"): key = "exp:%s:" % (self.exp_id) +name return self.db.set_theta(values, key, context, action, all_action, all_context) def get_theta(self, context = None, action=None, all_action=False, all_context=False, all_float=True, name="theta"): key = "exp:%s:" % (self.exp_id) +name return self.db.get_theta(key, context, action, all_action, all_context, all_float)
def __init__(self, exp_id, key = "notUsedForLoopBack"): self.db = Database() self.advice_db = Advice() self.exp_id = exp_id # sets the experimentID self.properties = self.db.get_one_experiment(self.exp_id) self.key = key self.valid = False # should be taken from Redis
def __init__(self, exp_id, key): self.db = Database() self.advice_db = Advice() self.exp_id = exp_id # sets the experimentID self.properties = self.db.get_one_experiment(self.exp_id) self.key = key self.valid = False # should be taken from Redis self.log_advice_bool = (self.properties['adviceID'] == 'true') # should be taken from Redis