output.add([time, 'Oncoming', oncomPosition[0], oncomPosition[1], oncomAngle, vOncom, accOncom]) # switch lanes leadHeadway = calcHeadway(passPosition[0]-leadPosition[0]-5.8, vLead, accLead) if time > parameters['tpr'] and passlane=='main' and overtakeNotComplete: sim.moveVehicle('Passing','overtaking') if passlane=='overtaking' and leadHeadway >= 1.: sim.moveVehicle('Passing','main') overtakeNotComplete = False # move vehicles vehicleLeft = 0 vehicleLeft += sim.moveVehicleAlong('Passing', vPass*DT+currentAccPass/2*DT*DT) vehicleLeft += sim.moveVehicleAlong('Lead', vLead*DT+accLead/2*DT*DT) vehicleLeft += sim.moveVehicleAlong('Oncoming', vOncom*DT+accOncom/2*DT*DT) vPass += currentAccPass*DT vLead += accLead*DT vOncom += accOncom*DT time += DT sim.updateGUI() continueSim &= vehicleLeft == 0 # at least one vehicle exits sim continueSim &= time < 30. # shouldn't take this long continueSim &= (time <= parameters['tpr']+.1 or leadHeadway <= 1.1) #continueSim &= leadHeadway <= 1.4 # for better GUI sim.end() outputFile = os.path.realpath(outputFolder+'/'+str(simIndex+1)+'.csv') output.write(outputFile, restart=True) print simIndex
vLead, accLead) if time > parameters[ 'tpr'] and passlane == 'main' and overtakeNotComplete: sim.moveVehicle('Passing', 'overtaking') if passlane == 'overtaking' and leadHeadway >= 1.: sim.moveVehicle('Passing', 'main') overtakeNotComplete = False # move vehicles vehicleLeft = 0 vehicleLeft += sim.moveVehicleAlong( 'Passing', vPass * DT + currentAccPass / 2 * DT * DT) vehicleLeft += sim.moveVehicleAlong('Lead', vLead * DT + accLead / 2 * DT * DT) vehicleLeft += sim.moveVehicleAlong( 'Oncoming', vOncom * DT + accOncom / 2 * DT * DT) vPass += currentAccPass * DT vLead += accLead * DT vOncom += accOncom * DT time += DT sim.updateGUI() continueSim &= vehicleLeft == 0 # at least one vehicle exits sim continueSim &= time < 30. # shouldn't take this long continueSim &= (time <= parameters['tpr'] + .1 or leadHeadway <= 1.1) #continueSim &= leadHeadway <= 1.4 # for better GUI sim.end() outputFile = os.path.realpath(outputFolder + '/' + str(simIndex + 1) + '.csv') output.write(outputFile, restart=True) print simIndex
status = -1 totaldist = 0 while status < 1 and totaldist < 50: lane,lanepos,pos,angle = Sim.getVehicleState(road) status = 1*(lane[1]=='o' and lanepos > 5.) - 1*(lane[1]=='i' and len(lane)==4) if lane[1]=='o': # must be included since back of vehicle lanepos = lanepos + roadMap.getLength(road) if status==0: # add vehicle info to the dataframe thisdf.add([[pos[0],pos[1],angle,VEHsize[0],VEHsize[1],lanepos]]) Sim.moveVehicleAlong(road, dist, turn) totaldist += dist Sim.updateGUI() allcars[road] = thisdf.out() Sim.removeVehicle(road) Sim.end(waitOnEnd = False) for car in allcars.itervalues(): car['angle'] = car['angle'] + 1.5708 # ## now use dataframes of position+angle to find collision points #collisions = WriteFrame(['lane','lane2','begin_lp','end_lp']) #for an in range(len(intersections)): # for bn in range(an): # print "checking "+str(an)+", "+str(bn) # starta,enda = intersections[an] # startb,endb = intersections[bn] # roada = combineRoad(starta,enda) # roadb = combineRoad(startb,endb) # cara = allcars[roada] # carb = allcars[roadb]
'1o_1': (92, 101.65, 0, 101.65), '4o_0': (105, 108, 105, 200), '4o_1': (101.65, 108, 101.65, 200), '3o_0': (95, 92, 95, 0), '3o_1': (98.35, 92, 98.35, 0) } # which roads connect to other roads intersections = [['1i_0', '3o_0'], ['1i_0', '2o_0'], ['1i_1', '4o_1'], ['2i_0', '4o_0'], ['2i_0', '1o_0'], ['2i_1', '3o_1'], ['3i_0', '2o_0'], ['3i_0', '4o_0'], ['3i_1', '1o_1'], ['4i_0', '1o_0'], ['4i_0', '3o_0'], ['4i_1', '2o_1']] roadMap = RoadMap(roads, intersections) Sim = Simulator(roadMap, gui=GUI, delay=SIMDELAY) Sim.createVehicle('mycar', '1i_0', 50.) Sim.moveVehicle('mycar', '1i_1', 50.) for k in range(20): exited = Sim.moveVehicleAlong('mycar', 5., '4o_1') carLane, carLanePos, carPos, carAngle = Sim.getVehicleState('mycar') print carPos manual_escape = Sim.updateGUI(allowPause=True) if exited or manual_escape: break Sim.end()
speedChange = [0,-10.,10.][int(speedChange)]/MS2KPH cars.loc[carID,'speed'] = min(max(car['speed']+speedChange, 0.),60./MS2KPH) distance = car['speed']*DELTAT # the vehicle is about to make a transition to a new roa exited = Sim.moveVehicleAlong(str(carID), distance, turn) if exited: # car leaving simulation Sim.removeVehicle(str(carID)) cars.loc[carID,'status'] = 1 WrongPath = WrongPath + int(route != splitRoad(car['dest'])[0]) Sim.updateGUI(allowPause=True) ttime += 0.1 # finish step Sim.end() print "iteration "+str(iteration)+" : "+str(time.time()-starttime) print "collisions "+str(collisionCount) print "wrong "+str(WrongPath) Stats_train=Stats_train+[collisionCount,TotReward,ttime,WrongPath,float(cars.shape[0])] time.sleep(.2) iteration += 1 # save info afterwards iteration = iteration - 1 if trainingTable: np.save('qtable.npy',QTable) Stats_train.loc['Collision'] = Stats_train.loc['Collision'] / Stats_train['Cars'] Stats_train.loc['Reward'] = Stats_train.loc['Reward'] / Stats_train['Cars'] Stats_train.loc['WrongPath'] = Stats_train.loc['WrongPath'] / Stats_train['Cars'] Stats_train.loc['Time'] = Stats_train.loc['Time']/iteration
while status < 1 and totaldist < 50: lane, lanepos, pos, angle = Sim.getVehicleState(road) status = 1 * (lane[1] == 'o' and lanepos > 5.) - 1 * (lane[1] == 'i' and len(lane) == 4) if lane[1] == 'o': # must be included since back of vehicle lanepos = lanepos + roadMap.getLength(road) if status == 0: # add vehicle info to the dataframe thisdf.add( [[pos[0], pos[1], angle, VEHsize[0], VEHsize[1], lanepos]]) Sim.moveVehicleAlong(road, dist, turn) totaldist += dist Sim.updateGUI() allcars[road] = thisdf.out() Sim.removeVehicle(road) Sim.end(waitOnEnd=False) for car in allcars.itervalues(): car['angle'] = car['angle'] + 1.5708 # ## now use dataframes of position+angle to find collision points #collisions = WriteFrame(['lane','lane2','begin_lp','end_lp']) #for an in range(len(intersections)): # for bn in range(an): # print "checking "+str(an)+", "+str(bn) # starta,enda = intersections[an] # startb,endb = intersections[bn] # roada = combineRoad(starta,enda) # roadb = combineRoad(startb,endb) # cara = allcars[roada] # carb = allcars[roadb]