Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/moving.py @ 1112:956a66096e91
removed code now available in simulation project, and issue with deprecated find function in matplotlib
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 05 Jun 2019 13:12:10 -0400 |
| parents | 345cd9cd62d8 |
| children | 7135b5eaa6b4 |
comparison
equal
deleted
inserted
replaced
| 1111:345cd9cd62d8 | 1112:956a66096e91 |
|---|---|
| 1290 positions = Trajectory.generate(p, v, nPoints) | 1290 positions = Trajectory.generate(p, v, nPoints) |
| 1291 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = Trajectory([[v.x]*nPoints, [v.y]*nPoints])) | 1291 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = Trajectory([[v.x]*nPoints, [v.y]*nPoints])) |
| 1292 | 1292 |
| 1293 def updatePositions(self): | 1293 def updatePositions(self): |
| 1294 inter, self.positions, self.velocities = MovingObject.aggregateTrajectories(self.features, self.getTimeInterval()) | 1294 inter, self.positions, self.velocities = MovingObject.aggregateTrajectories(self.features, self.getTimeInterval()) |
| 1295 | |
| 1296 def addNewellAttributes(self, desiredSpeed, tau, d, initialCumulatedHeadway, initialAlignmentIdx): | |
| 1297 '''adds attributes necessary for Newell car following model | |
| 1298 using curvilinear trajectories''' | |
| 1299 # Newell model parameters | |
| 1300 self.desiredSpeed = desiredSpeed | |
| 1301 self.tau = tau | |
| 1302 self.d = d | |
| 1303 self.leader = None | |
| 1304 # other attributes necessary for computation | |
| 1305 self.initialCumulatedHeadway = initialCumulatedHeadway | |
| 1306 self.initialAlignmentIdx = initialAlignmentIdx | |
| 1307 self.timeAtS0 = None # time at which the vehicle's position is s=0 on the alignment | |
| 1308 | |
| 1309 def updateCurvilinearPositions(self, method, instant, timeStep, _nextAlignmentIdx = None, maxSpeed = None, acceleration = None): | |
| 1310 '''Update curvilinear position of user at new instant''' | |
| 1311 # TODO changer nextAlignmentIdx pour l'alignment en cours, reflechir pour des control devices | |
| 1312 | |
| 1313 if method == 'newell': | |
| 1314 if self.curvilinearPositions is None: # vehicle without positions | |
| 1315 if self.timeAtS0 is None: | |
| 1316 if self.leader is None: | |
| 1317 self.timeAtS0 = self.initialCumulatedHeadway | |
| 1318 elif self.leader.curvilinearPositions is not None and self.leader.curvilinearPositions.getSCoordAt(-1) > self.d and len(self.leader.curvilinearPositions) >=2: | |
| 1319 firstInstantAfterD = self.leader.getLastInstant() | |
| 1320 while self.leader.existsAtInstant(firstInstantAfterD) and self.leader.getCurvilinearPositionAtInstant(firstInstantAfterD-1)[0] > self.d:# find first instant after d | |
| 1321 firstInstantAfterD -= 1 # if not recorded position before self.d, we extrapolate linearly from first known position | |
| 1322 leaderSpeed = self.leader.getCurvilinearVelocityAtInstant(firstInstantAfterD-1)[0] | |
| 1323 self.timeAtS0 = self.tau + firstInstantAfterD*timeStep - (self.leader.getCurvilinearPositionAtInstant(firstInstantAfterD)[0]-self.d)*timeStep/leaderSpeed # second part is the time at which leader is at self.d | |
| 1324 if self.timeAtS0 < self.initialCumulatedHeadway: #obj appears at instant initialCumulatedHeadway at x=0 with desiredSpeed | |
| 1325 self.timeAtS0 = self.initialCumulatedHeadway | |
| 1326 elif instant*timeStep > self.timeAtS0: | |
| 1327 #firstInstant = int(ceil(self.timeAtS0/timeStep))# this first instant is instant by definition | |
| 1328 leaderInstant = instant-self.tau/timeStep | |
| 1329 if self.leader is None: | |
| 1330 s = (timeStep*instant-self.timeAtS0)*self.desiredSpeed | |
| 1331 self.timeInterval = TimeInterval(instant, instant) | |
| 1332 self.curvilinearPositions = CurvilinearTrajectory([s], [0.], [self.initialAlignmentIdx]) | |
| 1333 self.curvilinearVelocities = CurvilinearTrajectory() | |
| 1334 elif self.leader.existsAtInstant(leaderInstant): | |
| 1335 self.timeInterval = TimeInterval(instant, instant) | |
| 1336 freeFlowCoord = (instant*timeStep - self.timeAtS0)*self.desiredSpeed | |
| 1337 # constrainedCoord at instant = xn-1(t = instant*timeStep-self.tau)-self.d | |
| 1338 constrainedCoord = self.leader.interpolateCurvilinearPositions(leaderInstant)[0]-self.d | |
| 1339 self.curvilinearPositions = CurvilinearTrajectory([min(freeFlowCoord, constrainedCoord)], [0.], [self.initialAlignmentIdx]) | |
| 1340 self.curvilinearVelocities = CurvilinearTrajectory() | |
| 1341 else: | |
| 1342 if _nextAlignmentIdx is not None: | |
| 1343 laneChange = (self.curvilinearPositions.getLaneAt(-1), _nextAlignmentIdx) | |
| 1344 nextAlignmentIdx = _nextAlignmentIdx | |
| 1345 else: | |
| 1346 laneChange = None | |
| 1347 nextAlignmentIdx = self.curvilinearPositions.getLaneAt(-1) | |
| 1348 s1 = self.curvilinearPositions.getSCoordAt(-1) | |
| 1349 freeFlowCoord = s1 + self.desiredSpeed*timeStep | |
| 1350 if self.leader is None: | |
| 1351 if self.getLastInstant() < instant: | |
| 1352 s2 = freeFlowCoord | |
| 1353 self.curvilinearPositions.addPositionSYL(freeFlowCoord, 0., nextAlignmentIdx) | |
| 1354 else: | |
| 1355 constrainedCoord = self.leader.interpolateCurvilinearPositions(instant-self.tau/timeStep)[0]-self.d | |
| 1356 s2 = min(freeFlowCoord, constrainedCoord) | |
| 1357 self.curvilinearPositions.addPositionSYL(s2, 0., nextAlignmentIdx) | |
| 1358 self.setLastInstant(instant) | |
| 1359 self.curvilinearVelocities.addPositionSYL(s2-s1, 0., laneChange) | |
| 1360 | 1295 |
| 1361 @staticmethod | 1296 @staticmethod |
| 1362 def concatenate(obj1, obj2, num = None, newFeatureNum = None, computePositions = False): | 1297 def concatenate(obj1, obj2, num = None, newFeatureNum = None, computePositions = False): |
| 1363 '''Concatenates two objects, whether overlapping temporally or not | 1298 '''Concatenates two objects, whether overlapping temporally or not |
| 1364 | 1299 |
