def run(self): left_speed = 0 right_speed = 0 while self.robot.step(rcj_soccer_robot.TIME_STEP) != -1: if self.is_new_data(): data = self.get_new_data() # Get the position of our robot robot_pos = data[self.name] # Get the position of the ball ball_pos = data['ball'] ball_distanceX = abs(-ball_pos['x']) ball_distanceY = abs(robot_pos['y'] - ball_pos['y']) goalPosition = {'x': -0.75, 'y': 0} # Get angle between the robot and the ball # and between the robot and the north ball_angle, robot_angle = self.get_angles(ball_pos, robot_pos) goal_angle, robot_angle = self.get_angles( goalPosition, robot_pos) # Compute the speed for motors direction = utils.get_direction(ball_angle) direction2 = utils.get_direction(goal_angle) left_speed, right_speed = self.goToPoint(robot_pos, ball_pos) # If the robot has the ball right in front of it, go forward, # rotate otherwise """ if ball_pos["y"] > 0: point1 = {'x':robot_pos['x'],'y':ball_pos["y"]} point1_angle, robot_angle = self.get_angles(point1, robot_pos) point_direction = utils.get_direction(point1_angle) if robot_pos["y"] != ball_pos["y"]+0.5 and point_direction == 0: left_speed = -10 right_speed = -10 print("going down") else: left_speed = point_direction * 10 right_speed = point_direction * -10 print("turning") elif ball_pos["y"] < -0: point1 = {'x':robot_pos['x'],'y':ball_pos["y"]} point1_angle, robot_angle = self.get_angles(point1, robot_pos) point_direction = utils.get_direction(point1_angle) if robot_pos["y"] != ball_pos["y"]+0.5 and point_direction == 0: left_speed = -10 right_speed = -10 print("going up") else: left_speed = point_direction * 10 right_speed = point_direction * -10 print("turning") """ # Set the speed to motors self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed)
def goToPosition(self, robot_pos, point): point1 = {x: robot_pos["x"], y: point["y"]} point2 = {x: point["x"], y: robot_pos["y"]} pointAngle1, robot_angle = self.get_angles(point1, robot_pos) pointAngle2, robot_angle = self.get_angles(point2, robot_pos) direction = utils.get_direction(pointAngle1) direction = utils.get_direction(pointAngle2)
def lambda_handler(event, context): """Takes in an event from Api.ai, through Api Gateway. Returns a dict with keys "speech", "displayText", and "Source". Source is always the "BART API" """ dict() = { "speech": get_station_name() + "from" + get_direction(), "displayText": get_station_name() + "from" + get_direction(), "Source": "BART API" } return dict()
def generate(self, chance=0.2, exponent=0.5, diagonals=False): # self._generator.generate(stairs_up, stairs_down, 0.2, 0.5, False) ''' Crawl from start and go toward a point.''' # self.fill('.') grid = [list(['#'] * self.width) for _ in range(self.height)] for _ in range(3): dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)] stairs_up = (random.randint(0, self.width // 3), random.randint(0, self.height // 3)) stairs_down = (random.randint(self.width * 2 // 3 + 1, self.width - 1), random.randint(self.height * 2 // 3 + 1, self.height - 1)) current = tuple(stairs_up) grid[current[1]][current[0]] = '<' grid[stairs_down[1]][stairs_down[0]] = '>' holes = 0 org_chance = chance while True: dx, dy = random.choice(dirs) scale = math.hypot(stairs_down[0] - current[0], stairs_down[1] - current[1]) / math.hypot( self.width, self.height) chance = org_chance * scale**exponent chance = max(0.05, chance) if random.random() < chance: dx = get_direction(current, stairs_down).x if random.random() < chance: dy = get_direction(current, stairs_down).y if not diagonals and dx != 0 and dy != 0: if random.random() < 0.5: dx = 0 else: dy = 0 if current[0]+dx < 0 or current[0]+dx >= len(grid[0]) or \ current[1]+dy < 0 or current[1]+dy >= len(grid): continue current = (current[0] + dx, current[1] + dy) if grid[current[1]][current[0]] not in ['.']: if current == stairs_down: break grid[current[1]][current[0]] = '.' holes += 1 else: continue grid[stairs_up[1]][stairs_up[0]] = '.' grid[stairs_down[1]][stairs_down[0]] = '.' self.zone = Zone(grid, [], [], self._temperature) self.zone.recommended_stairs_coords = [ tuple(stairs_up), tuple(stairs_down) ]
def run(self): while self.robot.step(TIME_STEP) != -1: if self.is_new_data(): data = self.get_new_data() # Get the position of our robot robot_pos = data[self.name] # Get the position of the ball ball_pos = data['ball'] # Get angle between the robot and the ball # and between the robot and the north ball_angle, robot_angle = self.get_angles(ball_pos, robot_pos) print(ball_angle, robot_angle) # Compute the speed for motors direction = utils.get_direction(ball_angle) # If the robot has the ball right in front of it, go forward, # rotate otherwise if direction == 0: left_speed = -5 right_speed = -5 else: left_speed = direction * 4 right_speed = direction * -4 # Set the speed to motors self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed)
def add_edges(self, subgraph1, subgraph2, graph_edge_label_mapping, graph_number_of_edge_labels, number_of_edges): sg1 = subgraph1["sg"] sg2 = subgraph2["sg"] g1_vcount = subgraph1.vcount() g2_vcount = subgraph2.vcount() new_ecount = 0 invalid_cnt = 0 new_edges_in_sg1 = list() new_edge_labels_in_sg1 = list() new_edge_keys_in_sg1 = list() while invalid_cnt < 10 and new_ecount < number_of_edges: v1 = np.random.randint(0, g1_vcount) v2 = np.random.randint(0, g2_vcount) edge_label = np.random.randint(0, graph_number_of_edge_labels) if get_direction(): x = (subgraph1.vs[v1]["label"], subgraph2.vs[v2]["label"]) y = (sg1, v1, sg2, v2) else: x = (subgraph2.vs[v2]["label"], subgraph1.vs[v1]["label"]) y = (sg2, v2, sg1, v1) if edge_label in self.pattern_nec_tree_vertex_edge_label_mapping[ x]: invalid_cnt += 1 continue graph_edge_labels = graph_edge_label_mapping[y] if edge_label in graph_edge_labels: invalid_cnt += 1 continue graph_edge_labels.add(edge_label) invalid_cnt = 0 new_ecount += 1 return new_ecount
def lambda_handler(event, context): """Takes in an event from Api.ai, through Api Gateway. Returns a dict with keys "speech", "displayText", and "Source". Source is always the "BART API" """ station_name = get_station_name(event) spoken_station_name = station_info(station_name)["spoken_name"] written_station_name = station_info(station_name)["written_name"] direction = get_direction(event)[0].lower() """Sets the parameters of the request to the bart api.""" param = {} param["cmd"] = "etd" param["orig"] = station_info(station_name)["abbr"] param["key"] = os.environ["BART_API_KEY"] param["dir"] = direction param["json"] = "y" response = requests.get(BART_URL, params = param) response_json = response.json() minutes = get_departure_info(response_json)["minutes"] minutes_next = get_departure_info(response_json)["minutes next"] destination = get_departure_info(response_json)["destination"] spoken_text = get_spoken_or_written_string(spoken_station_name, direction, minutes, destination, minutes_next, True) written_text = get_spoken_or_written_string(written_station_name, direction, minutes, destination, minutes_next, False) return {"speech" : spoken_text, "display API" : written_text, "source" : "BART API"}
def move_idle(self): if random.random() < 0.90: return if distance_between_points((self.x, self.y), (self.org_x, self.org_y)) < 2: self.move_delta(random.randint(-1, 1), random.randint(-1, 1)) else: direction = get_direction((self.x, self.y), (self.org_x, self.org_y)) self.move_delta(direction.x, direction.y)
def update(self): for ai in self.ai_comps: if ai is None: continue ent = entities[ai.entity] if ai.type_ == 'patrol': # move between point_a and point_b trans = ent.get_comp(TransformComponent) x, y = trans.x, trans.y if ai.current_point == ai.point_a: direction = get_direction((x, y), (ai.point_b.x, ai.point_b.y)) trans.x += direction.x trans.y += direction.y if Vector(trans.x, trans.y) == ai.point_b: ai.current_point = ai.point_b elif ai.current_point == ai.point_b: direction = get_direction((x, y), (ai.point_a.x, ai.point_a.y)) trans.x += direction.x trans.y += direction.y if Vector(trans.x, trans.y) == ai.point_a: ai.current_point = ai.point_a print(f'E{ai.entity}', trans.x, trans.y)
def goToPoint(self, robot_pos, point): pointAngle, robot_angle = self.get_angles(point, robot_pos) direction = utils.get_direction(pointAngle) if direction == 0: left_speed = -5 right_speed = -5 else: left_speed = -5 right_speed = 5 return left_speed, right_speed
def agent(obs_dict, config_dict): global prev_direction env = make('hungry_geese') # agent = QAgent(rows=11, columns=11, num_actions=3) agent = PPOAgent(rows=11, columns=11, num_actions=3) model_name = '' agent.load_model_weights('models/' + model_name + '.h5') state = preprocess_state(obs_dict, prev_direction) action = agent.select_action(state) direction = get_direction(prev_direction, action) prev_direction = direction return env.specification.action.enum[direction]
def generate(self): # random.seed(12) grid = [list([' '] * self.width) for _ in range(self.height)] max_width = 30 max_height = 10 boxes = [ Box(random.randint(0, WIDTH - max_width), random.randint(0, HEIGHT - max_height), random.randint(7, max_width), random.randint(4, max_height)) for _ in range(30) ] for box1 in list(boxes): for box2 in list(boxes): if box1 is not box2: if box1.intersects(box2): if box1 in boxes: # continue boxes.remove(box1) for box in boxes: box.apply_on(grid) for box1, box2 in zip(boxes, boxes[1:]): x, y = box1.center.x, box1.center.y while True: dx, dy = get_direction((x, y), (box2.center.x, box2.center.y)) if random.random() < 0.5: dx = 0 else: dy = 0 x, y = x + dx, y + dy grid[y][x] = '.' if box2.touches(x, y): break # border for y in range(1, HEIGHT - 1): for x in range(1, WIDTH - 1): if grid[y][x] == ' ': if '.' in [ grid[y - 1][x], grid[y + 1][x], grid[y][x - 1], grid[y][x + 1], grid[y - 1][x - 1], grid[y - 1][x + 1], grid[y + 1][x - 1], grid[y + 1][x + 1] ]: grid[y][x] = '#' self.zone = Zone(grid, [], [], self._temperature)
def form_snake(snake_locs): for _ in range(50): done = 0 for idx, (ent, snake_loc) in enumerate(zip(entities, snake_locs)): if idx == 0: ent.head = True direction = get_direction( (ent.x, ent.y), (center_x + snake_loc[0], center_y + snake_loc[1])) if direction == (0, 0): # NOTE add this kind of comparison done += 1 continue ent.x += direction.x ent.y += direction.y if done == len(entities): return
def moveTo(x,y): robot_angle_2= robot_pos['orientation'] angle = math.atan2( y - robot_pos['y'], x - robot_pos['x'], ) if angle < 0: angle = 2 * math.pi + angle if robot_angle_2 < 0: robot_angle_2 = 2 * math.pi + robot_angle_2 angle2 = math.degrees(angle + robot_angle_2) angle2 -= 90 if angle2 > 360: angle2 -= 360 d2 = utils.get_direction(angle2) return d2
def run(self): while self.robot.step(TIME_STEP) != -1: if self.is_new_data(): data = self.get_new_data() # noqa: F841 while self.is_new_team_data(): team_data = self.get_new_team_data() # noqa: F841 # Do something with team data if self.is_new_ball_data(): ball_data = self.get_new_ball_data() else: # If the robot does not see the ball, stop motors self.left_motor.setVelocity(0) self.right_motor.setVelocity(0) continue # Get data from compass heading = self.get_compass_heading() # noqa: F841 # Get GPS coordinates of the robot robot_pos = self.get_gps_coordinates() # noqa: F841 # Get data from sonars sonar_values = self.get_sonar_values() # noqa: F841 # Compute the speed for motors direction = utils.get_direction(ball_data["direction"]) # If the robot has the ball right in front of it, go forward, # rotate otherwise if direction == 0: left_speed = 5 right_speed = 5 else: left_speed = direction * 4 right_speed = direction * -4 # Set the speed to motors self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed) # Send message to team robots self.send_data_to_team(self.player_id)
def run(self): if self.name[0] == 'Y': t_m = 1 else: t_m = -1 frameCounter = 0 while self.robot.step(rcj_soccer_robot.TIME_STEP) != -1: if self.is_new_data(): data = self.get_new_data() # Get the position of our robot robot_pos = data[self.name] # Get the position of the ball ball_pos = data['ball'] # Get angle between the robot and the ball # and between the robot and the north ball_angle, robot_angle = self.get_angles(ball_pos, robot_pos) # Compute the speed for motors direction = utils.get_direction(ball_angle) # If the robot has the ball right in front of it, go forward, # rotate otherwise if direction == 0: left_speed = -10 right_speed = -10 elif direction == 2: left_speed = 10 right_speed = 10 else: left_speed = direction * 4 right_speed = direction * -4 # Set the speed to motors self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed) frameCounter += 1
def Move(self,current_map): if pygame.time.get_ticks() - self.movement_last_updated>=self.movement_time: self.movement_last_updated = pygame.time.get_ticks() self.move_t-=1 if self.level_difference !=0: self.jumping = 1 elif self.jumping !=0: self.jumping = 0 if self.move_t<0: if self.mov_vector==[]: self.moving = 0 self.level_difference = 0 self.jumping = 0 else: self.facing = utils.get_direction(self.pos,self.mov_vector[0]) self.level_difference = 0 if self.facing[0]=='s': [self.pos[0],self.pos[1]] = self.mov_vector[0] if self.level != utils.top_level(current_map,self.mov_vector[0]): self.level_difference = utils.top_level(current_map,self.mov_vector[0])-self.level self.level = utils.top_level(current_map,self.mov_vector[0]) self.mov_vector.remove(self.mov_vector[0]) self.move_t=20 elif self.update_position==1: [self.pos[0],self.pos[1]] = self.mov_vector[0] if self.level != utils.top_level(current_map,self.mov_vector[0]): self.level_difference = utils.top_level(current_map,self.mov_vector[0])-self.level self.level = utils.top_level(current_map,self.mov_vector[0]) self.mov_vector.remove(self.mov_vector[0]) self.offset = [0,0] self.update_position = 0 else: self.update_position = 1 self.move_t=20 self.level_difference = utils.top_level(current_map,self.mov_vector[0])-self.level #self.offset=[0,0] if self.level_difference==0: if self.facing == 'se': self.offset[0] = -self.move_t*2 self.offset[1] = -self.move_t*1 elif self.facing == 'sw': self.offset[0] = self.move_t*2 self.offset[1] = -self.move_t*1 elif self.facing== 'nw' and self.update_position==1: self.offset[0] = self.move_t*2-40 self.offset[1] = self.move_t*1-20 elif self.facing == 'ne' and self.update_position==1: self.offset[0] = -self.move_t*2+40 self.offset[1] = self.move_t*1-20 elif self.level_difference<0: if self.facing == 'se': self.offset[0] = -self.move_t*2 self.offset[1] = utils.parabola_down(self.level_difference,self.move_t) elif self.facing == 'sw': self.offset[0] = self.move_t*2 self.offset[1] = utils.parabola_down(self.level_difference,self.move_t) elif self.facing== 'nw' and self.update_position==1: self.offset[0] = self.move_t*2-40 self.offset[1] = utils.parabola_down_n(self.level_difference,self.move_t) elif self.facing == 'ne' and self.update_position==1: self.offset[0] = -self.move_t*2+40 self.offset[1] = utils.parabola_down_n(self.level_difference,self.move_t) elif self.level_difference>0: if self.facing == 'nw' and self.update_position==1: self.offset[0] = self.move_t*2-40 self.offset[1] = -20-utils.parabola_up(self.level_difference,self.move_t) if self.facing == 'ne' and self.update_position==1: self.offset[0] = -self.move_t*2+40 self.offset[1] = -20-utils.parabola_up(self.level_difference,self.move_t)
def run(self): while self.robot.step(TIME_STEP) != -1: if self.is_new_data(): data = self.get_new_data() # Get the position of our robot robot_pos = data[self.name] # Get the position of the ball ball_pos = data['ball'] # Get angle between the robot and the ball # and between the robot and the north ball_angle, robot_angle = self.get_angles(ball_pos, robot_pos) # Compute the speed for motors direction = utils.get_direction(ball_angle) # If the robot has the ball right in front of it, go forward, # rotate otherwise #if direction == 0: # left_speed = -5 # right_speed = -5 #else: # left_speed = direction * 4 # right_speed = direction * -4 real_robot_angle = robot_angle*57 robot_x = robot_pos["x"]*100 robot_y = robot_pos["y"]*100 ball_x = ball_pos["x"]*100 ball_y = ball_pos["y"]*100 global a if a == 0: if robot_x < 0: polovica = -1 else: polovica = 1 a = 1 def get_angles(pos1_x, pos1_y): if polovica == 1: pos2_x = 75 else: pos2_x = -75 pos2_y = 0 if pos1_y < 0: pos1_y = pos1_y * (-1) if pos1_x < 0: pos1_x = pos1_x * (-1) a = pos2_x - pos1_x b = pos2_y - pos1_y c = math.sqrt(a**2 + b**2) global alfa alfa = math.degrees(math.asin(a / c)) beta = math.degrees(math.asin(b / c)) gama = 90 return beta def novy_uhol(): if robot_y < 0: u = 90 + get_angles(robot_x, robot_y) else: u = 90 - get_angles(robot_x, robot_y) return u #def sanca(): # if ball_x < 1 and ball_x > -1 and ball_y < 1 and ball_y > -1: # if direction == 0: # vl = -8 # vr = -8 # else: # vl = direction * 8 # vr = direction * -8 def lokalizacia(): if ball_y < 15 and ball_y > -15: lokalita = "stred" else: if polovica == 1: if ball_x > 35: if ball_y < 0: lokalita = "vpravo" else: lokalita = "vlavo" else: lokalita = "stoj" else: if ball_x > 35: if ball_y < 0: lokalita = "vpravo" else: lokalita = "vlavo" else: lokalita = "stoj" return lokalita if len(robotX) == 2: robotX.pop(0) robotY.pop(0) robotX.append(int(robot_x)) robotY.append(int(robot_y)) global k k = 1 else: robotX.append(int(robot_x)) robotY.append(int(robot_y)) left_speed = 0 right_speed = 0 global uhol uhol = novy_uhol() def cakaj(): global e, f, g g = 1 if e == 0: global start_time, v start_time = time.time() e = 1 v = 0 else: if f == 1: if (time.time() - start_time) > 3: if polovica == 1: if robot_x < 78 and robot_x > 72: g = 0 e = 0 f = 0 v = 0 else: v = 6 else: if robot_x > -78 and robot_x < -72: g = 0 e = 0 f = 0 v = 0 else: v = 6 else: if (time.time() - start_time) > 5: if polovica == 1: if robot_x > 48: v = -6 else: if f == 0: start_time = time.time() v = 0 f = 1 else: if robot_x < -48: v = -6 else: if f == 0: start_time = time.time() v = 0 f = 1 else: v = 0 def domov(): global uhol, vl, vr if k == 1: if robotX[0] == robotX[1] and robotY[0] == robotY[1]: if real_robot_angle - 2 <= uhol and real_robot_angle + 2 >= uhol: vl = -7 vr = -7 else: rozdiel = real_robot_angle - uhol if rozdiel > 0: vl = -2 vr = 2 else: vl = 2 vr = -2 else: uhol = novy_uhol() if polovica == 1: if ball_x < 25: global g if robot_x > 0 and g == 1: cakaj() left_speed = v right_speed = v else: g = 0 if robot_x < 78 and robot_x > 72: if robot_y < 5 and robot_x > -5: if real_robot_angle < 275 and real_robot_angle > 265: cakaj() left_speed = v right_speed = v else: left_speed = -3 right_speed = 3 else: if g == 0: domov() left_speed = vl right_speed = vr else: if g == 0: domov() left_speed = vl right_speed = vr else: g = 0 if lokalizacia() == "stred": if direction == 0: left_speed = -8 right_speed = -8 else: left_speed = direction * 8 right_speed = direction * -8 else: if lokalizacia() == "vpravo": left_speed = -10 right_speed = -5 else: left_speed = -5 right_speed = -10 else: if ball_x > -25: if robot_x < 0 and g == 1: cakaj() left_speed = v right_speed = v else: g = 0 if robot_x > -78 and robot_x < -72: if robot_y < 5 and robot_x > -5: if real_robot_angle < 275 and real_robot_angle > 265: cakaj() left_speed = v right_speed = v else: left_speed = -3 right_speed = 3 else: if g == 0: domov() left_speed = vl right_speed = vr else: if g == 0: domov() left_speed = vl right_speed = vr else: g = 0 if lokalizacia() == "stred": if direction == 0: left_speed = -8 right_speed = -8 else: left_speed = direction * 8 right_speed = direction * -8 else: if lokalizacia() == "stoj": left_speed = 0 right_speed = 0 else: if lokalizacia() == "vpravo": left_speed = -10 right_speed = -5 else: left_speed = -5 right_speed = -10 # Set the speed to motors self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed)
def move_toward_object(self, o): dx, dy = get_direction((self.x, self.y), (o.x, o.y)) self.move_delta(dx, dy)
def run(self): frame = 0 center = True facing = False while self.robot.step(rcj_soccer_robot.TIME_STEP) != -1: if self.is_new_data(): frame += 1 #print("hi") data = self.get_new_data() # Get the position of our robot robot_pos = data[self.name] #print(robot_pos) # Get the position of the ball ball_pos = data['ball'] # go to center # togo = [0.375,0] # v1 = robot_pos['x']-togo[0] # v2 = robot_pos['y']-togo[1] # # if robot_pos['x'] < 0.38 and robot_pos['x'] > -0.37 and robot_pos['y'] < 0.06 and robot_pos['y'] > 0.04: # center = True # else: # center = False # # if frame % 60 == 0: # print(center) # # theta=math.radians(math.atan(float(v2/v1))) # # if center == True: # print("hi") # if facing == True: # left_speed=-5 # rights_speed=-5 # # 0.174 radians is 10 degrees # left_speed=-5 # right_speed=-3 # if abs(robot_pos['orientation']-theta) <= 0.174: # facing=True # # if (robot_pos['x']-togo[0]) <= 0.05: # if robot_pos['y'] <= 0+0.025 and robot_pos['y'] >= 0-0.025: # left_speed=-5 # right_speed=5 # if robot_pos['orientation']==0: # left_speed=0 # right_speed=0 # center = False # If robot isn't centered on x, # if center == False: # if frame % 2 == 0: # left_speed=-0.1 # right_speed=-0.1 # else: # left_speed=0.1 # right_speed=0.1 # # if ball_pos['x'] == 0 and ball_pos['y'] == 0: # left_speed = -10 # right_speed = -10 # Get angle between the robot and the ball # and between the robot and the north ball_angle, robot_angle = self.get_angles(ball_pos, robot_pos) # Compute the speed for motors direction = utils.get_direction(ball_angle) # If the robot has the ball right in front of it, go forward, # rotate otherwise if direction == 0: left_speed = -10 right_speed = -10 else: left_speed = direction * 4 right_speed = direction * -4 # Set the speed to motors self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed) frame += 1
def evanMethod(self): moving = True shooting = False ball_moving = False ball_pos_last = [0,0] waiting_for_ball = False team = -1 while self.robot.step(rcj_soccer_robot.TIME_STEP) != -1: if self.name[0] == 'B': team = 1 if self.is_new_data(): data = self.get_new_data() # Get the position of our robot robot_pos = data[self.name] # Get the position of the ball ball_pos = data['ball'] if not (ball_pos['x'] == ball_pos_last[0]) and not (ball_pos['y']==ball_pos_last[0]): ball_moving = True ball_change_x = ball_pos['x'] - ball_pos_last[0] ball_change_y = ball_pos['y'] - ball_pos_last[1] # print(ball_change_x, ball_change_y) robot_angle_2= robot_pos['orientation'] # Get angle between the robot and the ball # and between the robot and the north ball_angle, robot_angle = self.get_angles(ball_pos, robot_pos) # Compute the speed for motors direction = utils.get_direction(ball_angle) # If the robot has the ball right in front of it, go forward, # rotate otherwise rx = robot_pos['x'] ry = robot_pos['y'] bx = ball_pos['x'] by = ball_pos['y'] if team == 1: ball_x_dist_from_goal = abs(bx+0.75) else: ball_x_dist_from_goal = bx-0.75 ball_y_dist_from_goal = by if team == 1: if (rx-0.05<bx): moving = True shooting = False elif (rx+0.05>bx): moving = True shooting = False # print("byd"+str(ball_y_dist_from_goal)) # shotx = bx + 0.15/math.sqrt(ball_x_dist_from_goal**2+ball_y_dist_from_goal**2)*ball_x_dist_from_goal + 20*ball_change_x shotx = bx + 0.2/math.sqrt(ball_x_dist_from_goal**2+ball_y_dist_from_goal**2)*ball_x_dist_from_goal + 10*ball_change_x shoty = by + 0.2/math.sqrt(ball_x_dist_from_goal**2+ball_y_dist_from_goal**2)*ball_y_dist_from_goal + 23*ball_change_y if shoty > 0.65: shoty = shoty = by + 0.2/math.sqrt(ball_x_dist_from_goal**2+ball_y_dist_from_goal**2)*ball_y_dist_from_goal+ 23*ball_change_y if shoty > 0.65: shoty = 0.65 if shoty < -0.65: shoty = -0.65 if shotx > 0.75: shotx = 0.75 if shotx < -0.75: shotx = -0.75 # print(ball_x_dist_from_goal) # print(shotx, shoty) #if not on wall or not behind robot and compensate for movement, improve shooting mechanism (focusing on center of goal instead of ball) xtarget = shotx ytarget = shoty ball_pos = data['ball'] xb = ball_pos['x'] yb = ball_pos['y'] b1 = data[self.name] bX = b1['x'] bY = b1['y'] bdist = math.sqrt((bX-xb)**2+(bY-yb)**2) # if bdist < 0.1: # xtarget = 0.2 # ytarget = 0 # TO KEEP THE ATTACKER ON OFFENSE # if (xtarget > 0.2 and bdist > 0.1) or xtarget > 0.4: # xtarget = 0.12 # ytarget = 0 # def moveTo(x,y): robot_angle_2= robot_pos['orientation'] angle = math.atan2( y - robot_pos['y'], x - robot_pos['x'], ) if angle < 0: angle = 2 * math.pi + angle if robot_angle_2 < 0: robot_angle_2 = 2 * math.pi + robot_angle_2 angle2 = math.degrees(angle + robot_angle_2) angle2 -= 90 if angle2 > 360: angle2 -= 360 d2 = utils.get_direction(angle2) return d2 if moving: sp = moveTo(xtarget,ytarget) if abs(xtarget-rx) < 0.02 and abs(ytarget-ry) < 0.02: moving = False # print("DONE") shooting = True direction = sp if shooting: if team == 1: angle = math.atan2( -robot_pos['y'], -0.75 - robot_pos['x'], ) else: angle = math.atan2( -robot_pos['y'], 0.75 - robot_pos['x'], ) if angle < 0: angle = 2 * math.pi + angle if robot_angle_2 < 0: robot_angle_2 = 2 * math.pi + robot_angle_2 angle2 = math.degrees(angle + robot_angle_2) angle2 -= 90 if angle2 > 360: angle2 -= 360 direction = utils.get_direction(angle2) if direction == 0: left_speed = -10 right_speed = -10 else: right_speed = direction * -10 left_speed = direction * 10 self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed) ball_pos_last = [ball_pos['x'],ball_pos['y']]
def update(self): # if in launch point if self.wait: return print(self.eta_left, self.arrived_pids) if self._arrived() and self.pid_next != self.arrived_pids[-1]: if 'F' in g_graph.i2c[self.pid_next]: self.wait = True return self.arrived_pids.append(self.pid_next) for p in np.random.permutation(g_graph.after[self.pid_next]): if 'F' in g_graph.i2c[p] and not g_graph.ants_in_F[p] and \ g_graph.ants_on_normal_road[self.pid_prev, p].qsize() < 1: self.pid_prev = self.pid_next self.pid_next = p self.pos = g_graph.nodes_position[self.pid_prev] self.eta = g_graph.nodes_distance[ self.pid_prev, self.pid_next] / self._speed('normal') self.eta_left = self.eta self.direction = utils.get_direction( g_graph, self.pid_prev, self.pid_next) g_graph.ants_in_F[p] = True g_graph.ants_on_normal_road[self.pid_prev, self.pid_next].append(self) break # print(globals.g_graph.i2c[self.pid_next], globals.g_graph.i2c[p]) if g_graph.flags_main_road[self.pid_prev, self.pid_next]: g_graph.ants_on_main_road[self.pid_prev, self.pid_next, self.previous_side].remove(self) anti_ants0 = g_graph.ants_on_main_road[self.pid_next, self.pid_prev, 0] anti_ants1 = g_graph.ants_on_main_road[self.pid_next, self.pid_prev, 1] # if both ways are blocked, wait if anti_ants0.qsize() > 0 and anti_ants1.qsize() > 0: continue # if either is clear self.pid_prev = self.pid_next self.pid_next = p self.pos = g_graph.nodes_position[self.pid_prev] self.eta = g_graph.nodes_distance[ self.pid_prev, self.pid_next] / self._speed('main') self.eta_left = self.eta self.direction = utils.get_direction( g_graph, self.pid_prev, self.pid_next) if anti_ants0.qsize() > 0: self.previous_side = 0 g_graph.ants_on_main_road[self.pid_prev, self.pid_next, 1].append(self) else: self.previous_side = 1 g_graph.ants_on_main_road[self.pid_prev, self.pid_next, 0].append(self) break else: g_graph.ants_on_normal_road[self.pid_prev, self.pid_next].remove(self) anti_ants = g_graph.ants_on_normal_road[self.pid_next, self.pid_prev] if anti_ants.qsize() > 0: continue else: self.pid_prev = self.pid_next self.pid_next = p self.pos = g_graph.nodes_position[self.pid_prev] self.eta = g_graph.nodes_distance[ self.pid_prev, self.pid_next] / self._speed('normal') self.eta_left = self.eta self.direction = utils.get_direction( g_graph, self.pid_prev, self.pid_next) g_graph.ants_on_normal_road[self.pid_prev, self.pid_next].append(self) break if self.direction is not None: interp = self.eta_left / self.eta pos_next = g_graph.nodes_position[self.pid_next] pos = self.pos[0] * interp + (1 - interp) * pos_next[0], self.pos[ 1] * interp + (1 - interp) * pos_next[1] self.rect.center = utils.lb2lt(pos) self.eta_left -= 0.01 print(self.eta_left)
def _proc_broadside_move(self, move): """Process an broadside move, e.g. I5-I7-H5""" line_start, line_end, end = move.split('-') line_start_row, line_start_col = utils.parse_position(line_start) line_end_row, line_end_col = utils.parse_position(line_end) end_row, end_col = utils.parse_position(end) ######### # Check 1 - Is the line actually a line and <= 3 in length? line_direction, line_distance = utils.get_direction(line_start_row, line_start_col, line_end_row, line_end_col) if line_distance > 2: raise ValueError("Invalid move - can only move 1, 2, or 3 marbles broadside") if line_distance == 2: # Direction: # NE = 1, E = 2, SE = 3 # SW = 4, W = 5, NW = 6 # Col changes in all cases except for SE and NW if line_direction not in (3, 6): line_mid_col = max(line_start_col, line_end_col) - 1 else: line_mid_col = line_start_col # Row changes in all cases except for E and W if line_direction not in (2, 5): line_mid_row = max(line_start_row, line_end_row) - 1 else: line_mid_row = line_start_row if line_distance == 0: # This is the same as an inline push of 1 marble inline_result, inline_was_pushed = self._proc_inline_move(move[3:8]) return inline_result marble = self.turn ######### # Check 2 - Is there a matching marble in the line start position? try: if self._get_marble(line_start_row, line_start_col) != marble: raise ValueError("Invalid move - line start marble is not yours!") ####### # Check 3 - If we're here, the line start position is not on the board. except IndexError: raise ValueError("Invalid move - line start position is off the board") ######### # Check 4 - Is there a matching marble in the line end position? try: if self._get_marble(line_end_row, line_end_col) != marble: raise ValueError("Invalid move - line end marble is not yours!") ####### # Check 5 - If we're here, the line end position is not on the board. except IndexError: raise ValueError("Invalid move - line end position is off the board") ######### # Check 6 - If the line is of length 3, is there a matching marble in the middle position? try: if line_distance == 2 and self._get_marble(line_mid_row, line_mid_col) != marble: raise ValueError("Invalid move - middle marble is not yours!") ####### # Check 7 - If we're here, the middle position is not on the board... defying the laws of physics, somehow except IndexError: raise ValueError("Invalid move - middle marble position is off the board") move_direction, move_distance = utils.get_direction(line_start_row, line_start_col, end_row, end_col) if move_distance != 1: raise ValueError("Invalid move - can only move a distance of 1") ###### # Check 8 - Is the end position of the first marble empty? try: if self._get_marble(end_row, end_col) != empty: raise ValueError("Invalid move - end position of the first marble is not empty") ###### # Check 9 - If we're here, the end position of the first marble is not on the board. except IndexError: raise ValueError("Invalid move - end position of the first marble is off the board") ###### # Check 10 - Is the end position of the last marble empty? try: if self._get_marble(line_end_row, line_end_col, move_direction, 1) != empty: raise ValueError("Invalid move - end position of the last marble is not empty") ###### # Check 11 - If we're here, the end position of the last marble is not on the board. except IndexError: raise ValueError("Invalid move - end position of the last marble is off the board") if line_distance == 2: ###### # Check 14 - Is the end position of the middle marble empty? try: if self._get_marble(line_mid_row, line_mid_col, move_direction, 1) != empty: raise ValueError("Invalid move - end position of the middle marble is not empty") ###### # Check 15 - If we're here, the end position of the middle marble is not on the board - somehow. except IndexError: raise ValueError("Invalid move - end position of the middle marble is off the board") board = self._set_marble(line_start_row, line_start_col, empty) board = self._set_marble(end_row, end_col, marble, board) board = self._set_marble(line_end_row, line_end_col, empty, board) r, c = utils.calc_position_at_distance(line_end_row, line_end_col, move_direction, 1) board = self._set_marble(r, c, marble, board) if line_distance == 2: board = self._set_marble(line_mid_row, line_mid_col, empty, board) r, c = utils.calc_position_at_distance(line_mid_row, line_mid_col, move_direction, 1) board = self._set_marble(r, c, marble, board) return board
def generate(self): grid = [list(['#'] * self.width) for _ in range(self.height)] grids = [] # random.seed(2) c = Vector(self.width * 2 // 4, self.height + 3) size = 2 for idx, door in enumerate([ Vector(c.x - size, c.y - size - 5), Vector(c.x + size, c.y - size - 5), Vector(c.x + 0, c.y - size // 2 - 5), Vector(c.x - size, c.y - size // 2 - 5), ]): grid = [list(['#'] * self.width) for _ in range(self.height)] holes = [door] for y in range(self.height): for x in range(self.width): if distance_between_points((c.x, c.y), (x, y)) <= size: grid[y][x] = '.' elif random.random() < 0.2: grid[y][x] = '.' holes.append(Vector(x, y)) if idx % 2 == 0: holes = sorted(holes, key=lambda h: (distance_between_points( (h.x, h.y), (door.x, door.y)), h.y, 0)) else: holes = sorted(holes, key=lambda h: (distance_between_points( (h.x, h.y), (door.x, door.y)), h.y, -h.x)) new_holes = [holes[0]] for hole in holes[1:]: if hole.y < new_holes[-1].y: new_holes.append(hole) holes = new_holes grid[door.y][door.x] = '+' for hole in holes: x, y = hole grid[y][x] = '+' path = [] for prev, curr in zip(holes, holes[1:]): x, y = prev while True: dx, dy = get_direction((x, y), (curr.x, curr.y)) if grid[y + dy][x + dx] == '+': break if dx != 0 and dy != 0: # diagonal if random.random() < 0.5: dx = 0 else: dy = 0 grid[y + dy][x + dx] = '.' path.append(Vector(x + dx, y + dy)) x += dx y += dy for y in range(self.height): for x in range(self.width): if grid[y][x] == '.': if Vector(x, y) not in path: if (x > c.x + size or x < c.x - size) or (y > c.y + size or y < c.y - size): # not in room grid[y][x] = '#' elif grid[y][x] == '+': grid[y][x] = '.' grids.append(grid) for y in range(self.height): for x in range(self.width): if '.' in [g[y][x] for g in grids]: grid[y][x] = '.' self.zone = Zone(grid, [], [], self._temperature)
def _proc_inline_move(self, move): """Process an inline move, e.g. I5-H5. Returns a tuple of: the new board position, T if a black marble was removed, T if a white marble was removed""" ######### # Check 1 - Are we moving a distance of 1? start, end = move.split('-') start_row, start_col = utils.parse_position(start) end_row, end_col = utils.parse_position(end) direction, distance = utils.get_direction(start_row, start_col, end_row, end_col) #print "Direction is", direction if distance != 1: raise ValueError("Invalid move - can only move a distance of 1") marble = self.turn ######### # Check 2 - Is there a matching marble in the starting position? try: if self._get_marble(start_row, start_col) != marble: raise ValueError("Invalid move - start marble is not yours!") ####### # Check 3 - If we're here, the starting position is not on the board. except IndexError: raise ValueError("Invalid move - start position is off the board") ######### # Check 4 - The end position can't have a marble of the opposite color. try: if self._get_marble(end_row, end_col) == utils.get_opposite_marble(marble): # Clearly illegal. Is this a pac, though? if self._get_marble(start_row, start_col, direction, 2, True) == empty: raise ValueError("Pac - 1 v 1") else: raise ValueError("Invalid move - don't have superior strength to push") ####### # Check 5 - If we're here, the end position is not on the board. except IndexError: raise ValueError("Invalid move - end position is off the board") # For documentation, we'll assume our marble is white if self._get_marble(start_row, start_col, direction, 1) == empty: # We're pushing one white into an empty space - Legal move board = self._set_marble(start_row, start_col, empty) r, c = utils.calc_position_at_distance(start_row, start_col, direction, 1) board = self._set_marble(r, c, marble, board) return board, False else: # Two white marbles in a row. We dealt with black in check 4. if self._get_marble(start_row, start_col, direction, 2) == empty: # We're pushing two whites into an empty space - Legal move board = self._set_marble(start_row, start_col, empty) r, c = utils.calc_position_at_distance(start_row, start_col, direction, 2) board = self._set_marble(r, c, marble, board) return board, False elif self._get_marble(start_row, start_col, direction, 2) == utils.get_opposite_marble(marble): # Two whites against one black. What's in the next space? if self._get_marble(start_row, start_col, direction, 3, True) == empty: # Two whites pushing one black into an empty space - Legal move board = self._set_marble(start_row, start_col, empty) r, c = utils.calc_position_at_distance(start_row, start_col, direction, 2) board = self._set_marble(r, c, marble, board) try: r, c = utils.calc_position_at_distance(start_row, start_col, direction, 3) board = self._set_marble(r, c, utils.get_opposite_marble(marble), board) except IndexError: # We just pushed a black off the edge return board, True return board, False elif self._get_marble(start_row, start_col, direction, 3) == utils.get_opposite_marble(marble): # Two whites against two blacks. Can't do this, but is this a pac or just an invalid move? if self._get_marble(start_row, start_col, direction, 4) == empty: # We're pushing two whites against two blacks followed by an empty space raise ValueError("Pac - 2 v 2") else: # We're pushing two whites against two blacks and some other stuff raise ValueError("Invalid move - blocked by other marbles behind it") else: # Two whites, one black, one white raise ValueError("Invalid move - blocked by one of your marbles behind it") else: # Three white marbles in a row. if self._get_marble(start_row, start_col, direction, 3) == empty: # We're pushing three whites into an empty space - Legal move board = self._set_marble(start_row, start_col, empty) r, c = utils.calc_position_at_distance(start_row, start_col, direction, 3) board = self._set_marble(r, c, marble, board) return board, False elif self._get_marble(start_row, start_col, direction, 3) == utils.get_opposite_marble(marble): # Three whites against one black. What's in the next space? if self._get_marble(start_row, start_col, direction, 4, True) == empty: # Three whites pushing one black into an empty space - Legal move board = self._set_marble(start_row, start_col, empty) r, c = utils.calc_position_at_distance(start_row, start_col, direction, 3) board = self._set_marble(r, c, marble, board) try: r, c = utils.calc_position_at_distance(start_row, start_col, direction, 4) board = self._set_marble(r, c, utils.get_opposite_marble(marble), board) except IndexError: # We just pushed a black off the edge return board, True return board, False elif self._get_marble(start_row, start_col, direction, 4) == utils.get_opposite_marble(marble): # Three whites against two blacks. What's in the next space? if self._get_marble(start_row, start_col, direction, 5, True) == empty: # Three whites pushing two blacks into an empty space - Legal move board = self._set_marble(start_row, start_col, empty) r, c = utils.calc_position_at_distance(start_row, start_col, direction, 3) board = self._set_marble(r, c, marble, board) try: r, c = utils.calc_position_at_distance(start_row, start_col, direction, 5) board = self._set_marble(r, c, utils.get_opposite_marble(marble), board) except IndexError: # We just pushed a black off the edge return board, True return board, False elif self._get_marble(start_row, start_col, direction, 5) == utils.get_opposite_marble(marble): # Three whites against three blacks. Can't do this, but is this a pac or just an invalid move? if self._get_marble(start_row, start_col, direction, 6) == empty: # We're pushing three whites against three blacks followed by an empty space raise ValueError("Pac - 3 v 3") else: # We're pushing three whites against three blacks and some other stuff raise ValueError("Invalid move - blocked by other marbles behind it") else: # Three whites, two blacks, white raise ValueError("Invalid move - blocked by your marble behind it") else: # Three whites, one black, white raise ValueError("Invalid move - blocked by your marble behind it") else: # Four whites raise ValueError("Invalid move - can't push 4 marbles")
def ddqn_train(model_name, load_model=False, model_filename=None, optimizer_filename=None): print("DDQN -- Training") env = make('hungry_geese') trainer = env.train( ['greedy', None, 'agents/boilergoose.py', 'agents/handy_rl.py']) agent = DDQNAgent(rows=11, columns=11, num_actions=3) buffer = ReplayBuffer() strategy = EpsilonGreedyStrategy(start=0.5, end=0.0, decay=0.00001) if load_model: agent.load_model_weights(model_filename) agent.load_optimizer_weights(optimizer_filename) start_episode = 0 end_episode = 50000 epochs = 32 batch_size = 128 training_rewards = [] evaluation_rewards = [] last_1000_ep_reward = [] for episode in range(start_episode + 1, end_episode + 1): obs_dict = trainer.reset() epsilon = strategy.get_epsilon(episode - start_episode) ep_reward, ep_steps, done = 0, 0, False prev_direction = 0 while not done: ep_steps += 1 state = preprocess_state(obs_dict, prev_direction) action = agent.select_epsilon_greedy_action(state, epsilon) direction = get_direction(prev_direction, action) next_obs_dict, _, done, _ = trainer.step( env.specification.action.enum[direction]) reward = calculate_reward(obs_dict, next_obs_dict) next_state = preprocess_state(next_obs_dict, direction) buffer.add(state, action, reward, next_state, done) obs_dict = next_obs_dict prev_direction = direction ep_reward += reward if len(buffer) >= batch_size: for _ in range(epochs): states, actions, rewards, next_states, dones = buffer.get_samples( batch_size) agent.fit(states, actions, rewards, next_states, dones) print("EPISODE " + str(episode) + " - REWARD: " + str(ep_reward) + " - STEPS: " + str(ep_steps)) if len(last_1000_ep_reward) == 1000: last_1000_ep_reward = last_1000_ep_reward[1:] last_1000_ep_reward.append(ep_reward) if episode % 10 == 0: agent.update_target_network() if episode % 1000 == 0: print('Episode ' + str(episode) + '/' + str(end_episode)) print('Epsilon: ' + str(round(epsilon, 3))) last_1000_ep_reward_mean = np.mean(last_1000_ep_reward).round(3) training_rewards.append(last_1000_ep_reward_mean) print('Average reward in last 1000 episodes: ' + str(last_1000_ep_reward_mean)) print() if episode % 1000 == 0: eval_reward = 0 for i in range(100): obs_dict = trainer.reset() epsilon = 0 done = False prev_direction = 0 while not done: state = preprocess_state(obs_dict, prev_direction) action = agent.select_epsilon_greedy_action(state, epsilon) direction = get_direction(prev_direction, action) next_obs_dict, _, done, _ = trainer.step( env.specification.action.enum[direction]) reward = calculate_reward(obs_dict, next_obs_dict) obs_dict = next_obs_dict prev_direction = direction eval_reward += reward eval_reward /= 100 evaluation_rewards.append(eval_reward) print("Evaluation reward: " + str(eval_reward)) print() if episode % 5000 == 0: agent.save_model_weights('models/ddqn_' + model_name + '_' + str(episode) + '.h5') agent.save_optimizer_weights('models/ddqn_' + model_name + '_' + str(episode) + '_optimizer.npy') agent.save_model_weights('models/ddqn_' + model_name + '_' + str(end_episode) + '.h5') agent.save_optimizer_weights('models/ddqn_' + model_name + '_' + str(end_episode) + '_optimizer.npy') plt.plot([i for i in range(start_episode + 1000, end_episode + 1, 1000)], training_rewards) plt.title('Reward') plt.show() plt.plot([i for i in range(start_episode + 1000, end_episode + 1, 1000)], evaluation_rewards) plt.title('Evaluation rewards') plt.show()
P.itemset((i, i), 1) if observation.type_ == 'distance': P.itemset((i, i), 15625 * 4) i = i + 1 observation_number = 1 n = 0 for set_up_point_name in set_up_points: for observation in observations: if observation.from_point.name == set_up_point_name: row = [0, 0] + [0] * number_of_set_ups observed = None calculated = None if observation.type_ == 'direction': observed = observation.value calculated = get_direction(observation.from_point, observation.to_point) if observation.to_point.type_ == 'provisional': d = get_distance(observation.to_point, observation.from_point) y = 206264.8 * (observation.to_point.x - observation.from_point.x) / d**2 x = -206264.8 * (observation.to_point.y - observation.from_point.y) / d**2 row[0], row[1] = y, x row[1 + observation_number] = -1 A = numpy.vstack([A, row]) if observation.to_point.type_ == 'fixed': n = n + 1 row[1 + observation_number] = -1 A = numpy.vstack([A, row]) oc = (math.degrees(observed-calculated)*3600) l = numpy.vstack([l, oc]) observation_number = observation_number + 1
def move(): data = {} time_remaining = [150] # leave 50ms for network position = None path = None next_move = list() thread_pool = list() potential_snake_positions = list() direction = None with timing("bottle", time_remaining): data = bottle.request.json try: with timing("data parsing", time_remaining): board = Board(**data) snake = board.get_snake(data['you']) direction = general_direction(board, snake.head, snake.attributes['health_points']) move = direction # fallback for enemy_snake in board.snakes: if enemy_snake.attributes['id'] != snake.attributes[ 'id']: # and enemy_snake.attributes['health_points'] >= snake.attributes['health_points']: potential_snake_positions.extend([ position for position in enemy_snake.potential_positions() if board.inside(position) ]) number_of_squares = list() # find number of empty squares in every direction. for cell in neighbours(snake.head): if board.inside(cell): count = len(flood_fill(board, cell, False)) number_of_squares.append((cell, count)) if count <= 10: potential_snake_positions.append(cell) if number_of_squares[0][1] <= 10 and number_of_squares[1][ 1] <= 10 and number_of_squares[2][ 1] <= 10 and number_of_squares[3][1] <= 10: largest = reduce( lambda carry, direction: carry if carry[1] > direction[1] else direction, number_of_squares, number_of_squares[0]) potential_snake_positions.remove(largest[0]) print potential_snake_positions with timing("need_food", time_remaining): food = need_food(board, snake.head, snake.attributes['health_points']) if food: #if snake.attributes['health_points'] < 30: #potential_snake_positions = [] with timing("find_food", time_remaining): food_positions = find_food(snake.head, snake.attributes['health_points'], board, food) positions = [position[0] for position in food_positions] # positions = list(set([ position[0] for position in food_positions ]) - set(potential_snake_positions)) print positions print[board.get_cell(position) for position in positions] for position in positions: t = Thread( target=bfs(snake.head, position, board, potential_snake_positions, next_move)) t = Thread( target=bfs(snake.head, position, board, [], next_move)) thread_pool.append(t) for thread in thread_pool: thread.start() thread.join() next_move = filter(lambda path: not len(path) == 0, next_move) path = min(next_move, key=len) move = get_direction(snake.head, path[0]) else: #with timing("flood_fill", time_remaining): # flood_fill(board.vacant, snake.head, True) with timing("find_safest_position", time_remaining): positions = find_safest_position(snake.head, direction, board) positions = [position[0] for position in positions] # positions = list(set([position[0] for position in positions]) - set(potential_snake_positions)) print positions print[board.get_cell(position) for position in positions] for position in positions: t = Thread( target=bfs(snake.head, position, board, potential_snake_positions, next_move)) t = Thread( target=bfs(snake.head, position, board, [], next_move)) thread_pool.append(t) for thread in thread_pool: thread.start() thread.join() path = max(next_move, key=len) move = get_direction(snake.head, path[0]) except Exception as e: print "WTF", e.message print next_move print path print move if len(next_move) == 0: print "CHANGING MOVE" with timing("floodfill", time_remaining): floods = { "up": len(flood_fill(board, (snake.head[0], snake.head[1] - 1))), "down": len(flood_fill(board, (snake.head[0], snake.head[1] + 1))), "right": len(flood_fill(board, (snake.head[0] + 1, snake.head[1]))), "left": len(flood_fill(board, (snake.head[0] - 1, snake.head[1]))) } move = max(floods.iterkeys(), key=(lambda key: floods[key])) # don't be stupid m_move = add(snake.head, DIR_VECTORS[DIR_NAMES.index(move)]) if board.inside(m_move) and board.get_cell(m_move) == 1: print "CHANGING MOVE" for direction in DIR_NAMES: m_move = add(snake.head, DIR_VECTORS[DIR_NAMES.index(direction)]) if board.inside(m_move) and board.get_cell(m_move) != 1: move = direction print "moving", move return {'move': move, 'taunt': random.choice(TAUNTS)}
def ppo_train(model_name, load_model=False, actor_filename=None, critic_filename=None, optimizer_filename=None): print("PPO -- Training") env = make('hungry_geese') trainer = env.train(['greedy', None, 'agents/boilergoose.py', 'agents/handy_rl.py']) agent = PPOAgent(rows=11, columns=11, num_actions=3) memory = Memory() if load_model: agent.load_model_weights(actor_filename, critic_filename) agent.load_optimizer_weights(optimizer_filename) episode = 0 start_episode = 0 end_episode = 50000 reward_threshold = None threshold_reached = False epochs = 4 batch_size = 128 current_frame = 0 training_rewards = [] evaluation_rewards = [] last_1000_ep_reward = [] for episode in range(start_episode + 1, end_episode + 1): obs_dict = trainer.reset() ep_reward, ep_steps, done = 0, 0, False prev_direction = 0 while not done: current_frame += 1 ep_steps += 1 state = preprocess_state(obs_dict, prev_direction) action = agent.select_action(state, training=True) direction = get_direction(prev_direction, action) next_obs_dict, _, done, _ = trainer.step(env.specification.action.enum[direction]) reward = calculate_reward(obs_dict, next_obs_dict) next_state = preprocess_state(next_obs_dict, direction) memory.add(state, action, reward, next_state, float(done)) obs_dict = next_obs_dict prev_direction = direction ep_reward += reward if current_frame % batch_size == 0: for _ in range(epochs): states, actions, rewards, next_states, dones = memory.get_all_samples() agent.fit(states, actions, rewards, next_states, dones) memory.clear() agent.update_networks() print("EPISODE " + str(episode) + " - REWARD: " + str(ep_reward) + " - STEPS: " + str(ep_steps)) if len(last_1000_ep_reward) == 1000: last_1000_ep_reward = last_1000_ep_reward[1:] last_1000_ep_reward.append(ep_reward) if reward_threshold: if len(last_1000_ep_reward) == 1000: if np.mean(last_1000_ep_reward) >= reward_threshold: print("You solved the task after" + str(episode) + "episodes") agent.save_model_weights('models/ppo_actor_' + model_name + '_' + str(episode) + '.h5', 'models/ppo_critic_' + model_name + '_' + str(episode) + '.h5') threshold_reached = True break if episode % 1000 == 0: print('Episode ' + str(episode) + '/' + str(end_episode)) last_1000_ep_reward_mean = np.mean(last_1000_ep_reward).round(3) training_rewards.append(last_1000_ep_reward_mean) print('Average reward in last 1000 episodes: ' + str(last_1000_ep_reward_mean)) print() if episode % 1000 == 0: eval_reward = 0 for i in range(100): obs_dict = trainer.reset() done = False prev_direction = 0 while not done: state = preprocess_state(obs_dict, prev_direction) action = agent.select_action(state) direction = get_direction(prev_direction, action) next_obs_dict, _, done, _ = trainer.step(env.specification.action.enum[direction]) reward = calculate_reward(obs_dict, next_obs_dict) obs_dict = next_obs_dict prev_direction = direction eval_reward += reward eval_reward /= 100 evaluation_rewards.append(eval_reward) print("Evaluation reward: " + str(eval_reward)) print() if episode % 5000 == 0: agent.save_model_weights('models/ppo_actor_' + model_name + '_' + str(episode) + '.h5', 'models/ppo_critic_' + model_name + '_' + str(episode) + '.h5') agent.save_optimizer_weights('models/ppo_' + model_name + '_' + str(episode) + '_optimizer.npy') agent.save_model_weights('models/ppo_actor_' + model_name + '_' + str(end_episode) + '.h5', 'models/ppo_critic_' + model_name + '_' + str(end_episode) + '.h5') agent.save_optimizer_weights('models/ppo_' + model_name + '_' + str(end_episode) + '_optimizer.npy') if threshold_reached: plt.plot([i for i in range(start_episode + 1000, episode, 1000)], training_rewards) else: plt.plot([i for i in range(start_episode + 1000, end_episode + 1, 1000)], training_rewards) plt.title("Reward") plt.show() plt.plot([i for i in range(start_episode + 1000, end_episode + 1, 1000)], evaluation_rewards) plt.title('Evaluation rewards') plt.show()
def run(self): ATTACK = False BLOCK = False SPOTONE = True xbOLD = 0 ybOLD = 0 ball_pos_last = [0, 0] #worth it to take the penalty spotY = 0.1 while self.robot.step(rcj_soccer_robot.TIME_STEP) != -1: if self.is_new_data(): data = self.get_new_data() if self.name[0] == 'B': team = 1 else: team = -1 if team == 1: spotX = 0.3 else: spotX = -0.3 # Get the position of our robot robot_pos = data[self.name] orientation = robot_pos['orientation'] xr = robot_pos['x'] yr = robot_pos['y'] # Get the position of the ball ball_pos = data['ball'] xb = ball_pos['x'] yb = ball_pos['y'] b2 = data['B2'] b3 = data['B3'] if abs(xr - spotX) <= 0.05 and abs(yr - spotY) <= 0.05: SPOTONE = False if abs(xr - spotX) <= 0.05 and abs(yr + spotY) <= 0.05: SPOTONE = True #universal if abs(xb - 0) < 0.1 and abs(yb - 0) < 0.1: ATTACK = True if team == -1: if (xb - xbOLD) < 0 and xb < -0.1: BLOCK = True else: BLOCK = False else: if (xb - xbOLD) > 0 and xb > 0.1: BLOCK = True else: BLOCK = False # GOALIE DECLUMP # if BLOCK and utils.decideWho(robot_pos,b2,ball_pos) != "you": # BLOCK = False if ATTACK: self.evanMethod2(ball_pos_last) ATTACK = False elif BLOCK: self.evanMethod(ball_pos_last) elif SPOTONE: robotPointAngle, robot_angle = utils.getPointAngle( orientation, xr, yr, spotX, spotY) direction = utils.get_direction(robotPointAngle) # If the robot has the ball right in front of it, go forward, # rotate otherwise if direction == 0: left_speed = -10 right_speed = -10 elif direction == -1: left_speed = direction * 10 right_speed = direction * -10 else: left_speed = direction * 10 right_speed = direction * -10 # Set the speed to motors self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed) else: robotPointAngle, robot_angle = utils.getPointAngle( orientation, xr, yr, spotX, spotY * -1) direction = utils.get_direction(robotPointAngle) # If the robot has the ball right in front of it, go forward, # rotate otherwise if direction == 0: left_speed = -10 right_speed = -10 elif direction == -1: left_speed = direction * 10 right_speed = direction * -10 else: left_speed = direction * 10 right_speed = direction * -10 # Set the speed to motors self.left_motor.setVelocity(left_speed) self.right_motor.setVelocity(right_speed) xbOLD = xb yBOLD = yb ball_pos_last = [ball_pos['x'], ball_pos['y']]