def start(self): '''Starts trying to solve the task, slowly making the solution better. It's ok if start() finishes by itself (e.g. if it thinks it found the best possible solution). Should periodically (at least once in 10 seconds) check if self.stopped is True, and if it is, stop executing. ''' self.stopped = False while True: m = World.from_string(self.data) program = '' while True: if random.randint(1, 100) <= 33: ch = 'A' else: ch = random.choice('ULDR') program += ch m, final_score = m.apply_command(ch) if final_score is not None: break if final_score > self.solution[1]: self.solution = (program, final_score) if self.stopped: break
def test_processing_step(tests, step, verbose=False): fails = 0 for test, result in tests: if verbose: print "-" * 10 print test assert test.startswith("\n") test = test[1:] world = World.from_string(test) width = world.width data = world.data reachable = step(width, data) if verbose: print data_to_string(width, reachable) w, expected = parse_result(result) assert w == width assert len(expected) == len(data), (len(expected), len(data)) expected_bool = [{"0": False, "1": True}[e] for e in expected] actual_string = [{False: "0", True: "1"}[e] for e in reachable] if expected_bool != reachable: if not verbose: print "-" * 10 print test print data_to_string(width, actual_string) print "fail, expected" print data_to_string(width, expected) fails += 1 assert fails == 0, fails print "ok"
score = World(world).apply_commands(solution).score print 'genetic done', score if score > 0: solver.best_score = score solver.best_solution = solution if __name__ == '__main__': realout = sys.stdout sys.stdout = sys.stderr signal.signal(signal.SIGINT, handler) data = sys.stdin.read() world = World.from_string(data) world.show() solver = Solver(world, timeout=1000000) greedy(World(world)) call_genetic(World(world)) solver.solve() print>>realout, solver.get_best()[1] print 'final score', solver.get_best()[0] sys.stdout = realout
def from_string(s): from dict_world import DictWorld from world import World return DualWorld(DictWorld.from_string(s), World.from_string(s))