def domain_state(tconf): state = State() state.txn_list = {} state.get = lambda key, isCommitted=False: state.txn_list.get(key, None) state.set = lambda key, value, isCommitted=False: state.txn_list.update( {key: value}) return state
def test_dispatches_to_hardware(self): with MockHardware() as hardware_service: dispatcher = Dispatcher(hardware_service) current_state = State(color=Color(100, 100, 100), buzzer_pattern=BuzzerPattern.NONE) command = Command(color=Color(110, 110, 110), duration=100, buzzer_pattern=BuzzerPattern.NONE) now = 0 next_state = dispatcher.dispatch(current_state, command, now) self.assertEqual(next_state.color.r, 100) self.assertEqual(next_state.color.g, 100) self.assertEqual(next_state.color.b, 100) self.assertEqual(hardware_service.color_last_called_with.r, 100) self.assertEqual(hardware_service.color_last_called_with.g, 100) self.assertEqual(hardware_service.color_last_called_with.b, 100) self.assertEqual(hardware_service.color_called_count, 1) now = 10 next_state = dispatcher.dispatch(next_state, command, now) self.assertEqual(next_state.color.r, 101) self.assertEqual(next_state.color.g, 101) self.assertEqual(next_state.color.b, 101) self.assertEqual(hardware_service.color_last_called_with.r, 101) self.assertEqual(hardware_service.color_last_called_with.g, 101) self.assertEqual(hardware_service.color_last_called_with.b, 101) self.assertEqual(hardware_service.color_called_count, 2) now = 90 next_state = dispatcher.dispatch(next_state, command, now) self.assertEqual(next_state.color.r, 109) self.assertEqual(next_state.color.g, 109) self.assertEqual(next_state.color.b, 109) self.assertEqual(hardware_service.color_last_called_with.r, 109) self.assertEqual(hardware_service.color_last_called_with.g, 109) self.assertEqual(hardware_service.color_last_called_with.b, 109) self.assertEqual(hardware_service.color_called_count, 3) now = 100 next_state = dispatcher.dispatch(next_state, command, now) self.assertEqual(next_state.color.r, 110) self.assertEqual(next_state.color.g, 110) self.assertEqual(next_state.color.b, 110) self.assertEqual(hardware_service.color_last_called_with.r, 110) self.assertEqual(hardware_service.color_last_called_with.g, 110) self.assertEqual(hardware_service.color_last_called_with.b, 110) self.assertEqual(hardware_service.color_called_count, 4)
def txn_author_agreement_handler(db_manager): f = FakeSomething() f.validate = lambda request, action_list: True handler = TxnAuthorAgreementHandler(db_manager, FakeSomething(), f) state = State() state.txn_list = {} state.get = lambda key, isCommitted=False: state.txn_list.get(key, None) state.set = lambda key, value, isCommitted=False: state.txn_list.update({key: value}) db_manager.register_new_database(handler.ledger_id, FakeSomething(), state) return handler
def nym_handler(tconf): data_manager = DatabaseManager() handler = NymHandler(tconf, data_manager) state = State() state.txn_list = {} state.get = lambda key, isCommitted: state.txn_list.get(key, None) state.set = lambda key, value: state.txn_list.update({key: value}) data_manager.register_new_database(handler.ledger_id, FakeSomething(), state) return handler
def node_handler(): data_manager = DatabaseManager() bls = FakeSomething() handler = NodeHandler(data_manager, bls) state = State() state.txn_list = {} state.get = lambda key, is_committed: state.txn_list.get(key, None) state.set = lambda key, value: state.txn_list.update({key: value}) data_manager.register_new_database(handler.ledger_id, FakeSomething(), state) return handler
def __init__(self, dispatcher_service: Dispatcher, network_service: INetwork): self.dispatcher_service = dispatcher_service self.network_service = network_service self.commands = [] self.commands.append(DefaultCommand) self.current_command = DefaultCommand self.state = State(Color(255, 255, 255), BuzzerPattern.NONE) self.last_run = time() self.last_poll = 0
def test_main_page(self): response = self.app.get('/', follow_redirects=True) cookies = response.headers.getlist('Set-Cookie') name = 'state' for cookie in cookies: for c_key, c_value in parse_cookie(cookie).items(): if c_key == name: state = State(c_value) self.assertEqual(c_value, state.state) state.delete() print(c_value) self.assertEqual(response.status_code, 200)
def db_manager(tconf, tdir): db_manager = DatabaseManager() name = 'name' idr_cache = IdrCache( name, initKeyValueStorage(KeyValueStorageType.Rocksdb, tdir, tconf.idrCacheDbName, db_config=tconf.db_idr_cache_db_config)) db_manager.register_new_store('idr', idr_cache) db_manager.register_new_database(DOMAIN_LEDGER_ID, get_fake_ledger(), State()) return db_manager
def txn_author_agreement_aml_handler(tconf, domain_state): data_manager = DatabaseManager() handler = TxnAuthorAgreementAmlHandler(data_manager, FakeSomething()) state = State() state.txn_list = {} state.get = lambda key, isCommitted=False: state.txn_list.get(key, None) state.set = lambda key, value, isCommitted=False: state.txn_list.update( {key: value}) data_manager.register_new_database(handler.ledger_id, FakeSomething(), state) data_manager.register_new_database(DOMAIN_LEDGER_ID, FakeSomething(), domain_state) return handler
def parse_input(filename): """Read input from filename.""" with open(filename, 'r') as f: # create state while extracting first line state = State(*[int(x) for x in next(f).split()]) # save info in state for i in range(state.num_pizzas): line = next(f).split() state.pizzas.append(Pizza(i, int(line[0]), line[1:])) return state
def dispatch(self, command: Command, now: int): start_time = command.start_time end_time = command.start_time + command.duration next_color = self.determine_color(command.start_color, command.color, start_time, end_time, now) next_buzzer = self.determine_buzzer(command.buzzer_pattern, start_time, end_time, now) next_state = State(next_color, next_buzzer) self.state_to_hardware(next_state) return next_state
def index(): token = Token(request) if token.status: HandleCreds(token, credentials, UserData, session) access_token = session.get('access_token', False) if access_token: logging.debug('access_token: {}'.format(access_token)) resp = make_response(render_template('index.html')) session_state = session.get('state', 0) if session_state == 0: state = State(uuid.uuid4()) state.insert() resp.set_cookie('state', state.state) session['state'] = state.state else: state = State(session_state) state.insert() resp.set_cookie('state', state.state) return resp
def parse_input(filename): """Read input from filename.""" with open(filename, 'r') as f: # create state while extracting first line state = State(*[int(x) for x in next(f).split()]) # save info in state for _ in range(state.a): x, y, z = [int(i) for i in next(f).split()] state.a_values.append((x, y, z)) for _ in range(state.b): x, y = [int(i) for i in next(f).split()] state.b_values.append((x, y)) return state
def db_manager(tconf, tdir): db_manager = DatabaseManager() state = State() state.txn_list = {} state.get = lambda key, isCommitted=True: state.txn_list.get(key, None) state.set = lambda key, value: state.txn_list.update({key: value}) name = 'name' idr_cache = IdrCache(name, initKeyValueStorage(KeyValueStorageType.Rocksdb, tdir, tconf.idrCacheDbName, db_config=tconf.db_idr_cache_db_config)) db_manager.register_new_store(IDR_CACHE_LABEL, idr_cache) db_manager.register_new_database(DOMAIN_LEDGER_ID, get_fake_ledger(), state) return db_manager
def get_current_command(self, now): if self.current_command.is_expired(now): # Make sure our state is up to date if not self.current_command.color.is_no_change(): self.state = State(self.current_command.color, BuzzerPattern.NONE) if len(self.commands) > 0: self.current_command = self.commands[0] del self.commands[0] self.current_command.set_start_values(now, self.state.color) else: # We have no commands, use the default, but update the # start values self.current_command = DefaultCommand self.current_command.set_start_values(now, self.state.color) return self.current_command
def test_dispatches_to_hardware_with_buzzer(self): with MockHardware() as hardware_service: dispatcher = Dispatcher(hardware_service) current_state = State(color=Color(100, 100, 100), buzzer_pattern=BuzzerPattern.NONE) command = Command(color=Color.no_change(), duration=100, buzzer_pattern=BuzzerPattern(duration=10, strength=1)) now = 0 next_state = dispatcher.dispatch(current_state, command, now) self.assertEqual(next_state.color.r, 100) self.assertEqual(next_state.color.g, 100) self.assertEqual(next_state.color.b, 100) self.assertEqual(next_state.buzzer_pattern.strength, 1) self.assertEqual(hardware_service.motor_called_count, 1) self.assertEqual(hardware_service.motor_state, 1) now = 10 next_state = dispatcher.dispatch(next_state, command, now) self.assertEqual(next_state.color.r, 100) self.assertEqual(next_state.color.g, 100) self.assertEqual(next_state.color.b, 100) self.assertEqual(next_state.buzzer_pattern.strength, 1) self.assertEqual(hardware_service.motor_called_count, 2) self.assertEqual(hardware_service.motor_state, 1) now = 90 next_state = dispatcher.dispatch(next_state, command, now) self.assertEqual(next_state.color.r, 100) self.assertEqual(next_state.color.g, 100) self.assertEqual(next_state.color.b, 100) self.assertEqual(next_state.buzzer_pattern.strength, 0) self.assertEqual(hardware_service.motor_stop_called_count, 1) self.assertEqual(hardware_service.motor_state, 0)
def main(): black = (0, 0, 0) pygame.init() screen = pygame.display.set_mode((1000, 1000)) done = False foe_ball = Ball(200, 200, uuid4(), Team.FOE, 50, 50) friend_ball = Ball(100, 200, uuid4(), Team.FRIEND, 50, -50) ego_ball = Ball(500, 500, uuid4(), Team.EGO, 50, 50) flag = Flag(100, 100) world_state = State([foe_ball] + [friend_ball] + [ego_ball], flag) clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True screen.fill(black) # reset the screen before we redraw the state clock.tick(60) # tagpro runs at 60hz delta_t_ms: int = clock.get_time() pygame_pressed = pygame.key.get_pressed() if pygame_pressed[pygame.K_k]: ego_ball.handle_pop() elif pygame_pressed[pygame.K_r]: ego_ball.is_popped = False elif pygame_pressed[pygame.K_SPACE]: # Of all sensible keypress combinations, choose the one with the lowest score in dt. best_keypresses = myopic.best_keypresses(world_state, delta_t_ms) current_input = Input({ego_ball.id: best_keypresses}) else: current_input = Input( {ego_ball.id: Keys.from_pygame_pressed(pygame_pressed)}) world_state = world_state.next_state(current_input, delta_t_ms) world_state.draw(screen) pygame.display.flip()
def parseState(str): """文字列を読み取って状態と ID の組 (state(ID, name) の形で表される) を取得し、ID をキーとする辞書を返します Args: str (string): 入力文字列 Returns: dictionary: ID をキーとし、値に State オブジェクトをもつ辞書 """ states = {} state_group = re.findall("state\([0-9]+,\{.*?\}\)", str) for state in state_group: id = re.search("[0-9]+", state).group(0) name = re.search("\{.*?\}", state).group(0) lastPos = 0 # 状態に適宜改行を入れて読みやすくする while name.find(" ", lastPos + 15) != -1: lastPos = name.find(" ", lastPos + 15) if name[lastPos + 1] != "}": name = name[:lastPos] + "\n" + name[lastPos+1:] states[id] = State(name) return states
def test_model(episode_count, data_test, data_test_open, start_balance, model_name): # Define arrays to store per episode values Act_datasize = len(data_test) Act_Bench_Stock1_Bal = int( np.floor((start_balance / 2) / data_test_open[0])) Act_Bench_Open_cash = start_balance / 2 model = load_model("models/" + model_name) # Actual run episode_count = 0 # Define arrays to store per episode values total_Prof = [] total_stock1bal = [] total_open_cash = [] total_port_value = [] total_days_played = [] Act_total_Prof = [] Act_total_stock1bal = [] Act_total_open_cash = [] Act_total_port_value = [] Act_total_days_played = [] actions_done_perday = [] portfolio_value = [] for e in range(1): # here we run only for 1 episode, as it is Test run Bal_stock1_t2 = Act_Bench_Stock1_Bal done = False open_cash_t2 = Act_Bench_Open_cash total_profit = 0 reward = 0 # Initialize Agent agent_test = Agent(8, is_eval=True, model_name=model_name) # agent = Agent(8) agent_test.inventory1 = [] for i in range(Bal_stock1_t2): agent_test.inventory1.append(data_test_open[0]) # Timestep delta to make sure that with time reward increases for taking action timestep_delta = 0 # Running episode over all days in the datasize for t in range(Act_datasize): print("..........") print(data_test.iloc[t, 0]) state_class_obj = State(data_test_open, Bal_stock1_t2, open_cash_t2, t) state_array_obj = state_class_obj.getState() action = agent_test.act(state_array_obj) print("Total portfolio value: " + str(state_class_obj.portfolio_value) + " stock 1 number: " + str(len(agent_test.inventory1)) + " open cash" + str(state_class_obj.open_cash)) # reward should be more as time goes further. We will remove reward_timedelta from actual reward # reward_timedelta=(datasize-t)*timestep_delta change_percent_stock1 = (state_class_obj.Stock1Price - state_class_obj.fiveday_stock1 ) / state_class_obj.fiveday_stock1 * 100 # print("change_percent_stock1: "+str(change_percent_stock1)) # print("change_percent_stock2: "+str(change_percent_stock2)) if action == 0: # buy stock 1 if state_class_obj.Stock1Price > state_class_obj.open_cash: ''' print("Buy stock 1 when it did not have cash, so bankrupt, end of episode") reward=-reward_timedelta*10 done = True ''' done = True # end episode else: # print("In Buy stock 1") agent_test.inventory1.append(data_test_open[t]) Bal_stock1_t2 = len(agent_test.inventory1) open_cash_t2 = state_class_obj.open_cash - state_class_obj.Stock1Price # Here we are buying 1 stock if action == 1: # sell stock 1 if state_class_obj.Stock1Blnc < 1: # print("sold stock 2 when it did not have stock 2, so bankrupt, end of episode") done = True # end episode else: # print("In sell stock 1") agent_test.inventory1.pop(0) Bal_stock1_t2 = len(agent_test.inventory1) # Bal_stock2_t2 = len(agent_test.inventory2) open_cash_t2 = state_class_obj.open_cash + state_class_obj.Stock1Price # State[0] is the price of stock 1. Here we are buying 1 stoc if action == 2: # Do nothing action Bal_stock1_t2 = len(agent_test.inventory1) # Bal_stock2_t2 = len(agent_test.inventory2) # print("Do nothing") if t == Act_datasize - 1: # print("t==datasize") done = True next_state_class_obj = State(data_test_open, Bal_stock1_t2, open_cash_t2, t) next_state_array_obj = next_state_class_obj.getState() else: # print("t!=datasize"+str(open_cash_t2)) next_state_class_obj = State(data_test_open, Bal_stock1_t2, open_cash_t2, t + 1) next_state_array_obj = next_state_class_obj.getState() # print("Action is "+str(action)+" reward is" + str(reward)) actions_done_perday.append(action) portfolio_value.append(next_state_class_obj.portfolio_value) if done == True: print("--------------------------------") print("Total Profit: " + formatPrice(next_state_class_obj.portfolio_value - start_balance)) print("Total No. of days played: " + str(t) + " out of overall days: " + str(Act_datasize)) print("Total portfolio value: " + str(next_state_class_obj.portfolio_value) + " stock 1 number: " + str(len(agent_test.inventory1)) + " open cash" + str(next_state_class_obj.open_cash)) # + " stock 2 number: " + str(len(agent_test.inventory2)) Act_total_Prof.append(total_profit) Act_total_stock1bal.append(len(agent_test.inventory1)) # Act_total_stock2bal.append(len(agent_test.inventory2)) Act_total_open_cash.append(state_class_obj.open_cash) Act_total_port_value.append(state_class_obj.portfolio_value) Act_total_days_played.append(t) print("--------------------------------") state_class_obj.reset() break opencash = state_class_obj.open_cash return total_profit, portfolio_value, opencash, Act_total_days_played
def test_State(self): uuid = 'd1437765-TEST-4d1c-a8dc-adbf7d6bbded' state = State(uuid) state.read() state.insert() state.delete()
def parse(self, passages, mode="test"): """ Parse given passages :param passages: iterable of passages to parse :param mode: "train", "test" or "dev". If "train", use oracle to train on given passages. Otherwise, just parse with classifier. :return: generator of pairs of (parsed passage, original passage) """ train = mode == "train" dev = mode == "dev" test = mode == "test" assert train or dev or test, "Invalid parse mode: %s" % mode passage_word = "sentence" if Config().sentences else \ "paragraph" if Config().paragraphs else \ "passage" self.total_actions = 0 self.total_correct = 0 total_duration = 0 total_tokens = 0 num_passages = 0 for passage in passages: l0 = passage.layer(layer0.LAYER_ID) num_tokens = len(l0.all) total_tokens += num_tokens l1 = passage.layer(layer1.LAYER_ID) labeled = len(l1.all) > 1 assert not train or labeled, "Cannot train on unannotated passage" print("%s %-7s" % (passage_word, passage.ID), end=Config().line_end, flush=True) started = time.time() self.action_count = 0 self.correct_count = 0 self.state = State(passage, callback=self.pos_tag) self.state_hash_history = set() self.oracle = Oracle(passage) if train else None failed = False try: self.parse_passage( train) # This is where the actual parsing takes place except ParserException as e: if train: raise Config().log("%s %s: %s" % (passage_word, passage.ID, e)) if not test: print("failed") failed = True predicted_passage = passage if not train or Config().verify: predicted_passage = self.state.create_passage( assert_proper=Config().verify) duration = time.time() - started total_duration += duration if train: # We have an oracle to verify by if not failed and Config().verify: self.verify_passage(passage, predicted_passage, train) if self.action_count: print("%-16s" % ("%d%% (%d/%d)" % (100 * self.correct_count / self.action_count, self.correct_count, self.action_count)), end=Config().line_end) print("%0.3fs" % duration, end="") print("%-15s" % ("" if failed else " (%d tokens/s)" % (num_tokens / duration)), end="") print(Config().line_end, end="") if train: print(Config().line_end, flush=True) self.total_correct += self.correct_count self.total_actions += self.action_count num_passages += 1 yield predicted_passage, passage if num_passages > 1: print("Parsed %d %ss" % (num_passages, passage_word)) if self.oracle and self.total_actions: print("Overall %d%% correct transitions (%d/%d) on %s" % (100 * self.total_correct / self.total_actions, self.total_correct, self.total_actions, mode)) print( "Total time: %.3fs (average time/%s: %.3fs, average tokens/s: %d)" % (total_duration, passage_word, total_duration / num_passages, total_tokens / total_duration), flush=True)
def train_model(episode_count, start_balance, data_train, training, date): from os import path # Define arrays to store per episode values total_Prof = [] total_stock1bal = [] total_open_cash = [] total_port_value = [] total_days_played = [] batch_size = 64 # Training run for e in range(episode_count + 1): print("..........") print("Episode " + str(e) + "/" + str(episode_count)) Bal_stock1 = int(np.floor((start_balance / 2) / data_train[0])) open_cash = start_balance / 2 datasize = training done = False total_profit = 0 reward = 0 max = 0 # Initialize Agent agent = Agent(5) agent.inventory1 = [] for i in range(Bal_stock1): agent.inventory1.append(data_train[0]) # Timestep delta to make sure that with time reward increases for taking action # timestep_delta=0 # Running episode over all days in the datasize for t in range(datasize): # print("..........") # print(pd_data1_train.iloc[t,0]) state_class_obj = State(data_train, Bal_stock1, open_cash, t) state_array_obj = state_class_obj.getState() action = agent.act(state_array_obj) change_percent_stock1 = (state_class_obj.Stock1Price - state_class_obj.fiveday_stock1 ) / state_class_obj.fiveday_stock1 * 100 # profit=data1_train[t]-agent.inventory1(-1) # print("change_percent_stock1: "+str(change_percent_stock1)) # if action not in [0,1,2]: # reward= reward-1000 # decide_reward(action,data_train) if action == 0: # buy stock 1 if state_class_obj.Stock1Price > state_class_obj.open_cash: ''' print("Buy stock 1 when it did not have cash, so bankrupt, end of episode") reward=-reward_timedelta*10 done = True ''' reward = reward - 4000 # done = True # end episode else: # print("In Buy stock 1") agent.inventory1.append(data_train[t]) Bal_stock1_t1 = len(agent.inventory1) # Bal_stock2_t1 = len(agent.inventory2) open_cash_t1 = state_class_obj.open_cash - state_class_obj.Stock1Price # Here we are buying 1 stock # needs to be reviewed if (state_class_obj.open_cash < 500): reward = reward - 2000 elif (0.1 * Bal_stock1_t1 > Bal_stock1): reward = reward - (1000 * Bal_stock1_t1) # elif (abs(change_percent_stock1) <= 2): # reward = reward-2000 else: reward = reward - (change_percent_stock1 * 1000) if action == 1: # sell stock 1 if state_class_obj.Stock1Blnc < 1: # print("sold stock 2 when it did not have stock 2, so bankrupt, end of episode") reward = reward - 4000 # done = True # end episode else: # print("In sell stock 1") bought_price1 = agent.inventory1.pop(0) Bal_stock1_t1 = len(agent.inventory1) total_profit += data_train[t] - bought_price1 # Bal_stock2_t1 = len(agent.inventory2) open_cash_t1 = state_class_obj.open_cash + state_class_obj.Stock1Price # State[0] is the price of stock 1. Here we are selling 1 stoc if (0.1 * Bal_stock1_t1 > Bal_stock1): reward = reward - (1000 * Bal_stock1_t1) # elif (abs(change_percent_stock1) <= 2): # reward = -1000 elif total_profit > 200: reward = reward + (2000 * total_profit) else: reward = reward + ( change_percent_stock1 * 100 ) # State[0] is the price of stock 1. Here we are selling 1 stock # total_profit += data1_train[t] - bought_price1 # print("reward for sell stock1 " + str(reward)) if action == 2: # Do nothing action # if (abs(change_percent_stock1) <= 2): # reward = 100 if (state_class_obj.open_cash < 0.05 * start_balance): reward += 2000 else: reward = reward - 2000 Bal_stock1_t1 = len(agent.inventory1) # Bal_stock2_t1 = len(agent.inventory2) open_cash_t1 = open_cash # print("Do nothing") if t == datasize - 1: # print("t==datasize") done = True next_state_class_obj = State(data_train, Bal_stock1_t1, open_cash_t1, t) next_state_array_obj = next_state_class_obj.getState() else: next_state_class_obj = State(data_train, Bal_stock1_t1, open_cash_t1, t + 1) next_state_array_obj = next_state_class_obj.getState() agent.memory.append( (state_array_obj, action, reward, next_state_array_obj, done)) # print("Action is "+str(action)+" reward is" + str(reward)) Bal_stock1 = Bal_stock1_t1 # Bal_stock2 = Bal_stock2_t1 open_cash = open_cash_t1 if done == True: total_Prof.append(total_profit) total_stock1bal.append(len(agent.inventory1)) # total_stock2bal.append(len(agent.inventory2)) total_open_cash.append(state_class_obj.open_cash) total_port_value.append(state_class_obj.portfolio_value) total_days_played.append(t) print("--------------------------------") state_class_obj.reset() break if len(agent.memory) > batch_size: agent.expReplay(batch_size) print(reward) if reward > max: max = reward agent.model.save("models/model_" + date + "-max") if e % 30 == 0: agent.model.save("models/model_" + date + "-" + str(e)) if path.exists("models/model_" + date + "-max"): model_name = "model_" + date + "-max" else: model_name = "model_" + date + "-" + str(episode_count) return model_name