Ejemplo n.º 1
0
def init_random_node_start_location(n, axis):
    """
    initializes n cars at n random nodes and sets their destinations as a culdesac

    :param      n:  int
    :param   axis: list: x_range, y_range of road network
    :return state: dict
    """
    # TODO: combine this function with other car initialization functions using flags

    nodes = nav.find_nodes(n)
    culdesacs = nav.find_culdesacs()

    cars_data = []

    for i in range(n):
        if i < n - 1:
            origin = nodes[i]
            destination = culdesacs[i % len(culdesacs)]

            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

            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': 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)

    cars = pd.DataFrame(cars_data)

    # determine binning and assign bins to cars
    xbins, ybins = np.arange(axis[0], axis[1], 200), np.arange(axis[2], axis[3], 200)
    x_indices, y_indices = np.digitize(cars['x'], xbins), np.digitize(cars['y'], ybins)
    cars['xbin'], cars['ybin'] = pd.Series(x_indices), pd.Series(y_indices)

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

    return cars
Ejemplo n.º 2
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.º 3
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