use speed of agent to filter age of db records

This commit is contained in:
rgarcia-herrera 2021-11-01 14:34:50 -06:00
parent c7ec3c7b3a
commit 9feeb9c3d4
3 changed files with 36 additions and 15 deletions

View File

@ -8,7 +8,7 @@ This program is the server component of the [Social Cycling](https://gitlab.com/
Clone or download this repo. Clone or download this repo.
Install Python libraries, like so: Install Python libraries, like so:
$ pip install -r requirements.txt $ pip install -r requirements.txt
[...] [...]
### MongoDB setup ### MongoDB setup
@ -21,9 +21,9 @@ the **point** and **destination** fields.
[...] [...]
> use test > use test
switched to db test switched to db test
> db.bike.createIndex({point:"2dsphere"}); > db.bike.createIndex({point:"2dsphere"});
[...] [...]
> db.bike.createIndex({destination:"2dsphere"}); > db.bike.createIndex({destination:"2dsphere"});
[...] [...]
@ -36,11 +36,16 @@ dedicated webserver. Instructions for doing so are documented
For simplicity the app may be run with the included development For simplicity the app may be run with the included development
server, using the following commands: server, using the following commands:
$ export FLASK_APP=server.py $ export FLASK_APP=server.py
$ flask run $ flask run
* Serving Flask app "server.py" * Serving Flask app "server.py"
[...] [...]
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
## Instance at LANCIS
We're hosting an instance of this server at
http://flock.apps.lancis.ecologia.unam.mx/

View File

@ -9,8 +9,6 @@ from LatLon23 import LatLon, Latitude, Longitude
from geopy import distance from geopy import distance
# TODO: instead of centroid use speed and bearing of flockers to create a vector, point rider towards it
# use time elapsed since last update, speed vector and current point to compute future location
class Flock: class Flock:
""" """
Flock object is created from a list of agents Flock object is created from a list of agents
@ -92,17 +90,28 @@ class Bike(Document):
return (self.destination[1], return (self.destination[1],
self.destination[0]) self.destination[0])
def find_flock(self, point_altruism=0.1, dest_altruism=0.2): def find_flock(self,
point_altruism=0.1,
dest_altruism=0.2,
min_age=None):
""" """
min_age of agents in database, in seconds
:return: list of Bike objects :return: list of Bike objects
""" """
if min_age is None:
min_step = 100 # update at least every 100 meters
if self.speed > 0:
min_age = min_step / self.speed
else:
min_age = 60
trip_len = distance.geodesic(self.get_point(), self.get_dest()).meters trip_len = distance.geodesic(self.get_point(), self.get_dest()).meters
local_radius = trip_len * point_altruism local_radius = trip_len * point_altruism
destination_radius = trip_len * dest_altruism destination_radius = trip_len * dest_altruism
# these are bikes around me # these are bikes recently known to be around me
local = [bike for bike in local = [bike for bike in
self.db.bike.find( self.db.bike.find(
{'point': { {'point': {
@ -112,7 +121,7 @@ class Bike(Document):
'coordinates': self.point}, 'coordinates': self.point},
'$maxDistance': local_radius}}, '$maxDistance': local_radius}},
'last_update': { 'last_update': {
'$gte': datetime.datetime.now() - datetime.timedelta(minutes=15)}, # TODO: adjust this '$gte': datetime.datetime.now() - datetime.timedelta(seconds=min_age)},
'_id': {'$ne': bson.objectid.ObjectId(self.id)}})] '_id': {'$ne': bson.objectid.ObjectId(self.id)}})]
local_ids = set([bike['_id'] for bike in local]) local_ids = set([bike['_id'] for bike in local])
@ -121,10 +130,13 @@ class Bike(Document):
remote = [bike for bike in remote = [bike for bike in
self.db.bike.find( self.db.bike.find(
{'destination': {'destination':
{'$near': {'$near':
{'$geometry': {'type': 'Point', {'$geometry': {'type': 'Point',
'coordinates': self.destination}, 'coordinates': self.destination},
'$maxDistance': destination_radius}}})] '$maxDistance': destination_radius}},
'last_update': {
'$gte': datetime.datetime.now() - datetime.timedelta(seconds=min_age)}
})]
remote_ids = set([bike['_id'] for bike in remote]) remote_ids = set([bike['_id'] for bike in remote])
# intersect them! # intersect them!

View File

@ -37,6 +37,10 @@ def update(bike_id):
speed = float(request.args.get('speed', None)) speed = float(request.args.get('speed', None))
bearing = float(request.args.get('bearing', None)) bearing = float(request.args.get('bearing', None))
local_altruism = float(request.args.get('local_altruism', 0.1))
dest_altruism = float(request.args.get('dest_altruism', 0.2))
bike.update(lat, lon, speed, bearing) bike.update(lat, lon, speed, bearing)
return jsonify( return jsonify(