↑↑Please click the “blue text” above to follow↑↑
DeepSeek Campus Autonomous Vehicle Management: Scheduling for Campus Vehicles
Hello! Today, I want to learn with you about a very interesting and practical DeepSeek programming application – the scheduling management of campus autonomous vehicles. We will start from basic knowledge and gradually build a small system that can schedule campus autonomous vehicles. In this process, you will learn the basic usage of conditional statements, loops, functions, and classes, and you will also encounter some practical development tips.
Don’t worry, if you are a beginner, I will try to explain it simply and clearly. Grab your computer and let’s get started!
1. What is Campus Autonomous Vehicle Scheduling?
In some modern campuses, autonomous vehicles are used to transport students, teachers, or goods. To allow these autonomous vehicles to operate efficiently, we need a scheduling system to manage the vehicle’s status (such as “idle” or “busy”) and task assignments.
In simple terms, what we need to do is:
-
Track the status of autonomous vehicles. -
Update the vehicle’s status.
Next, let’s implement this logic step by step!
2. Using Dictionaries to Track Vehicle Status
In programming, a dictionary is a very suitable tool for storing data. We can use it to record the status of each vehicle, such as whether it is idle, its current location, etc.
Example Code
Run Python
# Define a dictionary containing vehicle information
vehicles = {
"Vehicle1": {"status": "idle", "location": "East Gate"},
"Vehicle2": {"status": "busy", "location": "Library"},
"Vehicle3": {"status": "idle", "location": "Playground"}
}
# Output the status of all vehicles
for vehicle_name, info in vehicles.items():
print(f"{vehicle_name} Current Status: {info['status']}, Location: {info['location']}")
Run Results
Copy
Vehicle1 Current Status: idle, Location: East Gate
Vehicle2 Current Status: busy, Location: Library
Vehicle3 Current Status: idle, Location: Playground
Tip: The
for
loop here iterates through each key-value pair in the dictionary (thevehicle_name
andinfo
). If you are not familiar with dictionaries, you can simply understand it as a structure of “name-information” pairs.
3. Writing Task Assignment Functions
Example Code
Run Python
def assign_vehicle(task_location):
# Iterate through vehicles to find an idle one
for vehicle_name, info in vehicles.items():
if info["status"] == "idle":
# Update vehicle status
info["status"] = "busy"
info["location"] = task_location
print(f"Task assigned to {vehicle_name}, heading to {task_location}")
return vehicle_name
print("No idle vehicles available!")
return None
Test Task Assignment
Run Python
# Assign a task to "Dormitory"
assign_vehicle("Dormitory")
# Check the updated vehicle status
for vehicle_name, info in vehicles.items():
print(f"{vehicle_name} Current Status: {info['status']}, Location: {info['location']}")
Run Results
Copy
Task assigned to Vehicle1, heading to Dormitory
Vehicle1 Current Status: busy, Location: Dormitory
Vehicle2 Current Status: busy, Location: Library
Vehicle3 Current Status: idle, Location: Playground
Note: If the vehicle status is not updated correctly, check whether you modified the values in the dictionary. Dictionaries are mutable types, and modifying internal data directly affects the original dictionary.
4. Managing Vehicles with Classes
While using dictionaries can solve the problem, when the system becomes complex, using classes will make our code clearer and easier to maintain. Next, we will use classes to implement vehicle management.
Defining the Vehicle Class
Run Python
class Vehicle:
def __init__(self, name, location):
self.name = name
self.status = "idle"
self.location = location
def assign_task(self, new_location):
if self.status == "idle":
self.status = "busy"
self.location = new_location
print(f"{self.name} assigned task, heading to {new_location}")
else:
print(f"{self.name} is busy, cannot assign task!")
def complete_task(self):
self.status = "idle"
print(f"{self.name} task completed, now idle!")
Creating Vehicle Objects
Run Python
# Create three vehicles
vehicle1 = Vehicle("Vehicle1", "East Gate")
vehicle2 = Vehicle("Vehicle2", "Library")
vehicle3 = Vehicle("Vehicle3", "Playground")
# Store them in a list
vehicle_list = [vehicle1, vehicle2, vehicle3]
Assigning Tasks and Completing Tasks
Run Python
# Iterate through the vehicle list to find an idle vehicle to assign a task
for vehicle in vehicle_list:
if vehicle.status == "idle":
vehicle_to_assign = vehicle
break
vehicle_to_assign.assign_task("Dormitory Area")
# Complete the task
vehicle_to_assign.complete_task()
Run Results
Copy
Vehicle1 assigned task, heading to Dormitory Area
Vehicle1 task completed, now idle!
Tip: Classes allow us to encapsulate the properties and behaviors of vehicles together, making them very suitable for managing complex data and operations.
5. Comprehensive Application: A Simple Scheduling System
Next, we will combine dictionaries and classes to build a simple scheduling system. We will use a dictionary to store vehicle objects and use functions to assign tasks.
Example Code
Run Python
# Create vehicles and store them in a dictionary
vehicle_dict = {
"Vehicle1": Vehicle("Vehicle1", "East Gate"),
"Vehicle2": Vehicle("Vehicle2", "Library"),
"Vehicle3": Vehicle("Vehicle3", "Playground")
}
def schedule_task(task_location):
for vehicle_name, vehicle in vehicle_dict.items():
if vehicle.status == "idle":
vehicle.assign_task(task_location)
return
print("All vehicles are busy, cannot assign task!")
# Test the scheduling system
schedule_task("Laboratory Building")
schedule_task("Cafeteria")
Run Results
Copy
Vehicle1 assigned task, heading to Laboratory Building
Vehicle2 assigned task, heading to Cafeteria
6. Summary and Exercises
Today, we learned how to manage the scheduling of campus autonomous vehicles using dictionaries and classes. With this knowledge, you can implement a simple yet practical task assignment system. Here are the key points we learned:
-
Dictionaries can store the status and information of vehicles. -
Conditional statements and loops help us handle task assignment logic. -
Classes encapsulate the properties and behaviors of vehicles, making the code clearer.
Exercises
-
Add a property “battery level” for each vehicle, and refuse to assign tasks when the battery level is below 20%. -
Add a function to count how many vehicles are currently in an idle state.
Give it a try! Don’t forget that practice is essential to truly mastering this knowledge!