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, 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 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 addObject(templateFile, position, rotation=OpenNero.Vector3f(0, 0, 0), scale=OpenNero.Vector3f(1, 1, 1), label="", type=0, collision=0): return OpenNero.getSimContext().addObject(templateFile, position, rotation, scale, label, collision, type)
def __init__(self, agent): self.id = agent.state.id self.agent = agent self.pose = (0, 0, 0) # current x, y, heading self.prev_pose = (0, 0, 0) self.initial_position = OpenNero.Vector3f(0, 0, 0) self.initial_rotation = OpenNero.Vector3f(0, 0, 0) self.total_damage = 0 self.curr_damage = 0
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 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(): 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 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 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 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_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 mouse_action(): import math global modify_object_id global object_ids if len(modify_object_id) == 0: return sim_context = OpenNero.getSimContext() cursor = sim_context.getMousePosition() location = sim_context.getClickedPosition(cursor.x, cursor.y) if 'move' in modify_object_id: sim_context.setObjectPosition( modify_object_id['move'], OpenNero.Vector3f(location.x, location.y, constants.HEIGHT + constants.OFFSET)) if 'rot' in modify_object_id: position = sim_context.getObjectPosition(modify_object_id['rot']) angle = math.atan2(location.x - position.x, location.y - position.y) sim_context.setObjectRotation( modify_object_id['rot'], OpenNero.Vector3f(0, 0, -math.degrees(angle))) if 'scale' in modify_object_id: position = sim_context.getObjectPosition(modify_object_id['scale']) rotation = sim_context.getObjectRotation(modify_object_id['scale']) theta = math.radians(rotation.z) # calculate mouse location in the frame of reference of the object localx = (location.x - position.x) * math.cos(theta) + ( location.y - position.y) * math.sin(theta) localy = -(location.x - position.x) * math.sin(theta) + ( location.y - position.y) * math.cos(theta) # scale < 1 if local coordinate is -ve and scale > 1 otherwise scalex = 1 + math.fabs(localx) scaley = 1 + math.fabs(localy) if localx < 0: scalex = 1 / scalex if localy < 0: scaley = 1 / scaley prev_scale = sim_context.getObjectScale(modify_object_id['scale']) sim_context.setObjectScale( modify_object_id['scale'], OpenNero.Vector3f(scalex, scaley, prev_scale.z))
def evaluate_trace(self): """ evaluate agent and compute fitness based on trace information """ # flush network from previous activations org = self.get_org() org.net.flush() environment = OpenNero.get_environment() trace = environment.trace # place the agent at the beginning of the trace self.state.position = OpenNero.Vector3f(trace.position[0].x, trace.position[0].y, trace.position[0].z) self.state.rotation = OpenNero.Vector3f(trace.rotation[0].x, trace.rotation[0].y, trace.rotation[0].z) current_step = trace.initial_step j = 0 # trace index at which error is calculated while j < len(trace.position ) - 1 and current_step < environment.STEPS_PER_EPISODE: self.state.position = position self.state.rotation = rotation sensors = environment.sense(self) actions = self.network_action(sensors) # error based on position - find index in trace where error based on # current position starts to increase, i.e. starting with the current # trace index, we find position in trace that is closest to the current # state position. error1 = trace.position[j].getDistanceFrom(position) error2 = trace.position[j + 1].getDistanceFrom(position) while error1 >= error2 and j < len(trace.position) - 2: j += 1 error1 = error2 error2 = trace.position[j + 1].getDistanceFrom(position) if error1 > self.ERROR_THRESHOLD: break # calculate new position, orientation, and velocity self.environment.act(self, actions) current_step += 1 self.passed_steps = j return float(j) / len(trace.position)
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 ClientMain(): # physics off, ai off by default #disable_physics() OpenNero.disable_ai() if not module.getMod().setup_map(): inputConfig.switchToHub() return # add a light source OpenNero.getSimContext().addLightSource(OpenNero.Vector3f(500, -500, 1000), 1500) #common.addSkyBox("data/sky/irrlicht2") # setup the gui CreateGui(common.getGuiManager()) # add a camera camRotateSpeed = 100 camMoveSpeed = 15000 camZoomSpeed = 200 cam = OpenNero.getSimContext().addCamera(camRotateSpeed, camMoveSpeed, camZoomSpeed) cam.setFarPlane(40000) cam.setEdgeScroll(False) recenter_cam = recenter(cam) recenter_cam() # create the io map ioMap = inputConfig.createInputMapping() ioMap.BindKey("KEY_SPACE", "onPress", recenter_cam) OpenNero.getSimContext().setInputMapping(ioMap)
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 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 ClientMain(): global modify_object_id global object_ids global guiMan OpenNero.disable_ai() if not module.getMod().setup_map(): switchToHub() return # add a light source OpenNero.getSimContext().addLightSource(OpenNero.Vector3f(500, -500, 1000), 1500) common.addSkyBox("data/sky/irrlicht2") # setup the gui guiMan = common.getGuiManager() object_ids = {} modify_object_id = {} # add a camera camRotateSpeed = 100 camMoveSpeed = 15000 camZoomSpeed = 200 cam = OpenNero.getSimContext().addCamera(camRotateSpeed, camMoveSpeed, camZoomSpeed) cam.setFarPlane(40000) cam.setEdgeScroll(False) def recenter(cam): def closure(): cam.setPosition(OpenNero.Vector3f(0, 0, 100)) cam.setTarget(OpenNero.Vector3f(100, 100, 0)) return closure recenter_cam = recenter(cam) recenter_cam() # create the io map ioMap = createInputMapping() ioMap.BindKey("KEY_SPACE", "onPress", recenter_cam) OpenNero.getSimContext().setInputMapping(ioMap)
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 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 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 closure(): cam.setPosition(OpenNero.Vector3f(0, 0, 100)) cam.setTarget(OpenNero.Vector3f(100, 100, 0))
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): """ 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 __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)
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)