def track(env, terminal, done, policy): # episode done if terminal or done: # save all data utils.save_as_pickle(env.world.episode_info, None, 'rp_ep{}_{}'.format(env.world.position_index, policy), force_save=True) # reset env.world.episode_info = utils.reset_tracking_info() env.world.episode_info['a1'].append( env.world.positions[env.world.position_index + 1][0].tolist()) env.world.episode_info['a2'].append( env.world.positions[env.world.position_index + 1][1].tolist()) env.world.episode_info['goal'] = env.world.positions[ env.world.position_index + 1][-1].tolist() env.world.episode_info['random_positions'] = env.world.positions[ env.world.position_index + 1] return a1_pos = utils.get_position(env, 'a1').tolist() a2_pos = utils.get_position(env, 'a2').tolist() goal_pos = utils.get_position(env, 'goal').tolist() env.world.episode_info['a1'].append(a1_pos) env.world.episode_info['a2'].append(a2_pos) env.world.episode_info['goal'] = goal_pos
def __init__(self, number): super().__init__() gate_height = {1: 48, 2: 64, 3: 56} gate_positions = { 1: Vector(0.19592592592592592, -0.1559259259259259, 0), 2: Vector(0.5624074074074075, -0.0024074074074074137, 0), 3: Vector(1.1218518518518519, -0.08185185185185184, 0), 4: Vector(0.24537037037037024, 4.75462962962963, 0), 5: Vector(0.6072222222222221, 4.9127777777777775, 0), 6: Vector(1.1700000000000002, 4.836666666666667, 0), } gtype = number if number > 3: gtype = number - 3 self.image = pygame.transform.scale( pygame.image.load( "assets/gate{}.png".format(gtype)).convert_alpha(), (25, gate_height[gtype])) self.rect = self.image.get_rect() self.position = gate_positions[number].copy() self.rect.x, self.rect.y = utils.get_position(*self.position.xyz())
def update(self, delta): if self.stunned == False: if not self.holds_ball: self.velocity.x = self.movement.x * self.speed self.velocity.y = self.movement.y * self.speed else: self.velocity.x = self.movement.x * self.speed_holding self.velocity.y = self.movement.y * self.speed_holding self.position.x += delta * self.velocity.x self.position.y += delta * self.velocity.y self.position.x, self.position.y, self.position.z = utils.cut_to_map( *self.position.xyz()) if round(self.movement.x, 2) == 0 and round(self.movement.y, 2) == 0: pass else: angle = round( math.degrees(math.atan2(self.velocity.y, self.velocity.x))) if angle in self.images: self.image = self.images[angle] else: self.stunned = not time.time() > self.stun_time if self.change_image_to_stunned: self.image = pygame.transform.rotate(self.image, 90) self.change_image_to_stunned = False self.rect.x, self.rect.y = utils.get_position(*self.position.xyz())
def parse(self, source): result = [] source = source length = len(source) index = 0 while index < length: match = self.parser_regex.match(source, index) if match: group_name = match.lastgroup tag, handler = self.groups[group_name] text = match.group(group_name) if handler: text = handler(text) if tag: result += JSONxToken(tag, text, index, source), else: raise exception.LexerException( 'Illegal character "{0}"'.format( source[index].encode('unicode-escape')), utils.get_position(source, index)) index = match.end() result += JSONxToken(Type.EOF, 'EOF', index, source), return result
def manhatan(state, goal): cost = 0 for row in range(len(state)): for col in range(len(state[0])): pos = get_position(goal, state[row][col]) cost += abs(row - pos[0]) + abs(col - pos[1]) return cost
def out_of(state, goal): cost = 0 for i, row in enumerate(state): for j, cell in enumerate(row): x, y = get_position(goal, state[i][j]) if x != j: cost += 1 if y != i: cost += 1 return cost
def generate_data(lvl, num_rollouts, max_steps, model): f = open(constants.SOLVEDPATH + lvl, "rb") lvl_data = pickle.load(f) f.close() # First, get the moves for the true path. position = utils.PushPosition(copy.deepcopy(lvl_data[0])) steps = lvl_data[2] for step in steps: position.step_in_direction(step) moves = position.moves # Now go back to the start and make some of # those moves, but not all of them. number_moves_to_make = random.randint(0, len(moves) - 2) position = utils.get_position(copy.deepcopy(lvl_data[0]), moves[:number_moves_to_make]) # Make some moves in this position with probability # according to the network. number_additional_moves = random.randint(2, 10) for i in range(number_additional_moves): results_var = [0, 0, 0, 1000] montecarlo.monte_carlo_advance([position], model, [0], [0], 1, results_var, 1, 1000) if results_var[0] > 0: print("Accidentally solved it") generate_data(lvl, num_rollouts, max_steps, model) return print(position.prettystring()) moves_for_data = position.moves # Figure out whether this position is possible. result = montecarlo.monte_carlo(position, model, num_rollouts, 512, max_steps, verbosity=0) solvable = result[0] > 0 num_steps = None if solvable: num_steps = result[3] print(solvable) print(num_steps) add_data_point(lvl, moves_for_data, solvable, num_steps)
def get_neigbors(state): neighbors = [] empty_x, empty_y = get_position(state, 0) size = len(state) for (dx, dy) in DIRECTIONS.values(): x, y = empty_x + dx, empty_y + dy if 0 <= x < size and 0 <= y < size: new_state = deepcopy(state) new_state[empty_x][empty_y] = state[x][y] new_state[x][y] = 0 neighbors.append(new_state) return neighbors
def update(self, delta): if self.grabbed == False: self.velocity.z -= delta * settings.GRAVITY self.position.x += delta * self.velocity.x self.position.y += delta * self.velocity.y self.position.z += delta * self.velocity.z bounce_x, bounce_y, bounce_z = utils.bounce_to_map(*self.position.xyz()) self.pos_x, self.pos_y, self.pos_z = utils.cut_to_map(*self.position.xyz()) self.velocity.x *= bounce_x self.velocity.y *= bounce_y self.velocity.z *= bounce_z else: pass self.rect.x, self.rect.y = utils.get_position(*self.position.xyz())
def env_step(self, action): land_x, land_y = get_landing_zone( ) # gets the x, y coordinate of the landing zone vel_x, vel_y = get_velocity( action) # gets the x, y velocity of the lander angle = get_angle(action) # gets the angle the lander is positioned in pos_x, pos_y = get_position( action) # gets the x, y position of the lander fuel = get_fuel( action) # get the amount of fuel remaining for the lander terminal = False reward = 0.0 observation = (vel_x, vel_y, angle, pos_x, pos_y, land_x, land_y, fuel) # use the above observations to decide what the reward will be, and if the # agent is in a terminal state. # Recall - if the agent crashes or lands terminal needs to be set to True # your code here if (fuel == 0): reward = -100 terminal = True else: if (pos_y > land_y): reward = 0 terminal = False else: if (pos_x != land_x): reward = -100 terminal = True else: if ((vel_y < -3) or (vel_x < -10) or (vel_x > 10) or ((angle < 355) and (angle > 5))): reward = -100 terminal = True else: reward = fuel self.reward_obs_term = (reward, observation, terminal) return self.reward_obs_term
def school_location(): """get lng and lat of schools""" data = [] schools = [{ 'name': '东南大学', 'city': '南京市' }, { 'name': '南京大学', 'city': '南京市' }, { 'name': '南京工程学院', 'city': '南京市' }] for idx, school in enumerate(schools): geo_result = get_position('%s(%s)' % (school['name'], school['city'])) datum = {'name': school['name']} datum.update(geo_result['result']['location']) data.append(datum) df = pandas.DataFrame(data) # 去除索引 df.to_csv('uploads/area.csv', index=False)
def parse(self, source): result = [] source = source length = len(source) index = 0 while index < length: match = self.parser_regex.match(source, index) if match: group_name = match.lastgroup tag, handler = self.groups[group_name] text = match.group(group_name) if handler: text = handler(text) if tag: result += JSONxToken(tag, text, index, source), else: raise exception.LexerException('Illegal character "{0}"' .format(source[index].encode('unicode-escape')), utils.get_position(source, index)) index = match.end() result += JSONxToken(Type.EOF, 'EOF', index, source), return result
# vid = cv2.VideoCapture('vid.mp4') vid = cv2.VideoCapture(0) print('Select application, waiting for 5 seconds..') time.sleep(5) count = 0 while (vid.isOpened()): check, frame = vid.read() count += 1 # cv2.imshow("image", frame) if check is False: break pose, meta = get_pred(frame, model, 0.60) rwrist, lwrist = meta['r-wrist'], meta['l-wrist'] # print(rwrist, lwrist) # using prediction coordinates as the aspect ratio can vary - # a lot and might be difficult to get a generalized position position = get_position(rwrist[2], lwrist[2]) print(position) # control_mouse(position) control_keyboard(position) cv2.imshow("image", pose) # show skeleton key = cv2.waitKey(1) & 0xFF if key == ord('q'): break vid.release() cv2.destroyAllWindows()
def line_col(self): if not self.__line_col: self.__line_col = utils.get_position(self.source, self.position) return self.__line_col
def update(self): pos = utils.get_position(*self.position.xyz()) self.rect.centerx = pos[0] self.rect.centery = pos[1]
def update(self): self.position.x = self.ball.position.x self.position.y = self.ball.position.y - 0.01 # Magic self.position.z = 0 self.rect.x, self.rect.y = utils.get_position(*self.position.xyz())