This manuscript presents a speed trajectory optimization to minimize driving energy consumption using four different nature-inspired algorithms. For this purpose, a model was developed that can simulate motion of a single train. To test the train motion model and speed trajectories, Istanbul M3 subway line was modeled with its actual parameters such as gradient, curve, and speed limit. Concerning computational time and energy usage, a straightforward speed profile and proposed speed profiles by the algorithms have been compared. Afterwards, variable passenger numbers at each station in a certain hour were taken as dynamic mass input. With the marine predator algorithm (MPA) that finds the best result among the four, speed profile optimization was performed again under the effect of unstable mass. Finally, a train was operated by a driver according to the optimal speed profile. As a result, energy saving is 18.75% in simulation environment and 21.27% in real-life test.