def initializeExtraTollLane(): global TOL_COUNT global freeway global REG_COUNT global TIME_SECONDS global starterTimeVal global MILES global LANES global timec global toll global numAttributes global timeStepList global numLanes global numTollLanes global simName LANES = 5 simName = "Extra Toll Lane" toll = 1 timec = 4.5 numLanes = 2 numTollLanes = 2 s = (MILES, LANES, numAttributes) freeway = np.zeros(s, dtype=object) freeway[:, :, 2] = None # initilaize all cars to none freeway[:, 1:3, 0] = REGULAR freeway[:, 3:5, 0] = TOLL freeway[:, 0, 0] = NOT_USED freeway[:, :, 3] = CANNOT_CHANGE_LANES freeway[:, :, 1] = starterTimeVal # getting number of regular cars that were initialized in the control simulation regCount = timeStepList[0][0] # getting number of toll cars tollCount = timeStepList[0][1] # we pick a random spot on the freeway to initialize a car, and set it to a car while regCount > 0: x = random.randint(1, 2) y = random.randint(0, freeway.shape[0] - 1) # check if the spot is occupied if (freeway[y, x, 2] == None): speed = random.randint(0, 1) freeway[y, x, 2] = car_agent.Car(y, x, False, TIME_SECONDS, speed) regCount -= 1 # Here we initialize the toll lane while tollCount > 0: x = random.randint(3, 4) y = random.randint(0, freeway.shape[0] - 1) if (freeway[y, x, 2] == None): speed = random.randint(0, 1) freeway[y, x, 2] = car_agent.Car(y, x, False, TIME_SECONDS, speed) tollCount -= 1 AddingRampsToFreeway() markAvailableSpaces()
def addAgent(): global REG_COUNT global TOL_COUNT global TIME_SECONDS global redLightSpeed global onramp1B global onramp2B global onramp3B global offramp4B global tollTrackedCount global trackedCount global trackedSpeed global timeStepList regAddCount = 0 tollAddCount = 0 for i in range(freeway.shape[1]): val = np.random.uniform(0, 1) hasSet = False if ((i == 1 or i == 2) and val < percentReg and freeway[0, i, 1] != TIME_SECONDS): freeway[0][i][2] = car_agent.Car(0, i, False, TIME_SECONDS, speed) REG_COUNT += 1 regAddCount += 1 if (TIME_SECONDS % trackedSpeed == 0 and hasSet == False): freeway[0][i][2].tracked = True trackedCount += 1 hasSet = True elif (i == 3 and val < percentTol and freeway[0, i, 1] != TIME_SECONDS): freeway[0, i, 2] = car_agent.Car(0, i, False, TIME_SECONDS, speed) TOL_COUNT += 1 tollAddCount += 1 if (TIME_SECONDS % trackedSpeed == 0 and hasSet == False): freeway[0][i][2].tracked = True tollTrackedCount += 1 hasSet = True t = (regAddCount, tollAddCount) timeStepList.append(t) # take timeseconds out of tuple regAddCount = 0 tollAddCount = 0 # adding cars to the on ramps if (TIME_SECONDS % redLightSpeed == 0): # each onramp is a different onramp point on the freeway. All the stoplights are synced up in this simulation freeway[onramp1][0][2] = car_agent.Car(onramp1, 0, False, TIME_SECONDS, onrampSpeed) freeway[onramp2][0][2] = car_agent.Car(onramp2, 0, False, TIME_SECONDS, onrampSpeed) freeway[onramp3][0][2] = car_agent.Car(onramp3, 0, False, TIME_SECONDS, onrampSpeed) REG_COUNT += 3
def initializeRoad(): global TOL_COUNT global freeway global timec global toll global REG_COUNT global TIME_SECONDS global starterTimeVal global MILES global LANES global numLanes global timeStepList global speed global simName # Lane type, time last visited, car, can change s = (MILES, LANES, numAttributes) freeway = np.zeros(s, dtype=object) freeway[:, :, 2] = None # initilaize all cars to none freeway[:, 1:3, 0] = REGULAR freeway[:, 3, 0] = TOLL freeway[:, 0, 0] = NOT_USED freeway[:, :, 3] = CANNOT_CHANGE_LANES freeway[:, :, 1] = starterTimeVal #If cars are not initialized all added vehicles will make it to the end of the freeway in 6.5 minutes for i in range(freeway.shape[0]): # placing vehicles on the map\ for j in range(freeway.shape[1]): val = np.random.uniform(0, 1) speed = random.randint(0, 1) if ((j == 1 or j == 2) and val < percentReg): # placing vehicles on regular lanes freeway[i][j][2] = car_agent.Car(i, j, False, TIME_SECONDS, speed) REG_COUNT += 1 elif (j == 3 and val < percentTol): # placing vehicles on toll lanes freeway[i][j][2] = car_agent.Car(i, j, False, TIME_SECONDS, speed) TOL_COUNT += 1 t = (REG_COUNT, TOL_COUNT) timeStepList.append(t) AddingRampsToFreeway() markAvailableSpaces()
def test_freeway(): global small_freeway freeway[:, :, 2] = None # initilaize all cars to none freeway[:, 1:3, 0] = REGULAR freeway[:, 3, 0] = TOLL freeway[:, 0, 0] = NOT_USED freeway[:, :, 3] = CAN_CHANGE_LANES for i in range(freeway.shape[0]): # placing vehicles on the map\ for j in range(freeway.shape[1]): val = np.random.uniform(0, 1) if ((j == 1 or j == 2) and val < .5): # placing vehicles on regular lanes freeway[i][j][2] = car_agent.Car(i, j, False, TIME_SECONDS, speed) elif (j == 3 and val < .25): # placing vehicles on toll lanes freeway[i][j][2] = car_agent.Car(i, j, False, TIME_SECONDS, speed)
def makeTollRegular(): global TOL_COUNT global freeway global REG_COUNT global TIME_SECONDS global starterTimeVal global timec global toll global MILES global LANES global numLanes global timeStepList global simName numLanes = 3 numTollLanes = 0 timec = 3.9 simName = "Make Toll Lane into Actual Lane" # Lane type, time last visited, car, can change s = (MILES, LANES, numAttributes) freeway = np.zeros(s, dtype=object) freeway[:, :, 2] = None # initilaize all cars to none freeway[:, 1:4, 0] = REGULAR freeway[:, 0, 0] = NOT_USED freeway[:, :, 3] = CANNOT_CHANGE_LANES freeway[:, :, 1] = starterTimeVal # getting number of regular cars that were initialized in the control simulation regCount = timeStepList[0][0] # getting number of toll cars tollCount = timeStepList[0][1] regCount += tollCount # we pick a random spot on the freeway to initialize a car, and set it to a car while regCount > 0: x = random.randint(1, 3) y = random.randint(0, freeway.shape[0] - 1) # check if the spot is occupied if (freeway[y, x, 2] == None): speed = random.randint(0, 1) freeway[y, x, 2] = car_agent.Car(y, x, False, TIME_SECONDS, speed) regCount -= 1 AddingRampsToFreeway() markAvailableSpaces()
def AddingRampsToFreeway(): global REG_COUNT for i in range(len(freeway)): val = np.random.uniform(0, 1) #setting the off ramps values to the freeway if (i >= offramp1B and i <= offramp1E) or (i >= offramp2B and i <= offramp2E) or ( i >= offramp3B and i <= offramp3E) or (i >= offramp4B and i <= offramp4E): freeway[i, 0, 0] = OFF_RAMP #setting the on ramps values to the freeway if (i >= onramp1B and i <= onramp1E) or ( i >= onramp2B and i <= onramp2E) or (i >= onramp3B and i <= onramp3E): freeway[i, 0, 0] = ON_RAMP if (val < percentOnramp): freeway[i, 0, 2] = car_agent.Car(i, 0, False, TIME_SECONDS, speed) REG_COUNT += 1 #setting the dotted lines on the toll lane if (i >= 0 and i <= 106) or (i >= 489 and i <= 630) or (i >= 1054 and i <= 1265): freeway[i, 3, 3] = CAN_CHANGE_LANES
def addAgentNewLayout(): global REG_COUNT global TOL_COUNT global TIME_SECONDS global redLightSpeed global onramp1 global onramp2 global onramp3 global onramp4 global tollTrackedCount global trackedCount global trackedSpeed global timeStepList global numLanes global numTollLanes backup = 0 tollBackup = 0 laneStart = -1 tollStart = -1 # finding when the lanes/toll lanes start for i in range(freeway.shape[1]): if freeway[0, i, 0] == 1 and laneStart == -1: laneStart = i elif freeway[0, i, 0] == 3 and tollStart == -1: tollStart = i if tollStart == -1: hasSet = False carsToAdd = timeStepList[TIME_SECONDS + 1][0] + timeStepList[TIME_SECONDS + 1][0] loopCount = 0 loopCountMax = numLanes * 20 # if loop count Max is reached TIME_SECONDS must be off while carsToAdd > 0 and loopCount != loopCountMax: loopCount += 1 x = random.randint(laneStart, laneStart + numLanes - 1) if freeway[0, x, 2] == None and freeway[0, x, 1] != TIME_SECONDS: freeway[0, x, 2] = car_agent.Car(0, x, False, TIME_SECONDS, speed) carsToAdd -= 1 if (TIME_SECONDS % trackedSpeed == 0 and hasSet == False): freeway[0][x][2].tracked = True trackedCount += 1 hasSet = True else: # if there are more cars to be added then there are lanes if timeStepList[TIME_SECONDS + 1][0] > numLanes: backup = timeStepList[TIME_SECONDS][0] - numLanes for j in range(freeway.shape[1]): if freeway[0, j, 0] == 1: freeway[0, j, 2] = car_agent.Car(0, j, False, TIME_SECONDS, speed) else: hasSet = False carsToAdd = timeStepList[TIME_SECONDS + 1][0] loopCount = 0 loopCountMax = numLanes * 10 # if loop count Max is reached TIME_SECONDS must be off #print("before while loop") while carsToAdd > 0 and loopCount != loopCountMax: x = random.randint(laneStart, laneStart + numLanes - 1) loopCount += 1 if freeway[0, x, 2] == None and freeway[0, x, 1] != TIME_SECONDS: freeway[0, x, 2] = car_agent.Car(0, x, False, TIME_SECONDS, speed) carsToAdd -= 1 if (TIME_SECONDS % trackedSpeed == 0 and hasSet == False): freeway[0][x][2].tracked = True trackedCount += 1 hasSet = True backup += carsToAdd # adding backed up cars into the simulation if backup > 0: for j in range(laneStart, laneStart + numLanes): if backup > 0: if freeway[0, j, 2] == None and freeway[0, x, 1] != TIME_SECONDS: freeway[0, j, 2] = car_agent.Car(0, j, False, TIME_SECONDS, speed) backup -= 1 # adding cars to the toll lanes tollCarsToAdd = timeStepList[TIME_SECONDS + 1][1] loopCount = 0 loopCountMax = numTollLanes * 10 # if loop count Max is reached TIME_SECONDS must be off while tollCarsToAdd > 0 and loopCount != loopCountMax: loopCount += 1 x = random.randint(tollStart, tollStart + numTollLanes - 1) if freeway[0, x, 2] == None and freeway[0, x, 1] != TIME_SECONDS: freeway[0, x, 2] = car_agent.Car(0, x, False, TIME_SECONDS, speed) tollCarsToAdd -= 1 if (TIME_SECONDS % trackedSpeed == 0 and hasSet == False): freeway[0][x][2].tracked = True tollTrackedCount += 1 hasSet = True tollBackup += tollCarsToAdd # adding backed up toll cars into the simulation if tollBackup > 0: for j in range(freeway.shape[1]): if tollBackup > 0: if freeway[0, j, 2] == None and freeway[0, x, 1] != TIME_SECONDS: freeway[0, j, 2] = car_agent.Car(0, j, False, TIME_SECONDS, speed) tollBackup -= 1 # adding cars to the on ramps if (TIME_SECONDS % redLightSpeed == 0): freeway[onramp1][0][2] = car_agent.Car(onramp1, 0, False, TIME_SECONDS, onrampSpeed) freeway[onramp2][0][2] = car_agent.Car(onramp2, 0, False, TIME_SECONDS, onrampSpeed) freeway[onramp3][0][2] = car_agent.Car(onramp3, 0, False, TIME_SECONDS, onrampSpeed) REG_COUNT += 3