Get Started

Modeling a car production line: Python generators

You have been asked to build a discrete-event model to help optimize a car production line. To get started, you had to identify the main groups of processes involved in the production line. These are (1) welding and painting and (2) assembly and testing. Of course, each of these groups of processes involves many sub-processes and tasks, but for now, you are focused on coding a first, high-level version of the model.

Since you have already identified the critical groups of processes, it's time to determine the average time each process takes to complete. You did your research and came up with 15 hours for welding and painting and 24 hours for assembling parts and testing.

The simpy package has been imported for you.

Time in the model is in hours.

This is a part of the course

“Discrete Event Simulation in Python”

View Course

Exercise instructions

  • Define the Python generator with the name car_production_line_gen.
  • Clock the time requirement for welding and panting into the production line.
  • Similarly, clock in the time taken to complete assembly of parts and testing.
  • Print the current simulation time.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Defining a Generator that includes the processes
def  ____(env):
  car_number = 0
  while True:
    car_number += 1

    # Process 1: Clock the time requirement for welding and painting
    yield  env.____(____)
    print(f"Car {car_number}: Welding and painting (completed) => time = {env.now}")

    # Process 2: Clock in time for process 2 and yield it
    ____
    print(f"Car {car_number}: Assembly of parts and testing (completed) => time = {env.now}")

    # Print car ready for shipment
    print(f"Car {car_number}: Car ready for shipping! time = {env.____} hours")

This exercise is part of the course

Discrete Event Simulation in Python

AdvancedSkill Level
4.3+
6 reviews

Discover the power of discrete-event simulation in optimizing your business processes. Learn to develop digital twins using Python's SimPy package.

Discover the power of the SimPy package to streamline your discrete-event simulations. In chapter 2, you’ll learn how to build a SimPy model environment and how to add processes and resources. You’ll also learn the different types of resources available, as well as options to control and schedule events. To finish this chapter, you’ll build a complete SimPy model for an aircraft assembly line.

Exercise 1: Introduction to the SimPy packageExercise 2: Building a car washer model with SimPyExercise 3: Modeling a car production line: Python generators
Exercise 4: Modeling a car production line: Create and run the modelExercise 5: SimPy package: Types of resourcesExercise 6: Identify appropriate SimPy resourcesExercise 7: Managing payment queuesExercise 8: Modeling a petrol station: Python generatorsExercise 9: Modeling a petrol station: Run the model and analyze the resultsExercise 10: SimPy Package: Managing the scheduling of eventsExercise 11: Restaurant model: Managing tables and waiting timesExercise 12: Restaurant model: Set up, run and analyze resultsExercise 13: Building a discrete-event model with SimPyExercise 14: Build your model: Create an environment and resourcesExercise 15: Build your model: Generate aircraft ordersExercise 16: Build your model: Control assembly line responseExercise 17: Build your model: Run the model and examine results

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free