# Predict parallel time intervals

I have the following problem. There is a service station that can provide service for a number of vehicles at the same time. The service data looks like this:

    Vehicle    ServicePlanStart      ServicePlanEnd    ServiceTrueStart      ServiceTrueEnd
0         A 2022-08-01 08:00:00 2022-08-01 13:00:00 2022-08-01 09:00:00 2022-08-01 14:00:00
1         B 2022-08-01 10:00:00 2022-08-01 15:00:00 2022-08-01 09:00:00 2022-08-01 14:00:00
2         C 2022-08-01 11:00:00 2022-08-01 13:00:00 2022-08-01 11:00:00 2022-08-01 13:00:00
3         D 2022-08-01 12:00:00 2022-08-01 17:00:00 2022-08-01 14:00:00 2022-08-01 18:00:00


We have plan times and true times that are often different, as not all vehicles come in time (too late/too early) for service, or some need to wait for a free slot (because others came too late, etc.). There may be a different number of vehicles at the service station simultaneously. My task is to predict the actual start and end times ('ServiceTrueStart' and 'ServiceTrueEnd') or the status (in service/not in service) of a vehicle at some time point in the future (in the next x hours). For example, let's say we have 9:30 now and want to predict true start/end values (or status in x hours) for all 4 vehicles: As we can see, vehicles A and B are already in service from 9:00 (so we already know their 'ServiceTrueStart' times). How can we predict the rest of the true values (or status in x hours) that we don't know at that time point? What kind of algorithm should I use? How can I transform my data, so it's possible to use classic machine/deep learning algorithms? Any ideas are highly appreciated. This example is simplified and in reality I have about 2000 vehicles and a few hundred service stations. The business problem I try to solve here is how many vehicles I have available at some time point in the future.

• I think that your data lacks a key piece: the time when the vehicle arrived. If that piece of data is not observable, maybe you should model it (or something analogous and more manageable, like the delay with respect to the expected arrival time) as a latent variable.
– noe
Aug 4 at 22:25
• I have no practical experience with Hidden Markov Models (HMM), but they may be a feasible approach for your problem. Anyone with experience in HMMs that could weight in?
– noe
Aug 4 at 22:27
• @noe I have both times: a time when the vehicle has arrived (didn't mention it here, it's a little challenging to join it to this table) and the service start time as well ServiceTrueStart. Aug 5 at 6:40

One option would be to simplify the problem and build a solution for the simpler version. Then evolve the framing of the problem and associated solutions towards complexity.

If you organize all your features into a tidy dataframe where the columns are features and the rows of individual instances, the data is in a format more amenable to classic machine learning. In this framing, since time will be shattered into features the data won't be organized for time-series modeling.

A simpler way is to predict the following targets: start time, duration (start time plus duration = stop time), and in-service/not in-service. Build a separate model for each of the three targets. Again, start with more straightforward algorithms like tree-based models.

After building simple models, you'll have something that could be useful for the business. The model will be capable of predictions that could be used for planning. An added bonus is you'll have learned more about the domain and which features are most predictive.

You can build more complex and (hopefully) better models later. Given the continuous and dynamic nature of the problem, Kalman filtering modeling would probably be an improvement over classical machine learning. The current cutting edge of complexity might be reinforcement learning (RL). RL could be useful because there is a time component and the vehicles could be modeled as agents.

• Thank you! I was searching for concrete ideas. Your answer includes mostly general concepts of machine learning. I don't think the reinforcement learning is a good Idea here as I have a lot of data, including target data. It's a good Idea to use 3 features, but how should I pack my data together? 16 hours ago

As Brian Spiering said, when you have to face massive numbers of vehicles, it is indeed better to study the case with a general approach first and increase in complexity.

Then, this kind of business case is often subject to seasonalities and special events that play an important part in the predictions like lockdowns, covid, fuel price, climate issues, gas shortage, etc.

Consequently, even if your model learns over years of data, if there are no mechanisms to detect seasonality or special events, the predictions might be wrong.

The special events can trigger a flag that a model could understand and make realistic predictions accordingly.

Please note that some external data could be important to include in your model such as the weather, fuel price variations, or business-related stock prices. For instance, people are more cautious when it rains and arrive at work later.

To do that, you can use multi-variate algorithms such as:

• Prophet
• Random Forest
• XGBoost
• LSTMs

Of course, it is better to start with a model learning on 1 or 2 main features and then increase in complexity with more features.

One last piece of advice: The best way to evaluate a predictive model is by comparing the prediction every day with the real values to have its real accuracy. I used to have great predictive models with 90% of accuracy with test data, but once I tested them in the "real world", the accuracy dropped to 60%. So having a good model with test data could not be good enough in reality.

That's why reality could help a lot for model improvement by noticing external dependencies or special events. Developing more models could be a good option to compare them in the long run.

Here is an interesting article that takes into account seasonality and events with Prophet:

https://medium.com/grabngoinfo/multivariate-time-series-forecasting-with-seasonality-and-holiday-effect-using-prophet-in-python-d5d4150eeb57

Random Forest can also achieve good results:

XGBoost might also be interesting :

https://cprosenjit.medium.com/multivariate-time-series-forecasting-using-xgboost-1728762a9eeb

https://machinelearningmastery.com/xgboost-for-time-series-forecasting/

LSTMs are fine, but they need good data preparation because their memory is quite limited:

https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/

• Thank you! Your answer as another one contains general machine learning concepts. The main problem of my data to bring it in a need shape and only then I can use classic algorithms like random forests etc. The easiest way to aggregate the number of vehicles per service station, but you lose a lot of valuable information in this way. I have tried LSTM as well. I made time series with a vehicle state each n hours. The issue here is that a vehicle goes to service not so often, and you get a kind of unbalanced data. 16 hours ago
• We don't have all the ins and outs of the problem and we can't be too specific because we don't know how imbalanced your data is. Maybe it could be better to consider this problem as a station improvement instead of a vehicle one? I don't know. In all cases, it could be interesting to study the different groups of vehicles to improve their assignment to stations or see how their path and their use have influenced their activity. 14 hours ago

I ended up with the transformer model because:

It makes no assumptions about the temporal/spatial relationships across the data. This is ideal for processing a set of objects (for example, StarCraft units).

To generate features, I iterated over the time range (every x hours) and got all vehicles that are in service at the time point t1 and plan to be in service at the time point t2 (t1 + y hours). Then I calculated features relative to these two time points: how long t1 vehicles have already spent in service, in how many hours t2 vehicles plan to arrive etc.

Now we have bags of vehicles with different time features. The target variable is the status for all vehicles at the time point t2 (in service/not in service, a vector of zeros and ones for each bag).

As the number of vehicles is different in each bag, I created a custom loss function and an attention mask to mask padding.