AbstractForced merge behavior on freeway merging areas is a major cause of traffic conflicts. Intelligent transportation systems can provide the possibility of information interaction and collaborative control, and optimized trajectory planning methods for merging vehicles in a vehicle-to-infrastructure (V2I) environment can improve the safety and efficiency of freeway merging areas. However, it is difficult for vehicles to determine suitable gaps for merging from the ramp to the mainline. Although some researchers have developed models to determine the appropriate insertable gap, they usually assume that the vehicles are driving at the same speed and acceleration, and there is no corresponding selection strategy for different traffic density scenarios. This study develops a trajectory planning method for merging vehicles according to characteristics of different mainline densities in a V2I environment, which can be applied in for the development of dynamic merging assistance systems to distribute control signals to drivers. The trajectory planning problem is transformed into a nonlinear optimal control problem, and the goal is to optimize the insertion gap, energy consumption, and passenger comfort. The decision variables include the time-varying longitudinal acceleration of a group of vehicles and the optimal insertable gap; the optimization problem is then solved by a heuristic algorithm. To verify the applicability and safety of the proposed method, numerical simulation experiments are carried out in different density traffic flow scenarios, and the safety of the experimental results was evaluated from the individual vehicle level and traffic flow level. Results show that the proposed method is suitable for different mainline densities and can effectively improve merge safety. Simulation examples are included to compare the performance of the proposed model to a baseline model with results showing significant improvements in both travel time and safety.