Simulating a electric car sharing operation

Simulating a electric car sharing operation

We are in the era of shared economy. Sharing a car is becoming more and more popular. More people are taking advantage of the car as a service instead of spending a lot of money to own and use it only 5% of the time. During 95% of the time the car is parked in somewhere... If we care about our planet, using a electric car is even better!

Electric car sharing systems are starting to operate in many countries, but they still face some problems mainly for the operator who manages them: they require an infrastructure of battery charging, have limited range if compared to gas fueled cars and need some time to recharge the batteries before being ready to be rented again.

How could the operator forecast the operation in order to optimize the availability of the fleet? One way is to simulate the operation of the system. There are some commercial software that model the operation of a system as a discrete sequence of events in time. Discrete event modeling techniques can be used when the system under analysis can naturally be described as a sequence of operations, like a car sharing system for example. 

If you like programming, I would like to present here a suggestion, instead of investing in a commercial software, how to write one simulation using a Python script. Python (https://www.python.org/) is a widely used high-level, general-purpose, interpreted, dynamic programming language. It is a very easy language to learn and use and is being more and more considered in the 4th Industrial Revolution because its easy and have great capacity in data analytics due to a lot of available libraries from third parties.

One interesting library for Python is the SimPy (https://simpy.readthedocs.io/en/latest/). SimPy is a discrete-event simulation library. The behavior of active components (like vehicles, customers or messages) is modeled with processes. All processes live in an environment. They interact with the environment and with each other via events.

Part of what I will show here is from the original Simpy documentation, slightly modified to reflect a car sharing system. I will not show here how to install the Python language neither the Simpy library, it is easy and well explained in both documentation.

Let's start with a simple example from Simpy documentation. Consider the following Python file:


You can save this file as a text file using any text editor and save with py extension, like "example1.py".  Run the software from the prompt using the command:

>> python example1.py

The result will be:

Let's understand what happened: we started the file importing the SimPy library with the command import simpy

Then we defined the car as a process which receives 5 arguments:

def car(env, name, bcs, driving_time, charge_duration)

To run our simulation we need to create an environment, which is the first argument it receives, defined later as env = simpy.Environment()

The second argument is just to identify the car, as self explained by its name.

The third argument is bcs (battery charging station) which is a limited resource, we are considering 2 bcs, defined later as: bcs = simpy.Resource(env, capacity = 2)

The remaining arguments are self explained: driving_time as the duration of the travel and charge_duration as the time needed for battery charge.

The behavior of active components is modeled with processes. All processes live in an environment. They interact with the environment and with each other via events.

Processes are described by simple Python generators. You can call them process function or process method, depending on whether it is a normal function or method of a class. During their lifetime, they create events and yield them in order to wait for them to be triggered.

When a process yields an event, the process gets suspended. SimPy resumes the process, when the event occurs (we say that the event is triggered). Multiple processes can wait for the same event. SimPy resumes them in the same order in which they yielded that event.

An important event type is the Timeout. Events of this type are triggered after a certain amount of (simulated) time has passed.

yield env.timeout(driving_time)

The resource’s request() method generates an event that lets you wait until the resource becomes available again. If you are resumed, you “own” the resource until you release it.

In our example we create 4 cars (from Car 0 to Car 3) and pass them the required arguments:

for i in range(4):
       env.process(car(env, 'Car %d' % i, bcs, i*2, 5))

i is the counter and we are considering the driving_time = i x 2 and the charging_duration = 5. They are units of time and could be considered minutes, hours or any time unit, since we use the same reference in the whole file.

Notice from the results that the first two cars can start charging immediately after they arrive at the BCS, while cars 2 an 3 have to wait.

It is a simple example from the Simpy documentation that makes easy to explain the Simpy logic. It is also simple to slightly modify it to consider another example: a round-trip electric car sharing system: 

Here we have used the previous example from Simpy documentation and modified some things:

  1. We replaced the name of car definition to travel, keeping the same definition;
  2. We considered car as a Resource and defined that we have 3 of them: car = simpy.Resource(env, capacity=3);
  3. We created a user definition that requires a car as resource. Here we included a decision: if the user waits for a car for more than a defined time (max_waiting_time), it goes to another process: give_up defined later;
  4. We defined a setup process to generate the users that came according to a random arriving interval time in average of 5 min (or other time unit if you wish);
  5. Run the simulation until 120 min. Here you can define a simulation time.

Now, running the simulation you have the following results:

It continues... I have interrupted just to save space here...

Notice that  now we can see how many people gave up and how many were serviced. You can play with the numbers like number of battery charging stations or number of cars, charging time or travel time and see the impact on the results. As we have used in the user's arriving interval time, it is possible to use random numbers according to a kind of distribution (that's why we imported the Python random library at the beginning of the file). It is possible to use it in all the time definitions like travel time or charging time for example. Here we considered a round-trip car station, but it is possible to create an environment with many parking stations, each one with a certain number of bcs and cars and define a different user's arrival interval for each parking station. It is also possible to consider it a one-way system, where the user can pick a car in one parking station and leave it in another one. The sky is the limit when you need to simulate a electric car sharing simulation. It is a powerful tool for sizing the fleet according to your demand and also possible to calculate a huge number of variables like energy consumption according to the total charging time, quantity of travels for each car, just to mention...

Here we fixed the seed: random.seed(100) just to repeat always the same result, but instead of fixing it, it is possible to run the same script multiple times creating a number of replications of the same conditions, so that the variability associated with the simulation can be estimated (statistics).

For the operator that has no idea how to size the fleet, it is possible to size it considering different distributions in time and create many scenarios for decision. For the experienced operator, it is a powerful tool if you enter with known variables and want to simulate totally different scenarios to decide some changing in the operation, mainly when you are considering the charging infrastructure and its impact on the operation. It can be very useful!

 

 

 

 

 

Hi Sergio, I have read your article ... really really interesting!! I will text you a private message!

To view or add a comment, sign in

More articles by Sergio Bastos

  • Biogás com Python

    #python é uma linguagem de programação de fácil aprendizado e muito poderosa. Vou colocar aqui um exemplo de como ela…

  • Compartilhamento de carros no Brasil: elétrico ou etanol?

    Dando sequência ao artigo anterior que escrevi comparando um carro convencional Flex com o seu similar na versão…

  • Vale a pena o carro elétrico no Brasil?

    Recentemente uma grande montadora disponibilizou para o Brasil a versão elétrica do seu carro convencional mais barato.…

    2 Comments
  • O Carro Elétrico do Brasil

    Em 2016 participei de um PMI na cidade de Curitiba com um estudo para a implementação de um sistema de compartilhamento…

    2 Comments
  • Do Biogás ao Bitcoin

    O Bitcoin foi a primeira das criptomoedas, sistemas digitais baseados na tecnologia chamada blockchain ou corrente de…

  • Atualizando estações de tratamento e ampliando a geração de biogás

    Praticamente metade do esgoto produzido no Brasil não é tratado. O Marco do Saneamento recentemente aprovado vai ajudar…

  • Sizing the needed resources using Python

    "Flatten the curve" is one of most heard terms nowadays. Its meaning is to adopt preventive actions in order to smooth…

    2 Comments
  • Como obter a curva epidêmica a partir dos casos confirmados usando Python

    Ainda estamos no início da epidemia do Coronavírus no Brasil. É hora de nos cuidarmos para tentar diminuir a velocidade…

  • She could save Harley-Davidson, again...

    After nearly five years as CEO of Harley-Davidson, Matthew Levatich is stepping down. Although that is not entirely his…

  • A Importância da Agitação na Produção de Biogás

    A geração de energia através de fontes renováveis está cada vez mais em evidência, não só no Brasil, mas no mundo…

Others also viewed

Explore content categories