def rec(state, depth, stack_size): if clock() - start > time_limit: return s = state.get_score_abort() if depth <= 0 or stack_size <= 0: check(s) return preprocessed = preprocess_world(state) if upper_bound(preprocessed)-state.time <= best.score: return aggressive_preprocess(preprocessed) frozen_state = preprocessed.get_hash() old_score = visited.get(frozen_state) if old_score is not None and s <= old_score: return visited[frozen_state] = s check(s) zzz = 'LRUDW' num_commands = len(commands) next_steps = set() greedy = path_to_nearest_lambda_or_lift(state) if greedy is not None: greedy = [greedy[1]] else: greedy = [] #greedy = [] for cmds in chain(greedy, product(zzz, zzz)): new_state = state for cmd in cmds: if new_state.final_score is None: new_state = new_state.apply_command(cmd) commands.append(cmd) if new_state.final_score is None: h = new_state.get_hash() if h not in next_steps: next_steps.add(h) if cmds in greedy: new_depth = depth else: new_depth = depth-1 rec(new_state, new_depth, stack_size-1) else: check(new_state.final_score) for _ in cmds: commands.pop() assert num_commands == len(commands)
def greedy(world): initial_world = World(world) global solver cmds = '' while not world.terminated: if len(cmds) > 10000: break #print cmds t = utils.path_to_nearest_lambda_or_lift(world) if t is None: break _, c = t #print c world = world.apply_commands(c) cmds += c if world.score > solver.best_score: solver.best_score = world.score solver.best_solution = cmds print 'greedy', solver.best_score, len(solver.best_solution) if initial_world.apply_commands(cmds).score < 0: solver.best_score = 0 solver.best_solution = '' print 'shit!'