def get_last_kart_coordinate(): #track = _load_track_example2() track = _load_track() N = 100000 for idx in range(N): res = track.do_tick() collisions = track.clean_crashed_karts() ## if collisions: ## log("================ %d" %idx) ## log("Collision(s) occured") ## for c in collisions: ## log("--- %s" % (c.coordinate(),)) ## log("%d karts remaining" % len(track.karts)) if len(track.karts) == 1: log("================ %d" %idx) c = track.karts[0].coordinate() return (c[1], c[0]) #inverted elif len(track.karts) == 0: log("no more karts!") return None return None
def del_finished(self): task_finished = [] for idx in range(len(self.workers)): if self.workers[idx] is not None and self.workers[idx].time == 0: log("worker '%d' completed %s" % (idx, self.workers[idx].name)) task_finished.append(self.workers[idx].name) self.workers[idx] = None return task_finished
def get_ordered_instructions(): instructions = _load_instructions() g = Graph() for i in instructions: g.add_instruction(i) ordered_instruction = "" while len(g.nodes): heads = g.get_heads() h = list(heads.keys()) h.sort() ordered_instruction += h[0] log(ordered_instruction) g.del_node(h[0]) return ordered_instruction
def get_first_collision_coordinate(): #track = _load_track_example() track = _load_track() log(track) N = 10000 for i in range(N): log("================ %d" %i) res = track.do_tick(return_when_crash=True) if res != False: log(track) return (res[1], res[0]) #inverted break log("no collision detected in this amount of ticks (%d)" % N) return None
def get_time_to_get_message(max_time=100000): stars = _get_stars() N = len(stars) stars_pos0 = np.array([s.pos for s in stars]) # init position stars_vel0 = np.array([s.vel for s in stars]) # init velocity log("compute integrated positions...") stars_poses = np.array( [stars_pos0 + stars_vel0 * t for t in range(max_time)]) log("compute system dimensions...") stars_sys_dim = _get_stars_systems_dim(stars_poses) log("filter possible systems (with small dimensions)...") possible_systems = [(t, sp) for t, sp in enumerate(stars_poses) if all(stars_sys_dim[t] < 2 * N)] log("%d possibility" % len(possible_systems)) log("compute connexity...") connexity = [(t, len(_get_subgraphs(sp)), sp) for t, sp in possible_systems] T, min_connex_size, min_connex = min(connexity, key=lambda x: x[1]) log("found at time %d with min connexity %d" % (T, min_connex_size)) return T
def get_message_in_sky(max_time=100000): stars = _get_stars() N = len(stars) stars_pos0 = np.array([s.pos for s in stars]) # init position stars_vel0 = np.array([s.vel for s in stars]) # init velocity log("compute integrated positions...") stars_poses = np.array( [stars_pos0 + stars_vel0 * t for t in range(max_time)]) log("compute system dimensions...") stars_sys_dim = _get_stars_systems_dim(stars_poses) log("filter possible systems (with small dimensions)...") possible_systems = [ sp for sp, dim in zip(stars_poses, stars_sys_dim) if all(dim < 2 * N) ] log("%d possibility" % len(possible_systems)) log("compute connexity...") connexity = [(len(_get_subgraphs(sp)), sp) for sp in possible_systems] min_connex_size, min_connex = min(connexity, key=lambda x: x[0]) log("found with min connexity %d" % min_connex_size) subgraphs = _get_subgraphs(min_connex) pl.figure() for i, sg in enumerate(subgraphs): pl.plot(sg[:, 0], -sg[:, 1], "o", color=[float(i) / len(subgraphs)] * 3) pl.grid() pl.axis('equal') pl.show()
def get_time_to_complete(): instructions = _load_instructions() g = Graph() for i in instructions: g.add_instruction(i) for k, v in g.nodes.items(): v.value = (ord(k)-64) + 60 T = 0 pool = PoolOfWorker(5) while len(g.nodes) or pool.has_tasks_in_process(): log("=========== time: %d =============" % T) log("remaining: %s (%d)" % (",".join(g.nodes.keys()), len(g.nodes.keys()))) log("in process: %s" % [(t.name, t.time)for t in pool.get_current_tasks()]) heads = g.get_heads() sheads = heads.keys() sheads.sort() for task in pool.get_current_tasks(): if task.name in sheads: sheads.remove(task.name) log("head tasks: %s || available workers: %d" % (sheads, pool.nb_worker_available())) log("--- assign ------------------") while len(sheads) and pool.nb_worker_available(): nname = sheads.pop(0) pool.add_node_as_task(heads[nname]) log("--- process ------------------") t = pool.process_until_one_finishes() T += t ## one can also do step by step: #pool.process(1) #T += 1 log("--- delete completed ------------------") task_finished = pool.del_finished() for tname in task_finished: g.del_node(tname) log("Final time: %d" % T) return T
def add_task(self, task): idx = self.get_available_worker_index() log("assign %s (%d) to worker '%d'" % (task.name, task.time, idx)) self.workers[idx] = task