From 9feeb9c3d40c4fc288677ade70c62221219ac25b Mon Sep 17 00:00:00 2001 From: rgarcia-herrera Date: Mon, 1 Nov 2021 14:34:50 -0600 Subject: [PATCH] use speed of agent to filter age of db records --- README.md | 17 +++++++++++------ model.py | 30 +++++++++++++++++++++--------- server.py | 4 ++++ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a49b665..905396f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Flock Server -This program is the server component of the [Social Cycling](https://gitlab.com/rgarcia-herrera/social-cycling) mobile app. +This program is the server component of the [Social Cycling](https://gitlab.com/rgarcia-herrera/social-cycling) mobile app. ## Installation and running @@ -8,7 +8,7 @@ This program is the server component of the [Social Cycling](https://gitlab.com/ Clone or download this repo. Install Python libraries, like so: - $ pip install -r requirements.txt + $ pip install -r requirements.txt [...] ### MongoDB setup @@ -21,9 +21,9 @@ the **point** and **destination** fields. [...] > use 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 server, using the following commands: - $ export FLASK_APP=server.py - $ flask run + $ export FLASK_APP=server.py + $ flask run * Serving Flask app "server.py" [...] * 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/ diff --git a/model.py b/model.py index 8f5f48b..a2b8470 100644 --- a/model.py +++ b/model.py @@ -9,8 +9,6 @@ from LatLon23 import LatLon, Latitude, Longitude 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: """ Flock object is created from a list of agents @@ -92,17 +90,28 @@ class Bike(Document): return (self.destination[1], 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 """ + 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 local_radius = trip_len * point_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 self.db.bike.find( {'point': { @@ -112,7 +121,7 @@ class Bike(Document): 'coordinates': self.point}, '$maxDistance': local_radius}}, '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)}})] local_ids = set([bike['_id'] for bike in local]) @@ -121,10 +130,13 @@ class Bike(Document): remote = [bike for bike in self.db.bike.find( {'destination': - {'$near': - {'$geometry': {'type': 'Point', - 'coordinates': self.destination}, - '$maxDistance': destination_radius}}})] + {'$near': + {'$geometry': {'type': 'Point', + 'coordinates': self.destination}, + '$maxDistance': destination_radius}}, + 'last_update': { + '$gte': datetime.datetime.now() - datetime.timedelta(seconds=min_age)} + })] remote_ids = set([bike['_id'] for bike in remote]) # intersect them! diff --git a/server.py b/server.py index 413018c..8cc8f1f 100644 --- a/server.py +++ b/server.py @@ -37,6 +37,10 @@ def update(bike_id): speed = float(request.args.get('speed', 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) return jsonify(