def spawnAgent(self, team=constants.OBJECT_TYPE_TEAM_0, agent_xml=None, first_person=True, z_pos=2): """ This is the function ran when an agent already in the field causes the generation of a new agent. """ if agent_xml is None: color = 'blue' if team is constants.OBJECT_TYPE_TEAM_1: color = 'red' agent_xml = "data/shapes/character/steve_still_%s.xml" % color if first_person: agent_pos = OpenNero.Vector3f(self.spawn_x[team], self.spawn_y[team], z_pos) self.curr_team = team self.first_person_agent = common.addObject(agent_xml, agent_pos, type=team) else: pos = self.random_spawn_position(team) agent_pos = OpenNero.Vector3f(pos[0], pos[1], z_pos) common.addObject(agent_xml, agent_pos, type=team)
def spawn_agent(self, agent, x, y): simId = common.addObject( "data/shapes/character/steve_%s.xml" % (constants.TEAM_LABELS[agent.team_type]), OpenNero.Vector3f(x, y, 2), type=agent.team_type) common.initObjectBrain(simId, agent) return simId
def spawnAgent(self, team=constants.OBJECT_TYPE_TEAM_0, ai=None): """ This is the function ran when an agent already in the field causes the generation of a new agent. Returns the id of the spawned agent. """ if not self.environment: return if ai == 'rtneat' and not OpenNero.get_ai('rtneat-%s' % team): self.start_rtneat(team) if ai == 'rtneatq' and not OpenNero.get_ai('rtneatq-%s' % team): self.start_rtneatq(team) self.curr_team = team color = constants.TEAM_LABELS[team] dx = random.randrange(constants.XDIM / 20) - constants.XDIM / 40 dy = random.randrange(constants.XDIM / 20) - constants.XDIM / 40 return common.addObject( "data/shapes/character/steve_%s_%s.xml" % (color, ai), OpenNero.Vector3f(self.spawn_x[team] + dx, self.spawn_y[team] + dy, 2), type=team)
def distribute_bots(self, num_bots, bot_type): """distribute bots so that they don't overlap""" # make a number of tiles to stick bots in N_TILES = 10 tiles = [(r, c) for r in range(N_TILES) for c in range(N_TILES)] random.shuffle(tiles) bots_to_add = num_bots while bots_to_add > 0: (r, c) = tiles.pop() # random tile x, y = r * constants.XDIM / float( N_TILES), c * constants.YDIM / float( N_TILES) # position within tile x, y = x + random.random( ) * constants.XDIM * 0.5 / N_TILES, y + random.random( ) * constants.YDIM * 0.5 / N_TILES # random offset if in_bounds(x, y): agent_id = common.addObject( bot_type, OpenNero.Vector3f(x, y, 0), scale=OpenNero.Vector3f(1, 1, 1), type=constants.OBJECT_TYPE_ROOMBA, collision=constants.OBJECT_TYPE_ROOMBA) self.agent_ids.append(agent_id) bots_to_add -= 1 else: pass # if a tile caused a bad starting point, we won't see it again
def mark(self, x, y, marker): """ Mark a position (x, y) with the specified color """ # remove the previous object, if necessary self.unmark(x, y) # add a new marker object id = common.addObject(marker, OpenNero.Vector3f(x, y, -1), OpenNero.Vector3f(0,0,0), OpenNero.Vector3f(0.5,0.5,0.5), type = constants.OBJECT_TYPE_MARKER) # remember the ID of the object we are about to create self.marker_map[(x, y)] = id
def add_wall(): obj_id = common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(location.x, location.y, constants.HEIGHT + constants.OFFSET), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(5, 30, constants.HEIGHT*2), type=1) object_ids[obj_id] = set(['rotate', 'move', 'scale', 'remove'])
def spawn_agent(self, agent, x, y): simId = common.addObject( "data/shapes/character/steve_%s.xml" % (constants.TEAM_LABELS[agent.team_type]), OpenNero.Vector3f(x, y, 2), type=agent.team_type, ) common.initObjectBrain(simId, agent) return simId
def change_flag(self, loc): if self.flag_id: common.removeObject(self.flag_id) self.flag_loc = OpenNero.Vector3f(*loc) self.flag_id = common.addObject("data/shapes/cube/BlueCube.xml", self.flag_loc, label="Flag", scale=OpenNero.Vector3f(1, 1, 10), type=constants.OBJECT_TYPE_FLAG)
def add_wall(): object_ids.append( common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f( location.x, location.y, constants.HEIGHT + constants.OFFSET), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(1, 30, constants.HEIGHT), type=1))
def spawnAgent(self, team = constants.OBJECT_TYPE_TEAM_0, agent_xml = None, first_person = True, z_pos = 2): """ This is the function ran when an agent already in the field causes the generation of a new agent. """ if agent_xml is None: color = 'blue' if team is constants.OBJECT_TYPE_TEAM_1: color = 'red' agent_xml = "data/shapes/character/steve_still_%s.xml" % color if first_person: agent_pos = OpenNero.Vector3f(self.spawn_x[team], self.spawn_y[team], z_pos) self.curr_team = team self.first_person_agent = common.addObject(agent_xml, agent_pos, type=team) else: pos = self.random_spawn_position(team) agent_pos = OpenNero.Vector3f(pos[0], pos[1], z_pos) common.addObject(agent_xml, agent_pos, type=team)
def add_wall(): obj_id = common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(location.x, location.y, constants.HEIGHT + constants.OFFSET), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(5, 30, constants.HEIGHT * 2), type=1) object_ids[obj_id] = set(['rotate', 'move', 'scale', 'remove'])
def start_tower1(self): #Problem reduction """ start the tower demo """ self.num_disks = 3 OpenNero.disable_ai() self.stop_agent() env = TowerEnvironment() env.initialize_blocks() self.set_environment(env) self.agent_id = common.addObject("data/shapes/character/BlocksRobot.xml", OpenNero.Vector3f(constants.GRID_DX, constants.GRID_DY, 2), type=constants.AGENT_MASK, scale=OpenNero.Vector3f(3,3,3)) OpenNero.enable_ai()
def start_nlp_extended(): #Natural Language Processing """ start the tower demo """ getMod().num_disks = 3 OpenNero.disable_ai() getMod().stop_agent() env = TowerEnvironment() env.initialize_blocks() getMod().set_environment(env) getMod().agent_id = common.addObject("data/shapes/character/MyNLPRobot.xml", OpenNero.Vector3f(TowerofHanoi.constants.GRID_DX, TowerofHanoi.constants.GRID_DY, 2), type=TowerofHanoi.constants.AGENT_MASK, scale=OpenNero.Vector3f(3,3,3)) OpenNero.enable_ai()
def add_wall(): object_ids.append( common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(location.x, location.y, constants.HEIGHT + constants.OFFSET), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(1, 30, constants.HEIGHT), type=1, ) )
def start_my_planner_2_disk(): """ start the tower demo """ getMod().num_disks = 2 OpenNero.disable_ai() getMod().stop_agent() env = TowerEnvironment() env.initialize_blocks() getMod().set_environment(env) getMod().agent_id = common.addObject("data/shapes/character/MyPlanningRobot2.xml", OpenNero.Vector3f(TowerofHanoi.constants.GRID_DX, TowerofHanoi.constants.GRID_DY, 2), type=TowerofHanoi.constants.AGENT_MASK, scale=OpenNero.Vector3f(3,3,3)) OpenNero.enable_ai()
def change_flag(self, loc): if self.flag_id: common.removeObject(self.flag_id) self.flag_loc = OpenNero.Vector3f(*loc) self.flag_id = common.addObject( "data/shapes/cube/BlueCube.xml", self.flag_loc, label="Flag", scale=OpenNero.Vector3f(1, 1, 10), type=constants.OBJECT_TYPE_FLAG)
def start_tower3(self): #2 Disk Goal Stack Planner """ start the tower demo """ self.num_disks = 2 OpenNero.disable_ai() self.stop_agent() env = TowerEnvironment() env.initialize_blocks() self.set_environment(env) #self.set_environment(TowerEnvironment()) self.agent_id = common.addObject("data/shapes/character/BlocksRobot3.xml", OpenNero.Vector3f(constants.GRID_DX, constants.GRID_DY, 2), type=constants.AGENT_MASK, scale=OpenNero.Vector3f(3,3,3)) OpenNero.enable_ai()
def set_environment(self, env): self.environment = env for id in self.wall_ids: # delete the walls common.removeObject(id) del self.wall_ids[:] # clear the ids OpenNero.set_environment(env) common.addObject("data/shapes/cube/WhiteCube.xml", OpenNero.Vector3f(1 * constants.GRID_DX, 2 * constants.GRID_DY, 0 * constants.GRID_DZ), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(.25, .25, 4)) common.addObject("data/shapes/cube/WhiteCube.xml", OpenNero.Vector3f(2 * constants.GRID_DX, 2 * constants.GRID_DY, 0 * constants.GRID_DZ), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(.25, .25, 4)) common.addObject("data/shapes/cube/WhiteCube.xml", OpenNero.Vector3f(3 * constants.GRID_DX, 2 * constants.GRID_DY, 0 * constants.GRID_DZ), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(.25, .25, 4))
def set_environment(self, env): self.environment = env for id in self.wall_ids: # delete the walls common.removeObject(id) del self.wall_ids[:] # clear the ids OpenNero.set_environment(env) common.addObject( "data/shapes/cube/WhiteCube.xml", OpenNero.Vector3f(1 * constants.GRID_DX, 2 * constants.GRID_DY, 0 * constants.GRID_DZ), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(0.25, 0.25, 4), ) common.addObject( "data/shapes/cube/WhiteCube.xml", OpenNero.Vector3f(2 * constants.GRID_DX, 2 * constants.GRID_DY, 0 * constants.GRID_DZ), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(0.25, 0.25, 4), ) common.addObject( "data/shapes/cube/WhiteCube.xml", OpenNero.Vector3f(3 * constants.GRID_DX, 2 * constants.GRID_DY, 0 * constants.GRID_DZ), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(0.25, 0.25, 4), )
def load_trace(self, filename): ''' load previously pickled trace from the given file ''' with open(filename, 'r') as file: self.trace = pickle.load(file) self.use_trace = True for pos in self.trace.position: id = common.addObject("data/shapes/cube/YellowCube.xml", position=pos, scale=OpenNero.Vector3f(0.25, 0.25, 0.25), type=constants.OBJECT_TYPE_LEVEL_GEOM) self.path_markers_trace.append(id)
def start_tower1(self): #Problem reduction """ start the tower demo """ self.num_disks = 3 OpenNero.disable_ai() self.stop_agent() env = TowerEnvironment() env.initialize_blocks() self.set_environment(env) self.agent_id = common.addObject( "data/shapes/character/BlocksRobot.xml", OpenNero.Vector3f(constants.GRID_DX, constants.GRID_DY, 2), type=constants.AGENT_MASK, scale=OpenNero.Vector3f(3, 3, 3)) OpenNero.enable_ai()
def load_trace(self, filename): ''' load previously pickled trace from the given file ''' with open(filename, 'r') as file: self.trace = pickle.load(file) self.use_trace = True for pos in self.trace.position: id = common.addObject( "data/shapes/cube/YellowCube.xml", position = pos, scale = OpenNero.Vector3f(0.25,0.25,0.25), type = constants.OBJECT_TYPE_LEVEL_GEOM) self.path_markers_trace.append(id)
def start_demonstration(self): ''' start the keyboard agent to collect demonstration example ''' OpenNero.disable_ai() team = constants.OBJECT_TYPE_TEAM_0 self.curr_team = team #self.environment.remove_all_agents(team) location = (self.spawn_x[team], self.spawn_y[team], 2) agnt = common.addObject("data/shapes/character/steve_keyboard.xml", position=OpenNero.Vector3f(*location), type=team) OpenNero.enable_ai() self.environment.start_tracing() return agnt
def start_my_planner_3_disk(): """ start the tower demo """ getMod().num_disks = 3 OpenNero.disable_ai() getMod().stop_agent() env = TowerEnvironment() env.initialize_blocks() getMod().set_environment(env) getMod().agent_id = common.addObject( "data/shapes/character/MyPlanningRobot3.xml", OpenNero.Vector3f(TowerofHanoi.constants.GRID_DX, TowerofHanoi.constants.GRID_DY, 2), type=TowerofHanoi.constants.AGENT_MASK, scale=OpenNero.Vector3f(3, 3, 3)) OpenNero.enable_ai()
def start_tower3(self): #2 Disk Goal Stack Planner """ start the tower demo """ self.num_disks = 2 OpenNero.disable_ai() self.stop_agent() env = TowerEnvironment() env.initialize_blocks() self.set_environment(env) #self.set_environment(TowerEnvironment()) self.agent_id = common.addObject( "data/shapes/character/BlocksRobot3.xml", OpenNero.Vector3f(constants.GRID_DX, constants.GRID_DY, 2), type=constants.AGENT_MASK, scale=OpenNero.Vector3f(3, 3, 3)) OpenNero.enable_ai()
def start_nlp_extended(): #Natural Language Processing """ start the tower demo """ getMod().num_disks = 3 OpenNero.disable_ai() getMod().stop_agent() env = TowerEnvironment() env.initialize_blocks() getMod().set_environment(env) getMod().agent_id = common.addObject( "data/shapes/character/MyNLPRobot.xml", OpenNero.Vector3f(TowerofHanoi.constants.GRID_DX, TowerofHanoi.constants.GRID_DY, 2), type=TowerofHanoi.constants.AGENT_MASK, scale=OpenNero.Vector3f(3, 3, 3)) OpenNero.enable_ai()
def start_agent_state_space_search(self): #State-space search """ start the tower demo """ self.num_disks = 3 OpenNero.disable_ai() self.stop_agent() env = TowerEnvironment() env.initialize_blocks() self.set_environment(env) #self.set_environment(TowerEnvironment()) self.agent_id = common.addObject( "data/shapes/character/BlocksRobot2.xml", OpenNero.Vector3f(constants.GRID_DX, constants.GRID_DY, 2), type=constants.AGENT_MASK, scale=OpenNero.Vector3f(3, 3, 3)) OpenNero.enable_ai()
def start_agent_state_space_search(self): # State-space search """ start the tower demo """ self.num_disks = 3 OpenNero.disable_ai() self.stop_agent() env = TowerEnvironment() env.initialize_blocks() self.set_environment(env) # self.set_environment(TowerEnvironment()) self.agent_id = common.addObject( "data/shapes/character/BlocksRobot2.xml", OpenNero.Vector3f(constants.GRID_DX, constants.GRID_DY, 2), type=constants.AGENT_MASK, scale=OpenNero.Vector3f(3, 3, 3), ) OpenNero.enable_ai()
def start_demonstration(self): ''' start the keyboard agent to collect demonstration example ''' OpenNero.disable_ai() team = constants.OBJECT_TYPE_TEAM_0 self.curr_team = team #self.environment.remove_all_agents(team) location = (self.spawn_x[team], self.spawn_y[team], 2) agnt = common.addObject( "data/shapes/character/steve_keyboard.xml", position = OpenNero.Vector3f(*location), type=team) OpenNero.enable_ai() self.environment.start_tracing() return agnt
def distribute_bots(self, num_bots, bot_type): """distribute bots so that they don't overlap""" # make a number of tiles to stick bots in N_TILES = 10 tiles = [ (r,c) for r in range(N_TILES) for c in range(N_TILES)] random.shuffle(tiles) bots_to_add = num_bots while bots_to_add > 0: (r,c) = tiles.pop() # random tile x, y = r * constants.XDIM / float(N_TILES), c * constants.YDIM / float(N_TILES) # position within tile x, y = x + random.random() * constants.XDIM * 0.5 / N_TILES, y + random.random() * constants.YDIM * 0.5 / N_TILES # random offset if in_bounds(x,y): agent_id = common.addObject(bot_type, OpenNero.Vector3f(x, y, 0), scale=OpenNero.Vector3f(1, 1, 1), type = constants.OBJECT_TYPE_ROOMBA, collision = constants.OBJECT_TYPE_ROOMBA) self.agent_ids.append(agent_id) bots_to_add -= 1 else: pass # if a tile caused a bad starting point, we won't see it again
def spawnAgent(self, team=constants.OBJECT_TYPE_TEAM_0, ai=None): """ This is the function ran when an agent already in the field causes the generation of a new agent. Returns the id of the spawned agent. """ if not self.environment: return if ai == 'rtneat' and not OpenNero.get_ai('rtneat-%s' % team): self.start_rtneat(team) self.curr_team = team color = constants.TEAM_LABELS[team] dx = random.randrange(constants.XDIM / 20) - constants.XDIM / 40 dy = random.randrange(constants.XDIM / 20) - constants.XDIM / 40 return common.addObject( "data/shapes/character/steve_%s_%s.xml" % (color, ai), OpenNero.Vector3f(self.spawn_x[team] + dx, self.spawn_y[team] + dy, 2), type=team)
def setup_map(self): """ setup the test environment """ OpenNero.disable_ai() if self.environment: error("Environment already created") return # create the environment - this also creates the rtNEAT object self.environment = self.create_environment() OpenNero.set_environment(self.environment) # world walls height = constants.HEIGHT + constants.OFFSET common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, 0, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.XDIM, constants.HEIGHT*2), label="World Wall0", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(0, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM, constants.HEIGHT*2), label="World Wall1", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM, constants.HEIGHT*2), label="World Wall2", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.XDIM, constants.HEIGHT*2), label="World Wall3", type=constants.OBJECT_TYPE_OBSTACLE) # Add an obstacle wall in the middle common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, 100, constants.HEIGHT*2), label="World Wall4", type=constants.OBJECT_TYPE_OBSTACLE) # # Add some trees # for i in (0.25, 0.75): # for j in (0.25, 0.75): # # don't collide with trees - they are over 500 triangles each # common.addObject( # "data/shapes/tree/Tree.xml", # OpenNero.Vector3f(i * constants.XDIM, j * constants.YDIM, constants.OFFSET), # OpenNero.Vector3f(0, 0, 0), # scale=OpenNero.Vector3f(1, 1, 1), # label="Tree %d %d" % (10 * i, 10 * j), # type=constants.OBJECT_TYPE_LEVEL_GEOM) # # collide with their trunks represented with cubes instead # common.addObject( # "data/shapes/cube/Cube.xml", # OpenNero.Vector3f(i * constants.XDIM, j * constants.YDIM, constants.OFFSET), # OpenNero.Vector3f(0,0,0), # scale=OpenNero.Vector3f(1,1,constants.HEIGHT), # type=constants.OBJECT_TYPE_OBSTACLE) # Add the surrounding Environment common.addObject( "data/terrain/NeroWorld.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM/2, 0), scale=OpenNero.Vector3f(1.2, 1.2, 1.2), label="NeroWorld", type=constants.OBJECT_TYPE_LEVEL_GEOM) return True
def setup(self): # world walls height = constants.HEIGHT + constants.OFFSET common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM / 2, 0, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.XDIM, constants.HEIGHT * 2), label="World Wall0", type=constants.OBJECT_TYPE_OBSTACLE, ) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(0, constants.YDIM / 2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM, constants.HEIGHT * 2), label="World Wall1", type=constants.OBJECT_TYPE_OBSTACLE, ) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM, constants.YDIM / 2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM, constants.HEIGHT * 2), label="World Wall2", type=constants.OBJECT_TYPE_OBSTACLE, ) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM / 2, constants.YDIM, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.XDIM, constants.HEIGHT * 2), label="World Wall3", type=constants.OBJECT_TYPE_OBSTACLE, ) # Add an obstacle wall in the middle common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM / 2, constants.YDIM / 2, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM / 4, constants.HEIGHT * 2), label="World Wall4", type=constants.OBJECT_TYPE_OBSTACLE, ) # Add some trees for i in (0.25, 0.75): for j in (0.25, 0.75): # don't collide with trees - they are over 500 triangles each common.addObject( "data/shapes/tree/Tree.xml", OpenNero.Vector3f(i * constants.XDIM, j * constants.YDIM, constants.OFFSET), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, 1, 1), label="Tree %d %d" % (10 * i, 10 * j), type=constants.OBJECT_TYPE_LEVEL_GEOM, ) # collide with their trunks represented with cubes instead common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(i * constants.XDIM, j * constants.YDIM, constants.OFFSET), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, 1, constants.HEIGHT), type=constants.OBJECT_TYPE_OBSTACLE, ) # Add the surrounding Environment common.addObject( "data/terrain/NeroWorld.xml", OpenNero.Vector3f(constants.XDIM / 2, constants.YDIM / 2, 0), scale=OpenNero.Vector3f(1.2, 1.2, 1.2), label="NeroWorld", type=constants.OBJECT_TYPE_LEVEL_GEOM, )
def setup(self): # world walls height = constants.HEIGHT + constants.OFFSET common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, 0, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.XDIM, constants.HEIGHT*2), label="World Wall0", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(0, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM, constants.HEIGHT*2), label="World Wall1", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM, constants.HEIGHT*2), label="World Wall2", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.XDIM, constants.HEIGHT*2), label="World Wall3", type=constants.OBJECT_TYPE_OBSTACLE) # Add an obstacle wall in the middle common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM / 4, constants.HEIGHT*2), label="World Wall4", type=constants.OBJECT_TYPE_OBSTACLE) # Add some trees for i in (0.25, 0.75): for j in (0.25, 0.75): # don't collide with trees - they are over 500 triangles each common.addObject( "data/shapes/tree/Tree.xml", OpenNero.Vector3f(i * constants.XDIM, j * constants.YDIM, constants.OFFSET), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, 1, 1), label="Tree %d %d" % (10 * i, 10 * j), type=constants.OBJECT_TYPE_LEVEL_GEOM) # collide with their trunks represented with cubes instead common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(i * constants.XDIM, j * constants.YDIM, constants.OFFSET), OpenNero.Vector3f(0,0,0), scale=OpenNero.Vector3f(1,1,constants.HEIGHT), type=constants.OBJECT_TYPE_OBSTACLE) # Add the surrounding Environment common.addObject( "data/terrain/NeroWorld.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM/2, 0), scale=OpenNero.Vector3f(1.2, 1.2, 1.2), label="NeroWorld", type=constants.OBJECT_TYPE_LEVEL_GEOM)
def setup_map(self): """ setup the test environment """ OpenNero.disable_ai() if self.environment: error("Environment already created") return # create the environment - this also creates the rtNEAT object self.environment = self.create_environment() OpenNero.set_environment(self.environment) # world walls height = constants.HEIGHT + constants.OFFSET common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM / 2, 0, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(1, constants.XDIM, constants.HEIGHT), label="World Wall0", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(0, constants.YDIM / 2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, constants.YDIM, constants.HEIGHT), label="World Wall1", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM, constants.YDIM / 2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, constants.YDIM, constants.HEIGHT), label="World Wall2", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM / 2, constants.YDIM, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(1, constants.XDIM, constants.HEIGHT), label="World Wall3", type=constants.OBJECT_TYPE_OBSTACLE) # Add an obstacle wall in the middle """ common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(1, constants.YDIM / 4, constants.HEIGHT), label="World Wall4", type=constants.OBJECT_TYPE_OBSTACLE) """ # Add the surrounding Environment common.addObject("data/terrain/NeroWorld.xml", OpenNero.Vector3f(constants.XDIM / 2, constants.YDIM / 2, 0), scale=OpenNero.Vector3f(1, 1, 1), label="NeroWorld", type=constants.OBJECT_TYPE_LEVEL_GEOM) self.spawnAgent(agent_xml='data/shapes/tree/Tree.xml', first_person=False) self.spawnAgent(agent_xml='data/shapes/character/steve_still_blue.xml', first_person=False) self.spawnAgent(agent_xml='data/shapes/character/sydney_still.xml', first_person=False, z_pos=5) self.spawnAgent(agent_xml='data/shapes/cube/GreenCube.xml', first_person=False, z_pos=7) return True
def setup_map(self): """ setup the test environment """ OpenNero.disable_ai() if self.environment: error("Environment already created") return # create the environment - this also creates the rtNEAT object self.environment = self.create_environment() OpenNero.set_environment(self.environment) # world walls height = constants.HEIGHT + constants.OFFSET common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM / 2, 0, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.XDIM, constants.HEIGHT * 2), label="World Wall0", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(0, constants.YDIM / 2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM, constants.HEIGHT * 2), label="World Wall1", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM, constants.YDIM / 2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM, constants.HEIGHT * 2), label="World Wall2", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM / 2, constants.YDIM, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.XDIM, constants.HEIGHT * 2), label="World Wall3", type=constants.OBJECT_TYPE_OBSTACLE) # Add an obstacle wall in the middle common.addObject("data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM / 2, constants.YDIM / 2, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(constants.WIDTH, constants.YDIM / 4, constants.HEIGHT * 2), label="World Wall4", type=constants.OBJECT_TYPE_OBSTACLE) # Add some trees for i in (0.25, 0.75): for j in (0.25, 0.75): # don't collide with trees - they are over 500 triangles each common.addObject("data/shapes/tree/Tree.xml", OpenNero.Vector3f(i * constants.XDIM, j * constants.YDIM, constants.OFFSET), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, 1, 1), label="Tree %d %d" % (10 * i, 10 * j), type=constants.OBJECT_TYPE_LEVEL_GEOM) # collide with their trunks represented with cubes instead common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(i * constants.XDIM, j * constants.YDIM, constants.OFFSET), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, 1, constants.HEIGHT), type=constants.OBJECT_TYPE_OBSTACLE) # Add the surrounding Environment common.addObject("data/terrain/NeroWorld.xml", OpenNero.Vector3f(constants.XDIM / 2, constants.YDIM / 2, 0), scale=OpenNero.Vector3f(1.2, 1.2, 1.2), label="NeroWorld", type=constants.OBJECT_TYPE_LEVEL_GEOM) return True
def sense(self, agent, observations): """ figure out what the agent should sense """ # the last observation is whether there is a target for the agent # update: this is now being done using a RaySensor #observations[constants.SENSOR_INDEX_TARGETING[0]] = int(self.target(agent) is not None) # friends, foes = self.getFriendFoe(agent) # if friends: # # the 2 before that are the angle and heading to the center of mass of # # the agent's team # ax, ay = agent.state.position.x, agent.state.position.y # cx, cy = self.get_friend_center(agent, friends) # fd = self.distance((ax, ay), (cx, cy)) # ah = agent.state.rotation.z # fh = self.angle((ax, ay, ah), (cx, cy)) + 180.0 # if fd <= constants.MAX_FRIEND_DISTANCE: # observations[constants.SENSOR_INDEX_FRIEND_RADAR[0]] = fd / 15.0 # observations[constants.SENSOR_INDEX_FRIEND_RADAR[1]] = fh / 360.0 # The code above miscalculates the friend center by looking at all teammates # instead of looking only inside the friend radius. # Updated friend sensing code: my_team = agent.get_team() all_friends = self.teams[my_team] ax, ay = agent.state.position.x, agent.state.position.y cx, cy = 0.0, 0.0 if all_friends: n = 0 for f in all_friends: fx, fy = f.state.position.x, f.state.position.y if self.distance((ax, ay), (fx, fy)) <= constants.MAX_FRIEND_DISTANCE: n += 1 cx += fx cy += fy if n > 0: cx, cy = cx / n, cy / n fd = self.distance((ax, ay), (cx, cy)) ah = agent.state.rotation.z fh = self.angle((ax, ay, ah), (cx, cy)) + 180.0 value = 1 - (fd / constants.MAX_FRIEND_DISTANCE) value = max(0, min(value, 1)) observations[constants.SENSOR_INDEX_FRIEND_RADAR[0]] = value observations[constants.SENSOR_INDEX_FRIEND_RADAR[1]] = fh / 360.0 if self.tracing: if self.trace.initial_step is None: self.trace.initial_step = agent.step self.trace.position.append(agent.state.position) self.trace.rotation.append(agent.state.rotation) self.trace.sensors.append([o for o in observations]) from agent import KeyboardAgent if self.tracing and isinstance(agent, KeyboardAgent): id = common.addObject( 'data/shapes/cube/YellowCube.xml', position = agent.state.position, scale = OpenNero.Vector3f(0.25, 0.25, 0.25), type = constants.OBJECT_TYPE_LEVEL_GEOM) self.path_markers_trace.append(id) return observations
def place_basic_turret(self, loc): return common.addObject("data/shapes/character/steve_basic_turret.xml", OpenNero.Vector3f(*loc), type=constants.OBJECT_TYPE_TEAM_1)
def place_basic_turret(self, loc): common.addObject( "data/shapes/character/steve_basic_turret.xml", OpenNero.Vector3f(*loc), type=constants.OBJECT_TYPE_TEAM_1)
def __init__(self, xdim, ydim): """ Create the environment """ OpenNero.Environment.__init__(self) self.XDIM = xdim self.YDIM = ydim self.max_steps = 500 self.states = {} # dictionary of agent states self.crumbs = world_handler.pattern_cluster(500, "Roomba/world_config.txt") # only keep crumbs that are inside the walls self.crumbs = [c for c in self.crumbs if in_bounds(c.x,c.y)] self.init_list = AgentInit() self.init_list.add_type("<class 'Roomba.roomba.RoombaBrain'>") self.init_list.add_type("<class 'Roomba.RTNEATAgent.RTNEATAgent'>") #print self.init_list.types roomba_abound = self.init_list.get_action("<class 'Roomba.roomba.RoombaBrain'>") roomba_sbound = self.init_list.get_sensor("<class 'Roomba.roomba.RoombaBrain'>") roomba_rbound = self.init_list.get_reward("<class 'Roomba.roomba.RoombaBrain'>") rtneat_abound = self.init_list.get_action("<class 'Roomba.RTNEATAgent.RTNEATAgent'>") rtneat_sbound = self.init_list.get_sensor("<class 'Roomba.RTNEATAgent.RTNEATAgent'>") rtneat_rbound = self.init_list.get_reward("<class 'Roomba.RTNEATAgent.RTNEATAgent'>") ### Bounds for Roomba ### # actions roomba_abound.add_continuous(-math.pi, math.pi) # amount to turn by # sensors roomba_sbound.add_discrete(0,1) # wall bump roomba_sbound.add_continuous(0,xdim) # self.x roomba_sbound.add_continuous(0,ydim) # self.y roomba_sbound.add_continuous(0,xdim) # closest.x roomba_sbound.add_continuous(0,ydim) # closest.y # rewards roomba_rbound.add_continuous(-100,100) # range for reward ### End Bounds for Roomba #### ### Bounds for RTNEAT ### # actions rtneat_abound.add_continuous(-math.pi, math.pi) # amount to turn by # sensors rtneat_sbound.add_continuous(-1, 1) rtneat_sbound.add_continuous(-1, 1) rtneat_sbound.add_continuous(-1, 1) rtneat_sbound.add_continuous(-1, 1) rtneat_sbound.add_continuous(-1, 1) rtneat_sbound.add_continuous(-1, 1) # rewards rtneat_rbound.add_continuous(-1, 1) ### End Bounds for RTNEAT ### # set up shop # Add Wayne's Roomba room with experimentally-derived vertical offset to match crumbs. common.addObject("data/terrain/RoombaRoom.xml", OpenNero.Vector3f(xdim/2,ydim/2, -1), OpenNero.Vector3f(0,0,0), OpenNero.Vector3f(xdim/245.0, ydim/245.0, constants.HEIGHT/24.5), type = constants.OBJECT_TYPE_WALLS) # OpenNero.getSimContext().addAxes() self.add_crumbs() for crumb in self.crumbs: self.add_crumb_sensors(roomba_sbound)
def setup_map(self): """ setup the test environment """ OpenNero.disable_ai() if self.environment: error("Environment already created") return # create the environment - this also creates the rtNEAT object self.environment = self.create_environment() OpenNero.set_environment(self.environment) # world walls height = constants.HEIGHT + constants.OFFSET common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, 0, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(1, constants.XDIM, constants.HEIGHT), label="World Wall0", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(0, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, constants.YDIM, constants.HEIGHT), label="World Wall1", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, constants.YDIM, constants.HEIGHT), label="World Wall2", type=constants.OBJECT_TYPE_OBSTACLE) common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(1, constants.XDIM, constants.HEIGHT), label="World Wall3", type=constants.OBJECT_TYPE_OBSTACLE) # Add an obstacle wall in the middle """ common.addObject( "data/shapes/cube/Cube.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM/2, height), OpenNero.Vector3f(0, 0, 90), scale=OpenNero.Vector3f(1, constants.YDIM / 4, constants.HEIGHT), label="World Wall4", type=constants.OBJECT_TYPE_OBSTACLE) """ # Add the surrounding Environment common.addObject( "data/terrain/NeroWorld.xml", OpenNero.Vector3f(constants.XDIM/2, constants.YDIM/2, 0), scale=OpenNero.Vector3f(1, 1, 1), label="NeroWorld", type=constants.OBJECT_TYPE_LEVEL_GEOM) self.spawnAgent(agent_xml = 'data/shapes/tree/Tree.xml', first_person = False) self.spawnAgent(agent_xml = 'data/shapes/character/steve_still_blue.xml', first_person = False) self.spawnAgent(agent_xml = 'data/shapes/character/sydney_still.xml', first_person = False, z_pos=5) self.spawnAgent(agent_xml = 'data/shapes/cube/GreenCube.xml', first_person = False, z_pos=7) return True
def __init__(self, xdim, ydim, world_file = WORLD_FILE): """ Create the environment """ #self.Qlog = open("Qlog", "wb+") OpenNero.Environment.__init__(self) OpenNero.getSimContext().delay = 0.0; self.XDIM = xdim self.YDIM = ydim self.max_steps = 500 self.states = {} # dictionary of agent states if WORLD_FILE is None: self.crumbs = world_handler.pattern_cluster(500, "Roomba/world_config.txt") else: # read external file data = world_handler.read_world(WORLD_FILE) self.XDIM = data['dims'][0] self.YDIM = data['dims'][1] self.crumbs = data['pellets'] print len(self.crumbs) # only keep crumbs that are inside the walls self.crumbs = [c for c in self.crumbs if in_bounds(c.x,c.y)] self.init_list = AgentInit() self.init_list.add_type("<class 'Roomba.roomba.RoombaBrain'>") self.init_list.add_type("<class 'Roomba.RTNEATAgent.RTNEATAgent'>") self.init_list.add_type("<class 'Roomba.RLAgent.TabularRLAgent'>") #print self.init_list.types roomba_abound = self.init_list.get_action("<class 'Roomba.roomba.RoombaBrain'>") roomba_sbound = self.init_list.get_sensor("<class 'Roomba.roomba.RoombaBrain'>") roomba_rbound = self.init_list.get_reward("<class 'Roomba.roomba.RoombaBrain'>") rtneat_abound = self.init_list.get_action("<class 'Roomba.RTNEATAgent.RTNEATAgent'>") rtneat_sbound = self.init_list.get_sensor("<class 'Roomba.RTNEATAgent.RTNEATAgent'>") rtneat_rbound = self.init_list.get_reward("<class 'Roomba.RTNEATAgent.RTNEATAgent'>") ### Bounds for Roomba ### # actions roomba_abound.add_continuous(-math.pi, math.pi) # amount to turn by # sensors roomba_sbound.add_discrete(0,1) # wall bump roomba_sbound.add_continuous(0,xdim) # self.x roomba_sbound.add_continuous(0,ydim) # self.y roomba_sbound.add_continuous(0,xdim) # closest.x roomba_sbound.add_continuous(0,ydim) # closest.y # rewards roomba_rbound.add_continuous(-100,100) # range for reward ### End Bounds for Roomba #### ### Bounds for RTNEAT ### # actions rtneat_abound.add_continuous(-math.pi, math.pi) # amount to turn by #rtneat_abound.add_continuous(0, 1) # signal # sensors rtneat_sbound.add_continuous(-math.pi, math.pi) # nearest crumb angle rtneat_sbound.add_continuous(0, 1) # proportion of crumb nearby # angle and distance sensors for (up to) 8 nearest neighbors rtneat_sbound.add_continuous(-math.pi, math.pi) rtneat_sbound.add_continuous(0, 1) rtneat_sbound.add_continuous(-math.pi, math.pi) rtneat_sbound.add_continuous(0, 1) rtneat_sbound.add_continuous(-math.pi, math.pi) rtneat_sbound.add_continuous(0, 1) rtneat_sbound.add_continuous(-math.pi, math.pi) rtneat_sbound.add_continuous(0, 1) rtneat_sbound.add_continuous(-math.pi, math.pi) rtneat_sbound.add_continuous(0, 1) rtneat_sbound.add_continuous(-math.pi, math.pi) rtneat_sbound.add_continuous(0, 1) rtneat_sbound.add_continuous(-math.pi, math.pi) rtneat_sbound.add_continuous(0, 1) rtneat_sbound.add_continuous(-math.pi, math.pi) rtneat_sbound.add_continuous(0, 1) # rewards rtneat_rbound.add_continuous(-math.pi, math.pi) ### End Bounds for RTNEAT ### #------------------------------------------------------------------------ rltabular_abound = self.init_list.get_action("<class 'Roomba.RLAgent.TabularRLAgent'>") rltabular_sbound = self.init_list.get_sensor("<class 'Roomba.RLAgent.TabularRLAgent'>") rltabular_rbound = self.init_list.get_reward("<class 'Roomba.RLAgent.TabularRLAgent'>") ### Bounds for RLTabular ### # actions rltabular_abound.add_continuous(-math.pi, math.pi) # amount to turn by # sensors rltabular_sbound.add_continuous(-1, 1) rltabular_sbound.add_continuous(-1, 1) rltabular_sbound.add_continuous(-1, 1) rltabular_sbound.add_continuous(-1, 1) rltabular_sbound.add_continuous(-1, 1) rltabular_sbound.add_continuous(-1, 1) # rewards rltabular_rbound.add_continuous(-1, 1) ### End Bounds for RLTabular ### #------------------------------------------------------------------------ # set up shop # Add Wayne's Roomba room with experimentally-derived vertical offset to match crumbs. common.addObject("data/terrain/RoombaRoom.xml", OpenNero.Vector3f(xdim/2,ydim/2, -1), OpenNero.Vector3f(0,0,0), OpenNero.Vector3f(xdim/245.0, ydim/245.0, constants.HEIGHT/24.5), type = constants.OBJECT_TYPE_WALLS) # OpenNero.getSimContext().addAxes() self.add_crumbs() for crumb in self.crumbs: self.add_crumb_sensors(roomba_sbound)