class BaseConfig(object): DEBUG = False TESTING = False SECRET_KEY = "it worked" AVAILABLE_VEHICLES = [ Vehicle(id_=1, name='Car', radius_wheel=2.0, amount_magnets=2, pollution_per_cm=5), Vehicle(id_=2, name='Bus', radius_wheel=2.0, amount_magnets=2, pollution_per_cm=2) ] POLLUTION_LOG_DELAY = 20 # Pollution Min Max Values MIN_POLLUTION = 100 MAX_POLLUTION = 5000 # Hardware HARDWARE_LOOP_SLEEP = 0.1 LED_STRIPE_PIN = board.D18 LED_STRIPE_ORDER = neopixel.GRB LED_STRIPE_SIZE = 60 LED_STRIPE_CENTER_LED_RIGHT = 52 LED_STRIPE_MODE = 1 SERVO_MOTOR_PINS = [5, 19, 13, 6]
class TestVehicle(TestCase): def setUp(self) -> None: self.v = Vehicle('KA 05 FB 1234', 23) def test_get_reg_num(self): self.assertEqual(self.v.get_reg_num(), 'KA 05 FB 1234') def test_get_age(self): self.assertEqual(self.v.get_age(), 23)
def test_get_slot_number_for_age_with_empty_result(self): service = ParkingService() vehicle_1 = Vehicle('mock_reg_num_1', 22) vehicle_2 = Vehicle('mock_reg_num_2', 25) service.create_parking_lot(3) service.park(vehicle_1) service.park(vehicle_2) self.assertEqual(service.get_slot_number_for_age(18), [])
def test_get_slot_number_for_reg_number_invalid_vehicle(self): service = ParkingService() vehicle_1 = Vehicle('mock_reg_num_1', 22) vehicle_2 = Vehicle('mock_reg_num_2', 25) service.create_parking_lot(3) service.park(vehicle_1) service.park(vehicle_2) self.assertIsNone( service.get_slot_number_for_reg_number('mock_reg_num_3'))
def test_get_slot_number_for_age_with_non_empty_result(self): service = ParkingService() vehicle_1 = Vehicle('mock_reg_num_1', 22) vehicle_2 = Vehicle('mock_reg_num_2', 25) vehicle_3 = Vehicle('mock_reg_num_3', 22) service.create_parking_lot(3) service.park(vehicle_1) service.park(vehicle_2) service.park(vehicle_3) expected = ['1', '3'] self.assertEqual(service.get_slot_number_for_age(22), expected)
def test_get_empty_slot_available(self): lot = ParkingLot(3) vehicle_1 = Vehicle('mock_reg_num_1', 22) vehicle_2 = Vehicle('mock_reg_num_2', 25) vehicle_3 = Vehicle('mock_reg_num_3', 21) self.assertEqual(lot.get_empty_slot(), 0) lot.fill_slot(vehicle_1) slot_id_2 = lot.fill_slot(vehicle_2) lot.fill_slot(vehicle_3) lot.empty_slot(slot_id_2) self.assertEqual(lot.get_empty_slot(), 1)
def genVehicle(tsPairNodePairTypeMap, distribution, vehicleId, medianValueTime, network): if distribution == "uniform": for tsPair in tsPairNodePairTypeMap.keys(): for nodePair in tsPairNodePairTypeMap[tsPair].keys(): for vehicleType in tsPairNodePairTypeMap[tsPair][ nodePair].keys(): vehicleVolume = tsPairNodePairTypeMap[tsPair][nodePair][ vehicleType] startTs, endTs = tsPair[0], tsPair[1] duration = endTs - startTs origin, dest = network.idNodeMap[ nodePair[0]], network.idNodeMap[nodePair[1]] if not vehicleVolume: continue interval = 1.0 * duration / vehicleVolume for i in range(vehicleVolume): vehicleStartTs = startTs + datetime.timedelta( seconds=int(math.ceil(interval.seconds * i))) vehicleId += 1 maxSpeed = vehicleMaxSpeed(vehicleType) driverType = genDriverType() valueTime = genDriverValueTimeGen(medianValueTime) probLaneChange = genProbLaneChange(driverType) Vehicle(vehicleId, vehicleType, driverType, maxSpeed, valueTime, probLaneChange, vehicleStartTs, origin, dest, network)
def test_is_empty_false_case(self): lot = ParkingLot(1) vehicle = Vehicle('mock_reg_num', 22) lot.fill_slot(vehicle) self.assertFalse(lot.is_empty())
def test_get_slots(self): lot = ParkingLot(3) vehicle_1 = Vehicle('mock_reg_num_1', 22) lot.fill_slot(vehicle_1) self.assertEqual(lot.get_slots(), [vehicle_1, None, None]) vehicle_2 = Vehicle('mock_reg_num_2', 25) slot_id_2 = lot.fill_slot(vehicle_2) self.assertEqual(lot.get_slots(), [vehicle_1, vehicle_2, None]) vehicle_3 = Vehicle('mock_reg_num_3', 21) lot.fill_slot(vehicle_3) lot.empty_slot(slot_id_2) self.assertEqual(lot.get_slots(), [vehicle_1, None, vehicle_3])
def create_vehicle_list(self, vehicleinfo_list): self.vehicle_list = [ Vehicle(env=self.env, vid=x["vid"], initial_location=Location(x["x"], x["y"]), capacity=x["capacity"], velocity=x["velocity"]) for x in vehicleinfo_list ]
def test_empty_slot_invalid_slot_id(self): lot = ParkingLot(1) vehicle = Vehicle('mock_reg_num', 22) lot.fill_slot(vehicle) result = lot.empty_slot(2) self.assertIsNone(result)
def test_fill_slot_parking_slot_available(self): lot = ParkingLot(1) vehicle = Vehicle('mock_reg_num', 22) result = lot.fill_slot(vehicle) self.assertIsNotNone(result) self.assertIsInstance(result, int) self.assertEqual(result, 1)
def test_empty_slot_valid_slot_id(self): lot = ParkingLot(1) vehicle = Vehicle('mock_reg_num', 22) slot_id = lot.fill_slot(vehicle) result = lot.empty_slot(slot_id) self.assertIsNotNone(result) self.assertIsInstance(result, Vehicle) self.assertEqual(result.get_reg_num(), 'mock_reg_num') self.assertEqual(result.get_age(), 22)
def execute_command(self, command, params): """Method to execute a valid command Args: command (Commands): Command enum, denoting the command to be executed params (List[str]): List of string parameters required to execute the command """ if command == Commands.CREATE_PARKING_LOT: capacity = self.parking_service.create_parking_lot( capacity=int(params[0])) print(f'Created parking of {capacity} slots') elif command == Commands.PARK: vehicle = Vehicle(reg_num=params[0], age=int(params[2])) slot_id = self.parking_service.park(vehicle=vehicle) if slot_id: print( f'Car with vehicle registration number "{vehicle.get_reg_num()}" has been parked at slot number {slot_id}' ) elif command == Commands.LEAVE: vehicle = self.parking_service.leave(slot_id=int(params[0])) if vehicle: print( f'Slot number {params[0]} vacated, the car with vehicle registration number "{vehicle.get_reg_num()}" left the space, the driver of the car was of age {vehicle.get_age()}' ) elif command == Commands.SLOT_NUMBER_FOR_CAR_WITH_NUMBER: slot_id = self.parking_service.get_slot_number_for_reg_number( reg_num=params[0]) if slot_id: print(slot_id) else: print('Vehicle not found.') elif command == Commands.SLOT_NUMBERS_FOR_DRIVER_OF_AGE: slot_ids = self.parking_service.get_slot_number_for_age( age=int(params[0])) if slot_ids: print(','.join(slot_ids)) else: print('null') elif command == Commands.VEHICLE_REGISTRATION_NUMBER_FOR_DRIVER_OF_AGE: reg_nums = self.parking_service.get_reg_number_for_age( age=int(params[0])) if reg_nums: print(','.join(reg_nums)) else: print('null')
def test_fill_slot_parking_lot_full(self): lot = ParkingLot(0) vehicle = Vehicle('mock_reg_num', 22) result = lot.fill_slot(vehicle) self.assertIsNone(result)
def setUp(self) -> None: self.v = Vehicle('KA 05 FB 1234', 23)
def genVehicle(tsPairNodePairTypeMap, distribution, vehicleId, medianValueTime, network, multiple = 1): """ This function generate vehicle by "distribution" :param tsPairNodePairTypeMap: in tsPAIR and nodePAIR generate type of VEHICLE :param distribution: "uniform";"uniform_whole";"random";"random_whole";"normal_whole" :param vehicleId: :param medianValueTime: :param network: :return: """ if distribution == "uniform": for tsPair in tsPairNodePairTypeMap.keys(): for nodePair in tsPairNodePairTypeMap[tsPair].keys(): #print('nodePair',nodePair) for vehicleType in tsPairNodePairTypeMap[tsPair][nodePair]: #print('vehicle type:', vehicleType) vehicleVolume = multiple * tsPairNodePairTypeMap[tsPair][nodePair][vehicleType] startTs, endTs = tsPair[0], tsPair[1] duration = endTs - startTs #print('duration:',duration) origin, dest = network.idNodeMap[nodePair[0]], network.idNodeMap[nodePair[1]] #print('vehicle vol',vehicleVolume) if vehicleVolume <= 0: continue interval = duration / vehicleVolume #print('interval:',interval, vehicleVolume) for i in range(vehicleVolume): vehicleStartTs = startTs + datetime.timedelta(seconds=int(math.ceil(interval.seconds * i ) + 1 )) print('vehicleStartTime:', vehicleStartTs) vehicleId += 1 maxSpeed = vehicleMaxSpeed(vehicleType) #print(maxSpeed) driverType = genDriverType() valueTime = genDriverValueTimeGen(medianValueTime) probLaneChange = genProbLaneChange(driverType) Vehicle(vehicleId, vehicleType, driverType, maxSpeed, valueTime, probLaneChange, vehicleStartTs, origin, dest, network) print('--------vehicle generation finish: uniform-------------------') elif distribution == 'uniform_whole': # THIS METHOD IS PROBLEMATIC listTsPair = [] # collect all the time stamps of OD start and end vehicleTotal = 0 # count the total number of vehicles in the OD matrix; not in pcu for tsPair in tsPairNodePairTypeMap.keys(): startTs, endTs = tsPair[0], tsPair[1] if not startTs in listTsPair: listTsPair.append(startTs) if not endTs in listTsPair: listTsPair.append(endTs) for nodePair in tsPairNodePairTypeMap[tsPair].keys(): for vehicleType in tsPairNodePairTypeMap[tsPair][nodePair]: vehicleTypeValue = multiple * tsPairNodePairTypeMap[tsPair][nodePair][vehicleType] vehicleTotal += vehicleTypeValue #print(vehicleTotal) listTsPair.sort() # re-order the time stamps of OD starts and ends startTs = listTsPair[0] endTs = listTsPair[-1] duration = endTs - startTs #print('duration:',duration) if vehicleTotal > 0: interval = duration / vehicleTotal # TODO: BUG HERE #print('interval',interval.seconds, 'vehicle total:', vehicleTotal) count_id = list(range(vehicleTotal)) for tsPair in tsPairNodePairTypeMap.keys(): for nodePair in tsPairNodePairTypeMap[tsPair].keys(): origin, dest = network.idNodeMap[nodePair[0]], network.idNodeMap[nodePair[1]] for vehicleType in tsPairNodePairTypeMap[tsPair][nodePair]: vehicleVolume = multiple * tsPairNodePairTypeMap[tsPair][nodePair][vehicleType] #print('current vehicle vol:',vehicleVolume,'in', tsPair) if not vehicleVolume: continue for i in range(vehicleVolume): vehicleId += 1 vehicleStartTs = startTs + datetime.timedelta(seconds=int(math.ceil(interval.seconds) + 1)*count_id.pop(0)) print('vehicle',i, 'start ts', vehicleStartTs) maxSpeed = vehicleMaxSpeed(vehicleType) driverType = genDriverType() valueTime = genDriverValueTimeGen(medianValueTime) probLaneChange = genProbLaneChange(driverType) Vehicle(vehicleId, vehicleType, driverType, maxSpeed, valueTime, probLaneChange,vehicleStartTs, origin, dest, network) print('--------vehicle generation finish: uniform_whole-------------------') # TODO: speed problem when multiple elif distribution == "random": # the vehicles are randomly generated/put onto the network during the time slot # a random time-stamp during the time slot # the calculated start-time-stamp of vehicle has to be modified a bit to suit the convenience of time-step # for simulation for tsPair in tsPairNodePairTypeMap.keys(): for nodePair in tsPairNodePairTypeMap[tsPair].keys(): for vehicleType in tsPairNodePairTypeMap[tsPair][nodePair]: vehicleVolume = multiple * tsPairNodePairTypeMap[tsPair][nodePair][vehicleType] startTs, endTs = tsPair[0], tsPair[1] # two time stamps are in string type #print start_timestamp,end_timestamp #print type(start_timestamp),type(end_timestamp) duration = endTs - startTs # the duration of time slot of each csv file; unit in seconds origin, dest = network.idNodeMap[nodePair[0]], network.idNodeMap[nodePair[1]] ######*******Origin and Destination if vehicleVolume>0: for i in range(vehicleVolume):# totally generate OD_value vehicles in this iteration pickId = random.randint(0, duration.seconds - 1) vehicleId += 1 vehicleStartTs = startTs + datetime.timedelta(seconds=int(math.ceil(pickId))) #print(vehicleStartTs) maxSpeed = vehicleMaxSpeed(vehicleType) driverType = genDriverType() valueTime = genDriverValueTimeGen(medianValueTime) probLaneChange = genProbLaneChange(driverType) Vehicle(vehicleId, vehicleType, driverType, maxSpeed, valueTime, probLaneChange, vehicleStartTs, origin, dest, network) print('--------vehicle generation finish: random-------------------') elif distribution == "random_whole": listTsPair = [] # collect all the time stamps of OD start and end vehicleTotal = 0 # count the total number of vehicles in the OD matrix; not in pcu for tsPair in tsPairNodePairTypeMap.keys(): startTs, endTs = tsPair[0], tsPair[1] if not startTs in listTsPair: listTsPair.append(startTs) if not endTs in listTsPair: listTsPair.append(endTs) for nodePair in tsPairNodePairTypeMap[tsPair].keys(): for vehicleType in tsPairNodePairTypeMap[tsPair][nodePair]: vehicleTypeValue = multiple * tsPairNodePairTypeMap[tsPair][nodePair][vehicleType] vehicleTotal += vehicleTypeValue print(vehicleTotal) listTsPair.sort() # re-order the time stamps of OD starts and ends startTs = listTsPair[0] endTs = listTsPair[-1] duration = endTs - startTs #print(duration.seconds) if vehicleTotal > 0: for tsPair in tsPairNodePairTypeMap.keys(): for nodePair in tsPairNodePairTypeMap[tsPair].keys(): origin, dest = network.idNodeMap[nodePair[0]], network.idNodeMap[nodePair[1]] for vehicleType in tsPairNodePairTypeMap[tsPair][nodePair]: vehicleVolume = multiple* tsPairNodePairTypeMap[tsPair][nodePair][vehicleType] if not vehicleVolume: continue for i in range(vehicleVolume): pickId = random.randint(0, duration.seconds - 1) vehicleId += 1 vehicleStartTs = startTs + datetime.timedelta(seconds=int(math.ceil(pickId) + 1 )) #print(vehicleStartTs, 'origin', origin, 'end', dest) maxSpeed = vehicleMaxSpeed(vehicleType) driverType = genDriverType() valueTime = genDriverValueTimeGen(medianValueTime) probLaneChange = genProbLaneChange(driverType) Vehicle(vehicleId, vehicleType, driverType, maxSpeed, valueTime, probLaneChange,vehicleStartTs, origin, dest, network) print('--------vehicle generation finish: random_whole-------------------') elif distribution == "normal_whole": listTsPair = [] # collect all the time stamps of OD start and end vehicleTotal = 0 # count the total number of vehicles in the OD matrix; not in pcu for tsPair in tsPairNodePairTypeMap.keys(): startTs, endTs = tsPair[0], tsPair[1] if not startTs in listTsPair: listTsPair.append(startTs) if not endTs in listTsPair: listTsPair.append(endTs) for nodePair in tsPairNodePairTypeMap[tsPair].keys(): for vehicleType in tsPairNodePairTypeMap[tsPair][nodePair]: vehicleTypeValue = multiple * tsPairNodePairTypeMap[tsPair][nodePair][vehicleType] vehicleTotal += vehicleTypeValue print(vehicleTotal) listTsPair.sort() # re-order the time stamps of OD starts and ends startTs = listTsPair[0] endTs = listTsPair[-1] duration = endTs - startTs mu = 900 ## need to be set sigma = 100 ## need to be set def normalTs(mu, sigma, size): """ special attention for mu and sigma!!! :param mu: :param sigma: :param size: :return: """ # set seeds np.random.seed(10) s = np.random.normal(mu, sigma, size) listTs = [] # initialize the output list of time stamps # convert items in s to int for i in range(0, len(s)): s[i] = int(math.ceil(s[i])) if s[i] <= 0: s[i] = 1 timestamp = startTs + datetime.timedelta(seconds=s[i]) listTs.append(timestamp) listTs.sort() return listTs normTsList = normalTs(mu, sigma, vehicleTotal) print(normTsList[:10]) if duration.seconds > 0 and vehicleTotal > 0: for tsPair in tsPairNodePairTypeMap.keys(): for nodePair in tsPairNodePairTypeMap[tsPair].keys(): origin, dest = network.idNodeMap[nodePair[0]], network.idNodeMap[nodePair[1]] for vehicleType in tsPairNodePairTypeMap[tsPair][nodePair]: vehicleVolume = multiple * tsPairNodePairTypeMap[tsPair][nodePair][vehicleType] for i in range(0, vehicleVolume): vehicleId += 1 vehicleStartTs = normTsList.pop(0) maxSpeed = vehicleMaxSpeed(vehicleType) driverType = genDriverType() valueTime = genDriverValueTimeGen(medianValueTime) probLaneChange = genProbLaneChange(driverType) Vehicle(vehicleId, vehicleType, driverType, maxSpeed, valueTime, probLaneChange,vehicleStartTs, origin, dest, network) print('--------vehicle generation finish: normal_whole-------------------')