parameters: (parameters)
return_statement: return ret_rerouters
docstring: Compute the rerouters for the given parking areas.
docstring_summary: Compute the rerouters for the given parking areas.
docstring_tokens: Compute the rerouters for the given parking areas .
function: def generate_rerouters_process(parameters):
      """ Compute the rerouters for the given parking areas."""
  
      sumo_net = sumolib.net.readNet(parameters['net_file'])
      rtree = initRTree(parameters['all_parking_areas'])
      ret_rerouters = dict()
  
      @functools.lru_cache(maxsize=None)
      def _cached_get_shortest_path(from_edge, to_edge, fromPos, toPos):
          """ Calls and caches sumolib: net.getShortestPath. """
          return sumo_net.getShortestPath(from_edge, to_edge, fromPos=fromPos, toPos=toPos)
  
      distances = collections.defaultdict(dict)
      routes = collections.defaultdict(dict)
      sequence = None
      if parameters['with_tqdm']:
          from tqdm import tqdm
          sequence = tqdm(parameters['selection'])
      else:
          sequence = parameters['selection']
  
      distWeights = None
      distWeightSum = None
      distThresholds = None
      if parameters['distribute'] is not None:
          distWeights = list(map(float, parameters['distribute'].split(',')))
          distWeightSum = sum(distWeights)
  
      for parking_id in sequence:
          parking_a = parameters['all_parking_areas'][parking_id]
          from_edge = sumo_net.getEdge(parking_a['edge'])
          fromPos = float(parking_a['endPos'])
          candidates = parameters['all_parking_areas'].values()
          if rtree is not None:
              allParkings = list(candidates)
              candidates = []
              x, y = parking_a['pos']
              r = parameters['dist_alternatives']
              for i in rtree.intersection((x - r, y - r, x + r, y + r)):
                  candidates.append(allParkings[i])
  
          for parking_b in candidates:
              if parking_a['id'] == parking_b['id']:
                  continue
              toPos = float(parking_b['startPos'])
              route, cost = _cached_get_shortest_path(from_edge,
                                                      sumo_net.getEdge(parking_b['edge']),
                                                      fromPos, toPos)
              if route:
                  distances[parking_a['id']][parking_b['id']] = cost
                  routes[parking_a['id']][parking_b['id']] = route
  
      cache_info = _cached_get_shortest_path.cache_info()
      total = float(cache_info.hits + cache_info.misses)
      perc = cache_info.hits * 100.0
      if total:
          perc /= float(cache_info.hits + cache_info.misses)
      print('Cache: hits {}, misses {}, used {}%.'.format(
          cache_info.hits, cache_info.misses, perc))
  
      # select closest parking areas
      sequence = None
      if parameters['with_tqdm']:
          from tqdm import tqdm
          sequence = tqdm(distances.items())
      else:
          sequence = distances.items()
  
      for pid, dists in sequence:
          list_of_dist = [tuple(reversed(x)) for x in dists.items() if x[1] is not None]
          list_of_dist = sorted(list_of_dist)
          temp_rerouters = [(pid, 0.0)]
  
          numAlternatives = min(len(list_of_dist), parameters['num_alternatives'])
  
          used = set()
          if parameters['prefer_visible']:
              for distance, parking in list_of_dist:
                  if parameters['all_parking_areas'][parking].get('capacity') < parameters['min_capacity']:
                      continue
                  if len(temp_rerouters) > parameters['num_alternatives']:
                      break
                  if isVisible(pid, parking, distance, sumo_net,
                               parameters['all_parking_areas'],
                               parameters['dist_threshold'],
                               parameters['capacity_threshold'],
                               parameters['opposite_visible'],
                               parameters['visible_ids']):
                      temp_rerouters.append((parking, distance))
                      used.add(parking)
  
          dist = None
          distThresholds = None
          if distWeights is not None:
              dist = [int(x / distWeightSum * numAlternatives) for x in distWeights]
              distThresholdIndex = [int(i * len(list_of_dist) / len(dist)) for i in range(len(dist))]
              distThresholds = [list_of_dist[i][0] for i in distThresholdIndex]
              # print("distWeights=%s" % distWeights)
              # print("dist=%s" % dist)
              # print("distances=%s" % [x[0] for x in list_of_dist])
              # print("distThresholdIndex=%s" % distThresholdIndex)
              # print("distThresholds=%s" % distThresholds)
  
          distIndex = 0
          found = 0
          required = dist[distIndex] if dist else None
          for distance, parking in list_of_dist:
              route = routes[pid][parking]
              if parking in used:
                  found += 1
                  continue
  
              if parameters['all_parking_areas'][parking].get('capacity') < parameters['min_capacity']:
                  continue
              # optionally enforce distance distribution
              # if dist is not None:
              #     print("found=%s required=%s distIndex=%s threshold=%s" % (found, required,
              #         distIndex, distThresholds[distIndex]))
  
              if dist is not None and found >= required:
                  if distIndex + 1 < len(dist):
                      distIndex += 1
                      required += dist[distIndex]
              if distThresholds is not None and distance < distThresholds[distIndex]:
                  continue
  
              if len(temp_rerouters) > parameters['num_alternatives']:
                  break
              if distance > parameters['dist_alternatives']:
                  break
              found += 1
              temp_rerouters.append((parking, distance))
  
          if not list_of_dist:
              print('Parking {} has 0 neighbours!'.format(pid))
  
          ret_rerouters[pid] = {
              'rid': pid,
              'edge': parameters['all_parking_areas'][pid]['edge'],
              'rerouters': temp_rerouters,
          }
      return ret_rerouters
function_tokens: def generate_rerouters_process ( parameters ) : sumo_net = sumolib . net . readNet ( parameters [ 'net_file' ] ) rtree = initRTree ( parameters [ 'all_parking_areas' ] ) ret_rerouters = dict ( ) @ functools . lru_cache ( maxsize = None ) def _cached_get_shortest_path ( from_edge , to_edge , fromPos , toPos ) : """ Calls and caches sumolib: net.getShortestPath. """ return sumo_net . getShortestPath ( from_edge , to_edge , fromPos = fromPos , toPos = toPos ) distances = collections . defaultdict ( dict ) routes = collections . defaultdict ( dict ) sequence = None if parameters [ 'with_tqdm' ] : from tqdm import tqdm sequence = tqdm ( parameters [ 'selection' ] ) else : sequence = parameters [ 'selection' ] distWeights = None distWeightSum = None distThresholds = None if parameters [ 'distribute' ] is not None : distWeights = list ( map ( float , parameters [ 'distribute' ] . split ( ',' ) ) ) distWeightSum = sum ( distWeights ) for parking_id in sequence : parking_a = parameters [ 'all_parking_areas' ] [ parking_id ] from_edge = sumo_net . getEdge ( parking_a [ 'edge' ] ) fromPos = float ( parking_a [ 'endPos' ] ) candidates = parameters [ 'all_parking_areas' ] . values ( ) if rtree is not None : allParkings = list ( candidates ) candidates = [ ] x , y = parking_a [ 'pos' ] r = parameters [ 'dist_alternatives' ] for i in rtree . intersection ( ( x - r , y - r , x + r , y + r ) ) : candidates . append ( allParkings [ i ] ) for parking_b in candidates : if parking_a [ 'id' ] == parking_b [ 'id' ] : continue toPos = float ( parking_b [ 'startPos' ] ) route , cost = _cached_get_shortest_path ( from_edge , sumo_net . getEdge ( parking_b [ 'edge' ] ) , fromPos , toPos ) if route : distances [ parking_a [ 'id' ] ] [ parking_b [ 'id' ] ] = cost routes [ parking_a [ 'id' ] ] [ parking_b [ 'id' ] ] = route cache_info = _cached_get_shortest_path . cache_info ( ) total = float ( cache_info . hits + cache_info . misses ) perc = cache_info . hits * 100.0 if total : perc /= float ( cache_info . hits + cache_info . misses ) print ( 'Cache: hits {}, misses {}, used {}%.' . format ( cache_info . hits , cache_info . misses , perc ) ) # select closest parking areas sequence = None if parameters [ 'with_tqdm' ] : from tqdm import tqdm sequence = tqdm ( distances . items ( ) ) else : sequence = distances . items ( ) for pid , dists in sequence : list_of_dist = [ tuple ( reversed ( x ) ) for x in dists . items ( ) if x [ 1 ] is not None ] list_of_dist = sorted ( list_of_dist ) temp_rerouters = [ ( pid , 0.0 ) ] numAlternatives = min ( len ( list_of_dist ) , parameters [ 'num_alternatives' ] ) used = set ( ) if parameters [ 'prefer_visible' ] : for distance , parking in list_of_dist : if parameters [ 'all_parking_areas' ] [ parking ] . get ( 'capacity' ) < parameters [ 'min_capacity' ] : continue if len ( temp_rerouters ) > parameters [ 'num_alternatives' ] : break if isVisible ( pid , parking , distance , sumo_net , parameters [ 'all_parking_areas' ] , parameters [ 'dist_threshold' ] , parameters [ 'capacity_threshold' ] , parameters [ 'opposite_visible' ] , parameters [ 'visible_ids' ] ) : temp_rerouters . append ( ( parking , distance ) ) used . add ( parking ) dist = None distThresholds = None if distWeights is not None : dist = [ int ( x / distWeightSum * numAlternatives ) for x in distWeights ] distThresholdIndex = [ int ( i * len ( list_of_dist ) / len ( dist ) ) for i in range ( len ( dist ) ) ] distThresholds = [ list_of_dist [ i ] [ 0 ] for i in distThresholdIndex ] # print("distWeights=%s" % distWeights) # print("dist=%s" % dist) # print("distances=%s" % [x[0] for x in list_of_dist]) # print("distThresholdIndex=%s" % distThresholdIndex) # print("distThresholds=%s" % distThresholds) distIndex = 0 found = 0 required = dist [ distIndex ] if dist else None for distance , parking in list_of_dist : route = routes [ pid ] [ parking ] if parking in used : found += 1 continue if parameters [ 'all_parking_areas' ] [ parking ] . get ( 'capacity' ) < parameters [ 'min_capacity' ] : continue # optionally enforce distance distribution # if dist is not None: #     print("found=%s required=%s distIndex=%s threshold=%s" % (found, required, #         distIndex, distThresholds[distIndex])) if dist is not None and found >= required : if distIndex + 1 < len ( dist ) : distIndex += 1 required += dist [ distIndex ] if distThresholds is not None and distance < distThresholds [ distIndex ] : continue if len ( temp_rerouters ) > parameters [ 'num_alternatives' ] : break if distance > parameters [ 'dist_alternatives' ] : break found += 1 temp_rerouters . append ( ( parking , distance ) ) if not list_of_dist : print ( 'Parking {} has 0 neighbours!' . format ( pid ) ) ret_rerouters [ pid ] = { 'rid' : pid , 'edge' : parameters [ 'all_parking_areas' ] [ pid ] [ 'edge' ] , 'rerouters' : temp_rerouters , } return ret_rerouters