Exemple #1
0
 def __init__(self, world_params):
     # Simulation global parameters
     self.world_params = world_params
     # List of robots active on map
     self.robots = {}
     # Moves aggregated each turn
     self.move_buffer = {}
     # Log buffer storing text information of events
     self.log = []
     wm = world_params['type']
     # Make simulation deterministic by setting seed
     if not world_params['random']:
         np.random.seed(1)
     if wm == 'maze':
         length = world_params['length']
         width = world_params['width']
         self.world_model = Maze(length, width)
     else:
         raise KeyError('Entered world model does not match existing')
Exemple #2
0
class Simulation:    
    
    # Initialize world model based on available ones
    def __init__(self, world_params):
        # Simulation global parameters
        self.world_params = world_params
        # List of robots active on map
        self.robots = {}
        # Moves aggregated each turn
        self.move_buffer = {}
        # Log buffer storing text information of events
        self.log = []
        wm = world_params['type']
        # Make simulation deterministic by setting seed
        if not world_params['random']:
            np.random.seed(1)
        if wm == 'maze':
            length = world_params['length']
            width = world_params['width']
            self.world_model = Maze(length, width)
        else:
            raise KeyError('Entered world model does not match existing')
    
    # Add robot to model at some random empty position
    def add_robot(self, robot_params, init_state):
        rname = robot_params['type']
        robot_id = robot_params['id']
        if rname == 'car':
            if not init_state:
                init_state = np.append(self.get_empty_pos(),0)
            robot = Car(robot_params, init_state)
        else:
            raise KeyError('Entered robot model does not match existing')
        self.robots[robot_id] = robot
    
    def add_sensor(self, robot_id, sensor_params):
        self.robots[robot_id].add_sensor(sensor_params)
    
    # Return empty state on map where a robot can be placed
    # Need to check that other robot states do no conflict
    def get_empty_pos(self):
        occupied = True
        while occupied:
            wm_empty = self.world_model.get_empty_pos()
            # Check if another robot occupies this state
            occupied = False
            for robot_id in self.robots:
                if self.robots[robot_id].get_position() == wm_empty:
                    print 'Issue: get_empty_pos - state occupied'
                    occupied = True
        return wm_empty
        
    
    # Return dictionary of state variables
    def get_state(self):
        # Map of the world
        world = self.world_model.get_map()
        # States/Poses of all robots on field
        robots = self.robots
        #Sensor readings for all robots on field
        sensors = self.read_sensors()
        data = {'world':world,'robots':robots,'sensors':sensors}
        return data
    
    # Returns dictionary of sensor readings with robot id being the key
    def read_sensors(self):
        readings = {}
        occupied = self.get_occupied()
        for robot_id in self.robots:
            read = self.robots[robot_id].get_sensor_readings(occupied)
            readings[robot_id] = read
        return readings
    
    # Return a matrix with ones where the space is occupied by either
    # an obstacle or another robot
    # Currently only returns matrix with world model obstacles
    def get_occupied(self):
        return self.world_model.get_map()
    
        
    # add move to the buffer to be executed at the end of the turn
    def add_action(self, action, robot_id):
        if robot_id in self.move_buffer:
            raise Exception('Each robot can only take one move per turn')
        self.move_buffer[robot_id] = action
        
    # Update state based on moves - moves taken based on FIFO order
    def update_turn(self):
        occupied= self.get_occupied();
        for robot_id in self.move_buffer:
            action = self.move_buffer[robot_id]
            # need to make sure wm is passed as reference
            self.robots[robot_id].make_move(action, occupied)
        self.move_buffer = {}