Ejemplo n.º 1
0
    def update(self, dt, lights):
        """
        update the position of the car by a dt time step

        Parameters
        __________
        :param       dt:  double
        :param   lights:  dataframe

        Returns
        _______
        :return self.state: dataframe
        """
        self.lights = lights
        self.time_elapsed += dt
        # determine binning and assign bins to cars
        # TODO: don't re-sort every time-step. Only place cars in a new bin if their bin is about to change
        self.state['xbin'], self.state['ybin'] = models.determine_bins(
            self.axis, self.state)

        node_distances, car_distances, light_distances = self.find_obstacles()

        self.state['distance-to-node'] = node_distances
        self.state['distance-to-car'] = car_distances
        self.state['distance-to-red-light'] = light_distances

        self.state['route'], self.state['xpath'], self.state['ypath'], self.state['vx'], \
            self.state['vy'], self.state['route-time'] = sim.update_cars(self.state, dt)

        self.state['x'] = self.state['x'] + self.state['vx'] * dt
        self.state['y'] = self.state['y'] + self.state['vy'] * dt

        return self.state
Ejemplo n.º 2
0
def init_traffic_lights(axis, prescale=10):
    """
    traffic lights are initialized here

    :return lights: list
    """
    epsilon = 0.3  # a factor which forces the positions of the light faces to be close to the intersection

    light_nodes = nav.find_traffic_lights(prescale)

    lights_data = []

    for i, light in enumerate(light_nodes):

        node_id = light[0]

        try:
            out_vectors = np.array(nav.determine_pedigree(node_id))
        except NetworkXNoPath or ValueError:
            print('Could not determine pedigree for light at node {}'.format(node_id))
            continue

        degree = len(out_vectors)
        position = nav.get_position_of_node(node_id)
        go = [False, True] * degree * 2
        go = go[:degree]

        light = {'object': 'light',
                 'node': node_id,
                 'degree': degree,
                 'x': position[0],
                 'y': position[1],
                 'switch-counter': 0,
                 'switch-time': models.determine_traffic_light_timer()
                 }

        light['out-xpositions'] = [position[0] + epsilon * out_vectors[j][0] for j in range(light['degree'])]
        light['out-ypositions'] = [position[1] + epsilon * out_vectors[j][1] for j in range(light['degree'])]
        light['out-xvectors'] = [out_vectors[j][0] for j in range(light['degree'])]
        light['out-yvectors'] = [out_vectors[j][1] for j in range(light['degree'])]
        light['go-values'] = np.array([go[j] for j in range(light['degree'])])

        lights_data.append(light)

    lights = pd.DataFrame(lights_data)

    # determine binning and assign bins to lights
    lights['xbin'], lights['ybin'] = models.determine_bins(axis, lights)

    # print('Number of traffic lights: {}'.format(len(lights)))
    return lights
Ejemplo n.º 3
0
def init_custom_lights(fig_axis, prescale=None):
    """
    traffic lights are initialized here

    :param   fig_axis:  list
    :param   prescale:   int
    :return    lights:  list
    """
    epsilon = 0.1  # a factor which forces the positions of the light faces to be close to the intersection

    lights_data = []

    node_id = 53119168

    try:
        out_vectors = np.array(nav.determine_pedigree(node_id))
    except NetworkXNoPath or ValueError:
        raise('Could not determine pedigree for light at node {}'.format(node_id))

    degree = len(out_vectors)
    x, y = nav.get_position_of_node(node_id)
    go = [False, True] * degree * 2
    go = go[:degree]

    light = {'object': 'light',
             'node': node_id,
             'degree': degree,
             'x': x,
             'y': y,
             'switch-counter': 0,
             'switch-time': models.determine_traffic_light_timer()
             }

    light['out-xpositions'] = [x + epsilon * out_vectors[j][0] for j in range(light['degree'])]
    light['out-ypositions'] = [y + epsilon * out_vectors[j][1] for j in range(light['degree'])]
    light['out-xvectors'] = [out_vectors[j][0] for j in range(light['degree'])]
    light['out-yvectors'] = [out_vectors[j][1] for j in range(light['degree'])]
    light['go-values'] = np.array([go[j] for j in range(light['degree'])])

    lights_data.append(light)

    lights = pd.DataFrame(lights_data)

    # determine binning and assign bins to lights
    lights['xbin'], lights['ybin'] = models.determine_bins(fig_axis, lights)

    # print('Number of traffic lights: {}'.format(len(lights)))
    return lights
Ejemplo n.º 4
0
def init_custom_agent(n=1, fig_axis=axis, car_id=None, alternate_route=None):
    """
    This function initializes a singular car with custom origin and destination

    :param               n:          int
    :param        fig_axis:         list
    :param          car_id:  None or int
    :param alternate_route: None or list
    :return     cars_frame:    DataFrame
    """

    origin = 53085387
    dest = 53082621

    path = nav.get_init_path(origin, dest)
    route = nav.get_route(origin, dest)

    x, y = nav.get_position_of_node(origin)

    car = {'object': 'car',
           'x': x,
           'y': y,
           'vx': 0,
           'vy': 0,
           'route-time': 0,
           'origin': origin,
           'destination': dest,
           'route': route,
           'xpath': np.array([path[i][0] for i in range(len(path))]),
           'ypath': np.array([path[i][1] for i in range(len(path))]),
           'distance-to-car': 0,
           'distance-to-node': 0,
           'distance-to-red-light': 0}

    cars_data = [car]

    if alternate_route:
        cars_data[car_id]['route'], cars_data[car_id]['xpath'], cars_data[car_id]['ypath'] = alternate_route

    cars_frame = pd.DataFrame(cars_data)

    # determine binning and assign bins to cars
    cars_frame['xbin'], cars_frame['ybin'] = models.determine_bins(fig_axis, cars_frame)

    return cars_frame
Ejemplo n.º 5
0
def init_culdesac_start_location(n, axis, car_id=None, alternate_route=None):
    """
    initializes N cars into N culdesacs

    Parameters
    __________
    :param               n:                    int
    :param            axis: list of x and y ranges
    :param          car_id:            None or int: optional, int if you wish to prescribe an alternate route for car
    :param alternate_route:                   list: optional, list of alternate route nodes for provided car

    Returns
    _______
    :return cars:   dataframe
    """
    # TODO: combine this function with other car initialization functions using flags

    culdesacs = nav.find_culdesacs()

    if n > len(culdesacs):
        raise ValueError('Number of cars greater than culdesacs to place them. '
                         'Choose a number less than {}'.format(len(culdesacs)))

    cars_data = []

    for i in range(n):
        # i = 17  # TEMP SETTING
        origin = culdesacs[i]
        destination = culdesacs[i + 1]
        """ TEMP SETTINGS FOR ONE-CAR-ONE-ROUTE STUDY """
        # destination = 53028190
        """ TEMP SETTINGS FOR ONE-CAR-ONE-ROUTE STUDY """
        try:
            path = nav.get_init_path(origin, destination)
            route = nav.get_route(origin, destination)
        except NetworkXNoPath:
            print('No path between {} and {}.'.format(origin, destination))
            continue

        position = nav.get_position_of_node(origin)

        car = {'object': 'car',
               'x': position[0],
               'y': position[1],
               'vx': 0,
               'vy': 0,
               'route-time': 0,
               'origin': origin,
               'destination': destination,
               'route': route,
               'xpath': [path[i][0] for i in range(len(path))],
               'ypath': [path[i][1] for i in range(len(path))],
               'distance-to-car': 0,
               'distance-to-node': 0,
               'distance-to-red-light': 0}

        cars_data.append(car)

    if alternate_route:
        cars_data[car_id]['route'], cars_data[car_id]['xpath'], cars_data[car_id]['ypath'] = alternate_route

    cars = pd.DataFrame(cars_data)

    # determine binning and assign bins to cars
    cars['xbin'], cars['ybin'] = models.determine_bins(axis, cars)

    # print('Number of cars: {}'.format(len(cars)))
    return cars