diff --git a/Car.py b/Car.py index 88cc642..2639279 100644 --- a/Car.py +++ b/Car.py @@ -3,6 +3,7 @@ from math import dist, ceil, sqrt, log, cos, sin, atan2, pi from random import randint, uniform from PySide6.QtGui import QPainter from PySide6.QtCore import QPointF, Signal, QObject, Qt +from itertools import islice class updateSignals(QObject): updateDisp = Signal(tuple) @@ -54,12 +55,14 @@ class Car(): self.v=0 self.a=10 self.b=20 - self.minSpace=10 + self.minSpace=20 self.leaderBefore=False self.distToInter=0 self.timeStopped=0 self.speedPercentage=0 self.ticksLived=0 + self.timeAtInter = 0 + self.forceThrough = False self.vmax=0 @@ -127,6 +130,7 @@ class Car(): l = 0 carsHere = self.controller.getCarsOnLane(self.route[edgeInd].getID(), laneId) carsHere = list(filter(lambda c: c.id != self.id, carsHere)) + self.leaderBefore = False while(l= len(self.route): + print(self.id,"fu") return None while self.route[prevInd].isSpecial(): prevInd -= 1 if prevInd < 0: + print(self.id, "fu2") return None inter = self.route[edgeInd-1].getFromNode() connections = self.route[prevInd].getConnections(self.route[edgeInd]) @@ -213,6 +218,7 @@ class Car(): connection = connections[0] linkInd = inter.getLinkIndex(connection) if(linkInd == -1): # Ca devrait pas arriver, mais de toute évidence ça arrive + print(self.id, "fu3") return; resp = inter._prohibits[linkInd] # Si je me souvient bien les variables précédées d'un _ doivent pas être touchées? connRaw = inter.getConnections() @@ -231,7 +237,9 @@ class Car(): intLane = self.map.getLane(conn[i].getViaLaneID()) intLaneLgt = intLane.getLength() carsInEdge = self.controller.getCarsOnLane(edge.getID(), laneInd) - carsInEdge = list(filter(lambda c: not c.turnNext(), carsInEdge)) + carsInEdge = list(filter(lambda c: c.nextNonSpecialEdge() == conn[i].getTo(), carsInEdge)) + #carsInEdge = list(carsInEdge) + #carsInEdge = list(filter(lambda c: c.nextNonSpecialEdge() == self.nextNonSpecialEdge(), carsInEdge)) if len(carsInEdge) != 0: carsInEdge = zip([self.getCarDist(c, edge, laneInd)+intLaneLgt for c in carsInEdge], carsInEdge) closest = min(carsInEdge, key=lambda c: c[0]) @@ -251,8 +259,15 @@ class Car(): def turnNext(self): return self.cligno[self.index] != self.CLIGNO_NONE + def nextNonSpecialEdge(self): + return next(filter(lambda e: not e.isSpecial(), islice(self.route, self.index + 1, None))) + def draw(self,painter): pt = QPointF(*self.pos) + if self.forceThrough: + painter.setPen(Qt.blue) + elif self.leader is None: + painter.setPen(Qt.gray) painter.drawEllipse(pt,self.size,self.size) painter.drawLine(self.pos[0], self.pos[1], self.pos[0]+5*cos(self.dir), self.pos[1]+5*sin(self.dir)) @@ -356,6 +371,9 @@ class Car(): if False and ("f_0" in self.id or "f_1" in self.id or "f_2" in self.id): self.v = 0 return + + if not self.leaderBefore: + self.forceThrough = False if leader is None: vd = min(self.v + self.a * dt, vmax) @@ -367,6 +385,11 @@ class Car(): # si on est à une intersection if(self.leaderBefore): + if self.v == 0: + self.timeAtInter += dt + else: + self.timeAtInter = 0 + # on calcule le temps qu'on va mettre à arriver à l'intersection # et le temps que le leader va mettre @@ -381,7 +404,19 @@ class Car(): tts = self.v/self.b # time to stop, temps pour s'arreter si on freine mnt dts = self.v*tts - (self.b*tts**2)/2 # distance to stop, distance parcouru en tts si on freine + #print(self.distToInter, self.minSpace, dts) + + # Si on est bloqué dans une dépendance circulaire + if self.timeAtInter > 10 and self.circularLeaderDep(): + self.forceThrough = True + + if self.forceThrough: + self.leader = None # On supprime le leader (pour que seulement la premiere voiture detecte la dependance circulaire) + self.v = min(vmax, self.v + self.a*dt) + return + + # si on est suffisement loin de l'intersection (i.e on s'en fout du leader) # ou si on as le temps d'arriver à l'intersection avant le leader (plus un marge pour garder un distance de sécu) # alors on accelere pour s'inserer @@ -400,6 +435,20 @@ class Car(): vd = min(self.v + self.a * dt, vsec, vmax) self.v = max(0, vd-self.nu) self.updateGraph(self.v, vmax, vsec) + + # fonction pour verifier si on as pas une dependence circulaire de leader + def circularLeaderDep(self): + l = self.leader + timeout = 5 + ls = [] + while l is not None and timeout > 0: + ls.append(l.id) + if l.id == self.id: + print(ls) + return True + timeout -= 1 + l = l.leader + return False def updateGraph(self, v, vmax, vsec): if self.infoWidg is None: @@ -460,7 +509,7 @@ class Car(): return self.signals.updateDisp.emit(("Position", self.pos)) self.signals.updateDisp.emit(("Vitesse", self.v)) - self.signals.updateDisp.emit(("Leader", self.leader if self.leader is None else f"{self.leader.id} @ {self.leaderDist:.2f}m")) + self.signals.updateDisp.emit(("Leader", self.leader if self.leader is None else f"{self.leader.id} @ {self.leaderDist:.2f}m (inter : {self.leaderBefore})")) def __copy__(self): copy = Car(self.id, self.rawRoute, self.startTime, self.map, self.controller, self.infoWidg) diff --git a/CarController.py b/CarController.py index e0a3f96..8eb3b0a 100644 --- a/CarController.py +++ b/CarController.py @@ -123,6 +123,7 @@ class CarController: def fromPath(self,path): self.cars=[] + self.flows=[] self.t=0 while self.infoWidget is not None and self.infoWidget.count() != 0: diff --git a/comp_inter.net.xml b/comp_inter.net.xml new file mode 100644 index 0000000..cbb57ea --- /dev/null +++ b/comp_inter.net.xml @@ -0,0 +1,216 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/comp_inter.rou.xml b/comp_inter.rou.xml new file mode 100644 index 0000000..b2a9e58 --- /dev/null +++ b/comp_inter.rou.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/comp_inter_t1.rou.xml b/comp_inter_t1.rou.xml new file mode 100644 index 0000000..fec2ddc --- /dev/null +++ b/comp_inter_t1.rou.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + diff --git a/comp_inter_t2.rou.xml b/comp_inter_t2.rou.xml new file mode 100644 index 0000000..c7d468a --- /dev/null +++ b/comp_inter_t2.rou.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + diff --git a/comp_rdp.net.xml b/comp_rdp.net.xml new file mode 100644 index 0000000..17045a3 --- /dev/null +++ b/comp_rdp.net.xml @@ -0,0 +1,250 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/comp_rdp.rou.xml b/comp_rdp.rou.xml new file mode 100644 index 0000000..db1b47f --- /dev/null +++ b/comp_rdp.rou.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/main.py b/main.py index 3deaf08..e6e769b 100644 --- a/main.py +++ b/main.py @@ -85,7 +85,7 @@ class MainWindow(QMainWindow): averageTimeStopped = controller.totalStopped / controller.carsDestroyed speedPercentage = controller.speedPercentageTotal / controller.carsDestroyed newline = '\n' - widget.setText(f"temps d'arrêt : {averageTimeStopped:.2f}s/voiture \nT : {speedPercentage:.2f} \nbacklog total : {controller.getFlowBacklog():.2f} \n{newline.join(f'{f.id} : {f.backlog(controller.t):.2f}' for f in controller.flows)}") + widget.setText(f"temps : {controller.t} \ntemps d'arrêt : {averageTimeStopped:.2f}s/voiture \nT : {speedPercentage:.2f} \nbacklog total : {controller.getFlowBacklog():.2f} \n{newline.join(f'{f.id} : {f.backlog(controller.t):.2f}' for f in controller.flows)}") @Slot(int) def updatePhysicsFps(self,t): diff --git a/mainLoop.py b/mainLoop.py index c8b41bd..108fd3a 100644 --- a/mainLoop.py +++ b/mainLoop.py @@ -63,6 +63,8 @@ class mainLoop(QObject): def update(self): try: self.controller.update() + if self.controller.t > 3600: + self.stopTimer() except: (type, value, traceback) = sys.exc_info() print(type, value, traceback.print_tb()) @@ -86,9 +88,9 @@ class mainLoop(QObject): @threadSafe def quickLoad(self): - self.map.fromPath("rdpt_polytech.net.xml") + self.map.fromPath("comp_inter.net.xml") self.painter.generateTransform() - self.controller.fromPath("rdpt_polytech.rou.xml") + self.controller.fromPath("comp_inter.rou.xml") self.controller.prepareRoute() def updateFps(self):