def get_features(board, zoid_str, controller, dictionaries=True): """ Given a board (with list of list representation), future zoid, and controller, this function returns the set of all possible options and their feature values. Params: board - [[int]] - list of list representation of board zoid_str - string - string value for a zoid (e.g. "T") controller - {string: int} - dictionary controller for a simulator If dictionaries=True then it will return it as a series of nested dictionaries organized as: return_val[orientation][row][col] = {zoid: '', board: [[]], features: {}} If dictionaries=False then it will return it as a list of dictionaries, with one dictionary for each option/feature set: return_val = [{'zoid': zoid, 'orient': orientation, 'row': row, 'col': col, 'board': [[]], 'features': {}}] """ sim = TetrisSimulator(controller=controller) sim.space = tetris_cow2.convert_old_board(board) sim.curr_z = all_zoids[zoid_str] if dictionaries == True: # Return all features as an organized dictionary structure all_feats = {} for orient, pos in sim.get_options(): if orient not in all_feats.keys(): all_feats[orient] = {} if pos[0] not in all_feats[orient].keys(): all_feats[orient][pos[0]] = {} zoid = sim.curr_z.get_copy() board = sim.space.get_cow() # Not really sure what the value=2 does, but was used in simulator.py board.imprint_zoid(zoid, orient=orient, pos=pos, value=2) board_and_feats = { 'zoid': zoid_str, 'board': board.row_space(), 'features': sim.get_features(board, sim.space) } all_feats[orient][pos[0]][pos[1]] = board_and_feats return all_feats else: all_feats = [] for orient, pos in sim.get_options(): option = {} zoid = sim.curr_z.get_copy() option['zoid'] = zoid_str option['row'] = pos[0] option['col'] = pos[1] option['orient'] = orient board = sim.space.get_cow() board.imprint_zoid(zoid, orient=orient, pos=pos, value=2) option['board'] = board.row_space() option['features'] = sim.get_features(board, sim.space) all_feats.append(option) return all_feats
def predict(space, zoid, controller): sim = TetrisSimulator(controller = controller) sim.space = sim.convert_space(space) sim.curr_z = zoid sim.get_options() return sim.control()
player_board = [[0]*10 for i in range(20)] else: prev_line = lines[x - 1] prev_line = prev_line.rstrip().split('\t') player_board = eval(eval(prev_line[board_ix])) line = line.rstrip().split('\t') if line[curr_zoid_ix] != 'NA': player_zoid = line[curr_zoid_ix] working_controller = CERLscore working_features = CERLscore.keys() sim = TetrisSimulator(controller=working_controller) sim.space = tetris_cow2.convert_old_board(player_board) sim.curr_z = all_zoids[player_zoid] feats = get_features(player_board, player_zoid, working_controller, dictionaries=False) for f in feats: lh_val = f['features']['landing_height'] * working_controller['landing_height'] ec_val = f['features']['eroded_cells'] * working_controller['eroded_cells'] rt_val = f['features']['row_trans'] * working_controller['row_trans'] ct_val = f['features']['col_trans'] * working_controller['col_trans'] pit_val = f['features']['pits'] * working_controller['pits'] wells_val = f['features']['cuml_wells'] * working_controller['cuml_wells'] move_score = lh_val + ec_val + rt_val + ct_val + pit_val + wells_val out_line = "" out_line += (str(line[ep_num_ix]) + "\t")