def main(): args = opt_parser.parse_args() logging.basicConfig(level=verbosity_to_log_level(args.verbosity), format=log_fmt) log.info('startup') w = world.read_world(args.files) w.remaining_lambdas = 3 log.debug('read world: %r', w) print 'STARTING MAP' print w print print '~~~~~~~~~~~~~~~~~' print for move in 'DDDLUULLDDL': w = w.move(move) print str(w) + ' after moving %s' % (move,) print print w.new_moved_rocks.rocks log.info('shutdown')
def main(): opt_parser = argparse.ArgumentParser() #opt_parser.add_argument('--verbose', '-v', dest='verbosity', #default=0, action='count') opt_parser.add_argument('--stdin', dest='use_stdin', default=False, action='store_true') opt_parser.add_argument('file') args = opt_parser.parse_args() if args.use_stdin: input = list(sys.stdin.read().strip()) log_fmt = u"%(asctime)s %(process)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s" logging.basicConfig(level=logging.DEBUG, format=log_fmt, filename="vis.log") log.debug("Starting vis") my_world = world.read_world(args.file) #pprint.pprint(my_world) worlds = [] moves = [] stdscr = curses.initscr() curses.start_color() curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_MAGENTA, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_RED, curses.COLOR_WHITE) curses.init_pair(5, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(6, curses.COLOR_GREEN, curses.COLOR_WHITE) curses.cbreak() curses.noecho() screen_y, screen_x = stdscr.getmaxyx() control_win = curses.newwin(5, screen_x, 0, 0) control_win.keypad(1) control_win.timeout(-1) control_win.border() control_win.refresh() world_win = curses.newwin(screen_y - 5, screen_x, 5, 0) world_win.border() world_win.refresh() if args.use_stdin: def input_iter(): while True: try: yield ord(input.pop(0)) time.sleep(0.2) except IndexError: yield -1 break else: def input_iter(): while True: yield control_win.getch() the_bot = None world_event = None chars = input_iter() try: while True: move = None display_moves(control_win, moves) display_score(control_win, my_world) draw_world(world_win, my_world) c = chars.next() log.info("c ==== %r", c) log.info(my_world) if c == -1: if args.use_stdin: time.sleep(0.5) break if c == ord('b'): if not the_bot: the_bot = bot.WeightedBot() move = the_bot.pick_move(my_world) if c in (ord('q'), ord('Q')): break if c in KEY_TO_MOVE.keys() and not my_world.is_done(): move = translate_key(c) if c == ord('u') and moves: my_world = worlds.pop() moves.pop() if move and move in my_world.valid_moves(): worlds.append(my_world) try: my_world = update_world(move, my_world) moves.append(move) except world.InvalidMove: my_world = worlds.pop() display_status(control_win, 'Invalid move') continue display_status(control_win, '') else: log.debug("Unused key, %r", curses.keyname(c)) if my_world.is_done(): display_status(control_win, 'done') if args.use_stdin: time.sleep(2) break except world.WorldEvent, e: world_event = e
self.child_nodes = {} # map from command character to node self.score = self.world.score() self.max_child_score = self.score def pprint(self, indent=0, depth_left=-1): print '%s[%s] %d %d %s' % (' '*indent, self.command_history, self.score, self.max_child_score, 'DONE' if self.world.is_done() else '') if depth_left == 0: return for cmd, child in self.child_nodes.items(): if child: child.pprint(indent+2, depth_left-1) else: print '%s[%s] None' % (' '*(indent+2), self.command_history+cmd) if __name__ == "__main__": initial_world = world.read_world([]) best_score = 0 best_commands = '' node_count = 0 map_to_node = {} # key is stringified map, value is node debug_mode = False def debug(s): if debug_mode: print s def add_node(parent, w, map_str, command_history): global node_count, best_score, best_commands
def main(): initial_world = world.read_world([]) explorable_nodes = [] explore_heapq = [] map_to_node = {} # key is stringified map, value is node debug_mode = False def debug(s): if debug_mode: print s def add_node(parent, w, map_str, command_history): global node_count, best_score, best_commands n = Node(parent, w, command_history) if best_score is None or n.score > best_score: print 'NEWBEST' best_score = n.score best_commands = n.command_history map_to_node[map_str] = n node_count += 1 if not w.is_done(): explorable_nodes.append(n) heappush(explore_heapq, (-n.score, n)) return n root = add_node(None, initial_world, world_to_map_str(initial_world), '') itercount = 0 while True: if debug_mode or ((itercount % 1000) == 0): print '%d nodes, %d explorable nodes' % (node_count, len(explorable_nodes)) print 'best score %d for [%s]' % (best_score, best_commands) #root.pprint(indent=0, depth_left=2) itercount += 1 # pick next node to explore if random.random() > 0.5: tries = 0 while True: if not explorable_nodes: from_node = None break debug('random pick') from_node = random.choice(explorable_nodes) debug('picked node [%s]' % from_node.command_history) if from_node.dominated: debug(' node was dominated, ignore') elif not from_node.unexplored_commands: debug(' no unexplored commands from this node') else: break tries += 1 if tries > 2: # compact pre_len = len(explorable_nodes) explorable_nodes = [n for n in explorable_nodes if (not n.dominated) and (n.unexplored_commands)] debug('compacted explore list from %d to %d' % (pre_len, len(explorable_nodes))) else: while True: if not explore_heapq: from_node = None break debug('queue pick') _, from_node = explore_heapq[0] debug('picked node [%s]' % from_node.command_history) if from_node.dominated: debug(' node was dominated, ignore') heappop(explore_heapq) # throw it out so we don't get it again elif not from_node.unexplored_commands: debug(' no unexplored commands from this node') heappop(explore_heapq) # throw it out so we don't get it again else: break # this will happen if we ran out of nodes during compacting if not from_node: break next_command = random.choice(from_node.unexplored_commands) from_node.unexplored_commands.remove(next_command) debug(' trying command %s from node %s' % (next_command, from_node.command_history)) assert next_command not in from_node.child_nodes next_world = from_node.world.move(next_command) # see if next world is already in some node next_map_str = world_to_map_str(next_world) matched_node = map_to_node.get(next_map_str) if matched_node is not None and next_world.num_moves >= matched_node.world.num_moves: # this command lead to a map we've already seen, with more moves, so it's useless debug(' dominated by [%s]' % matched_node.command_history) from_node.child_nodes[next_command] = None # mark this edge as useless else: # we're going to make a new node debug(' adding new node for command %s' % next_command) new_node = add_node(from_node, next_world, next_map_str, from_node.command_history+next_command) from_node.child_nodes[next_command] = new_node # if we outdid another node, need to mark it and all its children as dominated if matched_node: debug(' marking dominated nodes') matched_node.parent = from_node # who's your daddy now, bitch! q = [matched_node] while q: n = q.pop() n.dominated = True for ch in n.child_nodes.values(): if ch is not None: q.append(ch) # update max scores up chain as necessary ms = new_node.score p = new_node.parent_node while True: if not p or ms <= p.max_child_score: break p.max_child_score = ms p = p.parent_node
# opt_parser.add_argument('--verbose', '-v', dest='verbosity', default=0, action='count') opt_parser.add_argument("--iterations", "-i", dest="iterations", default=None, type=int) opt_parser.add_argument("--name", "-n", dest="name", default="random") opt_parser.add_argument("--time-based", default=0, type=int, help="max seconds to run") opt_parser.add_argument("--initial-path", default="") opt_parser.add_argument("--profile", default=False, action="store_true") opt_parser.add_argument("file") args = opt_parser.parse_args() log_fmt = u"%(asctime)s %(process)s %(levelname)s %(name)s %(message)s" logging.basicConfig(level=logging.DEBUG, format=log_fmt, filename="bot.log") log.debug("Starting vis") the_bot = bot_for_name(args.name) the_world = world.read_world(args.file) def on_finish(world, score, moves): print >> sys.stderr, "Moves: %s" % "".join(moves) print >> sys.stderr, "Score: %d (%d/%d)" % (score, world.lambdas_collected, world.remaining_lambdas) world.post_score(moves, args.file, args.name) sys.exit(0) class ascope: best = the_world def on_best(planner, world): global best print >> sys.stderr, "Got new best world:", world.score() print >> sys.stderr, world ascope.best = planner.best.key