def __compute_distance_matrix(self, road_network): """ Compute the grid distance matrix. :param road_network: RoadNetwork :return: None """ # Double-scan all the grid cells. for i in self.grid: anchor_i = self.grid[i].anchor anchor_i_location = road_network.get_vertex(anchor_i).location come_from = single_source_dijkstra(road_network, anchor_i) self.grid_distance_matrix[i] = dict() for j in self.grid: anchor_j = self.grid[j].anchor anchor_j_location = road_network.get_vertex(anchor_j).location d = get_distance(anchor_i_location, anchor_j_location) # the spatial distance shortest_path = construct_path(road_network, anchor_i, anchor_j, come_from) if len(shortest_path.vertex_list) == 0: t = d / AVERAGE_SPEED else: t = shortest_path.distance / AVERAGE_SPEED # the temporal distance # Put (d, t) into the grid distance matrix. matrix_cell = MatrixCell(d, t) self.grid_distance_matrix[i][j] = matrix_cell
def map_match(query_id, location, is_origin, road_network, database): """ Find the best matched vertex in the road network for the location of a query. :param query_id: id of a query :param location: location of a query :param is_origin: bool param indicates that if the location is an origin of a query :param road_network: the road network :param database: the spatio-temporal database :type query_id: int :type location: Location :type is_origin: bool :type road_network: RoadNetwork :type database: SpatioTemporalDatabase :return: a ScheduleNode :rtype: ScheduleNode """ geohash = location.geohash matched_vid = None min_dis = float('inf') # Scan all the vertex in the grid and pick the one closest to the location. for v_id in database.grid[geohash].vertex_list: vertex = road_network.get_vertex(v_id) v_loc = vertex.location dis = get_distance(v_loc, location) if dis < min_dis: matched_vid = v_id min_dis = dis # Create the ScheduleNode. schedule_node = ScheduleNode(query_id, is_origin, matched_vid) return schedule_node
def get_straight_distance(self, start_vid, end_vid): """ Return the straight-line distance between vertex start_vid and vertex end_vid. :param start_vid: int :param end_vid: int :return: float """ v_start = self.get_vertex(start_vid) v_end = self.get_vertex(end_vid) return get_distance(v_start.location, v_end.location)
def work_stop(local_ses, new_rcp_point): all_stops = get_map(local_ses, new_rcp_point) #maps = pokemon_pb2.maps() maps = pokemon_pb2.maps_1() #try: maps.ParseFromString(all_stops) #except: # print '[-] map bad' # work_stop(local_ses,new_rcp_point) if try_item: print '[+] deleting 9 pokeballs' info_prot = logic.delete_items(local_ses.ses, 1, 9) tmp_api = api.use_api(new_rcp_point, info_prot) exit() else: if show_pok: catch(maps, local_ses, new_rcp_point) else: data_list = location.get_near(maps) data_list = sorted(data_list, key=lambda x: x[3]) if len(data_list) > 0: print '[+] found: %s Pokestops near you' % (len(data_list), ) if local_ses is not None and data_list is not None: print '[+] starting show' if multi: a, b = split_list(data_list) p = Process(target=work_half_list, args=(a, local_ses.ses, new_rcp_point)) o = Process(target=work_half_list, args=(a, local_ses.ses, new_rcp_point)) p.start() o.start() p.join() o.join() print '[!] farming done..' else: for t in data_list: #print t[0] #print t[1],t[2] #print location.get_lat(),location.get_lot() #print location.l2f(t[1]),location.l2f(t[2]) #print location.l2f(location.get_lat()),location.l2f(location.get_lot()) #print location.get_distance(location.get_lat(),location.get_lot(),t[1],t[2]) print print '[!] %s m away' % (round( location.get_distance(location.get_lat(), location.get_lot(), t[1], t[2]), 3), ) if config.debug: print '[!] farming pokestop..' work_with_stops(t, local_ses.ses, new_rcp_point) else: walk_random()
def work_stop(local_ses,new_rcp_point): all_stops=get_map(local_ses,new_rcp_point) #maps = pokemon_pb2.maps() maps = pokemon_pb2.maps_1() #try: maps.ParseFromString(all_stops) #except: # print '[-] map bad' # work_stop(local_ses,new_rcp_point) if try_item: print '[+] deleting 9 pokeballs' info_prot= logic.delete_items(local_ses.ses,1,9) tmp_api=api.use_api(new_rcp_point,info_prot) exit() else: if show_pok: catch(maps,local_ses,new_rcp_point) else: data_list=location.get_near(maps) data_list = sorted(data_list, key = lambda x: x[3]) if len(data_list)>0: print '[+] found: %s Pokestops near you'%(len(data_list),) if local_ses is not None and data_list is not None: print '[+] starting show' if multi: a,b=split_list(data_list) p = Process(target=work_half_list, args=(a,local_ses.ses,new_rcp_point)) o = Process(target=work_half_list, args=(a,local_ses.ses,new_rcp_point)) p.start() o.start() p.join() o.join() print '[!] farming done..' else: for t in data_list: #print t[0] #print t[1],t[2] #print location.get_lat(),location.get_lot() #print location.l2f(t[1]),location.l2f(t[2]) #print location.l2f(location.get_lat()),location.l2f(location.get_lot()) #print location.get_distance(location.get_lat(),location.get_lot(),t[1],t[2]) print print '[!] %s m away'%(round(location.get_distance(location.get_lat(),location.get_lot(),t[1],t[2]),3),) if config.debug: print '[!] farming pokestop..' work_with_stops(t,local_ses.ses,new_rcp_point) else: walk_random()
def __init__(self, identifier, timestamp, origin, destination, o_schedule_node=None, d_schedule_node=None): """ Initialize a Query. In practice, a passenger only needs to explicitly indicate Query.destination, as most information of a query can be automatically obtained from a passenger's mobile phone, e.g., Query.origin and Query.timestamp. :param identifier: id of a query :param timestamp: current timestamp of the simulation :param origin: location of the origin :param destination: location of the destination :type identifier: int :type timestamp: int :type origin: Location :type destination: Location :type o_schedule_node: ScheduleNode :type d_schedule_node: ScheduleNode :return: None """ self.id = identifier self.timestamp = timestamp self.origin = origin self.destination = destination self.o_geohash = geo_encode(origin.lat, origin.lon, PRECISION) self.d_geohash = geo_encode(destination.lat, destination.lon, PRECISION) self.o_schedule_node = o_schedule_node self.d_schedule_node = d_schedule_node self.pickup_window = TimeWindow( timestamp, timestamp + PATIENCE) # @type pickup_window: TimeWindow dist_o_2_d = get_distance(self.origin, self.destination) expected_travel_time = (dist_o_2_d / AVERAGE_SPEED) * 2 self.delivery_window = TimeWindow( timestamp, timestamp + expected_travel_time + PATIENCE) # @type delivery_window: TimeWindow self.matched_taxi = None # @type matched_taxi: int self.status = WAITING self.waiting_time = 0
def find_taxi_nearest_vertex(self, taxi, database, road_network): ''' find the neareast vertex to the taxi :param taxi: :param database: :param road_network: :return: vertex_id ''' neareast_vertex = -1 min_dist = float('inf') for ver_id in database.grid[taxi.geohash].vertex_list: vertex = road_network.get_vertex(ver_id) dist = get_distance(taxi.location, vertex.location) if dist <= min_dist: neareast_vertex = ver_id min_dist = dist return neareast_vertex
def time_taxi_to_location(self, taxi, vertex_id, road_network, database): ''' :param taxi: Taxi :param vertex: query.o_schedule_node.matched_vid/ query.d_schedule_nde.matched_vid :param road_network: :param database: :return: time from taxi to query.o_schedule_node/d_schedule_node ''' #computer the distance from neareast vertex to taxi ver_id_2taxi = self.find_taxi_nearest_vertex(taxi, database, road_network) ver_2taxi = road_network.get_vertex(ver_id_2taxi) taxi_vertex_dist = get_distance(ver_2taxi.location, taxi.location) # compute dist from vertex_neareast_taxi to vertex_neareast_loc dist_v2v = get_shortest_path(road_network, ver_id_2taxi, vertex_id).distance time_taxi_vertex = (taxi_vertex_dist + dist_v2v) / AVERAGE_SPEED return time_taxi_vertex
def __determine_anchor(self, road_network): """ Determine the anchor of all grid cells. :param road_network: RoadNetwork :return: None """ for geohash in self.grid: center_location = self.grid[geohash].center_location anchor = None min_dis = float('inf') # Scan all the vertexes in the grid and pick the one closest to the center location. for v_id in self.grid[geohash].vertex_list: vertex = road_network.get_vertex(v_id) location = vertex.location dis = get_distance(location, center_location) if dis < min_dis: anchor = v_id min_dis = dis self.grid[geohash].anchor = anchor
def __schedule(self, query, candi_taxi_list, taxi_set, road_network): """ Taxi scheduling. The purpose of scheduling is to insert the origin and destination (ScheduleNode) of the query into the schedule of the taxi which satisfies the query with minimum additional travel distance. :param query: the query :param candi_taxi_list: list of taxi id :param taxi_set: the taxi set :param road_network: the road network :type query: Query :type candi_taxi_list: list[int] :type taxi_set: dict[int, Taxi] :type road_network: RoadNetwork :return: if the dispatch is successful or not :rtype: bool """ if len(candi_taxi_list) == 0: return False picked_taxi_id = candi_taxi_list[0] min_dis = MAX_INT for taxi_id in candi_taxi_list: taxi = taxi_set[taxi_id] if not taxi.is_available(): continue dis = get_distance(taxi.location, query.origin) if dis < min_dis: picked_taxi_id = taxi_id min_dis = dis picked_taxi = taxi_set[picked_taxi_id] query.matched_taxi = picked_taxi_id picked_taxi.schedule.append(query.o_schedule_node) picked_taxi.schedule.append(query.d_schedule_node) picked_taxi.update_route(road_network) return True
def catch(maps, local_ses, new_rcp_point): data_list = location.get_near_p(maps) data_list = sorted(data_list, key=lambda x: x[5]) if len(data_list) > 0: print print '[!] found %s pokemon' % (len(data_list), ) for idx, e in enumerate(data_list): if (e[0] not in config.list_banned_Pokemon): #Banned Pokemon print '[!] %s Type:%s its %s m away' % ( idx, e[0], e[len(e) - 1], ) print for idx, pok in enumerate(data_list): if (pok[0] not in config.list_banned_Pokemon): #Banned Pokemon #print '[!] %s Type:%s its %s m away'%(idx,pok[0],pok[len(pok)-1],) #if pok[0] < 22: if pok[len(pok) - 1] < 600: print '[!] Trying to catch Type:%s its %s m away' % ( pok[0], round( location.get_distance(location.get_lat(), location.get_lot(), pok[1], pok[2]), 3), ) lat1 = location.l2f(location.get_lat()) lot1 = location.l2f(location.get_lot()) lat2 = location.l2f(pok[1]) lot2 = location.l2f(pok[2]) #print location.l2f(lat1),location.l2f(lot1),location.l2f(lat2),location.l2f(lot2) if (lat1 > lat2): while (lat1 < lat2): lat1 = lat1 - config.steps location.set_lat(lat1) location.set_lot(lot1) info_prot = logic.get_info(local_ses.ses, pok) tmp_api = api.use_api(new_rcp_point, info_prot) time.sleep(2) else: while (lat1 < lat2): lat1 = lat1 + config.steps location.set_lat(lat1) location.set_lot(lot1) info_prot = logic.get_info(local_ses.ses, pok) tmp_api = api.use_api(new_rcp_point, info_prot) time.sleep(2) if (lot1 > lot2): while (lot1 > lot2): lot1 = lot1 - config.steps location.set_lat(lat1) location.set_lot(lot1) info_prot = logic.get_info(local_ses.ses, pok) tmp_api = api.use_api(new_rcp_point, info_prot) time.sleep(2) else: while (lot2 > lot1): lot1 = lot1 + config.steps location.set_lat(lat1) location.set_lot(lot1) info_prot = logic.get_info(local_ses.ses, pok) tmp_api = api.use_api(new_rcp_point, info_prot) time.sleep(2) #catch_prot= logic.catch_it(local_ses.ses,pok) #tmp_api=api.use_api(new_rcp_point,catch_prot) tmp_api = catch_t(local_ses, pok, new_rcp_point) if tmp_api is not None: catch_status = pokemon_pb2.catch_status() catch_status.ParseFromString(tmp_api) if catch_status.sess[0].status: print "[+] " + datetime.datetime.now().strftime( "%H:%M:%S") + " caught pok... %s" % ( catch_status.sess[0].status, ) else: print '[-] catch data is none' #exit() #exit() #walk_random() walk_random()
def summarize(thistask, club_id, sources, status, summaryfile, detailfile, resultsurl, minage=12, minagegrade=20, minraces=3 , mintrend=2, numyears=3, begindate=None, enddate=None): #---------------------------------------------------------------------- ''' render collected results :param thistask: this is required for task thistask.update_state() :param club_id: identifies club for which results are to be stored :param sources: list of sources / services we're keeping status for :param summaryfile: summary file name (.csv) :param detailfile: detail file name (.csv) :param resultsurl: base url to send results to, for link in summary table :param minage: minimum age to keep track of stats :param minagegrade: minimum age grade :param minraces: minimum races in the same year as enddate :param mintrend: minimum races over the full period for trendline :param begindate: render races between begindate and enddate, datetime :param enddate: render races between begindate and enddate, datetime ''' # get club slug and location for later club = Club.query.filter_by(id=club_id).first() clubslug = club.shname locsvr = LocationServer() clublocation = locsvr.getlocation(club.location) # get maxdistance by service services = RaceResultService.query.filter_by(club_id=club_id).join(ApiCredentials).all() maxdistance = {} for service in services: attrs = ServiceAttributes(club_id, service.apicredentials.name) # app.logger.debug('service {} attrs {}'.format(service, attrs.__dict__)) if attrs.maxdistance: maxdistance[service.apicredentials.name] = attrs.maxdistance else: maxdistance[service.apicredentials.name] = None maxdistance[productname] = None # set up date range. begindate and enddate take precedence, else use numyears from today if not (begindate and enddate): etoday = time.time() today = timeu.epoch2dt(etoday) begindate = datetime(today.year-numyears+1,1,1) enddate = datetime(today.year,12,31) firstyear = begindate.year lastyear = enddate.year yearrange = range(firstyear,lastyear+1) # get all the requested result data from the database and save in a data structure indexed by runner ## first get the data from the database results = RaceResult.query.join(Race).join(Runner).filter(RaceResult.club_id==club_id, Race.date.between(ftime.dt2asc(begindate), ftime.dt2asc(enddate)), Runner.member==True, Runner.active==True).order_by(Runner.lname, Runner.fname).all() ## then set up our status and pass to the front end for source in sources: status[source]['status'] = 'summarizing' status[source]['lastname'] = '' status[source]['processed'] = 0 status[source]['total'] = sum([1 for result in results if result.source==source]) thistask.update_state(state='PROGRESS', meta={'progress':status}) ## prepare to save detail file, for debugging detlfields = 'runnername,runnerid,dob,gender,resultid,racename,racedate,series,distmiles,distkm,time,timesecs,agpercent,source,sourceid'.split(',') detailfname = detailfile _DETL = open(detailfname,'wb') DETL = csv.DictWriter(_DETL,detlfields) DETL.writeheader() ## then fill in data structure to hold AnalyzeAgeGrade objects ## use OrderedDict to force aag to be in same order as DETL file, for debugging aag = collections.OrderedDict() for result in results: # skip results which are too far away, if a maxdistance is defined for this source if maxdistance[result.source]: locationid = result.race.locationid if not locationid: continue racelocation = Location.query.filter_by(id=locationid).first() distance = get_distance(clublocation, racelocation) if distance == None or distance > maxdistance[result.source]: continue thisname = (result.runner.name.lower(), result.runner.dateofbirth) initaagrunner(aag, thisname, result.runner.fname, result.runner.lname, result.runner.gender, ftime.asc2dt(result.runner.dateofbirth), result.runner.id) # determine location name. any error gets null string locationname = '' if result.race.locationid: location = Location.query.filter_by(id=result.race.locationid).first() if location: locationname = location.name thisstat = aag[thisname].add_stat(ftime.asc2dt(result.race.date), result.race.distance*METERSPERMILE, result.time, race=result.race.name, loc=locationname, fuzzyage=result.fuzzyage, source=result.source, priority=priority[result.source]) ### TODO: store result's agpercent, in AgeGrade.crunch() skip agegrade calculation if already present DETL.writerow(dict( runnername = result.runner.name, runnerid = result.runner.id, dob = result.runner.dateofbirth, gender = result.runner.gender, resultid = result.id, racename = result.race.name, racedate = result.race.date, series = result.series.name if result.seriesid else None, distmiles = result.race.distance, distkm = result.race.distance*(METERSPERMILE/1000), timesecs = result.time, time = rendertime(result.time,0), agpercent = result.agpercent, source = result.source, sourceid = result.sourceid, )) ## close detail file _DETL.close() # initialize summary file summfields = ['name', 'lname', 'fname', 'age', 'gender'] datafields = copy(summfields) distcategories = ['overall'] + [TRENDLIMITS[tlimit][0] for tlimit in TRENDLIMITS] datacategories = ['overall'] + [TRENDLIMITS[tlimit][1] for tlimit in TRENDLIMITS] stattypes = ['1yr agegrade','avg agegrade','trend','numraces','stderr','r-squared','pvalue'] statdatatypes = ['1yr-agegrade','avg-agegrade','trend','numraces','stderr','r-squared','pvalue'] for stattype, statdatatype in zip(stattypes, statdatatypes): for distcategory, datacategory in zip(distcategories, datacategories): summfields.append('{}\n{}'.format(stattype, distcategory)) datafields.append('{}-{}'.format(statdatatype, datacategory)) if stattype == 'numraces': for year in yearrange: summfields.append('{}\n{}'.format(stattype, year)) datafields.append('{}-{}'.format(statdatatype, lastyear-year)) # save summary file columns for resultsanalysissummary dtcolumns = json.dumps([{ 'data':d, 'name':d, 'label':l } for d,l in zip(datafields, summfields)]) columnsfilename = summaryfile + '.cols' with open(columnsfilename, 'w') as cols: cols.write(dtcolumns) # set up summary file summaryfname = summaryfile _SUMM = open(summaryfname,'wb') SUMM = csv.DictWriter(_SUMM,summfields) SUMM.writeheader() # loop through each member we've recorded information about for thisname in aag: fullname, fname, lname, gender, dob, runnerid = aag[thisname].get_runner() rendername = fullname.title() # check stats before deduplicating statcount = {} stats = aag[thisname].get_stats() for source in sources: statcount[source] = sum([1 for s in stats if s.source == source]) # remove duplicate entries aag[thisname].deduplicate() # crunch the numbers aag[thisname].crunch() # calculate age grade for each result stats = aag[thisname].get_stats() jan1 = ftime.asc2dt('{}-1-1'.format(lastyear)) runnerage = timeu.age(jan1, dob) # filter out runners younger than allowed if runnerage < minage: continue # filter out runners who have not run enough races stats = aag[thisname].get_stats() if enddate: lastyear = enddate.year else: lastyear = timeu.epoch2dt(time.time()).year lastyearstats = [s for s in stats if s.date.year==lastyear] if len(lastyearstats) < minraces: continue # fill in row for summary output summout = {} # get link for this runner's results chart # see http://stackoverflow.com/questions/2506379/add-params-to-given-url-in-python url_parts = list(urlparse(resultsurl)) query = dict(parse_qsl(url_parts[4])) query.update({'club': clubslug, 'runnerid': runnerid, 'begindate': ftime.dt2asc(begindate), 'enddate': ftime.dt2asc(enddate)}) url_parts[4] = urlencode(query) resultslink = urlunparse(url_parts) summout['name'] = '<a href={} target=_blank>{}</a>'.format(resultslink, rendername) summout['fname'] = fname summout['lname'] = lname summout['age'] = runnerage summout['gender'] = gender # set up to collect averages avg = collections.OrderedDict() # draw trendlines, write output allstats = aag[thisname].get_stats() if len(allstats) > 0: avg['overall'] = mean([s.ag for s in allstats]) trend = aag[thisname].get_trendline() oneyrstats = [s.ag for s in allstats if s.date.year == lastyear] if len(oneyrstats) > 0: summout['1yr agegrade\noverall'] = mean(oneyrstats) if len(allstats) > 0: summout['avg agegrade\noverall'] = avg['overall'] if len(allstats) >= mintrend and allstats[0].date != allstats[-1].date: summout['trend\noverall'] = trend.improvement summout['stderr\noverall'] = trend.stderr summout['r-squared\noverall'] = trend.r2**2 summout['pvalue\noverall'] = trend.pvalue summout['numraces\noverall'] = len(allstats) for year in yearrange: summout['numraces\n{}'.format(year)] = len([s for s in allstats if s.date.year==year]) for tlimit in TRENDLIMITS: distcategory,distcolor = TRENDLIMITS[tlimit] tstats = [s for s in allstats if s.dist >= tlimit[0] and s.dist < tlimit[1]] if len(tstats) > 0: avg[distcategory] = mean([s.ag for s in tstats]) summout['avg agegrade\n{}'.format(distcategory)] = avg[distcategory] summout['numraces\n{}'.format(distcategory)] = len(tstats) oneyrcategory = [s.ag for s in tstats if s.date.year == lastyear] if len(oneyrcategory) > 0: summout['1yr agegrade\n{}'.format(distcategory)] = mean(oneyrcategory) if len(tstats) >= mintrend and tstats[0].date != tstats[-1].date: try: trend = aag[thisname].get_trendline(thesestats=tstats) except ZeroDivisionError: app.logger.debug('ZeroDivisionError - processing {}'.format(rendername)) trend = None # ignore trends which can't be calculated if trend: summout['trend\n{}'.format(distcategory)] = trend.improvement summout['stderr\n{}'.format(distcategory)] = trend.stderr summout['r-squared\n{}'.format(distcategory)] = trend.r2 summout['pvalue\n{}'.format(distcategory)] = trend.pvalue SUMM.writerow(summout) # update status for source in sources: status[source]['processed'] += statcount[source] status[source]['lastname'] = rendername thistask.update_state(state='PROGRESS', meta={'progress':status}) _SUMM.close()
def catch(maps,local_ses,new_rcp_point): data_list=location.get_near_p(maps) data_list = sorted(data_list, key = lambda x: x[5]) if len(data_list)>0: print print '[!] found %s pokemon'%(len(data_list),) for idx, e in enumerate(data_list): if(e[0] not in config.list_banned_Pokemon): #Banned Pokemon print '[!] %s Type:%s its %s m away'%(idx,e[0],e[len(e)-1],) print for idx, pok in enumerate(data_list): if(pok[0] not in config.list_banned_Pokemon): #Banned Pokemon #print '[!] %s Type:%s its %s m away'%(idx,pok[0],pok[len(pok)-1],) #if pok[0] < 22: if pok[len(pok)-1] < 600: print '[!] Trying to catch Type:%s its %s m away'%(pok[0],round(location.get_distance(location.get_lat(),location.get_lot(),pok[1],pok[2]),3),) lat1=location.l2f(location.get_lat()) lot1=location.l2f(location.get_lot()) lat2=location.l2f(pok[1]) lot2=location.l2f(pok[2]) #print location.l2f(lat1),location.l2f(lot1),location.l2f(lat2),location.l2f(lot2) if (lat1>lat2): while(lat1<lat2): lat1=lat1-config.steps location.set_lat(lat1) location.set_lot(lot1) info_prot= logic.get_info(local_ses.ses,pok) tmp_api=api.use_api(new_rcp_point,info_prot) time.sleep(2) else: while(lat1<lat2): lat1=lat1+config.steps location.set_lat(lat1) location.set_lot(lot1) info_prot= logic.get_info(local_ses.ses,pok) tmp_api=api.use_api(new_rcp_point,info_prot) time.sleep(2) if (lot1>lot2): while(lot1>lot2): lot1=lot1-config.steps location.set_lat(lat1) location.set_lot(lot1) info_prot= logic.get_info(local_ses.ses,pok) tmp_api=api.use_api(new_rcp_point,info_prot) time.sleep(2) else: while(lot2>lot1): lot1=lot1+config.steps location.set_lat(lat1) location.set_lot(lot1) info_prot= logic.get_info(local_ses.ses,pok) tmp_api=api.use_api(new_rcp_point,info_prot) time.sleep(2) #catch_prot= logic.catch_it(local_ses.ses,pok) #tmp_api=api.use_api(new_rcp_point,catch_prot) tmp_api=catch_t(local_ses,pok,new_rcp_point) if tmp_api is not None: catch_status = pokemon_pb2.catch_status() catch_status.ParseFromString(tmp_api) if catch_status.sess[0].status: print "[+] " + datetime.datetime.now().strftime("%H:%M:%S")+ " caught pok... %s"%(catch_status.sess[0].status,) else: print '[-] catch data is none' #exit() #exit() #walk_random() walk_random()
def drive(self, timestamp, road_network, dispatcher, query_set, database): """ Simulate taxi's movement and update the status of the taxi. Including: 1. Update the new position of the taxi after a time step. 2. Update Some corresponding information such as Taxi.e_id, Taxi.schedule and Taxi.route. 3. Update stats such as Taxi.driving_distance. :param timestamp: current timestamp of the simulation system :param road_network: the road network :param dispatcher: the dispatcher :param query_set: the database of the query :param database: the s-t database :type timestamp: int :type road_network: RoadNetwork :type dispatcher: Dispatcher :type query_set: dict[Query] :type database: SpatioTemporalDatabase :return: None """ # First check if the taxi has any query to be done. if self.route is None or len(self.route.edge_list) == 0: return # Drive according to its route. d = self.speed * TIME_STEP # driving distance in a timestamp self.driving_distance += d self.schedule_distance -= d cur_edge = road_network.get_edge(self.e_id) to_vertex = road_network.get_vertex(cur_edge.end_vid) to_location = to_vertex.location theta = bearing(self.location, to_location) next_pos = end_pos(self.location, theta, d) e_start_vertex = road_network.get_vertex(cur_edge.start_vid) edge_offset = get_distance(e_start_vertex.location, next_pos) if edge_offset < cur_edge.weight: self.__update_pos(timestamp, next_pos, database) else: # The taxi has arrived at the end of the current edge. self.__update_pos(timestamp, to_location, database) self.v_id = road_network.get_edge(self.e_id).end_vid next_eid = self.__get_next_eid() if next_eid is not None: self.e_id = next_eid self.__eid_index += 1 else: # It is now the last edge of current route, which means the taxi arrives at the first ScheduleNode # in Taxi.schedule, so we need to re-compute a new route from current ScheduleNode to next ScheduleNode. schedule_node = self.schedule.pop(0) query = query_set[schedule_node.query_id] if schedule_node.is_origin: if query.status == WAITING: self.serve_query(query) dispatcher.add_serving_query(query) else: # delete the 'destination ScheduleNode' of the query in the schedule i = 0 for item in self.schedule: if item.query_id == query.id: self.schedule.pop(i) break i += 1 else: self.satisfy_query(query) dispatcher.add_completed_query(query) self.update_route(road_network, schedule_node) # update the route database.update_taxi_list(timestamp, self, self.route, road_network) # update the database
def insertion_feasibility_check(self, timestamp, query, o_index, d_index, database, candi_taxi, road_network, query_set, dist_between_schedule): ''' Return the distance and check the time constrain :param timestamp: :param query: :param o_index: :param d_index: :param database: :param candi_taxi: :param road_network: :param query_set: :return: distance of taxi.schedule ''' # print len(candi_taxi.schedule) time_taxi_o = self.time_taxi_to_location( candi_taxi, query.o_schedule_node.matched_vid, road_network, database) if timestamp + time_taxi_o > query.pickup_window.late or candi_taxi.num_riders >= 4: return [0, 0] cur_vertex = -1 min_dist = float('inf') pre_vertex_id = None total_dist = 0 cur_load = 0 flag_load = 0 arrive_time = timestamp index_shedule_node = -1 cur_riders = candi_taxi.num_riders for schedule_node in candi_taxi.schedule: # print "computing ......" index_shedule_node += 1 # print "index:",index_shedule_node if schedule_node.is_origin: cur_riders += 1 if cur_riders > candi_taxi.capacity: # print ".......capacity constraint........" return [0, 0] else: cur_riders -= 1 if (index_shedule_node == 0): total_dist += get_distance( candi_taxi.location, road_network.get_vertex( schedule_node.matched_vid).location) arrive_time = timestamp + total_dist / AVERAGE_SPEED if index_shedule_node == o_index: flag_load += 1 elif (index_shedule_node == o_index or index_shedule_node == d_index): path = get_shortest_path(road_network, pre_vertex_id, schedule_node.matched_vid) arrive_time += path.distance / AVERAGE_SPEED total_dist += path.distance if flag_load % 2 == 1: cur_load += path.distance flag_load += 1 elif (index_shedule_node == o_index + 1 or index_shedule_node == d_index + 1): path = get_shortest_path(road_network, pre_vertex_id, schedule_node.matched_vid) arrive_time += path.distance / AVERAGE_SPEED total_dist += path.distance if flag_load % 2 == 1: cur_load += path.distance elif (index_shedule_node > d_index): # print "taxi[%f]: order=>%f"%(candi_taxi.id, candi_taxi.num_order) # print "len(schedule)---index:", len(candi_taxi.schedule), " ", index_shedule_node # for node in candi_taxi.schedule: # print node # print "len_dist_between_schedule:", len(dist_between_schedule) arrive_time += dist_between_schedule[index_shedule_node - 3] / AVERAGE_SPEED total_dist += dist_between_schedule[index_shedule_node - 3] elif (index_shedule_node > o_index): arrive_time += dist_between_schedule[index_shedule_node - 2] / AVERAGE_SPEED total_dist += dist_between_schedule[index_shedule_node - 2] cur_load += dist_between_schedule[index_shedule_node - 2] else: arrive_time += dist_between_schedule[index_shedule_node - 1] / AVERAGE_SPEED total_dist += dist_between_schedule[index_shedule_node - 1] # print "index:", index_shedule_node, " dist:",total_dist # time.sleep(120) pre_vertex_id = schedule_node.matched_vid matched_query_id = schedule_node.query_id matched_query = query_set[matched_query_id] if schedule_node.is_origin: if arrive_time > matched_query.pickup_window.late: # print "......pickup_window......" return [0, 0] else: if arrive_time > matched_query.delivery_window.late: # print "[%f, %f]---[%f,%f]"%(matched_query.pickup_window.early, matched_query.pickup_window.late, matched_query.delivery_window.early, matched_query.delivery_window.late) # print "......deliver_window......",arrive_time return [0, 0] return [total_dist, cur_load]
def run(self): start_time = time.clock() query_num = len(self.query_set) print "query to be processed NUM:", query_num processed_order = 0 query_sum_dist = 0 waiting_queries = PriorityQueue() for timestamp in range(SIM_START_TIME, SIM_END_TIME + 1): print("Time: %d" % timestamp) sim_start_time = time.clock() # Catch the queries to be processed in this timestamp. The queries consists of two parts: # 1. queries that happened in this timestamp # 2. queries that stranded in previous timestamps while not self.query_queue.empty(): new_query = self.query_queue.get() if new_query.timestamp == timestamp: waiting_queries.put(new_query, new_query.timestamp) else: self.query_queue.put(new_query, new_query.timestamp) break while not self.dispatcher.failed_queries.empty(): old_query = self.dispatcher.failed_queries.get() waiting_queries.put(old_query, old_query.timestamp) # Process the queries. while not waiting_queries.empty(): query = waiting_queries.get() if query.status == CANCELLED: self.dispatcher.add_cancelled_query(query) else: if DEBUG_MODEL: print "......To process query:", query.id print "origin: %s ---> dest:%s" % (query.o_geohash, query.d_geohash) print "[%f, %f]----->[%f,%f]" % ( query.pickup_window.early, query.pickup_window.late, query.delivery_window.early, query.delivery_window.late) print "[%f, %f]----->[%f,%f]" % ( query.origin.lon, query.origin.lat, query.destination.lon, query.destination.lat) print "dist:", get_distance(query.origin, query.destination) flag_suc = self.dispatcher.dispatch_taxi( timestamp, query, self.db, self.taxi_set, self.road_network, self.query_set) if flag_suc: processed_order += 1 query_sum_dist += get_distance(query.origin, query.destination) print "order Num:", processed_order print "order Dist:", query_sum_dist if processed_order > 0 and processed_order % 100 == 0: self.print_load(processed_order) # Update the status of all the queries for query in self.query_set.values(): if query.timestamp <= timestamp and query.status == WAITING: query.update_status(timestamp) # All the taxis drive according to their schedule. for taxi in self.taxi_set.values(): taxi.drive(timestamp, self.road_network, self.dispatcher, self.query_set, self.db) print "Done in %f seconds" % (time.clock() - sim_start_time) [max, min, count] = self.print_utility() print("after:"), max - min, (" count:"), count print("satisfy ratio:"), self.print_satisfy(query_num) for load in self.maxLoad: print load, " " for load in self.sumLoad: print load, " " print("The simulation is end. Elapsed time is %f." % (time.clock() - start_time)) winsound.Beep(600, 1000)