Beispiel #1
0
def real_eg1():
    sig = 5
    # Algorithm parameters
    n = 100  # number of samples
    K = 100  # minimum number of candidate paths
    vmean = 10. # mean velocity
    vvar = 4. # velocity variance
    
    boundingboxbuffer_ratio = 2.0 
    z,obstimes,extent, endID = testdb.ecourier_vertex_data(8,boundingboxbuffer_ratio)
    print "measurements:"
    print z
    print "observation times"
    print obstimes
    G=testdb.london_roadmap(extent)

    #plot
    # position={}
    # for node in G:
    #   position[node]=G.node[node]['pos']
    # nx.draw(G,pos=position,node_size=50)
    # for point in z:
    #     plt.plot(point[0],point[1],'kx',markersize=10.0,markeredgewidth=2)
    # plt.show()

    t0 = time.time()
    [pp,sep,wp] = calcpostprobs(G,z,obstimes,n,K,vmean,vvar,sig*sig)
    et = time.time()-t0
    print et, "secs, done algorithm"
    print endID

    return pp, sep, wp, endID
Beispiel #2
0
def test_UTM():
    boundingboxbuffer_ratio = 2.0 
    z,obstimes,extent = testdb.ecourier_data(8,2,boundingboxbuffer_ratio)
    G=testdb.london_roadmap(extent)
    x0,y0 = z[0]
    dis=[]
    tmpx=G.node[315161]['pos'][0]
    tmpy=G.node[315161]['pos'][1]
    dis.append(math.sqrt((x0-tmpx)**2+(y0-tmpy)**2))
    print 'Min distance:',min(dis)
Beispiel #3
0
def real_eg1():
    
# Example to demonstrate interaction detection
    boundingboxratio = 0.1
    z1,obstimes1,z2,obstimes2,extent=testdb.ecourier_pair_data((72,'motorbike'),(44,'motorbike'),boundingboxratio)
    print "Found measurements."
    print obstimes1,obstimes2
    print z1
    print z2
    G=testdb.london_roadmap(extent)
    print "Graph generated. "
    # Environment parameters
    obstimebottom = max([obstimes1[0],obstimes2[0]])
    obstimetop = min([obstimes1[1],obstimes2[1]])
    print "path 1 length:", math.sqrt((z1[0][0]-z1[1][0])**2+(z1[1][1]-z1[0][1])**2)
    print "path 2 length:", math.sqrt((z2[0][0]-z2[1][0])**2+(z2[1][1]-z2[0][1])**2)
    print "obstimes1:",obstimes1
    print "obstimes2:",obstimes2
    # Measurement parameters
    sig = 5.
        
    # Algorithm parameters
    n = 100 # Sample size for path posterior
    n2 = 100    # sample size for position probability approximation
    K = 50 # Number of candidate paths
    # Movement statistics
    vmean = 36/3.6  
    vvar = 2.*2.
    
    # Times at which interaction is considered    
    ntpts = int(obstimetop-obstimebottom)-1
    tax = numpy.linspace(obstimebottom+1,obstimetop-1,ntpts)
    print "tax:",tax
    
    t0 = time.time()
    [H,P1,P2,s1,eidx1] = calcinter(G,z1,obstimes1,z2,obstimes2,n,n2,K,tax,1000,vmean,vvar,sig*sig)
    et = time.time()-t0
    print "Algorithm took "+str(et)+" seconds."
    return H,tax
Beispiel #4
0
def case1_vehicle(vehicleid):
	boundingboxratio=3.0
	sig = 5
	# Algorithm parameters
	n = 100  # number of samples
	K = 100  # minimum number of candidate paths
	vmean = 10. # mean velocity
	vvar = 4. # velocity variance

	conn = psycopg2.connect("dbname=gis user=postgres password=peach")
	cur = conn.cursor()
	pointsqlx ="ST_X(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	pointsqly ="ST_Y(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	cur.execute("select id,"+pointsqlx+","+pointsqly+", timestamp from ecourier_oneday a where vehicleid=%s and exists (select * from hh_2po_4pgr where st_dwithin(source_geom,a.geom,0.00014) or st_dwithin(target_geom,a.geom,0.00014)) order by timestamp",(vehicleid,))
	rows = cur.fetchall()
	mesnum = len(rows)
	results = []
	for i in range(mesnum-1):
		pid,x,y,timex=rows[i]
		startid = pid
		startpoint = numpy.array([x,y])
		starttime = timex
		pid,x,y,timex=rows[i+1]
		endid = pid
		endpoint = numpy.array([x,y])
		endtime = timex
		if 20<(endtime-starttime).total_seconds()<100:
			IDlist = [startid,endid]
			z=[startpoint,endpoint]
			t=[0.0,(endtime-starttime).total_seconds()]

			minx = startpoint[0] if startpoint[0]<endpoint[0] else endpoint[0]
			maxx = startpoint[0] if startpoint[0]>endpoint[0] else endpoint[0]
			miny = startpoint[1] if startpoint[1]<endpoint[1] else endpoint[1]
			maxy = startpoint[1] if startpoint[1]>endpoint[1] else endpoint[1]

			width=maxx-minx
			height=maxy-miny
			minx=minx-width*boundingboxratio/2.0
			maxx=maxx+width*boundingboxratio/2.0
			miny=miny-height*boundingboxratio/2.0
			maxy=maxy+height*boundingboxratio/2.0
			G=testdb.london_roadmap((minx,miny,maxx,maxy))
			if G.nodes():
				startvertex = nearest_vertex(G,startpoint)
				endvertex = nearest_vertex(G,endpoint)

			print z
			print t
			if len(G.nodes())>10 and nx.has_path(G,startvertex,endvertex) and startvertex!=endvertex and reasonable_distance(z[0],z[1]):

				# position={}
				# for node in G:
				#   position[node]=G.node[node]['pos']
				# nx.draw(G,pos=position,node_size=50)
				# for point in z:
				#     plt.plot(point[0],point[1],'kx',markersize=10.0,markeredgewidth=2)
				# plt.show()

				# try:
				t0 = time.time()
				[pp,sep,wp] = case1.calcpostprobs(G,z,t,n,K,vmean,vvar,sig*sig)
				et = time.time()-t0
				print et, "secs, done algorithm"
				possibleroutes = len(pp)
				result=[]
				for j in range(possibleroutes):
					edge = get_edges(pp[j],cur)
					allmeasurements = get_allmeasurements(IDlist,vehicleid,cur)
					result.append({'route':pp[j],'possibility':wp[j],'rmse':RSME(edge,allmeasurements,cur),'rmse_temporal':temporal_RSME(edge,allmeasurements,pp[j][0],cur),'period':t[-1]})
				results.append(result)
				# except:
				# 	pass

	return results
def case1_vehicle(vehicleid,interval):
	boundingboxratio=0.5
	sig = 5
	# Algorithm parameters
	n = 200  # number of samples
	K = 50  # minimum number of candidate paths
	vmean = 6. # mean velocity
	vvar = 4. # velocity variance
	includetruepath=False# without inputting true path

	conn = psycopg2.connect("dbname=gis user=postgres password=peach")
	cur = conn.cursor()
	pointsqlx ="ST_X(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	pointsqly ="ST_Y(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	cur.execute("select id, timestamp,"+pointsqlx+","+pointsqly+" from near_vertex_measurement where vehicleid=%s and type=%s order by timestamp",vehicleid)
	vehicle_records = MeasureList([measurement for measurement in cur],interval)
	zl,tl,idl=vehicle_records.next()
	# mesnum = len(rows)
	results = []
	while zl:
		starttime=tl[0]
		t=[(item-starttime).total_seconds() for item in tl]
		allmeasurements = get_allmeasurements(idl,vehicleid,cur)
		allmeasurements_id = [measurement[0] for measurement in allmeasurements]
		addrow_query = "INSERT INTO testcases3(time_slot, start_num, end_num, measurements) VALUES (%s, %s, %s, %s) RETURNING test_id;"
		measurementsstr = "{%s}" % (','.join([str(m_id) for m_id in allmeasurements_id]),)
		cur.execute(addrow_query,(t[-1],idl[0],idl[-1],measurementsstr))
		testid = cur.fetchone()[0]
		conn.commit()

		minx, maxx, miny, maxy = cal_min_bbox(allmeasurements,cur)
		width=maxx-minx
		height=maxy-miny
		minx=minx-width*boundingboxratio/2.0
		maxx=maxx+width*boundingboxratio/2.0
		miny=miny-height*boundingboxratio/2.0
		maxy=maxy+height*boundingboxratio/2.0

		G=testdb.london_roadmap((minx,miny,maxx,maxy))
		if G.nodes():
			startvertex = nearest_vertex(G,zl[0])
			endvertex = nearest_vertex(G,zl[-1])

		print zl
		print t
		# large graph take too long time to process
		if nx.has_path(G,startvertex,endvertex) and startvertex!=endvertex:
			try:
				t0 = time.time()
				tedge,tpath = cal_route(allmeasurements_id,testid,cur)
				updateroute = "{%s}" % (','.join(tedge),)
				updatevertex = "{%s}" % (','.join([str(x) for x in tpath]),)
				cur.execute('''update testcases3 set gt_mm_edges=%s where test_id = %s''',(updateroute,testid))
				cur.execute('''update testcases3 set gt_mm_vertex=%s where test_id = %s''',(updatevertex,testid))
				conn.commit()
				[pp,sep,wp] = case1.calcpostprobs(G,zl,t,n,K,vmean,vvar,sig*sig,tpath if includetruepath else None)
				et = time.time()-t0
				print et, "secs, done algorithm"
				possibleroutes = len(pp)
				# print pp
				if allmeasurements and allmeasurements[0][0]==idl[0] and allmeasurements[-1][0]==idl[-1]:
					result=[]
					for j in range(possibleroutes):
						# print "modeled path:",pp[j]
						edge = get_edges(pp[j],cur)
						sarea = surrounding_area(edge,pp[j][0],cur,testid)

						temproute = {'route':pp[j],
							'possibility':wp[j],
							'RMSE':RSME(edge,allmeasurements,cur),
							'temporal_RMSE':temporal_RSME(edge,allmeasurements,pp[j][0],cur,allmeasurements_id,testid),
							'common_length':share_distance(edge,pp[j][0],cur,testid),
							'common_edges':common_edge(tedge,edge),
							'area':sarea,
							'edgeIDList':edge
							}
						result.append(temproute)
					newtest = Test(result)
					newtest.test_para = {'period':t[-1],
						'measurements':allmeasurements_id,
						"tpath":tpath,
						"tedge":tedge,
						"vehicleid":vehicleid,
						"test_id":testid,
						'v':len(G.nodes()),
						'e':len(G.edges())}
					results.append(newtest)
					# conn.commit()
					if len(results)>30:
						return results
					else:
						print "Success.************************", len(results)
				else:
					print "vertx edge table error",idl,allmeasurements
			except:
				conn.rollback()
				print "FAIL............................."
		zl,tl,idl=vehicle_records.next()
	return results
Beispiel #6
0
def case1_vehicle(interval):
	sig = 5
	# Algorithm parameters
	n = 100  # number of samples
	K = 100  # minimum number of candidate paths
	vmean = 10. # mean velocity
	vvar = 4. # velocity variance

	conn = psycopg2.connect("dbname=gis user=postgres password=peach")
	cur = conn.cursor()

	cur.execute('''select measurements, gt_manul_edges, test_id
		from testcases
		where time_slot=%s and gt_manul_edges != '{}'
		''', (interval,))

	tests = cur.fetchall()
	results = []
	for test in tests:
		allmeasurements_id = test[0]
		manul_route = test[1]
		locations,timestamp = get_exact_record(allmeasurements_id,cur)
		zl = [locations[0],locations[-1]]
		t = [timestamp[0],timestamp[-1]]
		allmeasurements = get_allmeasurements(allmeasurements_id,cur)

		G= testdb.london_roadmap(allmeasurements_id)
		if G.nodes():
			startvertex = nearest_vertex(G,zl[0])
			endvertex = nearest_vertex(G,zl[-1])

		print zl
		print t

		if len(G.edges())!=501 and nx.has_path(G,startvertex,endvertex) and startvertex!=endvertex:
			try:
				t0 = time.time()
				[pp,sep,wp] = case1.calcpostprobs(G,zl,t,n,K,vmean,vvar,sig*sig)
				et = time.time()-t0
				print et, "secs, done algorithm"
				possibleroutes = len(pp)
				
				if allmeasurements:
					# tpath = real_path(G,allmeasurements,startvertex,endvertex,cur)
					# print "TRUE PATH", tpath
					# tedge = get_edges(tpath,cur)
					tedge = manul_route
					result=[]
					for j in range(possibleroutes):
						# print "modeled path:",pp[j]
						edge = get_edges(pp[j],cur)
						try:
							area = surrounding_area(edge,pp[j][0],tedge,startvertex,cur)
						except:
							conn.rollback()
							area = surrounding_area(edge,pp[j][-1],tedge,startvertex,cur)
						result.append({'route':pp[j],
							'possibility':wp[j],
							'RMSE':RSME(edge,allmeasurements,cur),
							'temporal_RMSE':temporal_RSME(edge,allmeasurements,pp[j][0],cur,tedge),
							'area':area,
							'common_length':share_distance(edge,pp[j][0],tedge,startvertex,cur),
							'edgeIDList':edge
							})
					newtest = Test(result)
					newtest.test_para = {'period':t[-1],"test_id":test[2]}
					results.append(newtest)
					print "Success.************************", len(results)
				else:
					print "vertx edge table error",idl,allmeasurements
			except:
				conn.rollback()
				print "FAIL............................."
	return results
def case1_vehicle(vehicleid,interval):
	boundingboxratio=0.6
	sig = 5
	# Algorithm parameters
	n = 100  # number of samples
	K = 100  # minimum number of candidate paths
	vmean = 10. # mean velocity
	vvar = 4. # velocity variance

	conn = psycopg2.connect("dbname=gis user=postgres password=peach")
	cur = conn.cursor()
	pointsqlx ="ST_X(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	pointsqly ="ST_Y(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	cur.execute("select id, timestamp,"+pointsqlx+","+pointsqly+" from near_vertex_measurement where vehicleid=%s and type=%s order by timestamp",vehicleid)
	vehicle_records = MeasureList([measurement for measurement in cur],interval)
	zl,tl,idl=vehicle_records.next()
	# mesnum = len(rows)
	results = []
	while zl:
		starttime=tl[0]
		t=[(item-starttime).total_seconds() for item in tl]
		allmeasurements = get_allmeasurements(idl,vehicleid,cur)

		minx, maxx, miny, maxy = cal_min_bbox(allmeasurements,cur)
		width=maxx-minx
		height=maxy-miny
		minx=minx-width*boundingboxratio/2.0
		maxx=maxx+width*boundingboxratio/2.0
		miny=miny-height*boundingboxratio/2.0
		maxy=maxy+height*boundingboxratio/2.0

		G=testdb.london_roadmap((minx,miny,maxx,maxy))
		if G.nodes():
			startvertex = nearest_vertex(G,zl[0])
			endvertex = nearest_vertex(G,zl[-1])

		print zl
		print t
		# large graph take too long time to process
		if 5000>len(G.nodes())>10 and nx.has_path(G,startvertex,endvertex) and startvertex!=endvertex:
			try:
				t0 = time.time()
				[pp,sep,wp] = case1.calcpostprobs(G,zl,t,n,K,vmean,vvar,sig*sig)
				et = time.time()-t0
				print et, "secs, done algorithm"
				possibleroutes = len(pp)
				
				if allmeasurements and allmeasurements[0][0]==idl[0] and allmeasurements[-1][0]==idl[-1]:
					tpath = real_path(G,allmeasurements,startvertex,endvertex,cur)
					# print "TRUE PATH", tpath
					tedge = get_edges(tpath,cur)
					result=[]
					for j in range(possibleroutes):
						# print "modeled path:",pp[j]
						edge = get_edges(pp[j],cur)
						try:
							area = surrounding_area(edge,pp[j][0],tedge,startvertex,cur)
						except:
							conn.rollback()
							area = surrounding_area(edge,pp[j][-1],tedge,startvertex,cur)
						result.append({'route':pp[j],
							'possibility':wp[j],
							'RMSE':RSME(edge,allmeasurements,cur),
							'temporal_RMSE':temporal_RSME(edge,allmeasurements,pp[j][0],cur),
							'area':area,
							'common_length':share_distance(edge,pp[j][0],tedge,startvertex,cur),
							'edgeIDList':edge
							})
					newtest = Test(result)
					newtest.test_para = {'period':t[-1],'measurements':[x[0] for x in allmeasurements],"tpath":tpath,"tedge":tedge,"vehicleid":vehicleid}
					results.append(newtest)
					if len(results)>30:
						return results
					else:
						print "Success.************************", len(results)
				else:
					print "vertx edge table error",idl,allmeasurements
			except:
				conn.rollback()
				print "FAIL............................."
		zl,tl,idl=vehicle_records.next()
	return results