Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/safety-analysis.py @ 614:5e09583275a4
Merged Nicolas/trafficintelligence into default
| author | Mohamed Gomaa <eng.m.gom3a@gmail.com> |
|---|---|
| date | Fri, 05 Dec 2014 12:13:53 -0500 |
| parents | 806df5f61c03 |
| children | 6d89520e269f |
comparison
equal
deleted
inserted
replaced
| 598:11f96bd08552 | 614:5e09583275a4 |
|---|---|
| 1 #! /usr/bin/env python | |
| 2 | |
| 3 import storage, prediction, events, moving | |
| 4 | |
| 5 import sys, argparse, random | |
| 6 | |
| 7 import matplotlib.pyplot as plt | |
| 8 import numpy as np | |
| 9 | |
| 10 # todo: very slow if too many predicted trajectories | |
| 11 # add computation of probality of unsucessful evasive action | |
| 12 | |
| 13 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene') | |
| 14 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True) | |
| 15 parser.add_argument('--prediction-method', dest = 'predictionMethod', help = 'prediction method (constant velocity (vector computation), constant velocity, normal adaptation, point set prediction)', choices = ['cvd', 'cv', 'na', 'ps']) | |
| 16 parser.add_argument('--display-cp', dest = 'displayCollisionPoints', help = 'display collision points', action = 'store_true') | |
| 17 parser.add_argument('-n', dest = 'nProcesses', help = 'number of processes to run in parallel', type = int, default = 1) | |
| 18 args = parser.parse_args() | |
| 19 | |
| 20 params = storage.ProcessParameters(args.configFilename) | |
| 21 | |
| 22 # parameters for prediction methods | |
| 23 if args.predictionMethod: | |
| 24 predictionMethod = args.predictionMethod | |
| 25 else: | |
| 26 predictionMethod = params.predictionMethod | |
| 27 | |
| 28 def accelerationDistribution(): | |
| 29 return random.triangular(-params.maxNormalAcceleration, params.maxNormalAcceleration, 0.) | |
| 30 def steeringDistribution(): | |
| 31 return random.triangular(-params.maxNormalSteering, params.maxNormalSteering, 0.) | |
| 32 | |
| 33 if predictionMethod == 'cvd': # TODO add cve: constant velocity exact (Sohail's) | |
| 34 predictionParameters = prediction.CVDirectPredictionParameters() | |
| 35 elif predictionMethod == 'cv': | |
| 36 predictionParameters = prediction.ConstantPredictionParameters(params.maxPredictedSpeed) | |
| 37 elif predictionMethod == 'na': | |
| 38 predictionParameters = prediction.NormalAdaptationPredictionParameters(params.maxPredictedSpeed, | |
| 39 params.nPredictedTrajectories, | |
| 40 accelerationDistribution, | |
| 41 steeringDistribution, | |
| 42 params.useFeaturesForPrediction) | |
| 43 elif predictionMethod == 'ps': | |
| 44 predictionParameters = prediction.PointSetPredictionParameters(params.maxPredictedSpeed) | |
| 45 # no else required, since parameters is required as argument | |
| 46 | |
| 47 # evasiveActionPredictionParameters = prediction.EvasiveActionPredictionParameters(params.maxPredictedSpeed, | |
| 48 # params.nPredictedTrajectories, | |
| 49 # params.minExtremeAcceleration, | |
| 50 # params.maxExtremeAcceleration, | |
| 51 # params.maxExtremeSteering, | |
| 52 # params.useFeaturesForPrediction) | |
| 53 | |
| 54 objects = storage.loadTrajectoriesFromSqlite(params.databaseFilename,'object') | |
| 55 if params.useFeaturesForPrediction: | |
| 56 features = storage.loadTrajectoriesFromSqlite(params.databaseFilename,'feature') # needed if normal adaptation | |
| 57 for obj in objects: | |
| 58 obj.setFeatures(features) | |
| 59 | |
| 60 interactions = events.createInteractions(objects) | |
| 61 for inter in interactions: | |
| 62 inter.computeIndicators() | |
| 63 inter.computeCrossingsCollisions(predictionParameters, params.collisionDistance, params.predictionTimeHorizon, params.crossingZones, nProcesses = args.nProcesses) | |
| 64 | |
| 65 storage.saveIndicators(params.databaseFilename, interactions) | |
| 66 | |
| 67 if args.displayCollisionPoints: | |
| 68 plt.figure() | |
| 69 allCollisionPoints = [] | |
| 70 for inter in interactions: | |
| 71 for collisionPoints in inter.collisionPoints.values(): | |
| 72 allCollisionPoints += collisionPoints | |
| 73 moving.Point.plotAll(allCollisionPoints) | |
| 74 plt.axis('equal') | |
| 75 |
